Reputation: 2478
I am using the MNIST dataset from TensorFlow 2.0 and am trying to pad it with zeros and increase the image size from (28, 28, 1) to (32, 32, 1). The code is:
# Load MNIST dataset-
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train.shape, y_train.shape
# ((60000, 28, 28), (60000,))
X_test.shape, y_test.shape
# ((10000, 28, 28), (10000,))
# Pad with 2 zeros on left and right hand sides-
X_train_padded = np.pad(X_train[:,], (2, 2), 'constant')
X_train_padded.shape
# (60004, 32, 32)
However, the 'np.pad()' function used above is not giving me the desired shape of (6000, 32, 32) and also, it returns arrays filled with zeros! Instead of the original values as in X_train.
Can you help?
I am using Python 3.8, TensorFlow 2.0 and numpy 1.18.
Thanks!
Upvotes: 2
Views: 2772
Reputation: 1097
You're using numpy.pad
wrong.
(6000,32,32)
, so you only want to pad axis 1 and 2, not axis 0.pad_width
argument to np.pad
works like this: ((axis 1 pad before, axis 1 pad after), ...)
so if you want to pad 1 pixel on each side you should do ((0,0), (1,1), (1,1))
. (Your code is padding all axes 2 on each side.)Here's a toy example:
z = np.arange(12).reshape(3,2,2)
print(z.shape)
# (3, 2, 2)
z = np.pad(z, ((0,0),(1,1),(1,1)), 'constant')
print(z.shape)
# (3, 4, 4)
# as an example, take a look at the second "image"
print(z[1])
# [[0 0 0 0]
# [0 4 5 0]
# [0 6 7 0]
# [0 0 0 0]]
Upvotes: 2