McLawrence
McLawrence

Reputation: 5245

input_shape 2D Convolutional layer in keras

In the Keras Documentation for Convolution2D the input_shape a 128x128 RGB pictures is given by input_shape=(3, 128, 128), thus I figured the first component should be the number of planes (or feature layers).

If I run the following code:

model = Sequential()
model.add(Convolution2D(4, 5,5, border_mode='same', input_shape=(3, 19, 19), activation='relu'))
print(model.output_shape)

I get an output_shapeof (None, 3, 19, 4), whereas in my understanding this should be (None, 4, 19, 19) with 4 the number of filters.

Is this an error in the example from the keras documentation or am I missing something?

(I am trying to recreate a part of AlphaGo so the 19x19 is the board size which would correspond to the images size. )

Upvotes: 3

Views: 1331

Answers (2)

Kashyap
Kashyap

Reputation: 6689

Yes it should be (None, 4, 19, 19). There is something called dim_ordering in keras that decides in which index should one place the number of input channels. Check the documentation of "dim_ordering" parameter in the documentation. Mine is set to 'tf'.

So; just change the input shape to (19, 19, 3) like so

model.add(Convolution2D(4, 5,5, border_mode='same', input_shape=(19, 19,3), activation='relu'))

Then check the output shape.

You can also modify the dim_ordering in the file usually at ~/.keras/keras.json to your liking

Upvotes: 2

Merwann Selmani
Merwann Selmani

Reputation: 1066

You are using the Theano dimension ordering (channels, rows, cols) as input but your Keras seems to use the Tensorflow one which is (rows, cols, channels).

So either you can switch to the Theano dimension ordering, directly in your code with :

import keras.backend as K K.set_image_dim_ordering('th')

Or editing the keras.json file in (usually in ~\.keras) and switching

"image_dim_ordering": "tf" to "image_dim_ordering": "th"

Or you can keep the Tensorflow dimension ordering and switch your input_shape to (19,19,3)

Upvotes: 3

Related Questions