Rescaling images in mnist

Viewed 192

So I am trying to load the data and rescale the images values and return the rescaled train and test sets. Here is what I am trying:

import numpy as np
from tensorflow import keras
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
datagen = ImageDataGenerator(rescale=1.0/255.0)

It is incomplete and I don't know how to proceed after this

1 Answers

You can use flow method in ImageDataGenerator (check more on documentation) as follows:

It expects the input dimension to be reshaped with 4 dimension. Since mnist dataset have 60000 images and 28*28 height and width with a single channel. The reshaped size would be (60000, 1, 28, 28)

training_set = datagen.flow(train_images.reshape(train_images.shape[0], 1, 28, 28), train_labels)

test_set = datagen.flow(test_images.reshape(test_images.shape[0], 1, 28, 28), test_labels)

You should define your model architecture as you needed then use fit_generator function.

Train your model

classifier.fit_generator(training_set, 
                         steps_per_epoch=10, 
                         validation_data= test_set, 
                         validation_steps=20, 
                         epochs=5)
Related