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

snippet from guide throws ValueError when using Convolution3D instead of 2D #5108

Closed
neptunes5thmoon opened this issue Jan 20, 2017 · 2 comments

Comments

@neptunes5thmoon
Copy link
Contributor

For the architecture I want to implement I need layers that work on inputs of different sizes while sharing weights.
The following code snippet from the guide to the functional API (The concept of layer 'node') achieves that for Convolution2D and works fine:

from keras.layers import Input, Convolution2D
a = Input(shape=(32, 32, 3))
b = Input(shape=(64, 64, 3))

conv = Convolution2D(16, 3, 3, border_mode='same')
conved_a = conv(a)

# only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 3)

conved_b = conv(b)
# now the `.input_shape` property wouldn't work, but this does:
assert conv.get_input_shape_at(0) == (None, 32, 32, 3)
assert conv.get_input_shape_at(1) == (None, 64, 64, 3)

However, running the equivalent code for Convolution3D results in a ValueError because the input shape expected by the layer seems to be fixed to the first shape it saw.

from keras.layers import Input, Convolution3D
a = Input(shape=(32, 32, 32, 3))
b = Input(shape=(64, 64, 64, 3))

conv = Convolution3D(16, 3, 3, 3, border_mode='same')
conved_a = conv(a)

# only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 32, 3)

conved_b = conv(b)
ValueError: Input 0 is incompatible with layer convolution3d_1: expected shape=(None, 32, 32, 32, 3), found shape=(None, 64, 64, 64, 3)

I'm using the tensorflow backend (and dim_ordering) on gpu.

Any ideas what might cause this inconsistency between Convolution2D and 3D?

@gvtulder
Copy link
Contributor

@lheinric: Convolution3D fixes the input shape when the layer is built, which happens during the first call. This input shape is useful for some optimisations. Convolution2D does not fix the input shape. The patch in the pull request should fix your problem.

@neptunes5thmoon
Copy link
Contributor Author

Thank you so much! Your PR fixes my problem - so I'll close this.

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