Search code examples
deep-learningmnist

Deep learning: cannot reshape array in changing MNIST images size


I am new on deep learning.I am trying to change mnist images from 28*28 into 224 * 224.

So I decided to use reshape method. After importing MNIST data set I try to reshape it:

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(X_train.shape[0], 224, 224, 1).astype('float32')

I am try to change all MNIST image to new size but I got this error:

X_train = X_train.reshape(X_train.shape[0], 224, 224, 1).astype('float32')
Traceback (most recent call last):

  File "<ipython-input-3-9d12d34bfd75>", line 1, in <module>
    X_train = X_train.reshape(X_train.shape[0], 224, 224, 1).astype('float32')

ValueError: cannot reshape array of size 47040000 into shape (60000,224,224,1)

How can I change all MNIST image size into new size?


Solution

  • As in the comment @datdinhquoc said reshaping keeps the same number of bytes

    e.g. if you have array of shape (4,3) , basically there m * n that is 12 (4*3) elements. Now you can reshape it into any size whose multiplication gives you 12. So the array cab be reshaped into size (2,6) or (12,1) .

    In your case there are 60,000 elements of size (28, 28), so there are total (60,000 * 28 * 28) and its trying make it (60,000 * 224 * 224), which is obviously impossible.

    You want to scale or resize the image. Means you want to increase its bytes. In that case you can use openCV resize function like below

     resized_image = cv2.resize(image, (224, 224))   
    

    So you need to resize all the images one by one.