gpt4 book ai didi

python - 在 TensorFlow 2.3 中规范化 BatchDataset

转载 作者:行者123 更新时间:2023-12-05 00:45:27 25 4
gpt4 key购买 nike

我正在使用 TF 2.3 中的 tf.keras.preprocessing.image_dataset_from_directory 从目录加载图像(训练/测试拆分)。我得到的是一个具有形状的 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)

我不知道如何使用该 Dataset 对象规范化图像 (/= 255)。我尝试使用 /= 运算符本身、mapapply 方法,甚至将该对象转换到列表中,如所述 here .似乎没有任何效果,我真的很想在数据集级别解决这个问题,而不是在我的网络中添加规范化层。

有什么想法吗?

最佳答案

试试这个方法:

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)

关于python - 在 TensorFlow 2.3 中规范化 BatchDataset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63572451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com