OpenCV - Tensorflow model import error

asked 2018-03-21 07:07:21 -0500

foreign222 gravatar image

I use ssd_mobilenet_v1_coco model with OpenCV for object detection.

cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')

When using original graph.pbtxt it is OK. But when i create pbtxt file using tf_text_graph_ssd https://github.com/opencv/opencv/blob...

python tf_text_graph_ssd.py --input frozen_inference_graph.pb --output pbtxt/frozen_inference_graph.pbtxt

and then:

cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'pbtxt/frozen_inference_graph.pbtxt')

it gives error:

OpenCV Error: Unspecified error (Input layer not found: 

FeatureExtractor/MobilenetV1/zeros) in 
cv::dnn::experimental_dnn_v3::`anonymous-
namespace'::TFImporter::populateNet, file C:
\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, 
line 1061
Traceback (most recent call last):
File "opencv_tensorflow.py", line 3, in <module>
cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 
'pbtxt/frozen_inference_graph.pbtxt')
cv2.error: C:\projects\opencv-
python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1061: error: (-2) 
Input layer not found: FeatureExtractor/MobilenetV1/zeros in function cv::d
nn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

What is the problem when converting pb to pbtxt?

Here my aim is create custom model using image retraining and use it on OpenCV. https://www.tensorflow.org/tutorials/...

So I have a custom model but not pbtxt file.

First i try to create pbtxt for an example ssd_mobilenet_v1_coco model.

edit retag flag offensive close merge delete

Comments

@foreign222, which version of TensorFlow did you use? Am I right that you use pre-trained model? From which version of TensorFlow it is?

dkurt gravatar imagedkurt ( 2018-03-21 07:39:19 -0500 )edit

tensorflow version: 1.6.0 Yes, I use pre-trained ssd_mobilenet_v1_coco_11_06_2017. It is ok when original graph.pbtxt was used. But here my problem is converting pb to pbtxt for pre-trained model.

foreign222 gravatar imageforeign222 ( 2018-03-21 08:10:58 -0500 )edit

@foreign222, Have you tried to use an origin graph.pbtxt for retrained model?

dkurt gravatar imagedkurt ( 2018-03-21 10:52:53 -0500 )edit

Yes, when use retrained model with origin graph.pbtxt of ssd_mobilenet_v1_coco_11_06_2017 :

OpenCV Error: Unspecified error (Const input blob for weights not found) in cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::getConstBlob, file C:\projects\opencv-p
ython\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 579
Traceback (most recent call last):
  File "opencv_tensorflow.py", line 3, in <module>
    cvNet = cv.dnn.readNetFromTensorflow('output.pb', 'graph.pbtxt')
cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:579: error: (-2) Const input blob for weights not found in function cv::dnn::experimental_dnn
_v3::`anonymous-namespace'::TFImporter::getConstBlob
foreign222 gravatar imageforeign222 ( 2018-03-21 23:19:00 -0500 )edit

@foreign222, does you apply any graph transformations such constants fusion?

dkurt gravatar imagedkurt ( 2018-03-21 23:44:47 -0500 )edit

@foreign222, BTW it's deprecated version of MobileNet-SSD and I hardly recommend you use reserialized graph. They both have similar accuracy but an old one has a quite strange internal architecture. However if you already have a model we'll try to help you import it.

dkurt gravatar imagedkurt ( 2018-03-21 23:48:42 -0500 )edit

Ok let's go step by step.

First i retrained a model such that:

python tensorflow/examples/image_retraining/retrain.py --image_dir D:/dev/detect_objects/flower_photos --bottleneck_dir D:/dev/detect_objects/retrain/bottleneck --architecture mobilenet_1.0_192 --output_graph D:/dev/detect_objects/retrain/output_graph/retrained.pb --output_labels D:/dev/detect_objects/retrain/output_labels/labels.txt --saved_model_dir D:/dev/detect_objects/retrain/saved_model_dir --how_many_training_steps 100

And now i have retrained.pb. What is next step?

foreign222 gravatar imageforeign222 ( 2018-03-22 00:22:22 -0500 )edit

@foreign222, Hmmm, flower photos? Do you train image classification or object detection model? Is where any SSD mentions?

dkurt gravatar imagedkurt ( 2018-03-22 00:31:38 -0500 )edit

I want to use this model for object detection (detect bottle types in an image). https://github.com/opencv/opencv/wiki...

Isn't it possible to use tensorflow retrain script for this purpose? So have my custom bottle classifier and then use it for object(bottle) detection?

foreign222 gravatar imageforeign222 ( 2018-03-22 00:41:53 -0500 )edit

@foreign222, according to README, retrain.py is an example script that shows how one can adapt a pretrained network for other classification problems so I think you need to look at TensorFlow Object Detection API.

dkurt gravatar imagedkurt ( 2018-03-22 00:48:32 -0500 )edit