Keras: Convolution2D documentation input_size

Created on 22 Sep 2016  路  6Comments  路  Source: keras-team/keras

In the documentation regarding Convolutional2D there is and example for
128x128 RGB image (3 channels) and input size is specified as

input_shape=(3, 128, 128) for 128x128 RGB pictures.

But if the image is B/W and has only one channel according to de documentation the obvious input_shape should be
input_shape=(1, 128, 128)

But as soon as I instantiate a model

from keras.models import Sequential
from keras.layers.convolutional import Convolution2D

img_rows, img_cols = 128, 128
nb_filters = 64
nb_conv = 3

model = Sequential()
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
                        border_mode='valid',
                        input_shape=(1, img_rows, img_cols)))

I got

ValueError: Filter must not be larger than the input: Filter: (3, 3) Input: (1, 128)

So it seems that the number of channel cannot be specified as first dimension in the input_shape parameter unless it is equal to the size of the kernel.

stale

Most helpful comment

I think it depends on the dim ordering you are using. The CHW ordering (1, 128, 128) is used by default for Theano backend, whereas HWC ordering (128, 128, 1) is used for Tensorflow backend. If you are using tensorflow and not explicitly changing the order, your image should be shaped as (128, 128, 1)

All 6 comments

model.add(Convolution2D(32, 3, 3, border_mode='same',
input_shape=X_train.shape[1:]))

@yogendratamang48 my point is try to understand if the documentation was coherent or not.
I wasn't looking for a working snippet of code to copy and paste.
The code you wrote has the effect of slicing the array removing the number of samples...
but still has the same problem if the remaining dimension are like (1, 128, 128)
Is equivalent to the one I wrote before.
My point is to understand if what is written in the doc:

input_shape=(3, 128, 128) for 128x128 RGB pictures.

is right or not... or should be written as

input_shape=(128, 128, 3) for 128x128 RGB pictures.

I think it depends on the dim ordering you are using. The CHW ordering (1, 128, 128) is used by default for Theano backend, whereas HWC ordering (128, 128, 1) is used for Tensorflow backend. If you are using tensorflow and not explicitly changing the order, your image should be shaped as (128, 128, 1)

I'm facing the same problem. I don't get how the layer is taking the input.

@bibhashthakur Read the previous answer by robertomest.
it's clearly explained there.

@abes975 Yeah I checked it out. It's working. Thanks @robertomest

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Imorton-zd picture Imorton-zd  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

nryant picture nryant  路  3Comments

LuCeHe picture LuCeHe  路  3Comments

KeironO picture KeironO  路  3Comments