Skip to content

Commit c631bdd

Browse files
committed
update 121_125(DNN) and model file
1 parent 255ce10 commit c631bdd

25 files changed

+5952
-2
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,9 @@ code_116 | [Decision Tree Algorithm](python/code_116) | ✔️
158158
code_117 | [Image Mean-shift Segmentation](python/code_117) | ✔️
159159
code_118 | [Grabcut-Image Segmentation](python/code_118) | ✔️
160160
code_119 | [Grabcut-Background Change](python/code_119) | ✏️
161-
code_120 | [Qrcode detect and decode](python/code_120) | ✏️
161+
code_120 | [Qrcode detect and decode](python/code_120) | ✏️
162+
code_121 | [DNN- Read the information of each layer of the model](python/code_121) | ✔️
163+
code_122 | [DNN- Realize image classification](python/code_122) | ✔️
164+
code_120 | [DNN- Model runs to set the target device and compute the background](python/code_123) | ✔️
165+
code_120 | [DNN- SSD Single Image Detection](python/code_124) | ✔️
166+
code_120 | [DNN- SSD Real-time Video Detection](python/code_125) | ✔️

README_CN.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,9 @@ code_116 | [决策树算法](python/code_116) | ✔️
157157
code_117 | [图像均值漂移分割](python/code_117) | ✔️
158158
code_118 | [Grabcut-图像分割](python/code_118) | ✔️
159159
code_119 | [Grabcut-背景替换](python/code_119) | ✏️
160-
code_120 | [二维码检测识别](python/code_120) | ✔️
160+
code_120 | [二维码检测识别](python/code_120) | ✔️
161+
code_121 | [DNN- 读取模型各层信息](python/code_121) | ✔️
162+
code_122 | [DNN- DNN实现图像分类](python/code_122) | ✔️
163+
code_120 | [DNN- 模型运行设置目标设备与计算后台](python/code_123) | ✔️
164+
code_120 | [DNN- SSD单张图片检测](python/code_124) | ✔️
165+
code_120 | [DNN- SSD实时视频检测](python/code_125) | ✔️

