Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask-RCNN with Keras : Tried to convert 'shape' to a tensor and failed. Error: None values not supported

I am trying to run the Keras implemention of Mask_RCNN in inference mode. It is basically running this code demo.ipynb

But when I run it, I get the following error on model creation :

ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.

And here is the stacktrace :

Traceback (most recent call last):
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1758, in <module>
    main()
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1147, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/128/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "[PATH/TO/Mask_RCNN/]/Own_code/Test.py", line 44, in <module>
    model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 1833, in __init__
    self.keras_model = self.build(mode=mode, config=config)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 2035, in build
    fc_layers_size=config.FPN_CLASSIF_FC_LAYERS_SIZE)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 947, in fpn_classifier_graph
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 554, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py", line 439, in call
    (array_ops.shape(inputs)[0],) + self.target_shape)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 7179, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 529, in _apply_op_helper
    (input_name, err))

It look like my problem is the same as described here, but there is no real answer to it.

Notes :

  • I am running it with Python 3.6 and Tensorflow 1.13.1
  • I have edited the Keras imports to use the Keras version embedded in Tensorflow
  • The code that I run to have this error correspond to the 3 first blocks of the demo notebook.

EDIT

Here is the code I am running :

# Root directory of the project
ROOT_DIR = os.path.abspath("../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library

# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

This is really a copy/paste of the demo.ipynb from the maskRCNN repo. The last line is where the error occur.

like image 256
Nakeuh Avatar asked May 24 '19 09:05

Nakeuh


3 Answers

I made all the changes recommended in this official issue thread.

Those are basically changes inside the model.py script to use tf.keras functions instead of pure keras functions.

Here is my working fork (updated for TF 2.4.1 - May 2021).

You can find my colab sample inside it or through this link

like image 78
Antonio Luis Sombra Avatar answered Dec 18 '22 13:12

Antonio Luis Sombra


I got the same error, until :

  1. Using keras.__version__ == 2.2.4
  2. Tensorflow-gpu version == 1.12.0
  3. Skimage.__version__ == 0.14.2

Not sure what is the error with rest versions, probably something does not fit with pycocotools or

optimizer = keras.optimizers.SGD(
            lr=learning_rate, momentum=momentum,
            clipnorm=self.config.GRADIENT_CLIP_NORM) #this portion.
like image 44
ASHu2 Avatar answered Dec 18 '22 12:12

ASHu2


Replace this code in model.py

if s[1] is None:
    mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
like image 43
umraz hussain Avatar answered Dec 18 '22 13:12

umraz hussain