matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.71k stars 11.71k forks source link

We could not automatically infer the shape of the Lambda's output. Please specify the `output_shape` argument for this Lambda layer. #3038

Open zlao3118 opened 5 months ago

zlao3118 commented 5 months ago

Traceback (most recent call last): File "D:\Mask_RCNN-2.1\Mask_RCNN-2.1\samples\balloon\balloon.py", line 332, in model = modellib.MaskRCNN(mode="inference", config=config, File "D:\Mask_RCNN-2.1\Mask_RCNN-2.1\samples\balloon\model.py", line 1768, in init self.keras_model = self.build(mode=mode, config=config) File "D:\Mask_RCNN-2.1\Mask_RCNN-2.1\samples\balloon\model.py", line 1950, in build fpn_classifier_graph(rpn_rois, mrcnn_feature_maps, config.IMAGE_SHAPE, File "D:\Mask_RCNN-2.1\Mask_RCNN-2.1\samples\balloon\model.py", line 916, in fpn_classifier_graph shared = KL.Lambda(lambda x: K.squeeze(K.squeeze(x, 3), 2), File "D:\python310\lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler raise e.with_traceback(filtered_tb) from None File "D:\python310\lib\site-packages\keras\src\layers\core\lambda_layer.py", line 95, in compute_output_shape raise NotImplementedError( NotImplementedError: Exception encountered when calling Lambda.call().

We could not automatically infer the shape of the Lambda's output. Please specify the output_shape argument for this Lambda layer.

Arguments received by Lambda.call(): • args=('<KerasTensor shape=(None, 1000, 1, 1, 1024), dtype=float32, sparse=False, name=keras_tensor_428>',) • kwargs={'mask': 'None'}

fata1error404 commented 5 months ago

Same problem here, which appears in the line model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

I am running Tensorflow 2.16.1 TensorRT 8.6.1 Cuda 12.3 Cudnn 8.0

Traceback (most recent call last): File "/home/fata1error404/Mask-RCNN-TF2/samples/test.py", line 36, in model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) File "/home/fata1error404/.local/lib/python3.10/site-packages/mrcnn/model.py", line 1831, in init self.keras_model = self.build(mode=mode, config=config) File "/home/fata1error404/.local/lib/python3.10/site-packages/mrcnn/model.py", line 2029, in build fpn_classifier_graph(rpn_rois, mrcnn_feature_maps, input_image_meta, File "/home/fata1error404/.local/lib/python3.10/site-packages/mrcnn/model.py", line 938, in fpn_classifier_graph shared = KL.Lambda(lambda x: K.squeeze(K.squeeze(x, 3), 2), File "/home/fata1error404/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 123, in error_handler raise e.with_traceback(filtered_tb) from None File "/home/fata1error404/.local/lib/python3.10/site-packages/keras/src/layers/core/lambda_layer.py", line 97, in compute_output_shape raise NotImplementedError( NotImplementedError: Exception encountered when calling Lambda.call().

We could not automatically infer the shape of the Lambda's output. Please specify the output_shape argument for this Lambda layer.

Arguments received by Lambda.call(): • args=('<KerasTensor shape=(None, 1000, 1, 1, 1024), dtype=float32, sparse=False, name=keras_tensor_393>',) • kwargs={'mask': 'None'}

userwatch commented 5 months ago

Hello I was able to run it in colab. I also encountered the same errors. It was very difficult.

https://github.com/z-mahmud22/Mask-RCNN_TF2.14.0 I used the repo here.

zlao3118 commented 4 months ago

still not solved

userwatch commented 4 months ago

Hello @zlao3118 zlao3188 Which versions do you use? tensorflow==2.5.0 keras==2.3.1 python 3.8.0 Have you tried?

knutsona commented 4 months ago

I was able to solve it with python 3.11 and tensorflow 2.12.0

like22co commented 1 month ago

I was able to solve it with python 3.11 and tensorflow 2.12.0 Which versions do you use for keras?

claudiaortiz1311 commented 1 month ago

Hi! I solved it by defining those output_shape parameters in my model.py file on functions that use Lambda layer. But as recommended above, it is better to update Tensorflow because other related errors will appear throughout the execution of the model.

🟢 Change this :

 gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(
                x, K.shape(input_image)[1:3]))(input_gt_boxes)

for this:

gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(
                x, K.shape(input_image)[1:3]), output_shape=(None, 4))(input_gt_boxes)

🟢 change this:

active_class_ids = KL.Lambda(
                lambda x: parse_image_meta_graph(x)["active_class_ids"]
                )(input_image_meta)

for this:

 active_class_ids = KL.Lambda(
                lambda x: parse_image_meta_graph(x)["active_class_ids"], 
                output_shape=(None,))(input_image_meta)

🟢 and this: output_rois = KL.Lambda(lambda x: x * 1, name="output_rois")(rois) for this: output_rois = KL.Lambda(lambda x: x * 1, output_shape=(None, 4), name="output_rois")(rois) in model.py file

like22co commented 1 month ago

Thank you very much for your help! And I would like to ask what version of Tensorflow you are using?

------------------ 原始邮件 ------------------ 发件人: "Claudia @.>; 发送时间: 2024年10月10日(星期四) 上午7:33 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [matterport/Mask_RCNN] We could not automatically infer the shape of the Lambda's output. Please specify the output_shape argument for this Lambda layer. (Issue #3038)

Hi! I solved it by defining those output_shape parameters in my model.py file on functions that use Lambda layer. But as recommended above, it is better to update Tensorflow because other related errors will appear throughout the execution of the model.

🟢 Change this : gt_boxes = KL.Lambda(lambda x: norm_boxes_graph( x, K.shape(input_image)[1:3]))(input_gt_boxes)
for this: gt_boxes = KL.Lambda(lambda x: norm_boxes_graph( x, K.shape(input_image)[1:3]), output_shape=(None, 4))(input_gt_boxes)
🟢 change this: active_class_ids = KL.Lambda( lambda x: parse_image_meta_graph(x)["active_class_ids"] )(input_image_meta)
for this: active_class_ids = KL.Lambda( lambda x: parse_image_meta_graph(x)["active_class_ids"], output_shape=(None,))(input_image_meta)
🟢 and this: output_rois = KL.Lambda(lambda x: x 1, name="output_rois")(rois) for this: output_rois = KL.Lambda(lambda x: x 1, output_shape=(None, 4), name="output_rois")(rois) in model.py file

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

claudiaortiz1311 commented 1 month ago

Probably a 1.* too. I also have to update Tensorflow and keep fighting to get Mask working in a week 🫠🙃