5

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

jlh
  • 103
  • 3
pseudomonas
  • 1,032
  • 3
  • 13
  • 30

1 Answers1

11

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".

stmax
  • 1,627
  • 15
  • 18
  • Thank you very much. What happens if the dimension ordering is not specified? – pseudomonas Oct 12 '16 at 18:02
  • I think the default is "tf". For example here: https://keras.io/layers/convolutional/#convolution2d it says "If you never set it, then it will be "tf"." – stmax Oct 13 '16 at 06:24
  • When working with a Theano backend, is it valid to work with 'channels_last' format or should it be 'channels_first' always? – AbhinavChoudhury Apr 13 '17 at 19:00
  • @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). – stmax Apr 13 '17 at 20:47
  • 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. – jlh Sep 12 '17 at 18:55