Skip to content

Commit ae5c13e

Browse files
authored
[Refactor] Inference and train tutorials for 1.x (#1952)
* inference, train_test tutorial * fix * add --work-dir
1 parent 99a8f59 commit ae5c13e

File tree

2 files changed

+212
-204
lines changed

2 files changed

+212
-204
lines changed

docs/en/user_guides/3_inference.md

Lines changed: 24 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,25 @@
1-
## Inference with pretrained models
2-
3-
We provide testing scripts to evaluate a whole dataset (Cityscapes, PASCAL VOC, ADE20k, etc.),
4-
and also some high-level apis for easier integration to other projects.
5-
6-
### Test a dataset
7-
8-
- single GPU
9-
- CPU
10-
- single node multiple GPU
11-
- multiple node
12-
13-
You can use the following commands to test a dataset.
14-
15-
```shell
16-
# single-gpu testing
17-
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show]
18-
19-
# CPU: If GPU unavailable, directly running single-gpu testing command above
20-
# CPU: If GPU available, disable GPUs and run single-gpu testing script
21-
export CUDA_VISIBLE_DEVICES=-1
22-
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show]
23-
24-
# multi-gpu testing
25-
./tools/dist_test.sh ${CONFIG_FILE} ${CHECKPOINT_FILE} ${GPU_NUM} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}]
1+
# Tutorial 3: Inference with existing models
2+
3+
MMSegmentation provides pre-trained models for semantic segmentation in [Model Zoo](../model_zoo.md), and supports multiple standard datasets, including Cityscapes, ADE20K, etc.
4+
This note will show how to use existing models to inference on given images.
5+
As for how to test existing models on standard datasets, please see this [guide](./4_train_test.md#Test-models-on-standard-datasets)
6+
7+
## Inference on given images
8+
9+
MMSegmentation provides high-level Python APIs for inference on images. Here is an example of building the model and inference on given images.
10+
Please download the [pre-trained model](https://download.openmmlab.com/mmsegmentation/v0.5/pspnet/pspnet_r50-d8_512x1024_80k_cityscapes/pspnet_r50-d8_512x1024_80k_cityscapes_20200606_112131-2376f12b.pth) to the path specified by `checkpoint_file` first.
11+
12+
```python
13+
from mmseg.apis import init_model, inference_model
14+
from mmsegseg.utils import register_all_modules
15+
# Specify the path to model config and checkpoint file
16+
config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_80k_cityscapes.py'
17+
checkpoint_file = 'checkpoints/pspnet_r50-d8_512x1024_80k_cityscapes_20200606_112131-2376f12b.pth'
18+
# register all modules in mmseg into the registries
19+
register_all_modules()
20+
# build the model from a config file and a checkpoint file
21+
model = init_model(config_file, checkpoint_file, device='cuda:0')
22+
# test image pair, and save the results
23+
img = 'demo/demo.png'
24+
result = inference_model(model, img)
2625
```
27-
28-
Optional arguments:
29-
30-
- `RESULT_FILE`: Filename of the output results in pickle format. If not specified, the results will not be saved to a file. (After mmseg v0.17, the output results become pre-evaluation results or format result paths)
31-
- `EVAL_METRICS`: Items to be evaluated on the results. Allowed values depend on the dataset, e.g., `mIoU` is available for all dataset. Cityscapes could be evaluated by `cityscapes` as well as standard `mIoU` metrics.
32-
- `--show`: If specified, segmentation results will be plotted on the images and shown in a new window. It is only applicable to single GPU testing and used for debugging and visualization. Please make sure that GUI is available in your environment, otherwise you may encounter the error like `cannot connect to X server`.
33-
- `--show-dir`: If specified, segmentation results will be plotted on the images and saved to the specified directory. It is only applicable to single GPU testing and used for debugging and visualization. You do NOT need a GUI available in your environment for using this option.
34-
- `--eval-options`: Optional parameters for `dataset.format_results` and `dataset.evaluate` during evaluation. When `efficient_test=True`, it will save intermediate results to local files to save CPU memory. Make sure that you have enough local storage space (more than 20GB). (`efficient_test` argument does not have effect after mmseg v0.17, we use a progressive mode to evaluation and format results which can largely save memory cost and evaluation time.)
35-
36-
Examples:
37-
38-
Assume that you have already downloaded the checkpoints to the directory `checkpoints/`.
39-
40-
1. Test PSPNet and visualize the results. Press any key for the next image.
41-
42-
```shell
43-
python tools/test.py configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py \
44-
checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth \
45-
--show
46-
```
47-
48-
2. Test PSPNet and save the painted images for latter visualization.
49-
50-
```shell
51-
python tools/test.py configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py \
52-
checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth \
53-
--show-dir psp_r50_512x1024_40ki_cityscapes_results
54-
```
55-
56-
3. Test PSPNet on PASCAL VOC (without saving the test results) and evaluate the mIoU.
57-
58-
```shell
59-
python tools/test.py configs/pspnet/pspnet_r50-d8_512x1024_20k_voc12aug.py \
60-
checkpoints/pspnet_r50-d8_512x1024_20k_voc12aug_20200605_003338-c57ef100.pth \
61-
--eval mAP
62-
```
63-
64-
4. Test PSPNet with 4 GPUs, and evaluate the standard mIoU and cityscapes metric.
65-
66-
```shell
67-
./tools/dist_test.sh configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py \
68-
checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth \
69-
4 --out results.pkl --eval mIoU cityscapes
70-
```
71-
72-
:::{note}
73-
There is some gap (~0.1%) between cityscapes mIoU and our mIoU. The reason is that cityscapes average each class with class size by default.
74-
We use the simple version without average for all datasets.
75-
:::
76-
77-
5. Test PSPNet on cityscapes test split with 4 GPUs, and generate the png files to be submit to the official evaluation server.
78-
79-
First, add following to config file `configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py`,
80-
81-
```python
82-
data = dict(
83-
test=dict(
84-
img_dir='leftImg8bit/test',
85-
ann_dir='gtFine/test'))
86-
```
87-
88-
Then run test.
89-
90-
```shell
91-
./tools/dist_test.sh configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py \
92-
checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth \
93-
4 --format-only --eval-options "imgfile_prefix=./pspnet_test_results"
94-
```
95-
96-
You will get png files under `./pspnet_test_results` directory.
97-
You may run `zip -r results.zip pspnet_test_results/` and submit the zip file to [evaluation server](https://www.cityscapes-dataset.com/submit/).
98-
99-
6. CPU memory efficient test DeeplabV3+ on Cityscapes (without saving the test results) and evaluate the mIoU.
100-
101-
```shell
102-
python tools/test.py \
103-
configs/deeplabv3plus/deeplabv3plus_r18-d8_512x1024_80k_cityscapes.py \
104-
deeplabv3plus_r18-d8_512x1024_80k_cityscapes_20201226_080942-cff257fe.pth \
105-
--eval-options efficient_test=True \
106-
--eval mIoU
107-
```
108-
109-
Using `pmap` to view CPU memory footprint, it used 2.25GB CPU memory with `efficient_test=True` and 11.06GB CPU memory with `efficient_test=False` . This optional parameter can save a lot of memory. (After mmseg v0.17, efficient_test has not effect and we use a progressive mode to evaluation and format results efficiently by default.)
110-
111-
7. Test PSPNet on LoveDA test split with 1 GPU, and generate the png files to be submit to the official evaluation server.
112-
113-
First, add following to config file `configs/pspnet/pspnet_r50-d8_512x512_80k_loveda.py`,
114-
115-
```python
116-
data = dict(
117-
test=dict(
118-
img_dir='img_dir/test',
119-
ann_dir='ann_dir/test'))
120-
```
121-
122-
Then run test.
123-
124-
```shell
125-
python ./tools/test.py configs/pspnet/pspnet_r50-d8_512x512_80k_loveda.py \
126-
checkpoints/pspnet_r50-d8_512x512_80k_loveda_20211104_155728-88610f9f.pth \
127-
--format-only --eval-options "imgfile_prefix=./pspnet_test_results"
128-
```
129-
130-
You will get png files under `./pspnet_test_results` directory.
131-
You may run `zip -r -j Results.zip pspnet_test_results/` and submit the zip file to [evaluation server](https://codalab.lisn.upsaclay.fr/competitions/421).

0 commit comments

Comments
 (0)