0 votes
1 view
in Machine Learning by (15.7k points)

I can not for the life of me figure out how to switch the image ordering. images are read in (x,x,3) format, theano requires it to be in (3,x,x) format. I tried changing the order with

numpy.array([img[:,:,i] for i in range(3)])

which I guess gets the job done, but it is both ugly and I can't figure out how to reverse it to get the original image back.

1 Answer

0 votes
by (33.2k points)

There are various methods to do that:

Reorder data

You can simply use numpy.rollaxis to move the axis 3 to position 1 

For example:

np.rollaxis(imagesArray, 3, 1)  

When using keras, you might want to change its configuration. But Theano library doesn't need anything if Keras is being used.

Keras can be configured with channels first or channels last, besides allowing you to define it in every individual layer, so you don't have to change your data.

To configure keras

You should find the keras.json file, then change it. The file is usually installed in




depending on your OS.

You should change "image_data_format": "channels_last" to "channels_first" or vice-versa

When working with "channels_last" is less troublesome because of a great number of other functions that work only on the last axis.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !