GETTING_STARTED.md 8.4 KB
Newer Older
Q
qingqing01 已提交
1 2
English | [简体中文](GETTING_STARTED_cn.md)

3 4
# Getting Started

K
Kaipeng Deng 已提交
5
For setting up the running environment, please refer to [installation
6 7 8
instructions](INSTALL.md).


W
wangguanzhong 已提交
9
## Training/Evaluation/Inference
10

L
LukeAI 已提交
11
PaddleDetection provides scripts for training, evalution and inference with various features according to different configurations.
12 13

```bash
Q
qingqing01 已提交
14 15
# set PYTHONPATH
export PYTHONPATH=$PYTHONPATH:.
W
wangguanzhong 已提交
16
# training in single-GPU and multi-GPU. specify different GPU numbers by CUDA_VISIBLE_DEVICES
17
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
18
python tools/train.py -c configs/faster_rcnn_r50_1x.yml
W
wangguanzhong 已提交
19 20 21 22 23
# GPU evalution
export CUDA_VISIBLE_DEVICES=0
python tools/eval.py -c configs/faster_rcnn_r50_1x.yml
# Inference
python tools/infer.py -c configs/faster_rcnn_r50_1x.yml --infer_img=demo/000000570688.jpg
24 25
```

W
wangguanzhong 已提交
26
### Optional argument list
27

W
wangguanzhong 已提交
28
list below can be viewed by `--help`
29

