我想更好地把握预处理图像的概念,以将图像输入到经过预先训练的神经网络(通常是从头开始制作的神经网络)中。我认为,经过预训练的NN的新数据应与原始数据具有相同的数据范围。在这种情况下,我愿意使用在 ImageNet 数据集上训练的 ResNet18 架构,如果我没记错的话,该架构会接受[-1,1]中的数据范围作为输入。
为简单起见,想象一下我在cifar10数据集上工作
import keras
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
给出rgb值在[0,256]范围内。为了达到[-1,1]范围,我使用以下代码:
# this makes data in [0,1] range
x_train = np.divide(x_train, 255.0)
x_test = np.divide(x_test, 255.0)
# this generates batches in [-1,1] range, if I'm correct
datagen = ImageDataGenerator(
featurewise_center = True,
featurewise_std_normalization = True
)
datagen.fit(x_train)
# always if I'm correct, this should apply the normalization configuration also to the test set,
# so that x_train and x_test have the same range [-1,1]
for i in range(len(x_test)):
x_test[i] = datagen.standardize(x_test[i])
# now I fit the generator
epochs = 20
batch_size = 32
model.fit_generator(datagen.flow(x_train, y_train,
batch_size = batch_size),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs = epochs,
validation_data = (x_test, y_test))
我的问题是:
x_train = np.divide(x_train, 255.0)
是有意义的操作吗?还是仅通过ImageDataGenerator
函数就能获得相同的结果吗?for i in range(len(x_test)): x_test[i] = datagen.standardize(x_test[i])
是正确使用的命令吗?关于图像预处理,我还没有非常清晰的想法。预先感谢您的帮助!