Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the order of picture channels by Keras

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how Keras changes the order of picture channels". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Error

When using Keras.layers.convolutional.Convolution2D

Convolution2D (32, 5, 5, border_mode='valid', input_shape= (1,28,28), activation='relu')

The following error is reported:

OverflowError: Range exceeds valid bounds

This is due to the wrong order in which Keras configures the image channel. The above code uses the image channel in the order of [channels] [height] [width]

two。 Modify the order of picture channels

Check the ~ / .keras / keras.json file

If "image_dim_ordering": is "th" and "backend": "theano", your input_shape must be (channels, height, width) if "image_dim_ordering": is "tf" and "backend": "tensorflow", your input_shape must be (height, width, channels)

Therefore, make sure that the order of the channels you use is the same as that of the configured channels.

Or by modifying it like this.

From keras import backendbackend.set_image_dim_ordering ('th')

Add: Keras sets the order of backend data dimensions

Keras is a more advanced deep learning framework, which encapsulates several common mainstream deep learning frameworks, that is, according to Keras, its back end is encapsulated on the basis of Tensorflow, Theano and CNTK.

I don't know much about CNTK. The dimension order of tensorflow's tensor is different from that of Theano, so pay special attention to this when using Keras. If the backend is different, the data dimension order should be different.

The default order of tensorflow's data dimensions is channels_last, that is to say, one of its tensor's data dimensions is [samples,rows,cols,channels], while Theano's data order is channels_first, that is, [samples,rows,cols,channels], which is actually the difference between the two frameworks. If you use keras to write a deep learning model, there is basically no problem as long as you set up the back end. There is no need to think too much about the data dimension.

But the data dimension of Keras can be changed, even if tensorflow is used as the backend, the data format of Keras can also be channels_first, and this can be changed through configuration files or code.

The configuration file of Keras is $HOME/.keras/keras.json by default. You can use the txt editor to directly modify the backend configuration:

{"floatx": "float32", "epsilon": 1e-07, "image_data_format": "channels_last", "backend": "tensorflow"}

This is the configuration information under my windows. By default, tensorflow is used as the backend, and the data format is channels_last. However, I have a small project that was written with Theano as the backend at the beginning, which only needs to be changed to Theano. However, since only this project needs to be changed to Theano, I do not directly change the configuration file, but modify the backend in the code. Here, you need to use the backend backend of Keras to implement it. The modified code is as follows:

From keras import backend as BKBK.set_image_data_format ("channels_first") BK.set_image_dim_ordering ("th")

After this setting, when the program runs, the back end will not be changed, only the dimensional order of the data will be changed, and only for the current program, it will not affect other programs.

This is the end of the content of "how Keras changes the order of picture channels". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report