python/code_121/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 读取DNN模型各层信息
2+
3+
### 概述
4+
✔️ OpenCV的DNN模块支持下面框架的预训练模型的前馈网络(预测图)使用:
5+
- Caffe
6+
- Tensorflow
7+
- Torch
8+
- DLDT
9+
- Darknet
10+
11+
同时还支持自定义层解析、非最大抑制操作、获取各层的信息等。
12+
OpenCV加载模型的通用API为:
13+
```
14+
cv2.dnn.readNet(model, # 模型
15+
config = "",
16+
framework = "" )
17+
```
18+
其中:
19+
- model二进制训练好的网络权重文件,可能来自支持的网络框架,扩展名为如下:
20+
21+
- *.caffemodel (Caffe,http://caffe.berkeleyvision.org/)
22+
- *.pb (TensorFlow, https://www.tensorflow.org/)
23+
- *.t7 | *.net (Torch, http://torch.ch/)
24+
- *.weights (Darknet, https://pjreddie.com/darknet/)
25+
- *.bin (DLDT, https://software.intel.com/openvino-toolkit)
26+
27+
- config针对模型二进制的描述文件,不同的框架配置文件有不同扩展名:
28+
29+
- *.prototxt (Caffe, http://caffe.berkeleyvision.org/)
30+
- *.pbtxt (TensorFlow, https://www.tensorflow.org/)
31+
- *.cfg (Darknet, https://pjreddie.com/darknet/)
32+
- *.xml (DLDT, https://software.intel.com/openvino-toolkit)
33+
34+
- framework显示声明参数,说明模型使用哪个框架训练出来的。
35+
36+
### 代码
37+
```python
38+
import cv2
39+
import numpy as np
40+
41+
bin_model = "bvlc_googlenet.caffemodel"
42+
protxt = "bvlc_googlenet.prototxt"
43+
44+
# load CNN model
45+
net = cv2.dnn.readNet(bin_model, protxt)
46+
47+
# 获取各层信息
48+
layer_names = net.getLayerNames()
49+
50+
for name in layer_names:
51+
id = net.getLayerId(name)
52+
layer = net.getLayer(id)
53+
print("layer id : %d, type : %s, name: %s"%(id, layer.type, layer.name))
54+
55+
print("successfully")
56+
```
57+
>输出
58+
```
59+
layer id : 1, type : Convolution, name: conv1/7x7_s2
60+
layer id : 2, type : ReLU, name: conv1/relu_7x7
61+
layer id : 3, type : Pooling, name: pool1/3x3_s2
62+
layer id : 4, type : LRN, name: pool1/norm1
63+
layer id : 5, type : Convolution, name: conv2/3x3_reduce
64+
layer id : 6, type : ReLU, name: conv2/relu_3x3_reduce
65+
layer id : 7, type : Convolution, name: conv2/3x3
66+
layer id : 8, type : ReLU, name: conv2/relu_3x3
67+
layer id : 9, type : LRN, name: conv2/norm2
68+
layer id : 10, type : Pooling, name: pool2/3x3_s2
69+
...
70+
successfully
71+
```

python/code_121/guinea_pig.jpg

128 KB
Loading

python/code_121/opencv_121.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cv2
2+
import numpy as np
3+
4+
bin_model = "../model/google/bvlc_googlenet.caffemodel"
5+
protxt = "../model/google/bvlc_googlenet.prototxt"
6+
7+
# Load names of classes
8+
# classes = None
9+
# with open("classification_classes_ILSVRC2012.txt", 'rt') as f:
10+
# classes = f.read().rstrip('\n').split('\n')
11+
12+
# print(classes)
13+
14+
# load CNN model
15+
net = cv2.dnn.readNet(bin_model, protxt)
16+
17+
# 获取各层信息
18+
layer_names = net.getLayerNames()
19+
20+
for name in layer_names:
21+
id = net.getLayerId(name)
22+
layer = net.getLayer(id)
23+
print("layer id : %d, type : %s, name: %s"%(id, layer.type, layer.name))
24+
25+
print("successfully loaded model...")

python/code_122/README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## DNN实现图像分类
2+
3+
### 概述
4+
5+
✔️ 我们使用ImageNet数据集支持1000分类的GoogleNet网络模型, 结合opencv实现图像分类标签预测。
6+
7+
>label标签是在一个单独的文本文件中读取。
8+
9+
读取模型的API:
10+
```python
11+
cv2.dnn.readNetFromCaffe(prototxt,
12+
caffeModel = String())
13+
```
14+
其中:
15+
- prototxt表示模型的配置文件
16+
- caffeModel表示模型的权重二进制文件
17+
18+
使用模型实现预测的时候,需要读取图像作为输入,网络模型支持的输入数据是四维的输入,所以要把读取到的Mat对象转换为四维张量,OpenCV的提供的API为如下:
19+
20+
```python
21+
cv2.dnn。blobFromImage(
22+
image,
23+
scalefactor = 1.0,
24+
size = Size(),
25+
mean = Scalar(),
26+
swapRB = false,
27+
crop = false,
28+
ddepth = CV_32F
29+
)
30+
```
31+
其中:
32+
- image输入图像
33+
- scalefactor 默认1.0
34+
- size表示网络接受的数据大小
35+
- mean表示训练时数据集的均值
36+
- swapRB 是否互换Red与Blur通道
37+
- crop剪切
38+
- ddepth 数据类型
39+
40+
### 代码
41+
```python
42+
import cv2
43+
import numpy as np
44+
45+
bin_model = "bvlc_googlenet.caffemodel"
46+
protxt = "bvlc_googlenet.prototxt"
47+
48+
# 加载类别
49+
classes = None
50+
with open("classification_classes_ILSVRC2012.txt", 'rt') as f:
51+
classes = f.read().rstrip('\n').split('\n')
52+
53+
# 加载模型
54+
net = cv2.dnn.readNetFromCaffe(protxt, bin_model)
55+
56+
# 读取输入数据
57+
image = cv2.imread("guinea_pig.jpg")
58+
blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117,123), False, crop=False)
59+
result = np.copy(image)
60+
61+
# 运行模型
62+
net.setInput(blob)
63+
out = net.forward()
64+
65+
# 获取最高分的类别
66+
out = out.flatten()
67+
classId = np.argmax(out)
68+
confidence = out[classId]
69+
70+
# 输出运行时间
71+
t, _ = net.getPerfProfile() #返回值是网络执行推断的时间
72+
label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
73+
cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
74+
75+
# 显示结果
76+
label = '%s: %.4f' % (classes[classId] if classes else 'Class #%d' % classId, confidence)
77+
cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
78+
79+
show_img = np.hstack((image, result))
80+
```
81+
>输出豚鼠的预测结果
82+
83+
![result]('./result.jpg')

python/code_122/dog.jpg

140 KB
Loading

python/code_122/guinea_pig.jpg

128 KB
Loading

python/code_122/opencv_122.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import cv2
2+
import numpy as np
3+
4+
bin_model = "../model/google/bvlc_googlenet.caffemodel"
5+
protxt = "../model/google/bvlc_googlenet.prototxt"
6+
7+
# Load names of classes
8+
classes = None
9+
with open("../model/google/classification_classes_ILSVRC2012.txt", 'rt') as f:
10+
classes = f.read().rstrip('\n').split('\n')
11+
12+
# load CNN model
13+
net = cv2.dnn.readNetFromCaffe(protxt, bin_model)
14+
15+
# read input data
16+
image = cv2.imread("guinea_pig.jpg")
17+
blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117,123), False, crop=False)
18+
result = np.copy(image)
19+
cv2.imshow("input", image)
20+
21+
# Run a model
22+
net.setInput(blob)
23+
out = net.forward()
24+
25+
# Get a class with a highest score.
26+
out = out.flatten()
27+
classId = np.argmax(out)
28+
confidence = out[classId]
29+
30+
# Put efficiency information.
31+
t, _ = net.getPerfProfile()
32+
label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
33+
cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
34+
35+
# Print predicted class.
36+
label = '%s: %.4f' % (classes[classId] if classes else 'Class #%d' % classId, confidence)
37+
cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
38+
39+
show_img = np.hstack((image, result))
40+
cv2.namedWindow('demo', cv2.WINDOW_NORMAL)
41+
cv2.imshow("demo", show_img)
42+
cv2.waitKey(0)
43+
cv2.destroyAllWindows()

python/code_122/result.jpg

91.6 KB
Loading

python/code_123/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## OpenCV DNN 为模型运行设置目标设备与计算后台
2+
3+
OpenCV中加载网络模型之后,可以设置计算后台与计算目标设备,OpenCV DNN模块支持这两个设置的相关API如下:
4+
cv::dnn::Net::setPreferableBackend(
5+
int backendId
6+
)
7+
backendId 表示后台计算id,
8+
- DNN_BACKEND_DEFAULT (DNN_BACKEND_INFERENCE_ENGINE)表示默认使用intel的预测推断库(需要下载安装Intel® OpenVINO™ toolkit, 然后重新编译OpenCV源码,在CMake时候enable该选项方可), 可加速计算!
9+
- DNN_BACKEND_OPENCV 一般情况都是使用opencv dnn作为后台计算,
10+
11+
void cv::dnn::Net::setPreferableTarget(
12+
int targetId
13+
)
14+
常见的目标设备id如下:
15+
- DNN_TARGET_CPU其中表示使用CPU计算,默认是的
16+
- DNN_TARGET_OPENCL 表示使用OpenCL加速,一般情况速度都很扯
17+
- DNN_TARGET_OPENCL_FP16 可以尝试
18+
- DNN_TARGET_MYRIAD 树莓派上的

python/code_123/guinea_pig.jpg

128 KB
Loading

python/code_123/opencv_123.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cv2
2+
import numpy as np
3+
4+
bin_model = "../model/google/bvlc_googlenet.caffemodel"
5+
protxt = "../model/google/bvlc_googlenet.prototxt"
6+
7+
# Load names of classes
8+
classes = None
9+
with open("../model/google/classification_classes_ILSVRC2012.txt", 'rt') as f:
10+
classes = f.read().rstrip('\n').split('\n')
11+
12+
# load CNN model
13+
net = cv2.dnn.readNetFromCaffe(protxt, bin_model)
14+
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
15+
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
16+
17+
# read input data
18+
image = cv2.imread("guinea_pig.jpg")
19+
blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117,123), False, crop=False)
20+
result = np.copy(image)
21+
cv2.imshow("input", image)
22+
23+
# Run a model
24+
net.setInput(blob)
25+
out = net.forward()
26+
27+
# Get a class with a highest score.
28+
out = out.flatten()
29+
classId = np.argmax(out)
30+
confidence = out[classId]
31+
32+
# Put efficiency information.
33+
t, _ = net.getPerfProfile()
34+
label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
35+
cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
36+
37+
# Print predicted class.
38+
label = '%s: %.4f' % (classes[classId] if classes else 'Class #%d' % classId, confidence)
39+
cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
40+
41+
show_img = np.hstack((image, result))
42+
cv2.namedWindow('demo', cv2.WINDOW_NORMAL)
43+
cv2.imshow("demo", show_img)
44+
cv2.waitKey(0)
45+
cv2.destroyAllWindows()

python/code_124/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## SSD实现目标检测
2+
3+
✔️ OpenCV的DNN模块支持常见得对象检测模型SSD, 以及Mobile Net-SSD。
4+
5+
✔️ 这里我们用基于Caffe训练好的mobile-net SSD来测试目标检测。
6+
7+
### 单张图片检测
8+
9+
```python
10+
import cv2
11+
12+
model_bin = "../model/ssd/MobileNetSSD_deploy.caffemodel";
13+
config_text = "../model/ssd/MobileNetSSD_deploy.prototxt";
14+
objName = ["background",
15+
"aeroplane", "bicycle", "bird", "boat",
16+
"bottle", "bus", "car", "cat", "chair",
17+
"cow", "diningtable", "dog", "horse",
18+
"motorbike", "person", "pottedplant",
19+
"sheep", "sofa", "train", "tvmonitor"];
20+
21+
# 加载模型
22+
net = cv2.dnn.readNetFromCaffe(config_text, model_bin)
23+
image = cv2.imread("dog.jpg")
24+
h = image.shape[0]
25+
w = image.shape[1]
26+
27+
# 获得所有层名称与索引
28+
layerNames = net.getLayerNames()
29+
lastLayerId = net.getLayerId(layerNames[-1])
30+
lastLayer = net.getLayer(lastLayerId)
31+
print(lastLayer.type)
32+
33+
# 加载图片检测
34+
blobImage = cv2.dnn.blobFromImage(image, 0.007843, (300, 300), (127.5, 127.5, 127.5), True, False);
35+
net.setInput(blobImage)
36+
Out = net.forward()
37+
print(Out.shape)
38+
for detection in Out[0,0,:,:]:
39+
score = float(detection[2])
40+
objIndex = int(detection[1])
41+
if score > 0.5:
42+
left = detection[3]*w
43+
top = detection[4]*h
44+
right = detection[5]*w
45+
bottom = detection[6]*h
46+
47+
# 绘制
48+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
49+
cv2.putText(image, "score:%.2f, %s"%(score, objName[objIndex]),
50+
(int(left) - 10, int(top) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, 8);
51+
52+
```
53+
>输出
54+
55+
![dog]('./dog.jpg)
56+
![Pedestrian]('./result.png)

python/code_124/dog.jpg

55.9 KB
Loading

0 commit comments

Comments
 (0)