Vega
Vega

Reputation: 17

why do we reshape the Mnist training images to (60000,28,28,1) instead of using it directly like this (60,28,28)?

This code is used in training model for image classification using Mnist dataset. what I don't understand is why we reshape the training images to (60000,28,28,1) instead of using it directly like this (60,28,28).

num_classes = 10
input_shape = (28, 28, 1)


(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

#print(x_train[0])

x_train = x_train.astype("float32") / 255 

#print(x_train[0])

x_test = x_test.astype("float32") / 255

print(x_train.shape)
print(x_test.shape)

x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print("x_train shape:", x_test.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")

print()
print(y_train)

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

print()
print(y_train)

Upvotes: 2

Views: 3472

Answers (2)

wanjiku
wanjiku

Reputation: 269

It is important to reshape inorder to accommodate other architectures for example inceptionv3 that needs a minimum of 75*75 using the tensorflow.image.resize

x_train = tensorflow.image.resize(x_train, [75,75])
x_test = tensorflow.image.resize(x_test, [75,75])

Upvotes: 0

Kuldeep Singh
Kuldeep Singh

Reputation: 108

In Machine Learning understanding data is very important, just like this case. There are 60000 train images to start with and 10000 images for testing purpose.

Each image have size of 28*28 pixels; that is 28 pixels height and 28 pixels width and hence (28, 28, 1), 1 in last part is to specify color depth of the pixel. 1 is for greyscale image(black and white image).

So using (60, 28, 28, 1) is out of the question here. Now why do we use (60000, 28, 28, 1)- this is the shape of matrix our data have since we have 60000 images in which 28*28 pixels and each pixel have a value in this matrix.

To simplify assume we have only 1 image then it would be like (1, 28, 28, 1) and would be written in matrix form easily as a 28*28 matrix.

Upvotes: 1

Related Questions