4

I've checked all the similar posts, but my error isn't being fixed with the fixes suggested. Thanks in advance for any help!

I'm using a tensorflow backend with Keras, and my images have dimensions 1185 by 676. Most of the code is from one of the Keras examples.

I'm getting ValueError: Negative dimension size caused by subtracting 2 from 1 for 'MaxPool' (op: 'MaxPool') with input shapes: [?,1,1183,32]. This error disappears when I switch to dim_ordering="th", which is odd, considering that I'm using tensorflow, not theano.

The code up to this point:

img_width, img_height = 1185, 676

train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 32
nb_validation_samples = 8
nb_epoch = 3

model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering="tf"))

And just in case the data generation is part of the issue:

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        batch_size=4,
        target_size=(img_width, img_height),
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        batch_size=4,
        target_size=(img_width, img_height),
        class_mode='binary')

model.fit_generator(
        train_generator,
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epoch,
        validation_data=validation_generator,
        nb_val_samples=nb_validation_samples)
daniel.rigberg
  • 118
  • 1
  • 8
  • Update: It looks like the dimensions weren't divisible by the pool size. Now I'm getting `ValueError: Negative dimension size caused by subtracting 3 from 1 for 'Conv2D' (op: 'Conv2D') with input shapes: [?,1,1185,676], [3,3,676,32].` – daniel.rigberg Jan 18 '17 at 00:04
  • Update: Fixed the pool sizes. Now, during fitting: `ValueError: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 1185, 676) but got array with shape (2, 1185, 676, 3)` – daniel.rigberg Jan 18 '17 at 00:10
  • Your input shape is inconsistent with image_ordering = "tf", why are you forcing an image ordering in just one layer? – Dr. Snoopy Jan 18 '17 at 09:01
  • Ah!!! I can't believe that I didn't see that sooner--that's exactly the problem. Thank you! – daniel.rigberg Jan 18 '17 at 15:17

1 Answers1

2

The image dimension ordering has been mixed in your code. Multiple way to fix this.

One way is to add

from keras import backend as K
K.set_image_dim_ordering('tf')

at the beginning of your code.

Other approaches are summarized in this answer

Community
  • 1
  • 1
pyan
  • 3,577
  • 4
  • 23
  • 36