mirror of
https://github.com/opencv/opencv.git
synced 2025-06-11 03:33:28 +08:00
Updated TensorFlow Object Detection API (markdown)
parent
cd3c21f3da
commit
cc2c61c7e4
@ -57,6 +57,60 @@ cv.waitKey()
|
||||
```
|
||||

|
||||
|
||||
## 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)
|
||||
* [COCO dataset](http://cocodataset.org/#home)
|
||||
* [Google Protobuf](https://developers.google.com/protocol-buffers/)
|
Loading…
Reference in New Issue
Block a user