Keras Maxpooling2d layer gives ValueError

后端 未结 7 1715
难免孤独
难免孤独 2021-02-07 00:10

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(C         


        
相关标签:
7条回答
  • 2021-02-07 00:32

    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.

    0 讨论(0)
  • 2021-02-07 00:39

    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'))
    
    0 讨论(0)
  • 2021-02-07 00:41

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

    0 讨论(0)
  • 2021-02-07 00:43

    For keras with TensorFlow try following:

    model.add(ZeroPadding2D((1, 1), input_shape=(img_rows, img_cols, channel)))
    
    0 讨论(0)
  • 2021-02-07 00:43

    Adding dim_ordering solved error for me:

    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="th"))
    
    0 讨论(0)
  • 2021-02-07 00:51

    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.

    0 讨论(0)
提交回复
热议问题