From cc2c61c7e4aa6d48d18b6e09da2566f6e9fdff98 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 29 Jan 2018 18:35:20 +0300 Subject: [PATCH] Updated TensorFlow Object Detection API (markdown) --- TensorFlow-Object-Detection-API.md | 56 +++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/TensorFlow-Object-Detection-API.md b/TensorFlow-Object-Detection-API.md index 3cb3080..dc6dc7b 100644 --- a/TensorFlow-Object-Detection-API.md +++ b/TensorFlow-Object-Detection-API.md @@ -57,6 +57,60 @@ cv.waitKey() ``` ![](https://user-images.githubusercontent.com/25801568/35504975-a5db962c-04f5-11e8-9a9f-1d803f86af7f.png) +## Run network in OpenCV +OpenCV needs an extra configuration file to import object detection models from TensorFlow. It's based on a text version of the same serialized graph in protocol buffers format (protobuf). + +### Use existing config file for your model +You can use one of the configs that has been tested in OpenCV. Choose it depends on your model and TensorFlow version: + +| Model | Version | || +|-------|-------------|----|----| +| MobileNet-SSD | TensorFlow >= 1.4 | [weights](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz) | | +| Inception v2 SSD | TensorFlow >= 1.4 | [weights](http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2017_11_17.tar.gz) | [config](https://github.com/opencv/opencv_extra/tree/master/testdata/dnn/ssd_inception_v2_coco_2017_11_17.pbtxt) | +| MobileNet-SSD | TensorFlow < 1.4 | [weights](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz) | [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/ssd_mobilenet_v1_coco.pbtxt) | + + + +### Get a text graph representation +You can use the following script to make a text graph representation. It removes weights nodes and some unused fields. + +```python +import tensorflow as tf + +# Read the graph. +with tf.gfile.FastGFile('graph.pb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + +# Remove Const nodes. +for i in reversed(range(len(graph_def.node))): + if graph_def.node[i].op == 'Const': + del graph_def.node[i] + for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim', + 'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training', + 'Tpaddings']: + if attr in graph_def.node[i].attr: + del graph_def.node[i].attr[attr] + +# Save as text. +tf.train.write_graph(graph_def, "", "graph.pbtxt", as_text=True) +``` +### Generate a config file +Run `optimize_for_inference.py` tool to make your model simpler: +```bash +python ~/tensorflow/tensorflow/python/tools/optimize_for_inference.py \ + --input frozen_inference_graph.pb \ + --output opt_graph.pb \ + --input_names image_tensor \ + --output_names "num_detections,detection_scores,detection_boxes,detection_classes" \ + --frozen_graph +``` + +Run a [graph transformation tool](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#using-the-graph-transform-tool) to fuse constant nodes. + +Run [tf_text_graph.py]() script. If your model has different values of `num_classes`, `min_scale`, `max_scale`, `num_layers` or `aspect_ratios` comparing to [origin configuration files](https://github.com/tensorflow/models/tree/master/research/object_detection/samples/configs), specify it in the script arguments. + ## References * [TensorFlow library](https://www.tensorflow.org/) -* [COCO dataset](http://cocodataset.org/#home) \ No newline at end of file +* [COCO dataset](http://cocodataset.org/#home) +* [Google Protobuf](https://developers.google.com/protocol-buffers/) \ No newline at end of file