Minnie
Minnie

Reputation: 5

Resize MNIST dataset from (60000, 28, 28) to (60000, 14, 14)

I found the method of resizing the MNIST training dataset from (60000, 28, 28) to (60000, 14, 14).

This is the code and results:

import tensorflow as tf
import numpy as np

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train[..., np.newaxis], x_test[..., np.newaxis]

x_train_small = tf.image.resize(x_train, (14,14)).numpy()
x_test_small = tf.image.resize(x_test, (14,14)).numpy()


print(x_train.shape)
print(x_test.shape)
print(x_train_small.shape)
print(x_test_small.shape)
​
>>>(60000, 28, 28, 1)
>>>(10000, 28, 28, 1)
>>>(60000, 14, 14, 1)
>>>(10000, 14, 14, 1)

  1. I'm confused about why it has to add a new axis to change the shape that I want.
  2. I would like to know whether there is another method to do the resize work without adding a new axis.

Upvotes: 0

Views: 813

Answers (1)

mcsoini
mcsoini

Reputation: 6642

This is all described in the docs:

  • The first argument of resize is images: "4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels]."
  • The second is size: "A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images."

Conclusion: You need the fourth dimension because those are the channels which tf.image.resize expects no matter what. The size along that dimension is 1 because the MNIST image are grayscale.

Of course you could use a some other library to resize, but personally I would avoid unnecessary dependencies, just for the sake of cleanliness.

Upvotes: 1

Related Questions