classification.md 2.9 KB
Newer Older
F
feilong 已提交
1 2 3 4
# opencv.dnn做图像分类

图像分类是基于深度学习的计算机视觉任务中最简单、也是最基础的一类,它其中用到的CNN特征提取技术也是目标检测、目标分割等视觉任务的基础。

F
feilong 已提交
5 6
![](https://gitcode.net/csdn/skill_tree_git_md_linux/-/raw/master/data/1.OpenCV初阶/7.OpenCV中的深度学习/1.图像分类/result.png)
<br/>
F
feilong 已提交
7 8

具体到图像分类任务而言,其具体流程如下:
F
feilong 已提交
9

F
feilong 已提交
10 11 12 13 14
1. 输入指定大小RGB图像,1/3通道,宽高一般相等
2. 通过卷积神经网络进行多尺度特征提取,生成高维特征值
3. 利用全连接网络、或其他结构对高维特征进行分类,输出各目标分类的概率值(概率和为1)
4. 选择概率值最高的作为图像分类结果

F
feilong 已提交
15 16
![](https://gitcode.net/csdn/skill_tree_git_md_linux/-/raw/master/data/1.OpenCV初阶/7.OpenCV中的深度学习/1.图像分类/classification.png)
<br/>
F
feilong 已提交
17 18 19 20 21 22 23 24 25 26

`opencv.dnn`模块可以直接加载深度学习模型,并进行推理输出运行结果。下面是opencv.dnn模块加载googlenet caffe模型进行图片分类的代码,请你完善其中TO-DO部分的代码。

> 代码中LABEL_MAP是图像分类名称字典,给定索引得到具体分类名称(string)。

```python
import cv2
import numpy as np
from labels import LABEL_MAP # 1000 labels in imagenet dataset

F
feilong 已提交
27 28 29 30
if __name__=='__main__':
    # caffe model, googlenet aglo
    weights = "bvlc_googlenet.caffemodel"
    protxt = "bvlc_googlenet.prototxt"
F
feilong 已提交
31

F
feilong 已提交
32 33
    # read caffe model from disk
    net = cv2.dnn.readNetFromCaffe(protxt, weights)
F
feilong 已提交
34

F
feilong 已提交
35 36 37 38
    # create input
    image = cv2.imread("ocean-liner.jpg")
    blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117, 123), False, crop=False)
    result = np.copy(image)
F
feilong 已提交
39

F
feilong 已提交
40 41 42
    # run!
    net.setInput(blob)
    out = net.forward()
F
feilong 已提交
43

F
feilong 已提交
44
    # TODO(You): 请在此实现代码
F
feilong 已提交
45

F
feilong 已提交
46 47 48 49
    # time cost
    t, _ = net.getPerfProfile()
    label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
    cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
F
feilong 已提交
50

F
feilong 已提交
51 52 53
    # render on image
    label = '%s: %.4f' % (LABEL_MAP[classId] if LABEL_MAP else 'Class #%d' % classId, confidence)
    cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
F
feilong 已提交
54

F
feilong 已提交
55
    show_img = np.hstack((image, result))
F
feilong 已提交
56

F
feilong 已提交
57 58 59
    # normal codes in opencv
    cv2.imshow("Image", show_img)
    cv2.waitKey(0)
F
feilong 已提交
60 61
```

F
feilong 已提交
62
以下对TODO部分实现正确的是?
F
feilong 已提交
63 64 65 66 67 68 69 70 71 72 73

## 答案

```python
# output probability, find the right index
out = out.flatten()
classId = np.argmax(out)
confidence = out[classId]

```

F
feilong 已提交
74 75 76
## 选项

### 输出理解错误
F
feilong 已提交
77 78 79 80 81 82 83 84

```python
# output probability, find the right index
classId = out[0]
confidence = out[1]

```

F
feilong 已提交
85
### 输出维度理解错误
F
feilong 已提交
86 87 88 89 90 91 92 93

```python
# output probability, find the right index
classId = np.argmax(out)
confidence = out[classId]

```

F
feilong 已提交
94
### 输出理解错误2
F
feilong 已提交
95 96 97 98 99 100 101 102

```python
# output probability, find the right index
out = out.flatten()
classId = np.argmax(out[1:])
confidence = out[classId + 1]

```