使用TensorFlow Keras预处理加载图像时像素值发生变化

时间:2020-09-30 08:25:45

标签: tensorflow keras deep-learning tensorflow-datasets preprocessor

我使用函数tf.keras.preprocessing.image_dataset_from_directory(),当我检查加载的图像的内容时,我发现它包含随机像素值,与原始图像不一致。

这就是我所说的功能:

ds = tf.keras.preprocessing.image_dataset_from_directory(
    images_path,
    label_mode=None,
    shuffle=False,
    seed=None,
    image_size=(input_height, input_width),
    batch_size=batch_size
)

然后我对数据集的第一个元素进行采样,如下所示:

it = iter(ds)
img = next(it).numpy()

生成的图像包含类似164.3462的值,这没有意义,因为原始图像文件只有整数作为像素值。如果要转换为float32,我希望所有像素的值的小数部分都应为.0。

我错过了什么吗?我只想以原始值或以.0开头的原始值加载图像,以防需要float32。

怎么了?

2 个答案:

答案 0 :(得分:0)

那是因为在尝试将图像调整为image_dataset_from_directory时,bilinear使用默认的'(input_height, input_width)'插值。检查this链接以了解插值方法的其他选项。

答案 1 :(得分:0)

要使用原始值,请使用“最近”作为插值来调整图像大小。

ds = tf.keras.preprocessing.image_dataset_from_directory(
    images_path,
    label_mode=None,
    shuffle=False,
    seed=None,
    interpolation='nearest',
    image_size=(input_height, input_width),
    batch_size=batch_size
)