在 OpenCV4.X 版本(OpenCV3.4.1之后版本) 可以采用 cv2.dnn.readNetFromTensorflow(pbmodel, pbtxt)
函数直接调用 TensorFlow 训练的目标检测模型.
TensorFlow 目标检测预训练模型: Tensorflow Detection Model Zoo
Model name | Speed (ms) | COCO mAP[^1] | Outputs |
---|---|---|---|
ssd_mobilenet_v1_coco | 30 | 21 | Boxes |
ssd_mobilenet_v1_0.75_depth_coco ☆ | 26 | 18 | Boxes |
ssd_mobilenet_v1_quantized_coco ☆ | 29 | 18 | Boxes |
ssd_mobilenet_v1_0.75_depth_quantized_coco ☆ | 29 | 16 | Boxes |
ssd_mobilenet_v1_ppn_coco ☆ | 26 | 20 | Boxes |
ssd_mobilenet_v1_fpn_coco ☆ | 56 | 32 | Boxes |
ssd_resnet_50_fpn_coco ☆ | 76 | 35 | Boxes |
ssd_mobilenet_v2_coco | 31 | 22 | Boxes |
ssd_mobilenet_v2_quantized_coco | 29 | 22 | Boxes |
ssdlite_mobilenet_v2_coco | 27 | 22 | Boxes |
ssd_inception_v2_coco | 42 | 24 | Boxes |
faster_rcnn_inception_v2_coco | 58 | 28 | Boxes |
faster_rcnn_resnet50_coco | 89 | 30 | Boxes |
faster_rcnn_resnet50_lowproposals_coco | 64 | Boxes | |
rfcn_resnet101_coco | 92 | 30 | Boxes |
faster_rcnn_resnet101_coco | 106 | 32 | Boxes |
faster_rcnn_resnet101_lowproposals_coco | 82 | Boxes | |
faster_rcnn_inception_resnet_v2_atrous_coco | 620 | 37 | Boxes |
faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco | 241 | Boxes | |
faster_rcnn_nas | 1833 | 43 | Boxes |
faster_rcnn_nas_lowproposals_coco | 540 | Boxes | |
mask_rcnn_inception_resnet_v2_atrous_coco | 771 | 36 | Masks |
mask_rcnn_inception_v2_coco | 79 | 25 | Masks |
mask_rcnn_resnet101_atrous_coco | 470 | 33 | Masks |
mask_rcnn_resnet50_atrous_coco | 343 | 29 | Masks |
注:
[1] - 带五角星符号(☆) 表示模型支持 TPU 训练.
[2] - 下载 quantized 模型的 .tar.gz
文件并解压后,会得到不同的文件,包括:checkpoint
文件,config
配置文件和 tfile frozen graphs(txt/binary)
文件.
Model name | Speed (ms) | Pascal mAP@0.5 | Outputs |
---|---|---|---|
faster_rcnn_resnet101_kitti | 79 | 87 | Boxes |
Model name | Speed (ms) | Open Images mAP@0.5[^2] | Outputs |
---|---|---|---|
faster_rcnn_inception_resnet_v2_atrous_oidv2 | 727 | 37 | Boxes |
faster_rcnn_inception_resnet_v2_atrous_lowproposals_oidv2 | 347 | Boxes | |
facessd_mobilenet_v2_quantized_open_image_v4 [^3] | 20 | 73 (faces) | Boxes |
Model name | Speed (ms) | Open Images mAP@0.5[^4] | Outputs |
---|---|---|---|
faster_rcnn_inception_resnet_v2_atrous_oidv4 | 425 | 54 | Boxes |
ssd_mobilenetv2_oidv4 | 89 | 36 | Boxes |
ssd_resnet_101_fpn_oidv4 | 237 | 38 | Boxes |
Model name | Speed (ms) | Pascal mAP@0.5 | Outputs |
---|---|---|---|
faster_rcnn_resnet101_fgvc | 395 | 58 | Boxes |
faster_rcnn_resnet50_fgvc | 366 | 55 | Boxes |
Model name | Speed (ms) | Pascal mAP@0.5 | Outputs |
---|---|---|---|
faster_rcnn_resnet101_ava_v2.1 | 93 | 11 | Boxes |
TensorFlow 中,深度学习网络被表示为图(graphs),其中图中每个节点(node) 是其输入的一种变换. 节点可以是常用网络层,如 C++ 实现的 Convolution
和 MaxPooling
层. 也可以采用 python 利用 TensorFlow 操作子(operations) 来构建自定义网络层.
TensorFlow 目标检测API 是用于创建目标检测深度网络的框架.
TensorFlow 训练得到的模型是 .pb
后缀的二值文件,其同时保存了训练网络的拓扑(topology)结构和模型权重.
这里以 ssd_mobilenet_v2_coco_2018_03_29
预训练模型(基于 COCO 数据集训练的 MobileNet-SSD模型)为例:
目标检测结果如:
https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API
OpenCV DNN 模块调用 TensorFlow 训练的目标检测模型时,需要一个额外的配置文件,其主要是基于与 protocol buffers(protobuf) 格式序列化图(graph) 相同的文本格式版本.
OpenCV 中已经提供的 TensorFlow 目标检测模型和配置文件有:
Model | Version | ||
---|---|---|---|
MobileNet-SSD v1 | 2017_11_17 | weights | config |
MobileNet-SSD v1 PPN | 2018_07_03 | weights | config |
MobileNet-SSD v2 | 2018_03_29 | weights | config |
Inception-SSD v2 | 2017_11_17 | weights | config |
Faster-RCNN Inception v2 | 2018_01_28 | weights | config |
Faster-RCNN ResNet-50 | 2018_01_28 | weights | config |
Mask-RCNN Inception v2 | 2018_01_28 | weights | config |
三种不同的 TensorFlow 目标检测模型转换脚本为:
From: https://github.com/opencv/opencv/tree/master/samples/dnn
转换脚本的输入参数:
[1] - --input
: TensorFlow frozen graph 文件路径.
[2] - --config
: TensorFlow 模型训练时的 *.config
文件路径.
注: TensorFlow *.config
配置文件:configuration file.
转换脚本的输出参数:
[1] - --output
: 输出的 text graph 文件.
如:
faster rcnn 模型:
ssd 模型:
mask rcnn 模型:
对于生成的 graph.pbtxt 可采用 Netron 工具进行可视化.
与 TensorFLow 目标检测 API -SSD 例示 一样,检测测试下基于 OpenCV DNN 的 SSD 目标检测.
[1] - 首先进行模型转换,如:
终端打印的转换过程如:
[2] - 然后,目标检测模型测试:
目标检测结果如: