tensorflowkerasdeep-learningneural-networkmnist

Reshaping image and Plotting in Python


I am working on mnist_fashion data. The images in mnist_data are 28x28 pixel. For the purpose of feeding it to a neural network(multi-layer perceptron), I transformed the data into (784,) shape.

Further, I need to again reshape it back to the original size.

For this, I used below given code:-

from keras.datasets import fashion_mnist
import numpy as np
import matplotlib.pyplot as plt


(train_imgs,train_lbls), (test_imgs, test_lbls) = fashion_mnist.load_data()
plt.imshow(test_imgs[0].reshape(28,28))

no_of_test_imgs  = test_imgs.shape[0]

test_imgs_trans  = test_imgs.reshape(test_imgs.shape[1]*test_imgs.shape[2], no_of_test_imgs).T

plt.imshow(test_imgs_trans[0].reshape(28,28))

Unfortunately, I am not getting the similar image. I am not able to understand why this is happening.

expected image: enter image description here

recieved image:enter image description here

Kindly help me to resolve the problem.


Solution

  • pay attention when you flatten the images in test_imgs_trans

    (train_imgs,train_lbls), (test_imgs, test_lbls) = tf.keras.datasets.fashion_mnist.load_data()
    
    plt.imshow(test_imgs[0].reshape(28,28))
    
    no_of_test_imgs  = test_imgs.shape[0]
    
    test_imgs_trans  = test_imgs.reshape(no_of_test_imgs, test_imgs.shape[1]*test_imgs.shape[2])
    
    plt.imshow(test_imgs_trans[0].reshape(28,28))