Skip to content

Commit 73e5960

Browse files
authored
[Docs] Add Chinese version of single_stage_as_rpn.md and test_results_submission.md (#9434)
* Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update test_results_submission.md * Update single_stage_as_rpn.md * Create test_results_submission.md * Update test_results_submission.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update test_results_submission.md * update * update * update * Update single_stage_as_rpn.md * Update .pre-commit-config.yaml * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update test_results_submission.md * Update single_stage_as_rpn.md * 已经运行pre-commit run --all-files,以下是其修改结果 * Update test_results_submission.md * Update test_results_submission.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md * Update single_stage_as_rpn.md
1 parent fdc7722 commit 73e5960

File tree

2 files changed

+345
-2
lines changed

2 files changed

+345
-2
lines changed
Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,171 @@
1-
# 将单阶段检测器作为 RPN(待更新)
1+
# 将单阶段检测器作为 RPN
2+
3+
候选区域网络 (Region Proposal Network, RPN) 作为 [Faster R-CNN](https://arxiv.org/abs/1506.01497) 的一个子模块,将为 Faster R-CNN 的第二阶段产生候选区域。在 MMDetection 里大多数的二阶段检测器使用 [`RPNHead`](../../../mmdet/models/dense_heads/rpn_head.py)作为候选区域网络来产生候选区域。然而,任何的单阶段检测器都可以作为候选区域网络,是因为他们对边界框的预测可以被视为是一种候选区域,并且因此能够在 R-CNN 中得到改进。因此在 MMDetection v3.0 中会支持将单阶段检测器作为 RPN 使用。
4+
5+
接下来我们通过一个例子,即如何在 [Faster R-CNN](../../../configs/faster_rcnn/faster-rcnn_r50_fpn_fcos-rpn_1x_coco.py) 中使用一个无锚框的单阶段的检测器模型 [FCOS](../../../configs/fcos/fcos_r50-caffe_fpn_gn-head_1x_coco.py) 作为 RPN ,详细阐述具体的全部流程。
6+
7+
主要流程如下:
8+
9+
1. 在 Faster R-CNN 中使用 `FCOSHead` 作为 `RPNHead`
10+
2. 评估候选区域
11+
3. 用预先训练的 FCOS 训练定制的 Faster R-CNN
12+
13+
## 在 Faster R-CNN 中使用 `FCOSHead` 作为` RPNHead`
14+
15+
为了在 Faster R-CNN 中使用 `FCOSHead` 作为 `RPNHead` ,我们应该创建一个名为 `configs/faster_rcnn/faster-rcnn_r50_fpn_fcos-rpn_1x_coco.py` 的配置文件,并且在 `configs/faster_rcnn/faster-rcnn_r50_fpn_fcos-rpn_1x_coco.py` 中将 `rpn_head` 的设置替换为 `bbox_head` 的设置,此外我们仍然使用 FCOS 的瓶颈设置,步幅为`[8,16,32,64,128]`,并且更新 `bbox_roi_extractor``featmap_stride`` [8,16,32,64,128]`。为了避免损失变慢,我们在前1000次迭代而不是前500次迭代中应用预热,这意味着 lr 增长得更慢。相关配置如下:
16+
17+
```python
18+
_base_ = [
19+
'../_base_/models/faster-rcnn_r50_fpn.py',
20+
'../_base_/datasets/coco_detection.py',
21+
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
22+
]
23+
model = dict(
24+
# 从 configs/fcos/fcos_r50-caffe_fpn_gn-head_1x_coco.py 复制
25+
neck=dict(
26+
start_level=1,
27+
add_extra_convs='on_output', # 使用 P5
28+
relu_before_extra_convs=True),
29+
rpn_head=dict(
30+
_delete_=True, # 忽略未使用的旧设置
31+
type='FCOSHead',
32+
num_classes=1, # 对于 rpn, num_classes = 1,如果 num_classes > 1,它将在 TwoStageDetector 中自动设置为1
33+
in_channels=256,
34+
stacked_convs=4,
35+
feat_channels=256,
36+
strides=[8, 16, 32, 64, 128],
37+
loss_cls=dict(
38+
type='FocalLoss',
39+
use_sigmoid=True,
40+
gamma=2.0,
41+
alpha=0.25,
42+
loss_weight=1.0),
43+
loss_bbox=dict(type='IoULoss', loss_weight=1.0),
44+
loss_centerness=dict(
45+
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)),
46+
roi_head=dict( # featmap_strides 的更新取决于于颈部的步伐
47+
bbox_roi_extractor=dict(featmap_strides=[8, 16, 32, 64, 128])))
48+
# 学习率
49+
param_scheduler = [
50+
dict(
51+
type='LinearLR', start_factor=0.001, by_epoch=False, begin=0,
52+
end=1000), # 慢慢增加 lr,否则损失变成 NAN
53+
dict(
54+
type='MultiStepLR',
55+
begin=0,
56+
end=12,
57+
by_epoch=True,
58+
milestones=[8, 11],
59+
gamma=0.1)
60+
]
61+
```
62+
63+
然后,我们可以使用下面的命令来训练我们的定制模型。更多训练命令,请参考[这里](train.md)
64+
65+
```python
66+
# 使用8个 GPU 进行训练
67+
bash
68+
tools/dist_train.sh
69+
configs/faster_rcnn/faster-rcnn_r50_fpn_fcos-rpn_1x_coco.py
70+
--work-dir /work_dirs/faster-rcnn_r50_fpn_fcos-rpn_1x_coco
71+
```
72+
73+
## 评估候选区域
74+
75+
候选区域的质量对检测器的性能有重要影响,因此,我们也提供了一种评估候选区域的方法。和上面一样创建一个新的名为 `configs/rpn/fcos-rpn_r50_fpn_1x_coco.py` 的配置文件,并且在 `configs/rpn/fcos-rpn_r50_fpn_1x_coco.py` 中将 `rpn_head` 的设置替换为 `bbox_head` 的设置。
76+
77+
```python
78+
_base_ = [
79+
'../_base_/models/rpn_r50_fpn.py', '../_base_/datasets/coco_detection.py',
80+
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
81+
]
82+
val_evaluator = dict(metric='proposal_fast')
83+
test_evaluator = val_evaluator
84+
model = dict(
85+
# 从 configs/fcos/fcos_r50-caffe_fpn_gn-head_1x_coco.py 复制
86+
neck=dict(
87+
start_level=1,
88+
add_extra_convs='on_output', # 使用 P5
89+
relu_before_extra_convs=True),
90+
rpn_head=dict(
91+
_delete_=True, # 忽略未使用的旧设置
92+
type='FCOSHead',
93+
num_classes=1, # 对于 rpn, num_classes = 1,如果 num_classes >为1,它将在 rpn 中自动设置为1
94+
in_channels=256,
95+
stacked_convs=4,
96+
feat_channels=256,
97+
strides=[8, 16, 32, 64, 128],
98+
loss_cls=dict(
99+
type='FocalLoss',
100+
use_sigmoid=True,
101+
gamma=2.0,
102+
alpha=0.25,
103+
loss_weight=1.0),
104+
loss_bbox=dict(type='IoULoss', loss_weight=1.0),
105+
loss_centerness=dict(
106+
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)))
107+
```
108+
109+
假设我们在训练之后有检查点 `./work_dirs/faster-rcnn_r50_fpn_fcos-rpn_1x_coco/epoch_12.pth` ,然后,我们可以使用下面的命令来评估建议的质量。
110+
111+
```python
112+
# 使用8个 GPU 进行测试
113+
bash
114+
tools/dist_test.sh
115+
configs/rpn/fcos-rpn_r50_fpn_1x_coco.py
116+
--work_dirs /faster-rcnn_r50_fpn_fcos-rpn_1x_coco/epoch_12.pth
117+
```
118+
119+
## 用预先训练的 FCOS 训练定制的 Faster R-CNN
120+
121+
预训练不仅加快了训练的收敛速度,而且提高了检测器的性能。因此,我们在这里给出一个例子来说明如何使用预先训练的 FCOS 作为 RPN 来加速训练和提高精度。假设我们想在 Faster R-CNN 中使用 `FCOSHead` 作为 `rpn_head`,并加载预先训练权重来进行训练 [`fcos_r50-caffe_fpn_gn-head_1x_coco`](https://download.openmmlab.com/mmdetection/v2.0/fcos/fcos_r50_caffe_fpn_gn-head_1x_coco/fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth)。 配置文件 `configs/faster_rcnn/faster-rcnn_r50-caffe_fpn_fcos- rpn_1x_copy .py` 的内容如下所示。注意,`fcos_r50-caffe_fpn_gn-head_1x_coco` 使用 ResNet50 的 caffe 版本,因此需要更新 `data_preprocessor` 中的像素平均值和 std。
122+
123+
```python
124+
_base_ = [
125+
'../_base_/models/faster-rcnn_r50_fpn.py',
126+
'../_base_/datasets/coco_detection.py',
127+
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
128+
]
129+
model = dict(
130+
data_preprocessor=dict(
131+
mean=[103.530, 116.280, 123.675],
132+
std=[1.0, 1.0, 1.0],
133+
bgr_to_rgb=False),
134+
backbone=dict(
135+
norm_cfg=dict(type='BN', requires_grad=False),
136+
style='caffe',
137+
init_cfg=None), # the checkpoint in ``load_from`` contains the weights of backbone
138+
neck=dict(
139+
start_level=1,
140+
add_extra_convs='on_output', # 使用 P5
141+
relu_before_extra_convs=True),
142+
rpn_head=dict(
143+
_delete_=True, # 忽略未使用的旧设置
144+
type='FCOSHead',
145+
num_classes=1, # 对于 rpn, num_classes = 1,如果 num_classes > 1,它将在 TwoStageDetector 中自动设置为1
146+
in_channels=256,
147+
stacked_convs=4,
148+
feat_channels=256,
149+
strides=[8, 16, 32, 64, 128],
150+
loss_cls=dict(
151+
type='FocalLoss',
152+
use_sigmoid=True,
153+
gamma=2.0,
154+
alpha=0.25,
155+
loss_weight=1.0),
156+
loss_bbox=dict(type='IoULoss', loss_weight=1.0),
157+
loss_centerness=dict(
158+
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)),
159+
roi_head=dict( # update featmap_strides due to the strides in neck
160+
bbox_roi_extractor=dict(featmap_strides=[8, 16, 32, 64, 128])))
161+
load_from = 'https://download.openmmlab.com/mmdetection/v2.0/fcos/fcos_r50_caffe_fpn_gn-head_1x_coco/fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth'
162+
```
163+
164+
训练命令如下。
165+
166+
```python
167+
bash
168+
tools/dist_train.sh
169+
configs/faster_rcnn/faster-rcnn_r50-caffe_fpn_fcos-rpn_1x_coco.py \
170+
--work-dir /work_dirs/faster-rcnn_r50-caffe_fpn_fcos-rpn_1x_coco
171+
```
Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,174 @@
1-
# 提交测试结果(待更新)
1+
# 提交测试结果
2+
3+
## 全景分割测试结果提交
4+
5+
下面几节介绍如何在 COCO 测试开发集上生成泛视分割模型的预测结果,并将预测提交到 [COCO评估服务器](https://competitions.codalab.org/competitions/19507)
6+
7+
### 前提条件
8+
9+
- 下载 [COCO测试数据集图像](http://images.cocodataset.org/zips/test2017.zip)[测试图像信息](http://images.cocodataset.org/annotations/image_info_test2017.zip),和[全景训练/相关注释](http://images.cocodataset.org/annotations/panoptic_annotations_trainval2017.zip),然后解压缩它们,把 `test2017` 放到 `data/coco/`,把 json 文件和注释文件放到 `data/coco/annotations/`
10+
11+
```shell
12+
# 假设 data/coco/ 不存在
13+
mkdir -pv data/coco/
14+
# 下载 test2017
15+
wget -P data/coco/ http://images.cocodataset.org/zips/test2017.zip
16+
wget -P data/coco/ http://images.cocodataset.org/annotations/image_info_test2017.zip
17+
wget -P data/coco/ http://images.cocodataset.org/annotations/panoptic_annotations_trainval2017.zip
18+
# 解压缩它们
19+
unzip data/coco/test2017.zip -d data/coco/
20+
unzip data/coco/image_info_test2017.zip -d data/coco/
21+
unzip data/coco/panoptic_annotations_trainval2017.zip -d data/coco/
22+
# 删除 zip 文件(可选)
23+
rm -rf data/coco/test2017.zip data/coco/image_info_test2017.zip data/coco/panoptic_annotations_trainval2017.zip
24+
```
25+
26+
- 运行以下代码更新测试图像信息中的类别信息。由于 `image_info_test-dev2017.json` 的类别信息中缺少属性 `isthing` ,我们需要用 `panoptic_val2017.json` 中的类别信息更新它。
27+
28+
```shell
29+
python tools/misc/gen_coco_panoptic_test_info.py data/coco/annotations
30+
```
31+
32+
在完成上述准备之后,你的 `data` 目录结构应该是这样:
33+
34+
```text
35+
data
36+
`-- coco
37+
|-- annotations
38+
| |-- image_info_test-dev2017.json
39+
| |-- image_info_test2017.json
40+
| |-- panoptic_image_info_test-dev2017.json
41+
| |-- panoptic_train2017.json
42+
| |-- panoptic_train2017.zip
43+
| |-- panoptic_val2017.json
44+
| `-- panoptic_val2017.zip
45+
`-- test2017
46+
```
47+
48+
### coco 测试开发的推理
49+
50+
要在 coco test-dev 上进行推断,我们应该首先更新 `test_dataloder``test_evaluator` 的设置。有两种方法可以做到这一点:1. 在配置文件中更新它们;2. 在命令行中更新它们。
51+
52+
#### 在配置文件中更新它们
53+
54+
相关的设置在 `configs/_base_/datasets/ coco_panoptical .py` 的末尾,如下所示。
55+
56+
```python
57+
test_dataloader = dict(
58+
batch_size=1,
59+
num_workers=1,
60+
persistent_workers=True,
61+
drop_last=False,
62+
sampler=dict(type='DefaultSampler', shuffle=False),
63+
dataset=dict(
64+
type=dataset_type,
65+
data_root=data_root,
66+
ann_file='annotations/panoptic_image_info_test-dev2017.json',
67+
data_prefix=dict(img='test2017/'),
68+
test_mode=True,
69+
pipeline=test_pipeline))
70+
test_evaluator = dict(
71+
type='CocoPanopticMetric',
72+
format_only=True,
73+
ann_file=data_root + 'annotations/panoptic_image_info_test-dev2017.json',
74+
outfile_prefix='./work_dirs/coco_panoptic/test')
75+
```
76+
77+
以下任何一种方法都可以用于更新 coco test-dev 集上的推理设置
78+
79+
情况1:直接取消注释 `configs/_base_/datasets/ coco_panoptical .py` 中的设置。
80+
81+
情况2:将以下设置复制到您现在使用的配置文件中。
82+
83+
```python
84+
test_dataloader = dict(
85+
dataset=dict(
86+
ann_file='annotations/panoptic_image_info_test-dev2017.json',
87+
data_prefix=dict(img='test2017/', _delete_=True)))
88+
test_evaluator = dict(
89+
format_only=True,
90+
ann_file=data_root + 'annotations/panoptic_image_info_test-dev2017.json',
91+
outfile_prefix='./work_dirs/coco_panoptic/test')
92+
```
93+
94+
然后通过以下命令对 coco test-dev et 进行推断。
95+
96+
```shell
97+
python tools/test.py \
98+
${CONFIG_FILE} \
99+
${CHECKPOINT_FILE}
100+
```
101+
102+
#### 在命令行中更新它们
103+
104+
coco test-dev 上更新相关设置和推理的命令如下所示。
105+
106+
```shell
107+
# 用一个 gpu 测试
108+
CUDA_VISIBLE_DEVICES=0 python tools/test.py \
109+
${CONFIG_FILE} \
110+
${CHECKPOINT_FILE} \
111+
--cfg-options \
112+
test_dataloader.dataset.ann_file=annotations/panoptic_image_info_test-dev2017.json \
113+
test_dataloader.dataset.data_prefix.img=test2017 \
114+
test_dataloader.dataset.data_prefix._delete_=True \
115+
test_evaluator.format_only=True \
116+
test_evaluator.ann_file=data/coco/annotations/panoptic_image_info_test-dev2017.json \
117+
test_evaluator.outfile_prefix=${WORK_DIR}/results
118+
# 用四个 gpu 测试
119+
CUDA_VISIBLE_DEVICES=0,1,3,4 bash tools/dist_test.sh \
120+
${CONFIG_FILE} \
121+
${CHECKPOINT_FILE} \
122+
8 \ # eights gpus
123+
--cfg-options \
124+
test_dataloader.dataset.ann_file=annotations/panoptic_image_info_test-dev2017.json \
125+
test_dataloader.dataset.data_prefix.img=test2017 \
126+
test_dataloader.dataset.data_prefix._delete_=True \
127+
test_evaluator.format_only=True \
128+
test_evaluator.ann_file=data/coco/annotations/panoptic_image_info_test-dev2017.json \
129+
test_evaluator.outfile_prefix=${WORK_DIR}/results
130+
# 用 slurm 测试
131+
GPUS=8 tools/slurm_test.sh \
132+
${Partition} \
133+
${JOB_NAME} \
134+
${CONFIG_FILE} \
135+
${CHECKPOINT_FILE} \
136+
--cfg-options \
137+
test_dataloader.dataset.ann_file=annotations/panoptic_image_info_test-dev2017.json \
138+
test_dataloader.dataset.data_prefix.img=test2017 \
139+
test_dataloader.dataset.data_prefix._delete_=True \
140+
test_evaluator.format_only=True \
141+
test_evaluator.ann_file=data/coco/annotations/panoptic_image_info_test-dev2017.json \
142+
test_evaluator.outfile_prefix=${WORK_DIR}/results
143+
```
144+
145+
例子:假设我们使用预先训练的带有 ResNet-50 骨干网的 MaskFormer 对 `test2017` 执行推断。
146+
147+
```shell
148+
# 单 gpu 测试
149+
CUDA_VISIBLE_DEVICES=0 python tools/test.py \
150+
configs/maskformer/maskformer_r50_mstrain_16x1_75e_coco.py \
151+
checkpoints/maskformer_r50_mstrain_16x1_75e_coco_20220221_141956-bc2699cb.pth \
152+
--cfg-options \
153+
test_dataloader.dataset.ann_file=annotations/panoptic_image_info_test-dev2017.json \
154+
test_dataloader.dataset.data_prefix.img=test2017 \
155+
test_dataloader.dataset.data_prefix._delete_=True \
156+
test_evaluator.format_only=True \
157+
test_evaluator.ann_file=data/coco/annotations/panoptic_image_info_test-dev2017.json \
158+
test_evaluator.outfile_prefix=work_dirs/maskformer/results
159+
```
160+
161+
### 重命名文件并压缩结果
162+
163+
推理之后,全景分割结果(一个 json 文件和一个存储掩码的目录)将在 `WORK_DIR` 中。我们应该按照 [COCO's Website](https://cocodataset.org/#upload)上的命名约定重新命名它们。最后,我们需要将 json 和存储掩码的目录压缩到 zip 文件中,并根据命名约定重命名该 zip 文件。注意, zip 文件应该**直接**包含上述两个文件。
164+
165+
重命名文件和压缩结果的命令:
166+
167+
```shell
168+
# 在 WORK_DIR 中,我们有 panoptic 分割结果: 'panoptic' 和 'results. panoptical .json'。
169+
cd ${WORK_DIR}
170+
# 将 '[algorithm_name]' 替换为您使用的算法名称
171+
mv ./panoptic ./panoptic_test-dev2017_[algorithm_name]_results
172+
mv ./results.panoptic.json ./panoptic_test-dev2017_[algorithm_name]_results.json
173+
zip panoptic_test-dev2017_[algorithm_name]_results.zip -ur panoptic_test-dev2017_[algorithm_name]_results panoptic_test-dev2017_[algorithm_name]_results.json
174+
```

0 commit comments

Comments
 (0)