README.md 4.27 KB
Newer Older
dcuai's avatar
dcuai committed
1
# DETR
chenych's avatar
chenych committed
2
3
4
5
6
## 论文
[End-to-End Object Detection with Transformers](https://arxiv.org/abs/2005.12872)

## 模型结构

chenych's avatar
chenych committed
7
对于输入图像, DETR使用传统的CNN backbone来学习2D特征, 并在将其传递到transformer encoder之前用positional encoding对其进行补充和平滑。然后, transformer decoder将少量固定数量的学习到的positional embeddings(对象查询)作为输入, 额外关注encoder输出。将解码器的每个输出embedding传递到预测检测(类和边界框)或“无对象”类的共享前馈网络(FFN)。
chenych's avatar
chenych committed
8
9
10
11
12
13

<div align=center>
    <img src="./doc/models.png"/>
</div>

## 算法原理
chenych's avatar
chenych committed
14
DETR将目标检测看作一种set prediction问题, 并提出了一个十分简洁的目标检测pipeline, 即CNN提取基础特征, 送入Transformer做关系建模, 得到的输出通过二分图匹配算法与图片上的ground truth做匹配。
chenych's avatar
chenych committed
15
16
17
18
19
20
21
22
23
24
25

<div align=center>
    <img src="./doc/DETR.png"/>
</div>

## 环境配置
-v 路径、docker_name和imageID根据实际情况修改

### Docker(方法一)

```
shantf's avatar
shantf committed
26
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10 
chenych's avatar
chenych committed
27

Rayyyyy's avatar
Rayyyyy committed
28
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal:/opt/hyhal:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
29
30
31
32
33
34
35
36
37
38
39

cd /your_code_path/detr_pytorch
pip install -r requirements.txt
```

### Dockerfile(方法二)

```
cd ./docker

docker build --no-cache -t detr:latest .
Rayyyyy's avatar
Rayyyyy committed
40
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal:/opt/hyhal:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
41
42
43
44
45
46

cd /your_code_path/detr_pytorch
pip install -r requirements.txt
```

### Anaconda(方法三)
chenzk's avatar
chenzk committed
47
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.sourcefind.cn/tool/
chenych's avatar
chenych committed
48
49

```
shantf's avatar
shantf committed
50
DTK软件栈: dtk24.04.1
Rayyyyy's avatar
Rayyyyy committed
51
python: python3.10
shantf's avatar
shantf committed
52
53
torch: 2.1.0+das1.1.git3ac1bdd.abi1.dtk2404
torchvision: 0.16.0+das1.1.git7d45932.abi1.dtk2404.torch2.1
chenych's avatar
chenych committed
54
55
```

chenych's avatar
chenych committed
56
Tips: 以上dtk软件栈、python、torch等DCU相关工具版本需要严格一一对应
chenych's avatar
chenych committed
57
58
59

2、其他非特殊库直接按照requirements.txt安装

Rayyyyy's avatar
Rayyyyy committed
60
```bash
chenych's avatar
chenych committed
61
62
63
64
65
pip install -r requirements.txt
```

## 数据集

chenzk's avatar
chenzk committed
66
[COCO2017](https://cocodataset.org/#home)
chenych's avatar
chenych committed
67

chenych's avatar
chenych committed
68
数据集的目录结构如下:
chenych's avatar
chenych committed
69

Rayyyyy's avatar
Rayyyyy committed
70
```bash
chenych's avatar
chenych committed
71
├── COCO2017
Rayyyyy's avatar
Rayyyyy committed
72
73
74
75
│   ├── train2017
│   ├── val2017
│   ├── test2017
│   └── annotations
chenych's avatar
chenych committed
76
77
78
79
│       ├── instances_train2017.json
│       └── instances_val2017.json
```

Rayyyyy's avatar
Rayyyyy committed
80
训练/验证集数据准备:训练/验证集都是采用的**COCO**的数据格式, 如果使用自己的标注数据, 请先将标注数据转换成**COCO**的格式, 并按照上面的目录结构进行存放。
chenych's avatar
chenych committed
81

Rayyyyy's avatar
Rayyyyy committed
82
83
84
85
本项目提供了`coco128`数据集可以进行功能验证使用,目录结构如下:
```bash
├── coco128
│   ├── train2017
Rayyyyy's avatar
Rayyyyy committed
86
│   ├── val2017
Rayyyyy's avatar
Rayyyyy committed
87
│   └── annotations
Rayyyyy's avatar
Rayyyyy committed
88
│       └── instances_train2017.json
Rayyyyy's avatar
Rayyyyy committed
89
```
chenych's avatar
chenych committed
90
91
92

## 训练
### 单机多卡
Rayyyyy's avatar
Rayyyyy committed
93
94
95
```bash
# --nproc_per_node 数据与显卡数量一致
# coco_path是训练数据集地址,数据是coco format
chenych's avatar
chenych committed
96
97
98
99
bash train.sh
```

## 推理
Rayyyyy's avatar
Rayyyyy committed
100
验证前需提前准备好预训练模型, 可从[参考资料](#参考资料)项目中提供的模型下载, 并将`coco_path`设置为当前环境中推理数据的对应地址, 数据应为**COCO**数据格式。
chenych's avatar
chenych committed
101

Rayyyyy's avatar
Rayyyyy committed
102
如果想要查看预测效果(预测结果输出到图片上), 请执行(其余参数如`backbone`等需与训练模型参数需一致, 详情请根据训练参数配置):
chenych's avatar
chenych committed
103

Rayyyyy's avatar
Rayyyyy committed
104
```bash
chenych's avatar
chenych committed
105
106
107
108
python test.py --pre_trained_model <checkpoint path> --coco_path <coco path>
```

#### 单卡推理
Rayyyyy's avatar
Rayyyyy committed
109
```bash
chenych's avatar
chenych committed
110
111
112
113
114
115
bash val.sh
```

## result

<div align=center>
Rayyyyy's avatar
Rayyyyy committed
116
    <img src="./doc/results.jpg"/>
chenych's avatar
chenych committed
117
118
119
</div>

### 精度
Rayyyyy's avatar
Rayyyyy committed
120
在COCO2017的val数据集上进行单卡测试, 结果如下表所示:
chenych's avatar
chenych committed
121

Rayyyyy's avatar
Rayyyyy committed
122
123
| device | backbone | schedule | epoch | box AP |
| :------: | :------: | :------: | :------: | :------: |
Rayyyyy's avatar
Rayyyyy committed
124
125
| Z100L  | R50 | 500 | 300 | 41.4 |
| V100 | R50 | 500 | 300 | 42 |
chenych's avatar
chenych committed
126
127
128
129
130
131
132
133
134

## 应用场景
### 算法类别
目标检测

### 热点应用行业
网安,交通,政府

## 源码仓库及问题反馈
chenzk's avatar
chenzk committed
135
http://developer.sourcefind.cn/codes/modelzoo/detr_pytorch.git
chenych's avatar
chenych committed
136
137
138

## 参考资料
https://github.com/facebookresearch/detr.git