Skip to content Skip to sidebar Skip to footer

Keras Maxpooling2d Layer Gives Valueerror

I am trying to replicate VGG16 model in keras, the following is my code: model = Sequential() model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) model.add(Convolution2D(64, 3,

Solution 1:

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 -

  1. specify 'tf' or 'th' in ~/.keras/keras.json like so - image_dim_ordering: 'th'. Note: this is a json file.
  2. 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.

Solution 2:

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'))

Solution 3:

You are using the input shape as (3,x,y) should change it to input_shape=x,y,3

Solution 4:

For keras with TensorFlow try following:

model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel)))

Solution 5:

The accepted answer works. But you can also do the following:

    model.add(MaxPooling2D((2, 2), name='block1_pool', data_format='channels_last')

Keras assumes input to be (width, height, channels) for TensorFlow backend and (channel, width, height) for Theano backend. Since your input_shape=(3,224,224), specifying data_format='channels_last' should do the trick.

Post a Comment for "Keras Maxpooling2d Layer Gives Valueerror"