JihongJu/keras-fcn

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (250, 250, 3)

Closed this issue · 3 comments

Curious what I might be doing wrong in this initialization:
I am copying this from the README and I get the following error when run:

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (250, 250, 3)

it must be in how I am loading my images?

fcn_vgg16 = FCN(
        input_shape=(250, 250, 3),
        classes=3,
        weights=None,
        trainable_encoder=True
    )

    fcn_vgg16.compile(
        optimizer='adam',
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )

    fcn_vgg16.fit_generator(training_dataset(), verbose=2, steps_per_epoch=1500, max_queue_size=10, epochs=1)

training_dataset loads the input images as follows:

img_input = img_to_array(load_img(path_input))
img_target = img_to_array(load_img(path_target))
yield (img_input, img_target)

# (Pdb++) img_input.shape
# (250, 250, 3)
# (Pdb++) img_target.shape
# (250, 250, 3)

where img_to_array and load_img are imported from

from keras.preprocessing.image import (
    load_img,
    img_to_array,
    array_to_img
)

I think I'm not passing the batch_size properly, which means I'm misunderstand Keras's fit_generator(...) requirements

Ok I refactored my generator after looking at your for voc2011:

    img_input = img_to_array(load_img(path_input))
    img_target = img_to_array(load_img(path_target))
    batch_input = np.zeros((1,) + (250, 250, 3), dtype=K.floatx())
    batch_target = np.zeros((1,) + (250, 250, 3), dtype=K.floatx())

    batch_input[0] = img_input
    batch_target[0] = img_target

    yield (batch_input, batch_target)

And that did the trick.

Also, question for my own ignorance:

https://github.com/JihongJu/keras-fcn/blob/master/keras_fcn/models.py#L61
https://github.com/JihongJu/keras-fcn/blob/master/keras_fcn/models.py#L98

On those 2 lines the classes are hard coded against 21 instead of taking the classes value from the passed in args. Is that intentional? I know there are default 21 classes, but in the case where you are doing something custom, wouldn't it make sense to allow that to be configured?

Hi @aventurella , thank you for the suggestion! Those two places indeed should be classes passed from the argument instead of hard coded 21.

It is now fixed. Thank you! @aventurella