openvinotoolkit / openvino

OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference
https://docs.openvino.ai
Apache License 2.0
6.84k stars 2.18k forks source link

Question about converting Tensorflow Object Detection 2.3 to Model Optimizer #2682

Closed campos537 closed 3 years ago

campos537 commented 3 years ago

i saw that in version 2021.1 it was added support to TF 2.X conversions, also using the DL Workbench there is an option there, but when trying to convert an object detection model trained with Tensorflow 2.3 it didn't worked giving this error: Shape is not defined for output 1 of "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/non_max_suppression_with_scores_3/NonMaxSuppressionV5". Is this the lack of support of this layer or my mistake?

Model architecture: SSD ResNet50 V1 FPN 640x640 (RetinaNet50)

`Stopped shape/value propagation at "StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/non_max_suppression_with_scores_3/NonMaxSuppressionV5" node Model Optimizer arguments: Common parameters:

jgespino commented 3 years ago

Hi @campos537

Thanks for reaching out, in the previous release (2020.4) TF2 Object Detection API models were not supported yet. I don't believe that has change but let me confirm with my peers and get back to you.

https://github.com/openvinotoolkit/openvino/issues/1801

Regards, Jesus

campos537 commented 3 years ago

Hey @jgespino

It will be really great be able convert the TF2 detection models to MO, i saw yesterday that you guys added the NonMaximumSupression on the Inference Engine. Thanks for the help!

agungor2 commented 3 years ago

Hi @campos537,

Could you try running it with Model Optimizer and let us know if the issue still persists? We also have this model available on Open Model Zoo which is I think trained on an earlier version of TF: ssd_resnet50_v1_fpn_coco You can refer to MO parameters in the link.

Can you also point me to the frozen graph location you are using so that I can also reproduce it on my platform?

campos537 commented 3 years ago

Hey @agungor2,

I tried to run also with the Model Optimizer directly but i had the same issue, i will try to run again following the same parameters you guys showed on Open Model Zoo. The problem is that the model was trained using custom dataset and only work as a saved model, it was trained by using the Tensorflow 2.3

campos537 commented 3 years ago

I was able to test with the same parameters as here ssd_resnet50_v1_fpn_coco but didn't worked (the output layers names were different) i was able to convert only the model available but not my model which has 2.3 version. This is the config which i used to train the model -> ssd_resnet50_v1_fpn_640x640_coco17_tpu-8

Also i tested and compared the 1.X model optimized and the Tensorflow 2.3 SavedModel Speed on CPU and it was almost the same which i found really weird, both got 600ms

jgespino commented 3 years ago

Hi @campos537

I apologize for the delay, TF2.x Object Detection API models are currently not supported on OpenVINO toolkit, only TF1.x Object Detection API models are supported.

Regards, Jesus

campos537 commented 3 years ago

Hey Jesus, thanks for your answer! It would be really great to have this support soon.

jgespino commented 3 years ago

@campos537 We have added your feedback to the feature request for the development team. Thank you for your input!

Regards, Jesus

lazarevevgeny commented 3 years ago

@campos537 We have added your feedback to the feature request for the development team. Thank you for your input!

Regards, Jesus

PR with adding support for these models is https://github.com/openvinotoolkit/openvino/pull/3556. Please, refer to the instructions on how to convert these types of models in the https://github.com/openvinotoolkit/openvino/blob/e1a955d528b3006b3e1598a617ffd97ba5663f5b/docs/MO_DG/prepare_model/convert_model/tf_specific/Convert_Object_Detection_API_Models.md

campos537 commented 3 years ago

Wow thats really great news! This will be a lot useful, thanks for the work on that!

abhishekthanki commented 3 years ago

@lazarevevgeny Are TF 2.0 Object Detection API models supported in OpenVINO 2021.2 or will it only be supported in the next official release?

lazarevevgeny commented 3 years ago

They will be supported in 2021.3 release. But you can take the MO from master branch to verify support right now.

abhishekthanki commented 3 years ago

@lazarevevgeny Got it. I tried the MO from the master branch and used OpenVINO 2021.2 to perform inference on Pi + NCS 2. Works like a charm. Thank you!

abhishekthanki commented 3 years ago

@campos537 Were you able to convert your model after @lazarevevgeny's PR was merged? I'm asking because I tried converting a custom trained MobileNetSSD v2 model using openvino files from master branch but I'm running into an error with BatchMultiClassNonMaxSuppression which is similar to what you had reported few months ago.

campos537 commented 3 years ago

I didn't tried yet :cry: , i choose to use the Tensorflow Object Detecion 1.X which had more compatibility with the product here and used without converting using MO

abhishekthanki commented 3 years ago

@campos537 Ah no worries.

@lazarevevgeny Did you test converting custom trained models using TFOD 2.X? I tried converting pre-trained version of MobileNetSSD v2 and had no issues but when I tried converting a custom trained MobileNetSSD v2, I'm running into an error regarding BatchMultiClassNonMaxSuppression.

lazarevevgeny commented 3 years ago

@abhishekthanki , no, I have not tried. If you can share your model I can take a look and find a root cause of the BatchMultiClassNonMaxSuppression issue.

rchuzh99 commented 3 years ago

Hi, I have successfully converted the SSD MobileNet v2 320x320 pre-trained model from the TF2 detection model zoo but when I tried to run it with the python sample for object detection from OpenVINO, I got this error

[ INFO ] Loading model to the device
Traceback (most recent call last):
  File "object_detection_sample_ssd.py", line 206, in <module>
    sys.exit(main() or 0)
  File "object_detection_sample_ssd.py", line 159, in main
    exec_net = ie.load_network(network=net, device_name=args.device)
  File "ie_api.pyx", line 306, in openvino.inference_engine.ie_api.IECore.load_network
  File "ie_api.pyx", line 315, in openvino.inference_engine.ie_api.IECore.load_network
RuntimeError: Number of priors must match number of location predictions (4 vs 7668)

I am relatively new to this topic, may I know what it meant by the number of priors and may I know which code did you guys used to run the converted TF2 OD API. XML models? I have also compared the graph with the TF1 .xml model and it seems that there are differences between them: For the TF2 .XML model image

For the TF1 .XML model image

lazarevevgeny commented 3 years ago

@rchuzh99, did you follow the instruction from the documentation. So your command line looks like this:

<INSTALL_DIR>/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir <dir_with_the_model>/ --transformations_config <INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf/ssd_support_api_v2.0.json --tensorflow_object_detection_api_pipeline_config <dir_with_the_model>/pipeline.config --reverse_input_channels

rchuzh99 commented 3 years ago

Yeah and with some additional arguments such as the --input_shape, --output_dir, as shown below: python <INSTALL_DIR>\openvino\model-optimizer\mo_tf.py --saved_model_dir <model_dir>\ssd_mobilenet_v2_320x320_coco17_tpu-8\saved_model --transformations_config <INSTALL_DIR>\Intel\openvino_2021\deployment_tools\model_optimizer\extensions\front\tf\ssd_support_api_v2.0.json --tensorflow_object_detection_api_pipeline_config <model_dir>\ssd_mobilenet_v2_320x320_coco17_tpu-8\pipeline.config --reverse_input_channels --output_dir <output_dir> --input_shape [1,300,300,3]

but I still get the same issue.

I have cloned both the mo_tf.py and ssd_support_api_v2.0.json from the master branch.

Weirdly, I got an error for the DetectionOutput layer when I run the MO with mo_tf.py from the Windows installation: image

whereas I was 'able' to get the IR files with the mo_tf.py from the master branch.

rchuzh99 commented 3 years ago

@lazarevevgeny ,here are the graphs visualised using Netron: Left and Bottom(TF2 OD model) ,and Right (IR model of the TF1 Frozen OD model from openvino) image image

lazarevevgeny commented 3 years ago

We are working on identifying the root cause of this issue.

lazarevevgeny commented 3 years ago

The fix is in https://github.com/openvinotoolkit/openvino/pull/4529

lazarevevgeny commented 3 years ago

@rchuzh99 , the fix has been merged to master. Please, try.

rchuzh99 commented 3 years ago

@lazarevevgeny, I've just tried the updated MO and it works great. Thanks for helping out cheers!!! If you don't mind, is there any documentation explaining the differences between the xml of the TF1 OD API (Left) and the TF2 OD API (Right) models? image image

lazarevevgeny commented 3 years ago

There is no such a documentation.

The difference is that MO for TF 1.X insert PriorBox/PriorBoxClustered operations to generate proposals (the third input to the DetectionOutput operation), while in the TF 2.X version of the model the MO const folds a TF sub-graph calculating these values into a single Const. Because of this you cannot change the input shape of the model input (image) or change the batch size (number of images) for the TF 2.X model IR. However, I am not sure that changing of the input image shape (as well as batch size) for TF 2.X models is actually possible and will not result in shape collision in the original TF mode...

rchuzh99 commented 3 years ago

@lazarevevgeny , thank you for your kind explanation. Thank you again for helping out with the issue.