preprocess_input 在keras中大幅增加火车的尺寸

preprocess_input in keras increase the size of train drastically

在使用 resnet50 模型进行训练之前,我使用以下方法对输入进行了预处理:

img = image.load_img(os.path.join(TRAIN, img), target_size=[224, 224])
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)

并保存一个 numpy 图像数组。 我发现没有preprocess_input,数组大小是1.5G,有preprocess_input,大小是7G。 这是正常行为吗?或者我错过了什么? 为什么 Zero-center by mean pixel 会大幅增加输入大小?

在 keras

zero center by mean pixel 是这样定义的

x = x[..., ::-1] x[..., 0] -= 103.939 x[..., 1] -= 116.779 x[..., 2] -= 123.68

正在读取 preprocess_input 的 keras 实现 图像通过减去数据集的图像均值进行归一化,这似乎是从 imagenet 获得的常数。这里的代码

def _preprocess_numpy_input(x, data_format, mode):
if mode == 'tf':
    x /= 127.5
    x -= 1.
    return x

if data_format == 'channels_first':
    if x.ndim == 3:
        # 'RGB'->'BGR'
        x = x[::-1, ...]
        # Zero-center by mean pixel
        x[0, :, :] -= 103.939
        x[1, :, :] -= 116.779
        x[2, :, :] -= 123.68
    else:
        x = x[:, ::-1, ...]
        x[:, 0, :, :] -= 103.939
        x[:, 1, :, :] -= 116.779
        x[:, 2, :, :] -= 123.68
else:
    # 'RGB'->'BGR'
    x = x[..., ::-1]
    # Zero-center by mean pixel
    x[..., 0] -= 103.939
    x[..., 1] -= 116.779
    x[..., 2] -= 123.68
return x

我不明白为什么使用这段代码会增加我的数据集的大小。

因为像素值是'uint8'类型,现在是'float'类型。 所以现在你有一个图像,它是一个 'float' 数组,它比 'uint8' 数组大。

根据 TensorFlow documentation 参数是: 浮点 numpy.array 或 tf.Tensor,具有 3 个颜色通道的 3D 或 4D,值在 [0, 255] 范围内。 和函数 returns Returns: 预处理 numpy.array 或类型为 float32 的 tf.Tensor。

我感觉整数使用的内存量不同。