OpenCV Python VGG16 模型的图像预处理

OpenCV Python Image Preprocessing for VGG16 Model

我想正确地预处理图像以将它们输入到 VGG16 模型中

作者在 original paper 中写道:

During training, the input to our ConvNets is a fixed-size 224 × 224 RGB image. The only preprocessing we do is subtracting the mean RGB value, computed on the training set, from each pixel.

调整大小部分很容易完成:

import cv2
import numpy as np


# Reading the image in RGB mode
image = cv2.imread(PATH_TO_IMAGE,1)

# Resize Image to original VGG16 input size
# from the paper: "During training, the input to our ConvNets 
# is a fixed-size 224 × 224 RGB image"

width = 224
height = 224
dim = (width, height)

# resize image
resized_image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

...但我不太确定减去平均 RGB 值:

meanRBB_substract_image = resized_image - np.mean(resized_image)

这是正确的做法吗?

平均RGB减法前:

平均 RGB 减法后:

有关 VGG16 模型的更多信息:https://neurohive.io/en/popular-networks/vgg16/#:~:text=The%20architecture%20depicted%20below%20is%20VGG16.&text=The%20input%20to%20cov1%20layer,stack%20of%20convolutional%20(conv.)

编辑: 我刚刚意识到他们写 "computed on the training set" -> 这是否意味着我需要 1. 在我的训练中找到所有图片的平均 RGB 值集,然后 2. 从所有训练集图像中减去这个均值?

尝试:

from keras.applications.vgg16 import preprocess_input
...
resized_image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
processedimage = preprocess_input(resized_image)

来自: https://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/