如何在 ImageDataGenerator 中调整图像大小

How are image resize in ImageDataGenerator

据我了解,在使用 tensorflow keras 时,我们可以使用 ImageDataGenerator 从目录中传输数据。当数据流入时,它们被resize到target_size。但是这个改造是如何完成的呢?

一个例子是这样的:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
trainDir = 'train'
dataGen = ImageDataGenerator(rescale=1/255)
gen = dataGen.flow_from_directories(
  trainDir,
  batch_size=64,
  target_size=(150, 150),
  class_mode='binary'
)

此处,trainDir 中的所有图像都将调整为 150x150。但是它们是如何调整大小的?

我检查了 documentation on tensorflow ImageDataGenerator。它只说 target_size 是“将调整所有找到的图像的尺寸”。但是我没有找到关于如何调整大小的解释。

target_size与输入图片大小不同时,使用interpolation参数。使用的默认插值方法是 nearest,但也可以使用其他方法。

来源:https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator

ImageDataGenerator 有一个参数 fill_mode。该参数可以设置如下

One of {"constant", "nearest", "reflect" or "wrap"}. Default is 'nearest'. Points outside the boundaries of the input are filled according to the given mode:
'constant': kkkkkkkk|abcd|kkkkkkkk (cval=k)
'nearest': aaaaaaaa|abcd|dddddddd
'reflect': abcddcba|abcd|dcbaabcd
'wrap': abcdabcd|abcd|abcdabcd
frankly I have no idea what the above is intending to illustrate