Tensorflow object detection api 2

Hello everyone, I know that I cannot export a model trained using tensoflow object detection api tf2 to a frozen graph format for opencv dnn. Does anyone know if it is possible to train a model in tf2 and export using tf1? If this cannot be done, then what working options could you suggest for using models using opencv dnn? Thanks

@Pavel_Grigorev Welcome to Tensorflow Forum !

Yes, it is possible to train a model in TF2 and export using TF1. You can do this by following these steps:

  1. Install TF1 and the TF Object Detection API.
  2. Convert your TF2 model to a TF1 SavedModel using the following command:

tf2onnx --input <tf2_model_path> --output <tf1_saved_model_path>

  1. Load the TF1 SavedModel in OpenCV using the following code:

import cv2 net = cv2.dnn.readNetFromTensorflow(<tf1_saved_model_path>)

However, this approach is not ideal because it requires you to install two different versions of TensorFlow.

A better approach is to convert your TF2 model to the ONNX format, which is a more portable format that can be used by a variety of frameworks, including OpenCV. You can convert your TF2 model to ONNX using the following command:

tf2onnx --input <tf2_model_path> --output <onnx_model_path>

Once your model is in ONNX format, you can load it in OpenCV using the following code:

import cv2 net = cv2.dnn.readNetFromONNX(<onnx_model_path>)

This is the recommended approach for using TF2 models with OpenCV.

Here are some other options for using models using OpenCV DNN:

  • Use the TFLite converter to convert your TF2 model to the TFLite format, which is a lightweight format that can be used on mobile devices.
  • Use the TensorFlow Hub to load pre-trained models that have been converted to the TFLite format.
  • Use the TensorFlow Model Garden to find and download pre-trained models that have been converted to the TFLite format.

I hope this helps! Let me know if you have any other questions.