【发布时间】:2021-10-07 08:47:10
【问题描述】:
我目前正在处理 ImageNet 数据集,您可能知道它非常大。
我已将其从 .tar 文件预处理为 tfrecord 文件。
我目前正在使用:
train, val = tfds.load(*)
所以我有两个 tfd:train 和 val。
然后我使用以下方法调整它们:
def resize_with_crop(image, label):
i = image
i = tf.cast(i, tf.float32)
i = tf.image.resize_with_crop_or_pad(i, 224, 224)
i = tf.keras.applications.mobilenet_v2.preprocess_input(i)
return (i, label)
# Preprocess the images
train = train.map(resize_with_crop)
val = val.map(resize_with_crop)
我从here关注。
在我尝试拟合我的模型后,d = model.fit(train, validation_data=val,...) 第一层具有形状(无、224、224、3),我收到错误:ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3
这个问题(我相信)是因为模型一次只有一张图像(所以它没有 4d 形状。我无法将数据集保存在内存中以将其重组为(无、224、224、 3)就像我对 cifar-10 数据集所做的那样)。
我的问题是,现在图像的格式为 (224, 224, 3),我如何将它们与期望 4d 形状但我无法在内存中重塑数据集的 tensorflow 模型一起使用?
或者有没有办法调整 tfds 的形状,使其作为模型的输入?
我不确定我是否完全了解 tfds,这就是我遇到此问题的原因。此外,我确信标签会导致问题(因为它们是整数),那么如何将 tfds 标签重构为模型的热编码?
【问题讨论】:
-
批量处理你的数据:
train = train.map(resize_with_crop).batch(32)和 val 也是如此。 -
@Kaveh 非常感谢。另外,您对标签的一种热编码有什么建议吗?
-
你可以使用
to_categoricalfrom Tensorflow orOneHotEncoderfrom Scikit-Learn orget_dummiesfrom Pandas,但是如果你使用@,你不需要one-hot编码987654333@ 作为损失函数。
标签: python tensorflow machine-learning keras imagenet