System information (version)
OS Platform: Windows 10 64 bits
Tensorflow Version: 2.8
Python version: 3.9.7
OpenCV version: 4.5.5
Detailed description
Hi all,
I am trying to convert my exported TensorFlow 2 model trained on my dataset to onnx format using tf2onnx . The aim is to use it with the OpenCV dnn.readNetFromONNX function. For Conversion, I used the following code since I am using TensorFlow 2
python -m tf2onnx.convert --saved-model tensorflow-model-path --output model.onnx --opset 15
The conversion process generates the model.onnx successfully and returns the following:
n -m tf2onnx.convert --saved-model C:/Tensorflow/workspace/training_demo/exported-models/my_model/saved_model --output model.onnx --opset 15 C:\Tensorflow\venv\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs: C:\Tensorflow\venv\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll C:\Tensorflow\venv\lib\site-packages\numpy\.libs\libopenblas.XWYDX2IKJW2NMTWSFYNGFUWKQU3LYTCZ.gfortran-win_amd64.dll warnings.warn("loaded more than 1 DLL from .libs:" C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py:127: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour warn(RuntimeWarning(msg)) 2022-04-07 16:58:02,939 - WARNING - '--tag' not specified for saved_model. Using --tag serve 2022-04-07 16:58:13,132 - INFO - Signatures found in model: [serving_default]. 2022-04-07 16:58:13,133 - WARNING - '--signature_def' not specified, using first signature: serving_default 2022-04-07 16:58:13,137 - INFO - Output names: ['detection_anchor_indices', 'detection_boxes', 'detection_classes', 'detection_multiclass_scores', 'detection_scores', 'num_detections', 'raw_detection_boxes', 'raw_detection_scores'] WARNING:tensorflow:From C:\Tensorflow\venv\lib\site-packages\tf2onnx\tf_loader.py:711: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version. Instructions for updating: Use tf.compat.v1.graph_util.extract_sub_graph2022-04-07 16:58:18,989 - WARNING - From C:\Tensorflow\venv\lib\site-packages\tf2onnx\tf_loader.py:711: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version. Instructions for updating: Usetf.compat.v1.graph_util.extract_sub_graph 2022-04-07 16:58:22,197 - INFO - Using tensorflow=2.8.0, onnx=1.11.0, tf2onnx=1.10.0/07e9e0 2022-04-07 16:58:22,198 - INFO - Using opset <onnx, 15> 2022-04-07 16:58:29,140 - INFO - Computed 0 values for constant folding 2022-04-07 16:58:29,144 - INFO - Computed 0 values for constant folding 2022-04-07 16:58:33,926 - INFO - Computed 4 values for constant folding 2022-04-07 16:58:40,244 - INFO - folding node using tf type=ExpandDims, name=StatefulPartitionedCall/Postprocessor/ExpandDims_2 2022-04-07 16:58:40,245 - INFO - folding node using tf type=Identity, name=StatefulPartitionedCall/MultiscaleGridAnchorGenerator/GridAnchorGenerator/Identity 2022-04-07 16:58:40,248 - INFO - folding node using tf type=Select, name=StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/PadOrClipBoxList/Select_1 2022-04-07 16:58:40,252 - INFO - folding node using tf type=Select, name=StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/PadOrClipBoxList/Select_8 2022-04-07 16:58:40,291 - INFO - folding node type=Range, name=StatefulPartitionedCall/Postprocessor/range 2022-04-07 16:58:43,261 - INFO - Optimizing ONNX model 2022-04-07 16:58:57,710 - INFO - After optimization: BatchNormalization -94 (97->3), Cast -60 (125->65), Const -913 (1196->283), Identity -76 (76->0), Less -1 (5->4), Mul -2 (48->46), Placeholder -2 (6->4), ReduceSum -1 (2->1), Reshape -22 (46->24), Shape -2 (17->15), Slice -9 (39->30), Split -2 (15->13), Squeeze -11 (53->42), Sub -1 (26->25), Transpose -401 (427->26), Unsqueeze -16 (59->43) 2022-04-07 16:58:58,654 - INFO - 2022-04-07 16:58:58,654 - INFO - Successfully converted TensorFlow model C:/Tensorflow/workspace/training_demo/exported-models/my_model/saved_model to ONNX 2022-04-07 16:58:58,656 - INFO - Model inputs: ['input_tensor'] 2022-04-07 16:58:58,658 - INFO - Model outputs: ['detection_anchor_indices', 'detection_boxes', 'detection_classes', 'detection_multiclass_scores', 'detection_scores', 'num_detections', 'raw_detection_boxes', 'raw_detection_scores'] 2022-04-07 16:58:58,661 - INFO - ONNX model is saved at model.onnx
However, when I try to read the converted model I get the following error:
File "C:\Tensorflow\testcovertedTF2ToONNX.py", line 10, in <module> net = cv2.dnn.readNetFromONNX('C:/Tensorflow/model.onnx') cv2.error: Unknown C++ exception from OpenCV code
Please, can someone help resolve this error.
Steps to reproduce
1.convert the saved model attached below to onnx model using tf2onnx tool from GitHub GitHub - onnx/tensorflow-onnx: Convert TensorFlow, Keras, Tensorflow.js and Tflite models to ONNX
2. load the model using the python script below with cv2.dnn.readNetFromONNX function
import cv2
import numpy as np
image = cv2.imread("img002500.jpg")
if image is None:
print("image emplty")
image_height, image_width, _ = image.shape
net = cv2.dnn.readNetFromONNX('model.onnx')
image = image.astype(np.float32)
input_blob = cv2.dnn.blobFromImage(image, 1, (640,640), 0, swapRB=False, crop=False)
net.setInput(input_blob)
output = net.forward()
for detection in output[0, 0, :, :]:
confidence = detection[2]
if confidence > .4:
class_id = detection[1]
class_name = 'person'
box_x = detection[3] * image_width
box_y = detection[4] * image_height
box_width = detection[5] * image_width
box_height = detection[6] * image_height
cv2.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), (255,0,0), thickness=2)
cv2.putText(image, class_name, (int(box_x), int(box_y - 5)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
cv2.imshow('image', image)
cv2.imwrite('image_result.jpg', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Issue submission checklist
File "C:\Tensorflow\testcovertedTF2ToONNX.py", line 10, in <module> net = cv2.dnn.readNetFromONNX('C:/Tensorflow/model.onnx') cv2.error: Unknown C++ exception from OpenCV code
#####Screenshots
#####saved model
You can find the saved model by downloading the zip file below