Problems importing TensorFlow Object Detection Models into OpenCV DNN C++

Hello everyone,

I would like to use the OpenCV DNN (C++) module to make predictions using a pre-trained TensorFlow 2.x object detection model (Mask R-CNN Inception ResNet V2 1024x1024). I have already adapted and retrained the model with Python and TesorFlow for my own input images and can also run the inference e.g. on a single test image. So everything seems to be working on the TensorFlow side.

Now I want to port this model to OpenCV and run the inference using the DNN module.
Since I used TensorFlow 2.x, which no longer supports the “frozen graph format” needed for the DNN module to work, I have to convert Tensorflow’s “saved model format” into the “frozen graph format”.

But now I fail at creating the “config file” (often also refered to as graph.pbtxt file), which I have to pass to the readNetFromTensorflow(const String &model, const String &config = String()) method.

The Python script that is supposed to create this file for me ( fails with the following error message:
File “”, line 41, in .
features_stride = float(config[‘feature_extractor’][0][‘first_stage_features_stride’][0])
KeyError: ‘first_stage_features_stride’

Can anyone help me here, has anyone had similar problems ?


Hi @joni7373 ,

I’m new to dnn, but since anyone replied I dare to share my shallow knowledge.

As you already know, readNetFromTensorflow accepts 2 parameters: model (aka weights) and config (aka net). The last one is optional.

I see a lot of examples using this function with one parameter, the model. And that’s possible when the file contains both weights and net, a pretty convenient thing to do as nets are one thousand times smaller than weights.

Did you tried not using a config file? May be you already know this is not possible in you case.

thank you for your answer. Actually, I wanted to delete this post because I have explained my problem in more detail in a new post ( I want to import TensorFlow 2.x - Object Detection - Mask R-CNN Model in OpenCV DNN API ). Unfortunately, I did not find a delete option. Please have a look at my new post. Thank you.