What is the correct way to change image channel ordering between channels first and channels last?

后端 未结 5 586
暗喜
暗喜 2021-02-02 07:28

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 ord

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 07:43

    To reorder data

    You can use numpy.rollaxis to roll the axis 3 to position 1 (considering you have the batch size as dimension 0).

    np.rollaxis(imagesArray, 3, 1)  
    

    But, if you're using keras, you might want to change its configuration or define it per layer. Theano doesn't require anything from you if you're using Keras.

    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

    Find the keras.json file and change it. The file is usually installed in C:\Users\yourusername\.keras or ~/.keras depending on your OS.

    Change "image_data_format": "channels_last" to "channels_first" or vice-versa, as you wish.

    Usually, working with "channels_last" is less troublesome because of a great amount of other (non convolutional) functions that work only on the last axis.

    Defining channel order in layers.

    The Keras documentation has all information about parameters for layers, including the data_format parameter.

提交回复
热议问题