# Proper way to reshape a image for training using CNN

I am new to Keras and facing some problems figuring out how to reshape the input image data properly. I have $$16 x 16$$ images, each with three layers, i.e., R, G, and B. The image data is in the form of a NumPy array with shape (100000,768); the first 256 entries are for the red layers, and so on.

Now, I would like to feed these images into a sequential model with an input shape of $$16 x 16 x 3$$. Is it okay if I reshape my image using:

X.reshape(100000,16,16,3)


here I have 100000 images. The reason I am asking this is that I have seen some posts where they have recommended to use:

X.reshape(100000,3,3,16).transpose(0,2,3,1)


Can someone please explain the correct method?

X.reshape(100000, 3, 16, 16).transpose(0, 2, 3, 1)


This method first reshapes the data into a 4D array with dimensions (100000, 3, 16, 16), where the first dimension represents the number of images, the second dimension represents the number of layers (R, G, and B), and the third and fourth dimensions represent the image height and width. Then, it uses transpose to rearrange the dimensions to the desired order (100000, 16, 16, 3), where the last dimension now represents the number of channels (R, G, and B).

## How transpose works:

• The first argument (0) indicates that the first dimension of the array remains unchanged. In this case, it's the number of images (100000), so it will stay as the first dimension.

• The second argument (2) indicates that the third dimension of the original array becomes the second dimension in the transposed array. In this case, it's the height of each image (16).

• The third argument (3) indicates that the fourth dimension of the original array becomes the third dimension in the transposed array. In this case, it's the width of each image (16).

• The fourth argument (1) indicates that the second dimension of the original array becomes the fourth dimension in the transposed array. In this case, it's the number of layers (R, G, and B), so it will become the channel dimension (3).

Both methods will yield the same final shape, which is what you need for your sequential model. You can use either of the methods based on your preference. The important thing is that the resulting shape should be (100000, 16, 16, 3) to represent 100000 images, each with a size of 16x16 and three-color channels (R, G, and B).