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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 71 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 83 additions & 0 deletions
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

Lines changed: 43 additions & 0 deletions
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

0 commit comments

Comments
 (0)