Reputation: 1266
I'm very new to Keras, and in my early experiments I've been encountering an error when I create convolutional layers. When I run this code (found here; "Shared vision model" section)
from keras.models import *
from keras.layers import *
from keras.layers.convolutional import *
from keras.layers.pooling import *
digit_input = Input(shape=(1, 27, 27))
x = Convolution2D(64, 3, 3)(digit_input)
x = MaxPooling2D((2, 2))(x)
mdl = Model(digit_input, x)
I get the following error (traceback points to the Convolution2D line):
ValueError: Filter must not be larger than the input: Filter: (3, 3) Input: (1, 27)
I'd assume I was misusing the interface--especially since no one else seems to have this particular problem--but I pulled the code directly from the Keras docs. The problem persists when I use the model.add()
syntax. Interestingly though, when I shrink the filter size to 1x1 or increase the number of input channels to 3, the error goes away. I have tried manually setting dim_ordering='tf'
for the convolution and my keras.json file reads:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
Keras looks like a great tool, but I'm completely unable to use it so far.
P.S. I am using Keras 1.1.0 and Tensorflow 0.10.
Upvotes: 1
Views: 1519
Reputation: 2144
Its a channel first-last problem. The default is channel last, and if your image will be in the shape (27,27,1) it will work.
digit_input = Input(shape=(27, 27, 1))
x = Convolution2D(64, 3, 3)(digit_input)
x = MaxPooling2D((2, 2))(x)
mdl = Model(digit_input, x)
mdl.summary()
You will get:
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 27, 27, 1)] 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 9, 9, 64) 640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 4, 4, 64) 0
=================================================================
Total params: 640
Trainable params: 640
Non-trainable params: 0
Upvotes: 0
Reputation: 76
Try this
digit_input = Input(shape=(27, 27, 1))
x = Convolution2D(64, 3, 3, dim_ordering='tf')(digit_input)
Upvotes: 0
Reputation: 368
'image_dim_ordering' : 'tf' is setting the input's third dimension as the channel input to the 2D Convolution: ie you've currently put a 1x27 input with 27 channels in to be convolved with a 3x3 kernel and no padding.
You should be able to switch 'image_dim_ordering' to 'th' and have this code immediately work. I think 'th' used to be the default and you've unfortunately hit an obsolete tutorial. Here's the Keras documentation from Convolution2D - it's there but not particularly prominent:
dim_ordering: 'th' or 'tf'. In 'th' mode, the channels dimension (the depth) is at index 1, in 'tf' mode is it at index 3. It defaults to the image_dim_ordering value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be "tf".
Hope that helps!
Upvotes: 2