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

**URL:** <https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360>\
**Category:** Uncategorized\
**Tags:** dnn\
**Created:** [December 16, 2020, 5:33pm UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360 "2020-12-16T17:33:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mke](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mke](https://forum.opencv.org/u/mke)\
**Post date:** [December 16, 2020, 5:33pm UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/1 "2020-12-16T17:33:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 7, 2021, 2:13pm UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/3 "2021-01-07T14:13:07Z")

</div>

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"

[https://docs.opencv.org/master/d6/d0f/group\_\_dnn.html#gad820b280978d06773234ba6841e77e8d](https://docs.opencv.org/master/d6/d0f/group__dnn.html#gad820b280978d06773234ba6841e77e8d)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 7, 2021, 9:50pm UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/5 "2021-01-07T21:50:24Z")

</div>

and what’s in your .pb files?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 8, 2021, 9:41am UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/7 "2021-01-08T09:41:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [January 8, 2021, 10:33am UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/8 "2021-01-08T10:33:46Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Gpc](https://avatars.discourse-cdn.com/v4/letter/g/2bfe46/32.png) [@Gpc](https://forum.opencv.org/u/Gpc)\
**Post date:** [January 8, 2021, 11:18am UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/9 "2021-01-08T11:18:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mostafa\_Badr](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_badr/32/859_2.png) [@Mostafa\_Badr](https://forum.opencv.org/u/Mostafa_Badr)\
**Post date:** [February 2, 2021, 4:18pm UTC](https://forum.opencv.org/t/cant-open-pb-in-cv-readnetfromtensorflow/360/11 "2021-02-02T16:18:19Z")

</div>

hey mke does the opencv work after using open vino ?
