我正在使用tf.keras.preprocessing.image_dataset_from_directory
TF 2.3从目录中加载图像(训练/测试拆分)。我得到的是一个tf.data.Dataset(tensorflow.python.data.ops.dataset_ops.BatchDataset
实际上)具有形状的对象:
train_ds.take(1)
# <TakeDataset shapes: ((None, 256, 256, 3), (None, 6)), types: (tf.float32, tf.float32)>
for images, labels in train_ds.take(1):
print(images.shape)
print(images[0])
# (32, 256, 256, 3)
# tf.Tensor(
# [[[225.75 225.75 225.75 ]
# [225.75 225.75 225.75 ]
# [225.75 225.75 225.75 ]
# ...
# [215. 214. 209. ]
# [215. 214. 209. ]
# [215. 214. 209. ]]
#
# ...], shape=(256, 256, 3), dtype=float32)
我无法弄清楚如何/= 255
使用该Dataset对象规范图像()。我试图打/=
运营商本身,map
和apply
方法,甚至流延对象名单提到这里。似乎没有任何效果,我真的很想在数据集级别解决此问题,而不是在网络中添加标准化层。
有任何想法吗?
尝试这种方式:
def process(image,label):
image = tf.cast(image/255. ,tf.float32)
return image,label
ds = tf.keras.preprocessing.image_dataset_from_directory(IMAGE_DIR)
ds = ds.map(process)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句