question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variable shaped input to convolutional layers not working anymore

See original GitHub issue

After 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:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
fcholletcommented, Nov 21, 2016

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…

1reaction
carlthomecommented, Nov 21, 2016

If you want variable sizes use None, like:

import numpy as np
from keras.models import Model
from keras.layers import Input, Conv2D

i = Input((None, None, 1))
o = Conv2D(1, 3, 3)(i)
model = Model(i, o)
model.compile('sgd', 'mse')

print(model.predict(np.random.rand(1, 10, 10, 1)))
print(model.predict(np.random.rand(1, 32, 16, 1)))

Note that I’m assuming image_dim_ordering: 'tf', meaning that the image channel is the last dimension.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found