Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: padding must be zero for average_exc_pad #4599

Closed
Shaofanl opened this issue Dec 5, 2016 · 4 comments
Closed

ValueError: padding must be zero for average_exc_pad #4599

Shaofanl opened this issue Dec 5, 2016 · 4 comments

Comments

@Shaofanl
Copy link
Contributor

Shaofanl commented Dec 5, 2016

I got ValueError: padding must be zero for average_exc_pad when training the inception network (predicting works). Here is a minimal script to cause that error:

import numpy as np
from keras.models import Model
from keras.utils.np_utils import to_categorical
from keras.layers import Flatten, Dense, Input, BatchNormalization, merge
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D

if __name__ == '__main__':
    input_shape = (None, 3, 12, 12)

    bs, c, h, w = input_shape
    data = Input(shape=(c, h, w))

    conv1 = Convolution2D(32, 3, 3, subsample=(1, 1), border_mode='same')(data)

    pool = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(conv1)

    flat = Flatten()(pool)

    fc = Dense(10, activation='softmax')(flat)

    m = Model(data, fc)
    m.compile('sgd', 'categorical_crossentropy')

    x = np.random.random((100,)+input_shape[1:])
    y = np.random.randint(10, size=(100,1))
    y = to_categorical(y, 10)
    m.predict(x)
    print 'predict done'
    m.fit(x, y)
    print 'fit done'
@Shaofanl
Copy link
Contributor Author

Shaofanl commented Dec 5, 2016

I tried to replace line

 pool = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(conv1)

into

#   pool = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(conv1)      # (32, 12, 12) cannot be trained
#   pool = AveragePooling2D((3, 3), strides=(1, 1))(conv1)                          # (32, 10, 10) can be trained
    pool = AveragePooling2D((3, 3), strides=(1, 1))(ZeroPadding2D((1,1))(conv1))    # (32, 12, 12) can be trained

and the third option works.

However, when I output the result of the first and the third line with:

    check_pool = Model(data, [AveragePooling2D((3, 3), strides=(1, 1))(ZeroPadding2D((1,1))(conv1)),
                              AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(conv1)])
    res = check_pool(x)
    res[0] == res[1]

, I found they are same in all cells except the border.

Is there something wrong with Keras or in the implementation in here?

@fchollet
Copy link
Member

The solution to this issue is to use the TensorFlow backend. This appears to be a Theano issue...

@Shaofanl
Copy link
Contributor Author

Shaofanl commented Apr 25, 2017

Manually using ZeroPadding2D((1,1)) to pad is another choice.
But why the padding results in the following comment are different?

@stale stale bot added the stale label Jul 24, 2017
@stale
Copy link

stale bot commented Jul 24, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot closed this as completed Aug 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants