【发布时间】:2023-04-05 10:09:01
【问题描述】:
尝试加载 Caltech tensorflow-dataset 时出现错误。我正在使用tensorflow-datasets GitHub中的标准代码
错误是这样的:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [204,300,3] and element 1 had shape [153,300,3]. [Op:IteratorGetNextSync]
错误指向for features in ds_train.take(1)这一行
代码:
ds_train, ds_test = tfds.load(name="caltech101", split=["train", "test"])
ds_train = ds_train.shuffle(1000).batch(128).prefetch(10)
for features in ds_train.take(1):
image, label = features["image"], features["label"]
【问题讨论】:
-
调用
tfds.load后运行print(ds_train.output_shapes)的结果是什么? -
{'image': TensorShape([Dimension(None), Dimension(None), Dimension(3)]), 'image/file_name': TensorShape([]), 'label': TensorShape([])} -
如果你在
ds_train = ds_train.shuffle(1000).batch(128).prefetch(10)之后放置同一行会打印什么? -
{'image': TensorShape([Dimension(None), Dimension(None), Dimension(None), Dimension(3)]), 'image/file_name': TensorShape([Dimension(None)]), 'label': TensorShape([Dimension(None)])} -
您是否可能在没有先清除 python 环境的情况下不小心运行了两次
ds_train = ds_train.shuffle(1000).batch(128).prefetch(10)行?从输出中可以看出,tfds.load没有预批处理数据(它不应该),但是您的错误消息显示代码正在尝试批处理整个数据集而不是单个样本。如果您重新启动 python 解释器并运行显示它们的行,中间没有任何其他内容,问题是否仍然存在?
标签: python python-3.x tensorflow runtime-error tensorflow-datasets