使用 Keras VGG 模型的预期输入范围是多少?

What is the expected input range for working with Keras VGG models?

我正在尝试使用来自 keras 的预训练 VGG 16。但我真的不确定输入范围应该是多少。

快答,这些颜色顺序是哪个?

哪个范围?

我注意到 the file where the model is defined 导入了一个输入预处理器:

from .imagenet_utils import preprocess_input

但是文件的其余部分从未使用过此预处理器。

此外,当我检查 code for this preprocessor 时,它有两种模式:caffetf (tensorflow)。

每种模式的工作方式不同。

最后,我无法在 Internet 上找到一致的文档。

那么,工作的最佳范围是多少?模型权重训练到什么范围?

模型权重是从caffe移植过来的,所以在BGR format.

Caffe uses a BGR color channel scheme for reading image files. This is due to the underlying OpenCV implementation of imread. The assumption of RGB is a common mistake.

可以找到原始的caffe模型权重文件on VGG website。 link 也可以在 Keras 文档中找到。

我认为第二个范围是最接近的范围。训练期间没有缩放,但作者减去了 ILSVRC2014 训练集的平均值。如 the original VGG paper 中所述,第 2.1 节:

The only preprocessing we do is subtracting the mean RGB value, computed on the training set, from each pixel.

这句话其实就是imagenet_utils.preprocess_input(mode='caffe')所做的

  1. 从 RGB 转换为 BGR:因为 keras.preprocessing.image.load_img() 以 RGB 格式加载图像,VGG16(以及从 caffe 移植的所有模型)需要此转换。
  2. 减去平均 BGR 值:(103.939, 116.779, 123.68) 从图像数组中减去。

预处理器未在 vgg16.py 中使用。它被导入到文件中,以便用户可以通过调用 keras.applications.vgg16.preprocess_input(rgb_img_array) 来使用预处理功能,而不用关心模型权重来自哪里。 preprocess_input() 的参数始终是 RGB 格式的图像数组。如果模型是用 caffe 训练的,preprocess_input() 会将数组转换为 BGR 格式。

请注意,函数 preprocess_input() 不打算从 imagenet_utils 模块中调用。如果您使用的是 VGG16,请调用 keras.applications.vgg16.preprocess_input(),图像将被转换为适合 VGG16 训练的格式和范围。同样,如果您使用的是 Inception V3,请调用 keras.applications.inception_v3.preprocess_input(),图像将转换为 Inception V3 训练的范围。