W
wangguanzhong 已提交
30 31
|         FLAG             |  script supported  |    description    |     default     |      remark      |
| :----------------------: | :------------: | :---------------: | :--------------: | :-----------------: |
L
LukeAI 已提交
32
|          -c              |      ALL       |  Select config file  |  None  |  **See [CONFIG.md](../advanced_tutorials/config_doc/CONFIG.md) for a description of the config options** |
W
wangguanzhong 已提交
33 34 35 36 37 38 39 40 41 42 43
|          -o              |      ALL       |  Set parameters in configure file  |  None  |  `-o` has higher priority to file configured by `-c`. Such as `-o use_gpu=False max_iter=10000`  |  
|   -r/--resume_checkpoint |     train      |  Checkpoint path for resuming training  |  None  |  `-r output/faster_rcnn_r50_1x/10000`  |
|        --eval            |     train      |  Whether to perform evaluation in training  |  False  |    |
|      --output_eval       |     train/eval |  json path in evalution  |  current path  |  `--output_eval ./json_result`  |
|       --fp16             |     train      |  Whether to enable mixed precision training  |  False  |  GPU training is required  |
|       --loss_scale       |     train      |  Loss scaling factor for mixed precision training  |  8.0  |  enable when `--fp16` is True  |  
|       --json_eval        |       eval     |  Whether to evaluate with already existed bbox.json or mask.json  |  False  |  json path is set in `--output_eval`  |
|       --output_dir       |      infer     |  Directory for storing the output visualization files  |  `./output`  |  `--output_dir output`  |
|    --draw_threshold      |      infer     |  Threshold to reserve the result for visualization  |  0.5  |  `--draw_threshold 0.7`  |
|      --infer_dir         |       infer     |  Directory for images to perform inference on  |  None  |    |
|      --infer_img         |       infer     |  Image path  |  None  |  higher priority over --infer_dir  |
44 45
|        --use_vdl          |   train/infer   |  Whether to record the data with [VisualDL](https://github.com/paddlepaddle/visualdl), so as to display in VisualDL  |  False  |  VisualDL requires Python>=3.5   |
|        --vdl\_log_dir     |   train/infer   |  VisualDL logging directory for image  |  train:`vdl_log_dir/scalar` infer: `vdl_log_dir/image`  |  VisualDL requires Python>=3.5   |
46 47


W
wangguanzhong 已提交
48
## Examples
49

W
wangguanzhong 已提交
50
### Training
51 52 53

- Perform evaluation in training

W
wangguanzhong 已提交
54 55 56 57
  ```bash
  export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
  python -u tools/train.py -c configs/faster_rcnn_r50_1x.yml --eval
  ```
58

W
wangguanzhong 已提交
59
  Perform training and evalution alternatively and evaluate at each snapshot_iter. Meanwhile, the best model with highest MAP is saved at each `snapshot_iter` which has the same path as `model_final`.
60

W
wangguanzhong 已提交
61
  If evaluation dataset is large, we suggest decreasing evaluation times or evaluating after training.
62

63 64
- Fine-tune other task

65 66 67 68 69 70 71 72 73 74 75 76
  When using pre-trained model to fine-tune other task, pretrain\_weights can be used directly. The parameters with different shape will be ignored automatically. For example:


  ```bash
  export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
  # If the shape of parameters in program is different from pretrain_weights,
  # then PaddleDetection will not use such parameters.
  python -u tools/train.py -c configs/faster_rcnn_r50_1x.yml \
                           -o pretrain_weights=output/faster_rcnn_r50_1x/model_final \
  ```

  Besides, the name of parameters which need to ignore can be specified explicitly as well. Two methods can be used:
77

W
wangguanzhong 已提交
78 79
  1. The excluded pre-trained parameters can be set by `finetune_exclude_pretrained_params` in YAML config
  2. Set -o finetune\_exclude\_pretrained_params in the arguments.
80

W
wangguanzhong 已提交
81 82 83
  ```bash
  export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
  python -u tools/train.py -c configs/faster_rcnn_r50_1x.yml \
W
wangguanzhong 已提交
84
                           -o pretrain_weights=output/faster_rcnn_r50_1x/model_final \
W
wangguanzhong 已提交
85 86 87
                              finetune_exclude_pretrained_params = ['cls_score','bbox_pred']
  ```

K
Kaipeng Deng 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
- Training YOLOv3 with fine grained YOLOv3 loss built by Paddle OPs in python

  In order to facilitate the redesign of YOLOv3 loss function, we also provide fine grained YOLOv3 loss function building in python code by common Paddle OPs instead of using `fluid.layers.yolov3_loss`,
  training YOLOv3 with python loss function as follows:

  ```bash
  export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
  python -u tools/train.py -c configs/yolov3_darknet.yml \
                           -o use_fine_grained_loss=true
  ```

  Fine grained YOLOv3 loss code is defined in `ppdet/modeling/losses/yolo_loss.py`.

W
wangguanzhong 已提交
101 102
##### NOTES

G
Guanghua Yu 已提交
103
- `CUDA_VISIBLE_DEVICES` can specify different gpu numbers. Such as: `export CUDA_VISIBLE_DEVICES=0,1,2,3`. GPU calculation rules can refer [FAQ](../FAQ.md)
W
wangguanzhong 已提交
104 105 106 107
- Dataset will be downloaded automatically and cached in `~/.cache/paddle/dataset` if not be found locally.
- Pretrained model is downloaded automatically and cached in `~/.cache/paddle/weights`.
- Checkpoints are saved in `output` by default, and can be revised from save_dir in configure files.
- RCNN models training on CPU is not supported on PaddlePaddle<=1.5.1 and will be fixed on later version.
108

W
wangguanzhong 已提交
109 110

### Mixed Precision Training
111 112 113 114 115 116 117 118 119 120 121 122 123 124

Mixed precision training can be enabled with `--fp16` flag. Currently Faster-FPN, Mask-FPN and Yolov3 have been verified to be working with little to no loss of precision (less than 0.2 mAP)

To speed up mixed precision training, it is recommended to train in multi-process mode, for example

```bash
python -m paddle.distributed.launch --selected_gpus 0,1,2,3,4,5,6,7 tools/train.py --fp16 -c configs/faster_rcnn_r50_fpn_1x.yml
```

If loss becomes `NaN` during training, try tweak the `--loss_scale` value. Please refer to the Nvidia [documentation](https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html#mptrain) on mixed precision training for details.

Also, please note mixed precision training currently requires changing `norm_type` from `affine_channel` to `bn`.


125

W
wangguanzhong 已提交
126
### Evaluation
127

W
wangguanzhong 已提交
128
- Evaluate by specified weights path and dataset path
129

W
wangguanzhong 已提交
130 131 132 133 134
  ```bash
  export CUDA_VISIBLE_DEVICES=0
  python -u tools/eval.py -c configs/faster_rcnn_r50_1x.yml \
                          -o weights=https://paddlemodels.bj.bcebos.com/object_detection/faster_rcnn_r50_1x.tar \
  ```
135

G
Guanghua Yu 已提交
136
  The path of model to be evaluted can be both local path and link in [MODEL_ZOO](../MODEL_ZOO_cn.md).
137

138
- Evaluate with json
W
wangguanzhong 已提交
139 140 141 142

  ```bash
  export CUDA_VISIBLE_DEVICES=0
  python tools/eval.py -c configs/faster_rcnn_r50_1x.yml \
W
wangguanzhong 已提交
143 144
             --json_eval \
             -f evaluation/
W
wangguanzhong 已提交
145
  ```
146

W
wangguanzhong 已提交
147
  The json file must be named bbox.json or mask.json, placed in the `evaluation/` directory.
148 149 150

#### NOTES

151 152 153 154
- Multi-GPU evaluation for R-CNN and SSD models is not supported at the
moment, but it is a planned feature


W
wangguanzhong 已提交
155
### Inference
156 157

- Output specified directory && Set up threshold
158

W
wangguanzhong 已提交
159 160 161
  ```bash
  export CUDA_VISIBLE_DEVICES=0
  python tools/infer.py -c configs/faster_rcnn_r50_1x.yml \
162 163
                      --infer_img=demo/000000570688.jpg \
                      --output_dir=infer_output/ \
164
                      --draw_threshold=0.5 \
165
                      -o weights=output/faster_rcnn_r50_1x/model_final \
走神的阿圆's avatar
走神的阿圆 已提交
166
                      --use_vdl=Ture
W
wangguanzhong 已提交
167 168 169 170
  ```

  `--draw_threshold` is an optional argument. Default is 0.5.
  Different thresholds will produce different results depending on the calculation of [NMS](https://ieeexplore.ieee.org/document/1699659).
171

172

W
wangguanzhong 已提交
173
- Export model
174

W
wangguanzhong 已提交
175
  ```bash
W
wangguanzhong 已提交
176 177 178 179
  python tools/export_model.py -c configs/faster_rcnn_r50_1x.yml \
                      --output_dir=inference_model \
                      -o weights=output/faster_rcnn_r50_1x/model_final \
                         FasterRCNNTestFeed.image_shape=[3,800,1333]
W
wangguanzhong 已提交
180
  ```
181

W
wangguanzhong 已提交
182
  Save inference model `tools/export_model.py`, which can be loaded by PaddlePaddle predict library.