bsky
bsky

Reputation: 20222

Negative dimension size on MaxPool

I've implemented the following model with Keras. Underneath, I am using Tensorflow.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

model = Sequential()
model.add(Conv2D(32, (2, 2), input_shape=(3, 150, 150), padding='SAME'))
model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_last"))

model.add(Conv2D(32, (2, 2), padding='SAME'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (2, 2), padding='SAME'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

However, I get this exception:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,75,32].

I am using SAME padding, and a convolution of 2*2, so the output should be 2*2 and I should be able to run MaxPooling on it.

I just can't understand how the input can get to the shape [?,1,75,32].

Upvotes: 2

Views: 1174

Answers (1)

lhk
lhk

Reputation: 30026

The problem is the input size of your first convolution.

You're using tensorflow and the "channels-last" layout. Therefore, you need to specify the x and y resolution before the channels.

model.add(Conv2D(32, (2, 2), input_shape=(150, 150, 3), padding='SAME'))

Your initial shape is [?, 3, 150, 150], pooling applies to the second and third item, that's how the shape [?, 1, 75, 32] was created. The 32 is the number of filters in your convolution.

If you want to keep your filters along the first axis, Keras has a configuration file, under Linux this should be in /home/user/.keras/keras.json. In this configuration file you can change the channel layout, the option is called image_data_format. There you can switch between channels_first and channels_last.

Upvotes: 3

Related Questions