Как подготовить тренировочный набор для тонкой настройки VGG16 в Керасе?

Я точно настроил модель 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)

Я думаю, что это правильный препроцесс, и я должен применить его перед тренировкой.

Спасибо за вашу помощь.

1 ответ

Решение

ImageDataGenerator имеет preprocessing_function аргумент, который позволяет вам передать то же самое preprocess_input функция, которую вы используете во время вывода. Эта функция выполнит для вас изменение масштаба, поэтому можно пропустить масштабирование:

from keras.applications.vgg16 import preprocess_input
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

Большинство предварительно обученных моделей в keras_applications используют одну и ту же функцию предварительной обработки. Вы можете проверить строку документации, чтобы увидеть, что она делает:

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.
Другие вопросы по тегам