1
$\begingroup$

LeNet accepts 32X32 image. So, to use LeNet for MNIST dataset,we have to change the size from 28X28 to 32X32. I came across This implementation. i am confused about how the following line of code work.

np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')

Above line of code pad 28X28 pixed image to become a 32X32 image. Can anyone help me understand how exactly its done.

$\endgroup$
3
  • $\begingroup$ What dimension does X_train have and what is encoded in each dimension? $\endgroup$
    – André
    Commented Sep 17, 2018 at 13:00
  • $\begingroup$ X_train is (10000,28,28) $\endgroup$
    – rawwar
    Commented Sep 17, 2018 at 13:01
  • $\begingroup$ its actually MNIST dataset loaded. so, its just gray scale image $\endgroup$
    – rawwar
    Commented Sep 17, 2018 at 13:02

1 Answer 1

2
$\begingroup$

Basically, it does exactly what you specify. The used numpy function appends values in each dimension. The amount of "pads" on each axis is specified by ((0,0),(2,2),(2,2),(0,0)), given the dimension of your dataset, which is:

10000 (samples) x 28 (image dimension 1) x 28 (image dim. 2) x 1 (grayscale value!)

Now let's see what your specification means in that regard:

(0,0)Pad 0(as in the amount) values before and after each row
(2,2)Pad 2 before and 2 after each value of dim. 1 of your image data: 28 values -> 32
(2,2)Pad 2 before and 2 after each value of dim. 2 of your image data: 28 values -> 32
(0,0)Pad, again, nothing in the grayscale value dimension

That means you will end up with a 32x32 image in the respective dimension. Now, the only thing that's left is: Which values do we pad? The answer is quite simple, you do not specify any constant_values, meaning it will pad with the default constant_values (which is specified on the above linked page). Namely this value is 0.

To sum it up, simply imagine you have a 32x32 image, your 28x28 is in the middle, and on the outside you have a 2-value-thick border of 0's.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.