Quoting an answer mentioned in github, you need to specify the dimension ordering:
Keras is a wrapper over Theano or Tensorflow libraries. Keras uses the setting variable image_dim_ordering
to decide if the input layer is Theano or Tensorflow format. This setting can be specified in 2 ways –
- specify
'tf'
or'th'
in~/.keras/keras.json
like so –image_dim_ordering: 'th'
. Note: this is a json file. - or specify the
image_dim_ordering
in your model like so:model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))
Update: Apr 2020 Keras 2.2.5 link seems to have an updated API where dim_ordering
is changed to data_format
so:
keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format="channels_first")
to get NCHW or use channels_last
to get NHWC
Appendix: image_dim_ordering
in 'th'
mode the channels dimension (the depth) is at index 1 (e.g. 3, 256, 256). In 'tf'
mode is it at index 3 (e.g. 256, 256, 3). Quoting @naoko from comments.