Keras Maxpooling2d layer gives ValueError
You are using the input shape as (3,x,y) should change it to input_shape=x,y,3
I faced the same issue, I solved it by changing my padding: 'valid' to padding:'SAME': I guess its enough to add a parameter padding:' same'
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
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.
For keras with TensorFlow try following:
model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel)))