How to import Tensorflow's MobileNet into Opencv dnn?

asked 2018-02-12 04:32:27 -0500

MennoK gravatar image


I'm retraining Mobilenet using tensorflow's script with following command:

python tensorflow/examples/image_retraining/ \
    --image_dir ~/trainingData/ \
    --learning_rate=0.001 \
    --testing_percentage=20 \
    --validation_percentage=20 \
    --train_batch_size=32 \
    --validation_batch_size=-1 \
    --eval_step_interval=100 \
    --architecture mobilenet_1.0_224

This returns a output_graph.pbwhich i'm trying to import into opencv. I'm following the steps in a previous answers (, However this isn't working yet.

  1. Run the script. From the graph I know the input_names and output_names arguments are respectively input and final_result.

  2. Generate a text_graph.pbtxt.

  3. Remove unimplemented layers in opencv, e.g. Flatten etc. However MobileNet doesn't have any Flatten layers

After doing this, I'm still not able to import the model in Opencv and I receive following Error message:

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:571: error: (-2) More than one input is Const op in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::getConstBlob

Any suggestions? I saw there was a SSD_MobileNet example, but I'm not interested in detections and I'm not sure how much the graph is similar.

edit retag flag offensive close merge delete


Here is a link to the output_graph.pb:

MennoK gravatar imageMennoK ( 2018-02-12 04:33:46 -0500 )edit

@MennoK, looks like it's because of unfused batch normalization layers. They compute weights in runtime using nodes over Const tensors. Can you try to run a graph transformation toolto fold constant ops as described at ? We'll work on that issue to reduce such kind of intermediate steps by including most common subgraphs into OpenCV (like flatten, dropout and batch normalization).

dkurt gravatar imagedkurt ( 2018-02-12 06:01:33 -0500 )edit

@dkurt After 3 days of trying, I'm still not able to build the graph transformation tool on Windows 10. Do you have any roadmap on including the flatten, dropout, etc. subgraphs?

MennoK gravatar imageMennoK ( 2018-02-15 09:45:05 -0500 )edit