Tensorflow2.x OpencV cannot read saved_model.pb?

The.pb file cannot be converted to.pbtxt after upgrading to Tensorflow2.0

Say first conclusion

It doesn’t seem to be working

I’m sorry I couldn’t solve the problem completely; I did load the tensorflow2.0 pb model with CV. But the prediction results obtained by using this model are not true, and do not conform to the real situation of this model, and the data is completely wrong. Time reasons did not continue to in-depth understanding, directly changed the idea to achieve the purpose.

When I first got to know Tensorflow, I met a lot of problems when I trained the model and put it into use. Maybe there is a better solution to this problem, I haven’t found it on the blog. Welcome to point out my code and communicate with you, thank you!

The problem

Calling code:

net = cv.dnn.readNetFromTensorflow("./model/saved_model.pb") 
Copy the code

Error stack:

[libprotobuf ERROR D:\a\opencv-python\opencv-python\opencv\3rdparty\protobuf\src\google\protobuf\wire_format_lite.cc:581] 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. Traceback (most recent call last): File "D:/Python/projects/RisklyBehaviorDetect/SceneDetect/test.py", line 102, in <module> net = cv.dnn.readNetFromTensorflow("./model/saved_model.pb") cv2.error: OpenCV (4.5.5) D: \ \ OpenCV - a python, OpenCV - python and OpenCV \ modules \ \ SRC \ tensorflow within DNN \ tf_io CPP: 42: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: ./model/saved_model.pb in function 'cv::dnn::ReadTFNetParamsFromBinaryFileOrDie'Copy the code

The solution

def write_graph_to_pb() :
    m = tf.saved_model.load('./model/')

    from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

    tfm = tf.function(lambda x: m(x))  # full model
    tfm = tfm.get_concrete_function(tf.TensorSpec(m.signatures['serving_default'].inputs[0].shape.as_list(),
                                                  m.signatures['serving_default'].inputs[0].dtype.name))
    frozen_func = convert_variables_to_constants_v2(tfm)
    frozen_func.graph.as_graph_def()
    tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir="./model/", name="saved_model_graph.pb", as_text=False)
Copy the code

Here./model/ This folder contains the saved_model.pb model file and variables folder. You have to have this folder. This should be a change since tensorflow2.0. PBTXT is no longer produced, nor can pb models be directly converted to PBTXT.

After the method transformation is called, the output saved_model_graph.pb can be loaded directly by CV without being converted to PBTXT. The call method is as follows:

net = cv.cnn.readNetFromTensorflow('saved_model_graph.pb')
Copy the code

As mentioned above, after loading the model in this way, the prediction result is wrong, I don’t know what went wrong.

Refer to the link

Opencv document