0

This is part of my learning. I Understood the normalization is really helping to improve accuracy, and hence divided by 255 the mnist values. This will divide all the pixels by 255, and hence all the pixels of 28*28 will have the values in range from 0.0 to 1.0 .

Now i tired to multiply the same with 255,this essentially means we should get the original value back. but when i display the picture, both the original and de-normalised pictures are different.

(trainX, trainY), (testX, testY) = mnist.load_data()


plt.subplot(2,2,1)
plt.imshow(trainX[143])

trainX /= 255

plt.subplot(2,2,2)
plt.imshow(trainX[143])


trainX *= 255

plt.subplot(2,2,3)
plt.imshow(trainX[143])
plt.show()

OUtput:

enter image description here

What am i missing?. Anything related to float and int data type of the input data?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Whoami
  • 13,930
  • 19
  • 84
  • 140
  • 1
    I am assuming this is strongly connected to the datatype of your matrices. Have you inspected the single matrix before you multiply it with 255? And what about after? – dennlinger Sep 19 '18 at 06:01
  • Thanks for reply. I didn't get you. sry, could you elaborate more? – Whoami Sep 19 '18 at 06:12
  • Have you manually checked the values of `trainX[143]` before and after you multiply? It seems there is a data type issue, i.e. instead of a floating point value, you get only integers or something the like. – dennlinger Sep 19 '18 at 06:21
  • @Whoami Are you sure this code runs for you without errors? Because `trainX /= 255` [would raise an error](https://stackoverflow.com/q/48948308/2099607) due to type change (i.e. from uint to float). What is your Python version? Plus, when I change it to `trainX = trainX / 255` it works for me and all the plotted images are the same. I am using python 3.5.2. – today Sep 19 '18 at 09:46

1 Answers1

1

MNIST is stored as 28x28 uint8 numpy arrays, when you divide by 255, it transform the data to floating point in order to make the division, which ends up as a float numpy array. So when you multiply with 255, its still a float array, and matplotlib might interpret it differently for purposes of plotting.

For this to work properly, you have to cast the data to uint8, like:

trainX = (trainX * 255).astype(np.uint8)

Then it should plot correctly.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140