Skip to content

Commit 794a87c

Browse files
authored
[Feature] Support CPU training (open-mmlab#7016)
* Modify docs * Support CPU training * Modify docs * Modify Chinese docs
1 parent e3ce10c commit 794a87c

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

docs/en/1_exist_data_model.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ We provide testing scripts for evaluating an existing model on the whole dataset
246246
The following testing environments are supported:
247247

248248
- single GPU
249+
- CPU
249250
- single node multiple GPUs
250251
- multiple nodes
251252

@@ -260,6 +261,15 @@ python tools/test.py \
260261
[--eval ${EVAL_METRICS}] \
261262
[--show]
262263

264+
# CPU: disable GPUs and run single-gpu testing script
265+
export CUDA_VISIBLE_DEVICES=-1
266+
python tools/test.py \
267+
${CONFIG_FILE} \
268+
${CHECKPOINT_FILE} \
269+
[--out ${RESULT_FILE}] \
270+
[--eval ${EVAL_METRICS}] \
271+
[--show]
272+
263273
# multi-gpu testing
264274
bash tools/dist_test.sh \
265275
${CONFIG_FILE} \
@@ -398,6 +408,15 @@ python tools/test.py \
398408
--options ${JSONFILE_PREFIX} \
399409
[--show]
400410

411+
# CPU: disable GPUs and run single-gpu testing script
412+
export CUDA_VISIBLE_DEVICES=-1
413+
python tools/test.py \
414+
${CONFIG_FILE} \
415+
${CHECKPOINT_FILE} \
416+
[--out ${RESULT_FILE}] \
417+
[--eval ${EVAL_METRICS}] \
418+
[--show]
419+
401420
# multi-gpu testing
402421
bash tools/dist_test.sh \
403422
${CONFIG_FILE} \
@@ -521,6 +540,20 @@ Difference between `resume-from` and `load-from`:
521540
`resume-from` loads both the model weights and optimizer status, and the epoch is also inherited from the specified checkpoint. It is usually used for resuming the training process that is interrupted accidentally.
522541
`load-from` only loads the model weights and the training epoch starts from 0. It is usually used for finetuning.
523542

543+
### Training on CPU
544+
545+
The process of training on the CPU is consistent with single GPU training. We just need to disable GPUs before the training process.
546+
547+
```shell
548+
export CUDA_VISIBLE_DEVICES=-1
549+
```
550+
551+
And then run the script [above](#training-on-a-single-GPU).
552+
553+
**Note**:
554+
555+
We do not recommend users to use CPU for training because it is too slow. We support this feature to allow users to debug on machines without GPU for convenience.
556+
524557
### Training on multiple GPUs
525558

526559
We provide `tools/dist_train.sh` to launch training on multiple GPUs.
@@ -533,7 +566,7 @@ bash ./tools/dist_train.sh \
533566
[optional arguments]
534567
```
535568

536-
Optional arguments remain the same as stated [above](#train-with-a-single-GPU).
569+
Optional arguments remain the same as stated [above](#training-on-a-single-GPU).
537570

538571
#### Launch multiple jobs simultaneously
539572

docs/en/get_started.md

-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ The following table lists affected algorithms.
181181
| CARAFE | CARAFE |
182182
| SyncBatchNorm | ResNeSt |
183183
184-
**Notice:** MMDetection does not support training with CPU for now.
185184
186185
### Another option: Docker Image
187186

docs/zh_cn/1_exist_data_model.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 1: 使用已有模型在标准数据集上进行推理
2+
23
MMDetection 在 [Model Zoo](https://mmdetection.readthedocs.io/en/latest/model_zoo.html) 中提供了数以百计的检测模型,并支持多种标准数据集,包括 Pascal VOC,COCO,Cityscapes,LVIS 等。这份文档将会讲述如何使用这些模型和标准数据集来运行一些常见的任务,包括:
34

45
- 使用现有模型在给定图片上进行推理
@@ -45,6 +46,7 @@ for frame in video:
4546
jupyter notebook 上的演示样例在 [demo/inference_demo.ipynb](https://github.com/open-mmlab/mmdetection/blob/master/demo/inference_demo.ipynb)
4647

4748
### 异步接口-支持 Python 3.7+
49+
4850
对于 Python 3.7+,MMDetection 也有异步接口。利用 CUDA 流,绑定 GPU 的推理代码不会阻塞 CPU,从而使得 CPU/GPU 在单线程应用中能达到更高的利用率。在推理流程中,不同数据样本的推理和不同模型的推理都能并发地运行。
4951

5052
您可以参考 `tests/async_benchmark.py` 来对比同步接口和异步接口的运行速度。
@@ -86,9 +88,11 @@ asyncio.run(main())
8688
```
8789

8890
### 演示样例
91+
8992
我们还提供了三个演示脚本,它们是使用高层编程接口实现的。 [源码在此](https://github.com/open-mmlab/mmdetection/tree/master/demo)
9093

9194
#### 图片样例
95+
9296
这是在单张图片上进行推理的脚本,可以开启 `--async-test` 来进行异步推理。
9397

9498
```shell
@@ -111,6 +115,7 @@ asyncio.run(main())
111115
```
112116

113117
#### 摄像头样例
118+
114119
这是使用摄像头实时图片的推理脚本。
115120

116121
```shell
@@ -131,6 +136,7 @@ asyncio.run(main())
131136
```
132137

133138
#### 视频样例
139+
134140
这是在视频样例上进行推理的脚本。
135141

136142
```shell
@@ -155,11 +161,13 @@ asyncio.run(main())
155161
```
156162

157163
## 在标准数据集上测试现有模型
164+
158165
为了测试一个模型的精度,我们通常会在标准数据集上对其进行测试。MMDetection 支持多个公共数据集,包括 [COCO](https://cocodataset.org/)
159166
[Pascal VOC](http://host.robots.ox.ac.uk/pascal/VOC)[Cityscapes](https://www.cityscapes-dataset.com/) 等等。
160167
这一部分将会介绍如何在支持的数据集上测试现有模型。
161168

162169
### 数据集准备
170+
163171
一些公共数据集,比如 Pascal VOC 及其镜像数据集,或者 COCO 等数据集都可以从官方网站或者镜像网站获取。
164172
注意:在检测任务中,Pascal VOC 2012 是 Pascal VOC 2007 的无交集扩展,我们通常将两者一起使用。
165173
我们建议将数据集下载,然后解压到项目外部的某个文件夹内,然后通过符号链接的方式,将数据集根目录链接到 `$MMDETECTION/data` 文件夹下,格式如下所示。
@@ -230,9 +238,11 @@ python tools/dataset_converters/cityscapes.py \
230238
```
231239

232240
### 测试现有模型
241+
233242
我们提供了测试脚本,能够测试一个现有模型在所有数据集(COCO,Pascal VOC,Cityscapes 等)上的性能。我们支持在如下环境下测试:
234243

235244
- 单 GPU 测试
245+
- CPU 测试
236246
- 单节点多 GPU 测试
237247
- 多节点测试
238248

@@ -247,6 +257,15 @@ python tools/test.py \
247257
[--eval ${EVAL_METRICS}] \
248258
[--show]
249259

260+
# CPU 测试:禁用 GPU 并运行单 GPU 测试脚本
261+
export CUDA_VISIBLE_DEVICES=-1
262+
python tools/test.py \
263+
${CONFIG_FILE} \
264+
${CHECKPOINT_FILE} \
265+
[--out ${RESULT_FILE}] \
266+
[--eval ${EVAL_METRICS}] \
267+
[--show]
268+
250269
# 单节点多 GPU 测试
251270
bash tools/dist_test.sh \
252271
${CONFIG_FILE} \
@@ -349,6 +368,7 @@ bash tools/dist_test.sh \
349368
生成的 png 和 txt 文件在 `./mask_rcnn_cityscapes_test_results` 文件夹下。
350369

351370
### 不使用 Ground Truth 标注进行测试
371+
352372
MMDetection 支持在不使用 ground-truth 标注的情况下对模型进行测试,这需要用到 `CocoDataset`。如果你的数据集格式不是 COCO 格式的,请将其转化成 COCO 格式。如果你的数据集格式是 VOC 或者 Cityscapes,你可以使用 [tools/dataset_converters](https://github.com/open-mmlab/mmdetection/tree/master/tools/dataset_converters) 内的脚本直接将其转化成 COCO 格式。如果是其他格式,可以使用 [images2coco 脚本](https://github.com/open-mmlab/mmdetection/tree/master/tools/dataset_converters/images2coco.py) 进行转换。
353373

354374
```shell
@@ -378,6 +398,15 @@ python tools/test.py \
378398
--options ${JSONFILE_PREFIX} \
379399
[--show]
380400

401+
# CPU 测试:禁用 GPU 并运行单 GPU 测试脚本
402+
export CUDA_VISIBLE_DEVICES=-1
403+
python tools/test.py \
404+
${CONFIG_FILE} \
405+
${CHECKPOINT_FILE} \
406+
[--out ${RESULT_FILE}] \
407+
[--eval ${EVAL_METRICS}] \
408+
[--show]
409+
381410
# 单节点多 GPU 测试
382411
bash tools/dist_test.sh \
383412
${CONFIG_FILE} \
@@ -414,6 +443,7 @@ data = dict(train=dict(...), val=dict(...), test=dict(samples_per_gpu=2, ...))
414443
或者你可以通过将 `--cfg-options` 设置为 `--cfg-options data.test.samples_per_gpu=2` 来开启它。
415444

416445
### 弃用 ImageToTensor
446+
417447
在测试模式下,弃用 `ImageToTensor` 流程,取而代之的是 `DefaultFormatBundle`。建议在你的测试数据流程的配置文件中手动替换它,如:
418448

419449
```python
@@ -461,12 +491,14 @@ MMDetection 也为训练检测模型提供了开盖即食的工具。本节将
461491
在 4 块 GPU 并且每张 GPU 上有 2 张图片的情况下,设置 `lr=0.01`; 在 16 块 GPU 并且每张 GPU 上有 4 张图片的情况下, 设置 `lr=0.08`
462492

463493
### 数据集
494+
464495
训练需要准备好数据集,细节请参考 [数据集准备](#数据集准备)
465496

466497
**注意**
467498
目前,`configs/cityscapes` 文件夹下的配置文件都是使用 COCO 预训练权值进行初始化的。如果网络连接不可用或者速度很慢,你可以提前下载现存的模型。否则可能在训练的开始会有错误发生。
468499

469500
### 使用单 GPU 训练
501+
470502
我们提供了 `tools/train.py` 来开启在单张 GPU 上的训练任务。基本使用如下:
471503

472504
```shell
@@ -495,7 +527,22 @@ evaluation = dict(interval=12)
495527

496528
`resume-from` 既加载了模型的权重和优化器的状态,也会继承指定 checkpoint 的迭代次数,不会重新开始训练。`load-from` 则是只加载模型的权重,它的训练是从头开始的,经常被用于微调模型。
497529

530+
### 使用 CPU 训练
531+
532+
使用 CPU 训练的流程和使用单 GPU 训练的流程一致,我们仅需要在训练流程开始前禁用 GPU。
533+
534+
```shell
535+
export CUDA_VISIBLE_DEVICES=-1
536+
```
537+
538+
之后运行单 GPU 训练脚本即可。
539+
540+
**注意**
541+
542+
我们不推荐用户使用 CPU 进行训练,这太过缓慢。我们支持这个功能是为了方便用户在没有 GPU 的机器上进行调试。
543+
498544
### 在多 GPU 上训练
545+
499546
我们提供了 `tools/dist_train.sh` 来开启在多 GPU 上的训练。基本使用如下:
500547

501548
```shell
@@ -505,9 +552,10 @@ bash ./tools/dist_train.sh \
505552
[optional arguments]
506553
```
507554

508-
可选参数和上一节所说的一致
555+
可选参数和单 GPU 训练的可选参数一致
509556

510557
#### 同时启动多个任务
558+
511559
如果你想在一台机器上启动多个任务的话,比如在一个有 8 块 GPU 的机器上启动 2 个需要 4 块GPU的任务,你需要给不同的训练任务指定不同的端口(默认为 29500)来避免冲突。
512560

513561
如果你使用 `dist_train.sh` 来启动训练任务,你可以使用命令来设置端口。
@@ -518,6 +566,7 @@ CUDA_VISIBLE_DEVICES=4,5,6,7 PORT=29501 ./tools/dist_train.sh ${CONFIG_FILE} 4
518566
```
519567

520568
#### 在多个节点上训练
569+
521570
MMDetection 是依赖 `torch.distributed` 包进行分布式训练的。因此,我们可以通过 PyTorch 的 [启动工具](https://pytorch.org/docs/stable/distributed.html#launch-utility) 来进行基本地使用。
522571

523572
#### 使用 Slurm 来管理任务

docs/zh_cn/get_started.md

-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ MIM 能够自动地安装 OpenMMLab 的项目以及对应的依赖包。
181181
| CARAFE | CARAFE |
182182
| SyncBatchNorm | ResNeSt |
183183

184-
**注意**: MMDetection 目前不支持使用 CPU 进行训练。
185-
186184
### 另一种选择: Docker 镜像
187185

188186
我们提供了 [Dockerfile](https://github.com/open-mmlab/mmdetection/blob/master/docker/Dockerfile) 来生成镜像,请确保 [docker](https://docs.docker.com/engine/install/) 的版本 >= 19.03。

mmdet/apis/train.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ def train_detector(model,
120120
broadcast_buffers=False,
121121
find_unused_parameters=find_unused_parameters)
122122
else:
123-
model = MMDataParallel(
124-
model.cuda(cfg.gpu_ids[0]), device_ids=cfg.gpu_ids)
123+
model = MMDataParallel(model, device_ids=cfg.gpu_ids)
125124

126125
# build runner
127126
optimizer = build_optimizer(model, cfg.optimizer)

0 commit comments

Comments
 (0)