When you're building a Convolutional Neural Network (CNN) from scratch and not using pre-trained weights from ImageNet or any other dataset, it's not mandatory to use the ImageNet mean for normalization. The mean values of 104, 117, and 123 are specific to the ImageNet dataset since they were computed from the statistics of that dataset's images.
Normalization is a technique used to change the range of pixel intensity values to a common scale, typically to have a zero mean and unit variance. This can help the CNN to train more efficiently as it helps to scale down the gradients and can make the training process more stable.
For your own dataset, you would ideally calculate the mean and standard deviation of your training set and then use these values to normalize your data. Here is a general way to perform this normalization:
- Compute the mean and standard deviation for each channel (R, G, B) of your training dataset.
- Subtract the mean from each pixel.
- Divide the result by the standard deviation.
Here's a pseudocode example:
# Compute the mean and standard deviation for each channel on your training set
mean = [np.mean(train_images[:, :, :, channel]) for channel in range(3)]
std = [np.std(train_images[:, :, :, channel]) for channel in range(3)]
# Normalize the images
for i in range(len(train_images)):
for channel in range(3):
batch_data[i, :, :, channel] = (train_images[i, :, :, channel] - mean[channel]) / std[channel]
Keep in mind that this normalization should be computed on the training set only to prevent information leakage from the test set. The same mean and standard deviation values should then be used to normalize the validation and test sets.
If you decide to use the ImageNet means without standard deviation normalization, it may still work, but it might not be as effective as using statistics computed from your own dataset. The benefit of using dataset-specific normalization parameters is that they are tailored to the particular data distribution you are working with.