Search code examples
pythonkerasconv-neural-networkcrop

Keras: It is possible crop only on side with Cropping2D?


I'm trying to implement a custom model with MNIST to use in Intel NCS2.

I need to change the dimension of the third layer from 11x11x32 to 10x10x32 so that the output of the next layer is even number.

I'm using Crop2D but it always crops at least two lines of pixels but I just need it to crop a line.

I already tried to put only one to pair of cropping values but don´t work.

model = models.Sequential()
model.add(layers.Conv2D(16, 3, activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPool2D())
model.add(layers.Conv2D(32, 3, activation='relu'))
model.add(layers.Cropping2D(cropping=((1, 1), (1, 1))))
model.add(layers.MaxPool2D())
model.add(layers.Conv2D(64, 3, activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))

I expected the output of 10x10x32 but the output is 9x9x32.

Is it possible to do this?


Solution

  • From the docs here (keras.io/layers/convolutional) it looks like you can input how many rows/cols to crop and whether it is from the top/bottom left/right using the cropping tuple you are passing, eg: "If tuple of 2 tuples of 2 ints: interpreted as ((top_crop, bottom_crop), (left_crop, right_crop))" If you only wanted to to crop the right of the image, for example, pass cropping=((0, 0), (0, 1))