0

I want to use mean subtraction and standardization as a normalization for my CNN model. I'm working on Keras classifying images.

However, I don't yet fully understand the difference between using mean subtraction, standardization and simple process such as rescaling images =/255.

In this question it was mentioned that there are three ways to do it:

np.mean(x) # calculates the mean of the array x
x-np.mean(x) # this is equivalent to subtracting the mean of x from each value in x
x-=np.mean(x) # the -= can be read as x = x- np.mean(x)

What I'm currently using is simple rescale:

train_data = train_data / 255

But my model performance is low. So, I decided to change the normalization and use mean subtraction but I don't know how to do it for a 3D array.

Christian Seitz
  • 728
  • 6
  • 15
user2340286
  • 69
  • 1
  • 6
  • It's the same for a 3D array; the question is just what kind of mean you want to take. For example, you can take the mean along one axis, which will give you a 2D array, or along two axes, which will give you a 1D array, or the mean of everything, which will give you a scalar. Check out the `axis` parameter in the `mean` function. – gmds Apr 18 '19 at 08:22

1 Answers1

0

There are different ways to do image normalization. It is explained here.

For your case you want to do normalization by subtracting the mean of your array. You can use the mean of a 3D array along two axis using np.mean. It will give you a scalar value that you can then subtract from your original array x.

train_data = np.random.rand(28,28,3)
mean = np.mean(train_data)
train_data -= mean

And if you want to subtract the mean for each channel than you can use axis parameter in the mean function.

mean = np.mean(train_data,axis=(0, 1))

This will give mean value for each channel and to subtract mean use as above train_data-=mean.

Further you can normalised data by subtracting mean and dividing by its standard deviation. It is used lot in machine learning applications.

Christian Seitz
  • 728
  • 6
  • 15
Krunal V
  • 1,255
  • 10
  • 23