In this code , line 13 is commented as Theano dimension ordering mode. What does this mean?


Let's say you're working with 128x128 pixel RGB images (that's 128x128 pixels with 3 color channels).

When you put such an image into a numpy array you can either store it with a shape of (128, 128, 3) or with a shape of (3, 128, 128).

The dimension ordering specifies if the color channel comes first (as with theano / "th") or if it comes last (as with tensorflow / "tf").

The code you've posted contains the following line:

inputs = Input((1, img_rows, img_cols))

It specifies the shape of the input as (1, img_rows, img_cols) - i.e. there's one color channel (gray scale) and it comes first. That's why it requires Theano's dimension ordering "th".

| improve this answer | |
  • $\begingroup$ Thank you very much. What happens if the dimension ordering is not specified? $\endgroup$ – Raghuram Oct 12 '16 at 18:02
  • $\begingroup$ I think the default is "tf". For example here: keras.io/layers/convolutional/#convolution2d it says "If you never set it, then it will be "tf"." $\endgroup$ – stmax Oct 13 '16 at 6:24
  • $\begingroup$ When working with a Theano backend, is it valid to work with 'channels_last' format or should it be 'channels_first' always? $\endgroup$ – AbhinavChoudhury Apr 13 '17 at 19:00
  • $\begingroup$ @AbhinavChoudhury afaik it is valid. You can use "tf" ordering with theano and "th" ordering with tensorflow - keras will convert it into the right format. There might be a small performance penalty though (I haven't tested it). $\endgroup$ – stmax Apr 13 '17 at 20:47
  • $\begingroup$ I found out that this ordering matters a lot for performance. Using keras+theano+cudnn I corrected my wrong ordering to the correct (channel, width, height) as it should be (channel_first in keras settings) and the performance went up by a factor 3.1x. YMMV. $\endgroup$ – jlh Sep 12 '17 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.