1
votes

I am trying to classify greyscale images of hand written digits (28 by 28 pixels) into their 10 categories.

I already checked similar questions on this site, but I failed at solving why I am getting the error:

ValueError: cannot reshape array of size 7840000 into shape (60000,784)

If you can please help me how to fix this.

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical

def load_dataset():
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    train_images = train_images.reshape((60000, 28 * 28))
    test_images = test_images.reshape((60000, 28 * 28))

    train_labels = to_categorical(train_labels)
    test_labels = to_categorical(test_labels)

    return train_images, train_labels, test_images, test_labels

def prep_pixels(train, test):
    train_images = train_images.astype('float32') / 255
    test_images = test_images.astype('float32') / 255

    return train_images, test_images



def define_model():
    network = models.Sequential()
    network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
    network.add(layers.Dense(10, activation='softmax'))

    return network

def compile(network):
    network.compile(optimizer='rmsprop',
                    loss='categorical_crossentropy',
                    metrics=['accuracy'])

def run():
    (train_images, train_labels), (test_images, test_labels) = load_dataset()

    train_images, test_images = prep_pixels(test_images, test_images)

    network = define_model()

    compiled_network = compile(network)

    compiled_network.fit(train_images, train_labels, epochs=5, batch_size=128)

run()
2

2 Answers

0
votes

MNIST dataset consist of 60000 training images and 10000 test images. Reshape as:

test_images = test_images.reshape((10000, 28 * 28))
0
votes

A more robust version of what Ioannis recommended is

train_images = train_images.reshape((train_images.shape[0], train_images.shape[1] * train_images.shape[2]))

This should work for datasets and images of any size.

However, what I would do is flatten the array within the network. In my experience, it's best to keep the datasets as close as possible to their original form, and modify them as late as possible. That can impact performance slightly, but in this case it will not be noticeable. To do this, remove the reshape calls, and then add a Flatten layer to the network.

image_shape = (28, 28)

def load_dataset():
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    train_labels = to_categorical(train_labels)
    test_labels = to_categorical(test_labels)

def define_model():
    network = models.Sequential()
    network.add(layers.Flatten(input_shape=image_shape))
    network.add(layers.Dense(512, activation='relu'))
    network.add(layers.Dense(10, activation='softmax'))

This should have the exact same effect, except that your dataset will remain in the form of 2D images for easier analysis.