Resnet50图像预处理

Resnet50 image preprocessing

我正在使用 https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3 来提取图像特征向量。但是,当涉及到如何在通过模块传递图像之前对图像进行预处理时,我感到很困惑。

根据相关的Github解释,据说应该做以下事情:

image_path = "path/to/the/jpg/image"
image_string = tf.read_file(image_path)
image = tf.image.decode_jpeg(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)

# All other transformations (during training), in my case:
image = tf.random_crop(image, [224, 224, 3])
image = tf.image.random_flip_left_right(image)

# During testing:
image = tf.image.resize_image_with_crop_or_pad(image, 224, 224)

但是,使用上述转换后,我得到的结果表明可能有问题。此外,Resnet paper 表示图像应通过以下方式预处理:

A 224×224 crop is randomly sampled from an image or its horizontal flip, with the per-pixel mean subtracted...

我不太明白什么意思。有人能指出我正确的方向吗?

期待您的解答!

TensorFlow Hub 上的图像模块都期望像素值在 [0,1] 范围内,就像您在上面的代码片段中得到的一样。这使得在模块之间切换变得简单和安全。

在模块内部,输入值被缩放到网络训练的范围内。模块 https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3 已从 TF-Slim 检查点发布(请参阅文档),该检查点使用另一种规范化输入的约定,而不是 He&al。 -- 但所有这一切都得到了照顾。

揭开 He&al. 中的语言神秘面纱:它指的是在他们研究的数据集的所有像素上聚合的平均 R、G 和 B 值,遵循将输入归一化为零均值有助于神经网络更好地训练的古老智慧。然而,后来关于图像分类的论文不再将这种程度的关注扩展到dataset-specific预处理。

您提到的 Resnet 论文的引用是基于 Alexnet paper 的以下解释:

ImageNet consists of variable-resolution images, while our system requires a constant input dimensionality. Therefore, we down-sampled the images to a fixed resolution of256×256. Given a rectangular image, we first rescaled the image such that the shorter side was of length 256, and thencropped out the central 256×256patch from the resulting image. We did not pre-process the images in any other way, except for subtracting the mean activity over the training set from each pixel.

所以在 Resnet 论文中,一个类似的过程包括获取图像的 224x224 像素部分(或其水平翻转版本)以确保网络获得 constant-sized 图像,然后将其居中通过减去平均值。