How to do fine tune and transfer learn CNNs in a new dataset using imagenet std and mean at data augmentation phase?

I did some fine-tuning training using CNNs for diverse applications. Honestly, I never really needed to perform Imagenet normalization in new datasets as the results were already quite good without that procedure.

Now I have for the first time a new dataset on which the results are really bad without the ImageNet normalization and I would like to give it a try. However, as I never used it, I did some research and tried to use it. However, to perform training with data augmentation, the source code seems strange to me.

Here is what the co-pilot recommended to me.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define the mean and standard deviation of the original dataset (e.g., ImageNet)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]

# Create an instance of ImageDataGenerator and specify the normalization parameters
datagen = ImageDataGenerator(
rescale=1./255,  # Scale the pixel values to [0, 1]
featurewise_center=True,  # Apply feature-wise centering
featurewise_std_normalization=True  # Apply feature-wise standard deviation scaling)

# Compute the mean and standard deviation of the original dataset (e.g., 
#ImageNet)
# Note: Replace `train_images` with the original dataset images
datagen.fit(train_images)

# Apply data augmentation and normalization to the input images
augmented_images = datagen.flow(train_images, train_labels, batch_size=batch_size)

However, this co-pilot source code seems strange, as the ImageDataGenerator documentation says, featurewise_center sets the input mean to 0 over the dataset, and samplewise_center sets each sample mean to 0. It is not said anywhere that the featurewise_center and samplewise_center will use the means and stds from Imagenet (it seems they use the means and stds of the dataset I am using) and also not from variables std and mean the co-pilot recommended me to declare at the beginning of the source code.

So, my question is: how do I add the std and mean normalization using values from Imagenet at the data augmentation phase? is the source code above correct?

Just to highlight: lots of tutorials on the internet on fine-tuning and transfer learning never explicitly showed or discussed the Imagenet normalization in the source code. Additionally to my first question, is the Imagenet normalization useful in new datasets?

  • If you want to use the ImageDataGenerator (beware that it is deprecated and not recommended for new code in its docs), I think you could use the preprocessing_function parameter to use a small function which applies the fixed mean and std dev to every image.

    – 

Leave a Comment