Reputation: 3
I need to train a LeNet5 CNN on the MNIST dataset. Regarding the original paper of LeNet5 I want to train on input image size 32 by 32 and not on image size 28 by 28, to have the same number of parameters.
I tried to use tf.pad and numpy.pad function but I'm facing the same problem : I can have (31,32) or (32,31) returned image size but when I try (32,32) or more, functions return arrays filled with zero instead of X_train original values + padding values. Here the code and results :
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
print(X_train.shape)
print('Before padding',X_train[0])
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=0, name=None)
print('After padding',X_train_pad[0])
(60000, 28, 28)
Before padding [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]
After padding tf.Tensor(
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]], shape=(32, 32), dtype=uint8)
I can see padding value and 0 instead of original value of X_train when I padding with 1 :
X_train_pad = tf.pad(X_train,([0,0],[2,2],[2,2]), mode='CONSTANT', constant_values=1, name=None)
After padding tf.Tensor(
[[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]
[1 1 0 ... 0 1 1]
...
[1 1 0 ... 0 1 1]
[1 1 1 ... 1 1 1]
[1 1 1 ... 1 1 1]], shape=(32, 32), dtype=uint8)
Have you an idea of what is my mistake ?
Thanks
Upvotes: 0
Views: 377
Reputation: 17585
There is no mistake.
The padding is working correctly.
You are visually inspecting it and drawing conclusions that it is incorrect, instead of using your computer to simply test it.
import numpy as np
import tensorflow as tf
# get data
(X_train, _), (_, _) = tf.keras.datasets.mnist.load_data()
# pad
X_train_pad = tf.pad(X_train, [[0, 0], [2, 2], [2, 2]], mode="CONSTANT")
# extract the original image by manually removing the
# padding from the borders
extract_original_image = X_train_pad[:, 2:-2, 2:-2]
print(extract_original_image.shape)
# TensorShape([60000, 28, 28])
# check padded image with padding removed equals original
np.testing.assert_equal(extract_original_image.numpy(), X_train)
# check padding is all zeros
assert tf.math.reduce_sum(X_train_pad[:, :2, :2]).numpy() == 0
assert tf.math.reduce_sum(X_train_pad[:, -2:, -2:]).numpy() == 0
# check the sums are the same
assert tf.math.reduce_sum(X_train_pad) == tf.math.reduce_sum(X_train)
Upvotes: 1