Keras VGG16 preprocess_input 模式

Keras VGG16 preprocess_input modes

我正在使用 Keras VGG16 model

我看到有一个 preprocess_input method to use in conjunction with the VGG16 model. This method appears to call the preprocess_input method in imagenet_utils.py which (depending on the case) calls _preprocess_numpy_input method in imagenet_utils.py.

preprocess_input 有一个 mode 参数,需要 "caffe"、"tf" 或 "torch"。如果我在 Keras 中使用带有 TensorFlow 后端的模型,我应该绝对使用 mode="tf" 吗?

如果是,这是因为 Keras 加载的 VGG16 模型使用经过相同预处理的图像进行训练(即将输入图像的范围从 [0,255] 更改为输入范围 [-1,1])?

另外,测试模式的输入图像是否也需要经过这种预处理?我相信最后一个问题的答案是肯定的,但我想要一些保证。

我希望 Francois Chollet 做对了,但是看看 https://github.com/fchollet/deep-learning-models/blob/master/vgg16.py 要么他是,要么我使用 mode="tf".

是错误的

更新信息

@FalconUA 将我带到了 VGG at Oxford which has a Models section with links for the 16-layer model. The information about the preprocessing_input mode argument tf scaling to -1 to 1 and caffe subtracting some mean values is found by following the link in the Models 16-layer model: information page。在描述部分它说:

"In the paper, the model is denoted as the configuration D trained with scale jittering. The input images should be zero-centered by mean pixel (rather than mean image) subtraction. Namely, the following BGR values should be subtracted: [103.939, 116.779, 123.68]."

这里的 mode 不是关于后端,而是关于 模型是在什么框架上训练和移植的。keras link到 VGG16,声明:

These weights are ported from the ones released by VGG at Oxford

所以 VGG16 和 VGG19 模型在 Caffe 中训练并移植到 TensorFlow,因此这里 mode == 'caffe'(范围从 0 到 255,然后提取平均值 [103.939, 116.779, 123.68])。

较新的网络,如 MobileNetShuffleNet 是在 TensorFlow 上训练的,因此 mode'tf'它们和输入在 -1 到 1 的范围内以零为中心。

根据我在 Keras 中训练 VGG16 的经验,输入应该是从 0 到 255,减去平均值 [103.939, 116.779, 123.68]。我尝试过迁移学习(冻结底部并在顶部堆叠分类器),输入集中在 -11,结果比 0..255 - [103.939, 116.779, 123.68].[=14= 差得多]

最近我自己再次尝试使用 VGG16,我无法通过像这样从 vgg16 导入 preprocess_input 来获得下降结果:

from keras.applications.vgg16 import VGG16, preprocess_input

这样做,preprocess_input 默认设置为 'caffe' 模式,但仔细查看 keras vgg16 code,我注意到权重名称

'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'

两次引用tensorflow。我认为预处理模式应该是 'tf'.

processed_img = preprocess_input(img, mode='tf')