Variable shaped input to convolutional layers not working anymore
See original GitHub issueAfter some months I updated keras (I was using keras 1.0.5 i think) and now my model can no longer work with input images with different shapes.
Take this small code as an example:
import numpy as np
from keras.models import Sequential
from keras.layers import Convolution2D
m = Sequential()
m.add(Convolution2D(1,3,3, input_shape=(1,10,10)))
m.compile(loss="mae", optimizer="sgd")
c = m.predict(np.random.rand(1,1,10,10).astype('float32'))
c = m.predict(np.random.rand(1,1,20,20).astype('float32'))
>>>Exception: Error when checking : expected convolution2d_input_1 to have shape (None, 1, 10, 10) but got array with shape (1, 1, 20, 20)
I managed to find out that a “image_dim_ordering”:“th” had to be added to the keras.json so that It could work with an input size matching the defined one, however I am unable to find any way to correct the problem when actual input size is different. This used to work in previous versions, generating an output of the correct shape.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How Do Convolutional Layers Work in Deep Learning Neural ...
In summary, we have a input, such as an image of pixel values, and we have a filter, which is a set of...
Read more >What should I do when my neural network doesn't learn?
Finally, the best way to check if you have training set issues is to use another training set. If you're doing image classification,...
Read more >What is wrong with Convolutional neural networks ?
Convolutional layers. A Convolutional layer have a set of matrices that get multiplied by the previous layer output in a process called the ......
Read more >How to handle variable sized input in CNN with Keras?
Usually, a model containing Dense layers cannot have variable size inputs, unless the outputs are also variable.
Read more >Convolutional Layer - an overview | ScienceDirect Topics
Convolutional layers receive as input an image A(m−1) (with Km channels) and ... This mapping function does not require any weight sharing anymore...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It has nothing to do with any new syntax. In your own code, you should use
m.add(Convolution2D(1,3,3, input_shape=(1, None, None)))
instead of
m.add(Convolution2D(1,3,3, input_shape=(1, 10, 10)))
Your code could never have worked on variable-size inputs in the first place…
If you want variable sizes use
None
, like:Note that I’m assuming
image_dim_ordering: 'tf'
, meaning that the image channel is the last dimension.