Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

Running TensorFlow network in OpenCV 3.3.1

$
0
0
I want to use TensorFlow models on OpenCV on a system without requiring to import the TensorFlow library. My ultimate aim is to do it in C++. I tried to run the TensorFlow(>=1.4) MobileNet-SSD on OpenCV using the code provided [here](https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API). Code: import cv2 as cv cvNet = cv.dnn.readNetFromTensorflow('/home/my_username/Downloads/tf1.4_wt_config/out_graph.pb', '/home/my_username/Downloads/tf1.4_wt_config/op_graph.pbtxt') img = cv.imread('/home/my_username/lena.jpg') rows = img.shape[0] cols = img.shape[1] cvNet.setInput(cv.dnn.blobFromImage(img, 1.0/127.5, (300, 300), (127.5, 127.5, 127.5), swapRB=True, crop=False)) cvOut = cvNet.forward() for detection in cvOut[0,0,:,:]: score = float(detection[2]) if score > 0.3: left = detection[3] * cols top = detection[4] * rows right = detection[5] * cols bottom = detection[6] * rows cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2) cv.imshow('img', img) cv.waitKey(0) On using the default [.pb](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz) and [.pbtxt](https://gist.github.com/dkurt/45118a9c57c38677b65d6953ae62924a) files, I get the following error: `OpenCV Error: Bad argument (PriorBox layer parameter does not contain min_size parameter.) in getParameter, file /home/my_username/opencv/modules/dnn/src/layers/prior_box_layer.cpp, line 87 Traceback (most recent call last): File "example_tf_2.py", line 10, in cvOut = cvNet.forward() cv2.error: /home/my_username/opencv/modules/dnn/src/layers/prior_box_layer.cpp:87: error: (-5) PriorBox layer parameter does not contain min_size parameter. in function getParameter ` I then tried to troubleshoot using [optimize_for_inference.py](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/optimize_for_inference.py). With the new .pb file, I received the following error: `OpenCV Error: Unspecified error (FAILED: fs.is_open(). Can't open "/home/my_username/Downloads/tf1.4_wt_config/ssd_mobilenet_v1_coco_2017_11_17.pbtxt") in ReadProtoFromTextFileTF, file /home/my_username/opencv/modules/dnn/src/tensorflow/tf_io.cpp, line 57 Traceback (most recent call last): File "example_tf_2.py", line 3, in cvNet = cv.dnn.readNetFromTensorflow('/home/my_username/opt_graph.pb', '/home/my_username/Downloads/tf1.4_wt_config/ssd_mobilenet_v1_coco_2017_11_17.pbtxt') cv2.error: /home/my_username/opencv/modules/dnn/src/tensorflow/tf_io.cpp:57: error: (-2) FAILED: fs.is_open(). Can't open "/home/my_username/Downloads/tf1.4_wt_config/ssd_mobilenet_v1_coco_2017_11_17.pbtxt" in function ReadProtoFromTextFileTF ` I cannot figure out why the .pbtxt file can't be opened when there was no such issue in the previous run. I then tried using [tf_text_graph_ssd.py](https://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py) to generate a new .pbtxt file. On running the code with the new .pbtxt file, the following error came up: `OpenCV Error: Bad argument (PriorBox layer parameter does not contain min_size parameter.) in getParameter, file /home/my_username/opencv/modules/dnn/src/layers/prior_box_layer.cpp, line 87 Traceback (most recent call last): File "example_tf_2.py", line 10, in cvOut = cvNet.forward() cv2.error: /home/my_username/opencv/modules/dnn/src/layers/prior_box_layer.cpp:87: error: (-5) PriorBox layer parameter does not contain min_size parameter. in function getParameter ` I can't figure out where the error is. Please help me out or at least point me towards something which would help me accomplish my goal. I can run Caffe models in OpenCV, but am unable to do the same with TensorFlow. I have OpenCV 3.3.1 installed on my 64-bit laptop with Ubuntu 17.10. I have been using Python 2.7.14.

Viewing all articles
Browse latest Browse all 2088

Trending Articles