Can't open .pb in cv::dnn::readNetFromTensorflow

I’m using OpenCV 4.5 and Tensorflow 2.5 in Ubuntu 18 in Docker container 20.03, which has CUDA 10.2 and TensorRT 7.0.0

I have saved_model.pb in /workspace/model/saved_model.pb and in /workspace. In /workspace, I’ve tried

    dnn:Net nt = cv::dnn::readNetFromTensorflow("saved_model.pb");
    dnn:Net nt = cv::dnn::readNetFromTensorflow("model/saved_model.pb");
    dnn:Net nt = cv::dnn::readNetFromTensorflow("./model/saved_model.pb");
    dnn:Net nt = cv::dnn::readNetFromTensorflow("/workspace/model/saved_model.pb");
    dnn:Net nt = cv::dnn::readNetFromTensorflow("/model/saved_model.pb");

But all of them are giving the same error of

String field 'opencv_tensorflow.FunctionDef.Node.ret' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.
what(): OpenCV(4.5.1-pre) /workspace/opencv/modules/dnn/src/tensorflow/tf_io.cpp:42: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: model/saved_model.pb in function 'ReadTFNetParamsFromBinaryFileOrDie'

Can anyone help?

what are the contents of your files? don’t describe, show contents. use a hex editor.

docs do state: " .pb file with binary protobuf description of the network architecture"

and what’s in your .pb files?

okay, that looks sufficiently binary to me. I’m at the end of my knowledge here. I hope you get responses from others.

Using tensorflow files in OpenCV is a real pain in the a**. You need to “freeze” and “optimize” the graph, and it’s not clear (at least for me) how to do it - especially that Tensorflow changed a lot with version 2, so many tutorials are not applicable. OpenCV also had problems by interpreting some layer types or parameters - especially those exported by Keras.

The only thing I can recommend it to continue using Tensorflow for processing instead of using the OpenCV bindings.

I’ll still follow this thread in case if somebody can write a good tutorial about using Tensorflow2 networks in OpenCV…

1 Like

After freezing your model, you can try to use open vino to optimize your model and then use it in opencv. (But it’s not an easy thing… )
You can also try to export your model to onnx format.

hey mke does the opencv work after using open vino ?