broadinstitute / keras-rcnn

Keras package for region-based convolutional neural networks (RCNNs)
Other
552 stars 224 forks source link

ObjectDetectionGenerator mask_size appears to be hardcoded #187

Closed brandenkmurray closed 6 years ago

brandenkmurray commented 6 years ago

It appears that the mask size in being hardcoded in https://github.com/broadinstitute/keras-rcnn/blob/d544263e7faef49ccd27d4240bf94c3086d57a18/keras_rcnn/preprocessing/_object_detection.py#L98

If I specify a mask_size other than (28, 28) I wind up getting errors during training because the mask_shape specified in keras_rcnn.models.RCNN doesn't match the output of the generator.

0x00b1 commented 6 years ago

Hi, @brandenkmurray! Thanks for the issue.

We’re still working on the Mask RCNN implementation so everything is still shaky. We expect that to settle down soon.

I wrote a hypothetical example where I modified the mask_shape that appears to start training:

target_bounding_boxes = numpy.random.randint(0, 224, (1, 10, 4))

target_categories = numpy.expand_dims(
    keras.utils.to_categorical(
        numpy.random.randint(0, 2, 10)
    ), 
    0
)

target_image = numpy.random.randint(0, 255, (1, 224, 224, 3))

target_masks = numpy.ones((1, 10, 32, 32))

target_metadata = numpy.expand_dims([224, 224, 1.0], 0)

model = keras_rcnn.models.RCNN(
    categories=["example"],
    input_shape=(224, 224, 3),
    mask_shape=(32, 32)
)

model.compile("sgd")

x = [
    target_bounding_boxes, 
    target_categories, 
    target_image, 
    target_masks, 
    target_metadata
]

model.fit(x)

Maybe it’s an issue with the generator?

Do you have an example you can share? It’d help the debugging process.

0x00b1 commented 6 years ago

Oh, did I misread your issue? Are you saying that you specified the mask_shape in the ObjectDetectionGenerator but didn’t specify the mask_shape in the RCNN model? You need to specify both. If that’s not the problem, what’s the returned shape from the target_masks returned by the generator? Is it wrong?

brandenkmurray commented 6 years ago

I'm specifying the mask_size in both, but at the beginning of model training I get an error that says the shape is different than expected. Removing the aforementioned line from the code stops the error from happening.

import keras_rcnn
import pickle
import keras

from keras_rcnn.preprocessing import ObjectDetectionGenerator

training_dictionary = pickle.load(open("./kaggle-dsbowl-2018-dataset-fixes/dict_dump.pkl", "rb"))

categories = {"nuclei": 1}

generator = ObjectDetectionGenerator()

generator = generator.flow_from_dictionary(
    dictionary=training_dictionary[:5],
    categories=categories,
    mask_size=(56,56),
    batch_size=1,
    shuffle=False,
    target_size=(224, 224)
)

validation_data = keras_rcnn.preprocessing.ObjectDetectionGenerator()

validation_data = validation_data.flow_from_dictionary(
    dictionary=training_dictionary[-1:],
    mask_size=(56,56),
    batch_size=1,
    shuffle=False,
    categories=categories,
    target_size=(224, 224)
)
model = keras_rcnn.models.RCNN((224, 224, 3), ["nuclei"], mask_shape=(56,56))

optimizer = keras.optimizers.Adam()

model.compile(optimizer)
model.fit_generator(
    epochs=1,
    generator=generator,
    max_queue_size=1,
    shuffle=False,
    validation_data=validation_data
)

Traceback (most recent call last):

  File "<ipython-input-1-0dbe8262a8bc>", line 78, in <module>
    validation_data=validation_data

  File "/home/branden/anaconda3/envs/dsb2018/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "/home/branden/anaconda3/envs/dsb2018/lib/python3.5/site-packages/keras/engine/training.py", line 2177, in fit_generator
    class_weight=class_weight)

  File "/home/branden/anaconda3/envs/dsb2018/lib/python3.5/site-packages/keras/engine/training.py", line 1843, in train_on_batch
    check_batch_axis=True)

  File "/home/branden/anaconda3/envs/dsb2018/lib/python3.5/site-packages/keras/engine/training.py", line 1426, in _standardize_user_data
    exception_prefix='input')

  File "/home/branden/anaconda3/envs/dsb2018/lib/python3.5/site-packages/keras/engine/training.py", line 120, in _standardize_input_data
    str(data_shape))

ValueError: Error when checking input: expected target_masks to have shape (None, 56, 56) but got array with shape (27, 28, 28)```