README.md 4.8 KB
Newer Older
chenych's avatar
chenych committed
1
2
3
4
5
# HDETR
## 论文
[DETRs with Hybrid Matching](https://arxiv.org/abs/2207.13080)

## 模型结构
chenych's avatar
chenych committed
6
基于DETR结构, 在匹配阶段加入一对多的匹配分支。
chenych's avatar
chenych committed
7
8
9
10
11
12

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

## 算法原理
chenych's avatar
chenych committed
13
H-DETR引入一对多匹配分支, 将原始的一对一匹配分支与一个辅助的一对多匹配分支结合起来, 允许多个查询分配给每个正样本, 增加正样本查询数量, 提高训练效果。此外, H-DETR在推理过程中仍然使用原始的一对一匹配分支, 以保持DETR的优势。
chenych's avatar
chenych committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

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

## 环境配置

注意:requirements.txt安装完成后,还需要额外安装下列包

```
pip install openmim
mim install mmcv-full  (注意版本是不是1.7.1)
pip install mmdet==2.26.0 (对应mmcv 1.7.1)
```

-v 路径、docker_name和imageID根据实际情况修改

### Docker(方法一)

```
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.13.1-centos7.6-dtk-23.04.1-py38-latest
docker run -it -v /path/your_code_data/:/path/ your_code_data/ --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

chenych's avatar
chenych committed
37
cd /your_code_path/hdetr_pytorch
chenych's avatar
chenych committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
pip install -r requirements.txt
```

### Dockerfile(方法二)

```
cd ./docker
cp ../requirements.txt requirements.txt

docker build --no-cache -t hdetr:latest .
docker run -it -v /path/your_code_data/:/path/your_code_data/ --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
```

### Anaconda(方法三)

chenych's avatar
chenych committed
53
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/
chenych's avatar
chenych committed
54
55

```
chenych's avatar
chenych committed
56
57
58
59
DTK软件栈: dtk23.04.1
python: python3.8
torch: 1.13.1
torchvision: 0.14.1
chenych's avatar
chenych committed
60
61
```

chenych's avatar
chenych committed
62
Tips:以上dtk软件栈、python、torch等DCU相关工具版本需要严格一一对应
chenych's avatar
chenych committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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

```
pip3 install -r requirements.txt
```

## 数据集

COCO2017

[训练数据](http://images.cocodataset.org/zips/train2017.zip)

[验证数据](http://images.cocodataset.org/zips/val2017.zip)

[测试数据](http://images.cocodataset.org/zips/test2017.zip)

[标签数据](https://github.com/ultralytics/yolov5/releases/download/v1.0/coco2017labels.zip)

chenych's avatar
chenych committed
82
数据集的目录结构如下:
chenych's avatar
chenych committed
83
84
85
86
87
88
89
90
91
92
93
94

```
├── COCO2017
│   ├── images
│       ├── train2017
│       ├── val2017
│       └── test2017
│   ├── annotations
│       ├── instances_train2017.json
│       └── instances_val2017.json
```

chenych's avatar
chenych committed
95
训练/验证集数据准备:
chenych's avatar
chenych committed
96

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

## 训练

chenych's avatar
chenych committed
101
训练前的准备工作:
chenych's avatar
chenych committed
102
103
104
105
106
107
108
109
110

1. 进行下面步骤编译

```
cd ./models/ops
bash ./make.sh
cd ../../
```

chenych's avatar
chenych committed
111
2. 选择需要训练的模型的config, config path 为需要训练的模型属性config文件地址, coco path 为当前环境中训练数据对应地址
chenych's avatar
chenych committed
112
113
114

Tips:

chenych's avatar
chenych committed
115
1. 如果有预训练模型, 修改config中的 --pretrained_backbone_path 为保存的预训练模型地址
chenych's avatar
chenych committed
116

chenych's avatar
chenych committed
117
118
119
2. 如果使用backbone为swin, 可前往 https://github.com/microsoft/Swin-Transformer 选择对应的预训练模型后再进行训练步骤

3. 如果out_of_memory, 设置--batch_size大小, 当前默认为2
chenych's avatar
chenych committed
120
121
122
123

### 单机单卡

```
chenych's avatar
chenych committed
124
bash ./train.sh
chenych's avatar
chenych committed
125
126
127
128
129
```

### 单机多卡

```
chenych's avatar
chenych committed
130
bash ./train_multi.sh
chenych's avatar
chenych committed
131
132
133
134
```

## 推理

chenych's avatar
chenych committed
135
验证前需提前准备好预训练模型, checkpoint path 为模型地址,  coco path 为当前环境中推理数据的对应地址, 数据应为COCO数据格式。
chenych's avatar
chenych committed
136

chenych's avatar
chenych committed
137
如没有预训练模型, 可从 参考资料 中提供的模型下载, 选择模型对应的config后进行效果验证。
chenych's avatar
chenych committed
138

chenych's avatar
chenych committed
139
如果想要查看预测效果(预测结果输出到图片上), 请执行:
chenych's avatar
chenych committed
140
141
142
143
144

```
python test.py --pre_trained_model <checkpoint path> --coco_path <coco path>
```

chenych's avatar
chenych committed
145
其余对应参数与训练模型参数需一致, 详情请参考代码里面的参数配置:
chenych's avatar
chenych committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

#### 单卡推理

```
bash val.sh
```

#### 多卡推理

```
bash val_multi.sh
```

## result

chenych's avatar
chenych committed
161
COCO2017测试集上的单张图像结果展示:
chenych's avatar
chenych committed
162
163
164
165
166
167
168

<div align=center>
    <img src="./doc/results.jpg"/>
</div>

### 精度

chenych's avatar
chenych committed
169
在COCO2017的测试集上进行单卡测试, 结果如下表所示:
chenych's avatar
chenych committed
170

chenych's avatar
chenych committed
171
根据测试结果情况填写表格:
chenych's avatar
chenych committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
| Name     | Backbone | query | epochs | AP |
| :--------: | :------: | :------: | :------: | :------: |
| H-Deformable-DETR + tricks(our) | R50 | 300 | 12 | xxx  |
| H-Deformable-DETR + tricks | R50 | 300 | 12 | 48.7 |


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

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


## 源码仓库及问题反馈
https://developer.hpccube.com/codes/modelzoo/hdetr_pytorch

## 参考资料
https://github.com/HDETR/H-Deformable-DETR