Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data format convention in Keras (channels_last or channels_first) should be used when?

I'm a newbie in deep learning and confused which data format convention should be used when. According to the https://keras.io/backend/, there are two data format conventions.

channels_last for 2D data: (rows, cols, channels)
channels_first: for 2D data: (channels, rows, cols)

Why there is a channels_first option in Keras? When should I use it? Is there any historical reason like BGR usage in OpenCV?

" BGR was a choice made for historical reasons and now we have to live with it. In other words, BGR is the horse’s ass in OpenCV."
https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/

like image 238
saki Avatar asked Sep 14 '25 15:09

saki


2 Answers

I believe the reason that there are two data formats, is that Keras supports Theano as another backend too. In Theano, the first axis represents the channels.

like image 53
Godfather Avatar answered Sep 17 '25 09:09

Godfather


Tensorflow data_format accepts 2 values- channels_last (default) or channels_first.

Its represents the ordering of the dimensions in the inputs.

channels_last corresponds to inputs with shape (batch_size, height, width, channels)

channels_first corresponds to inputs with shape (batch_size, channels, height, width).

Code: tf.keras.layers.ZeroPadding2D(padding=(3, 3), input_shape=(64, 64, 3), data_format='channels_last') No batch size

Look at Tensorflow document

like image 25
Abhishek Trivedi Avatar answered Sep 17 '25 10:09

Abhishek Trivedi