【问题标题】:preprocess_input in keras increase the size of train drasticallykeras 中的 preprocess_input 大大增加了火车的大小
【发布时间】:2018-05-07 11:28:41
【问题描述】:

在使用 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 会大幅增加输入大小?

zero center by mean pixel 在 keras 中是这样定义的

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

【问题讨论】:

  • preprocess_input 是如何定义的? Zero-center by mean pixel 是什么意思?
  • 如果你参考preprocess_input的keras实现你可以看到x[..., 0] -= 103.939 x[..., 1] -= 116.779 x[..., 2] -= 123.68这几行代码。
  • 我最终决定使用生成器。所以我不需要再担心数据的大小了。只预处理了一部分数据

标签: keras image-preprocessing


【解决方案1】:

这是因为像素值的类型为“uint8”,而现在它们的类型为“float”。 所以现在你有了一个图像,它是一个 'float' 数组,它比一个 'uint8' 数组大。

【讨论】:

    【解决方案2】:

    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
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-01-27
        • 1970-01-01
        • 2017-05-28
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2020-12-02
        • 2015-12-08
        • 2021-01-15
        相关资源
        最近更新 更多