GETTING_STARTED.md 9.0 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

W
wangguanzhong 已提交
11
PaddleDetection provides scripots for training, evalution and inference with various features according to different configure.
12 13

```bash
W
wangguanzhong 已提交
14
# set PYTHONPATH
15
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      |
| :----------------------: | :------------: | :---------------: | :--------------: | :-----------------: |
W
wangguanzhong 已提交
32
|          -c              |      ALL       |  Select config file  |  None  |  **The description of configure can refer to [CONFIG.md](../advanced_tutorials/CONFIG.md)** |
W
wangguanzhong 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
|          -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  |
|        --use_tb          |   train/infer   |  Whether to record the data with [tb-paddle](https://github.com/linshuliang/tb-paddle), so as to display in Tensorboard  |  False  |      |
|        --tb\_log_dir     |   train/infer   |  tb-paddle logging directory for image  |  train:`tb_log_dir/scalar` infer: `tb_log_dir/image`  |     |
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

W
wangguanzhong 已提交
65
  When using pre-trained model to fine-tune other task, two methods can be used:
66

W
wangguanzhong 已提交
67 68
  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.
69

W
wangguanzhong 已提交
70 71 72
  ```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 已提交
73
                           -o pretrain_weights=output/faster_rcnn_r50_1x/model_final \
W
wangguanzhong 已提交
74 75 76
                              finetune_exclude_pretrained_params = ['cls_score','bbox_pred']
  ```

K
Kaipeng Deng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
- 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 已提交
90 91 92 93 94 95 96
##### NOTES

- `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)
- 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.
97

W
wangguanzhong 已提交
98 99

### Mixed Precision Training
100 101 102 103 104 105 106 107 108 109 110 111 112 113

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`.


114

W
wangguanzhong 已提交
115
### Evaluation
116

W
wangguanzhong 已提交
117
- Evaluate by specified weights path and dataset path
118

W
wangguanzhong 已提交
119 120 121 122 123
  ```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 \
  ```
124

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

127
- Evaluate with json
W
wangguanzhong 已提交
128 129 130 131

  ```bash
  export CUDA_VISIBLE_DEVICES=0
  python tools/eval.py -c configs/faster_rcnn_r50_1x.yml \
W
wangguanzhong 已提交
132 133
             --json_eval \
             -f evaluation/
W
wangguanzhong 已提交
134
  ```
135

W
wangguanzhong 已提交
136
  The json file must be named bbox.json or mask.json, placed in the `evaluation/` directory.
137 138 139

#### NOTES

140 141 142 143
- Multi-GPU evaluation for R-CNN and SSD models is not supported at the
moment, but it is a planned feature


W
wangguanzhong 已提交
144
### Inference
145 146

- Output specified directory && Set up threshold
147

W
wangguanzhong 已提交
148 149 150
  ```bash
  export CUDA_VISIBLE_DEVICES=0
  python tools/infer.py -c configs/faster_rcnn_r50_1x.yml \
151 152
                      --infer_img=demo/000000570688.jpg \
                      --output_dir=infer_output/ \
153
                      --draw_threshold=0.5 \
154 155
                      -o weights=output/faster_rcnn_r50_1x/model_final \
                      --use_tb=Ture
W
wangguanzhong 已提交
156 157 158 159
  ```

  `--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).
160

161

W
wangguanzhong 已提交
162
- Export model
163

W
wangguanzhong 已提交
164
  ```bash
W
wangguanzhong 已提交
165 166 167 168
  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 已提交
169
  ```
170

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

## FAQ

Q
qingqing01 已提交
175 176
**Q:**  Why do I get `NaN` loss values during single GPU training? </br>
**A:**  The default learning rate is tuned to multi-GPU training (8x GPUs), it must
177 178
be adapted for single GPU training accordingly (e.g., divide by 8).
The calculation rules are as follows,they are equivalent: </br>
179

180

181 182
| GPU number  | Learning rate  | Max_iters | Milestones       |
| :---------: | :------------: | :-------: | :--------------: |
183 184 185
| 2           | 0.0025         | 720000    | [480000, 640000] |
| 4           | 0.005          | 360000    | [240000, 320000] |
| 8           | 0.01           | 180000    | [120000, 160000] |
186

187

Q
qingqing01 已提交
188 189 190 191 192
**Q:**  How to reduce GPU memory usage? </br>
**A:**  Setting environment variable FLAGS_conv_workspace_size_limit to a smaller
number can reduce GPU memory footprint without affecting training speed.
Take Mask-RCNN (R50) as example, by setting `export FLAGS_conv_workspace_size_limit=512`,
batch size could reach 4 per GPU (Tesla V100 16GB).
193 194 195 196


**Q:**  How to change data preprocessing? </br>
**A:**  Set `sample_transform` in configuration. Note that **the whole transforms** need to be added in configuration.
G
Guanghua Yu 已提交
197
For example, `DecodeImage`, `NormalizeImage` and `Permute` in RCNN models.