如何在 Keras 中为 VGG16 微调预处理训练集?
How to preprocess training set for VGG16 fine tuning in Keras?
我已经微调了 Keras VGG16 模型,但我不确定训练阶段的预处理。
我创建了一个火车生成器如下:
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_folder,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=train_batchsize,
class_mode="categorical"
)
重新缩放是否足够或者我必须应用其他预处理函数?
当我使用网络对图像进行分类时,我使用以下代码:
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
我认为这是正确的预处理,我应该在训练前应用它。
感谢您的帮助。
ImageDataGenerator 有一个 preprocessing_function
参数,它允许您传递在推理期间使用的相同 preprocess_input
函数。此函数将为您重新缩放,因此可以省略缩放:
from keras.applications.vgg16 import preprocess_input
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
keras_applications 中的大多数预训练模型都使用相同的 preprocessing function。您可以检查文档字符串以查看它的作用:
def preprocess_input(x, data_format=None, mode='caffe', **kwargs):
"""Preprocesses a tensor or Numpy array encoding a batch of images.
# Arguments
x: Input Numpy or symbolic tensor, 3D or 4D.
The preprocessed data is written over the input data
if the data types are compatible. To avoid this
behaviour, `numpy.copy(x)` can be used.
data_format: Data format of the image tensor/array.
mode: One of "caffe", "tf" or "torch".
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- tf: will scale pixels between -1 and 1,
sample-wise.
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.
# Returns
Preprocessed tensor or Numpy array.
我已经微调了 Keras VGG16 模型,但我不确定训练阶段的预处理。
我创建了一个火车生成器如下:
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_folder,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=train_batchsize,
class_mode="categorical"
)
重新缩放是否足够或者我必须应用其他预处理函数?
当我使用网络对图像进行分类时,我使用以下代码:
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
我认为这是正确的预处理,我应该在训练前应用它。
感谢您的帮助。
ImageDataGenerator 有一个 preprocessing_function
参数,它允许您传递在推理期间使用的相同 preprocess_input
函数。此函数将为您重新缩放,因此可以省略缩放:
from keras.applications.vgg16 import preprocess_input
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
keras_applications 中的大多数预训练模型都使用相同的 preprocessing function。您可以检查文档字符串以查看它的作用:
def preprocess_input(x, data_format=None, mode='caffe', **kwargs):
"""Preprocesses a tensor or Numpy array encoding a batch of images.
# Arguments
x: Input Numpy or symbolic tensor, 3D or 4D.
The preprocessed data is written over the input data
if the data types are compatible. To avoid this
behaviour, `numpy.copy(x)` can be used.
data_format: Data format of the image tensor/array.
mode: One of "caffe", "tf" or "torch".
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- tf: will scale pixels between -1 and 1,
sample-wise.
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.
# Returns
Preprocessed tensor or Numpy array.