未验证 提交 abc8ac5c 编写于 作者: T Tingquan Gao 提交者: GitHub

Modify to fit the dygraph (#343)

* Add English version of getting started.
上级 806675d2
# Getting Started
---
Please refer to [Installation](install.md) to setup environment at first, and prepare ImageNet1K data by following the instruction mentioned in the [data](data.md)
Please refer to [Installation](install_en.md) to setup environment at first, and prepare flower102 dataset by following the instruction mentioned in the [Quick Start](quick_start_en.md).
## 1. Training and Evaluation on Windows or CPU
If training and evaluation are performed on Windows system or CPU, it is recommended to use the `tools/train_multi_platform.py` and `tools/eval_multi_platform.py` scripts.
## 1. Training and Evaluation on CPU or Single GPU
If training and evaluation are performed on CPU or single GPU, it is recommended to use the `tools/train.py` and `tools/eval.py`.
For training and evaluation in multi-GPU environment on Linux, please refer to [2. Training and evaluation on Linux+GPU](#2-training-and-evaluation-on-linuxgpu).
<a name="1.1"></a>
## 1.1 Model training
After preparing the configuration file, The training process can be started in the following way.
```
python tools/train_multi_platform.py \
-c configs/ResNet/ResNet50.yaml \
-o model_save_dir=./output/ \
-o use_gpu=True
python tools/train.py \
-c configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
-o pretrained_model="" \
-o use_gpu=False
```
Among them, `-c` is used to specify the path of the configuration file, `-o` is used to specify the parameters needed to be modified or added, `-o model_save_dir=./output/` means to modify the `model_save_dir` in the configuration file to ` ./output/`. `-o use_gpu=True` means to use GPU for training. If you want to use the CPU for training, you need to set `use_gpu` to `False`.
Among them, `-c` is used to specify the path of the configuration file, `-o` is used to specify the parameters needed to be modified or added, `-o pretrained_model=""` means to not using pre-trained models.
`-o use_gpu=True` means to use GPU for training. If you want to use the CPU for training, you need to set `use_gpu` to `False`.
Of course, you can also directly modify the configuration file to update the configuration. For specific configuration parameters, please refer to [Configuration Document](config.md).
Of course, you can also directly modify the configuration file to update the configuration. For specific configuration parameters, please refer to [Configuration Document](config_en.md).
* The output log examples are as follows:
* If mixup or cutmix is used in training, only loss, lr (learning rate) and training time of the minibatch will be printed in the log.
......@@ -36,68 +38,89 @@ Of course, you can also directly modify the configuration file to update the con
epoch:0 train step:13 loss:7.9561 top1:0.0156 top5:0.1094 lr:0.100000 elapse:0.193s
```
During training, you can view loss changes in real time through `VisualDL`. The command is as follows.
```bash
visualdl --logdir ./scalar --host <host_IP> --port <port_num>
```
During training, you can view loss changes in real time through `VisualDL`, see [VisualDL](https://github.com/PaddlePaddle/VisualDL) for details.
### 1.2 Model finetuning
* After configuring the configuration file, you can finetune it by loading the pretrained weights, The command is as shown below.
After configuring the configuration file, you can finetune it by loading the pretrained weights, The command is as shown below.
```
python tools/train_multi_platform.py \
-c configs/ResNet/ResNet50.yaml \
-o pretrained_model="./pretrained/ResNet50_pretrained"
python tools/train.py \
-c configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
-o pretrained_model="./pretrained/MobileNetV3_large_x1_0_pretrained" \
-o use_gpu=True
```
Among them, `pretrained_model` is used to set the address to load the pretrained weights. When using it, you need to replace it with your own pretrained weights' path, or you can modify the path directly in the configuration file.
Among them, `-o pretrained_model` is used to set the address to load the pretrained weights. When using it, you need to replace it with your own pretrained weights' path, or you can modify the path directly in the configuration file.
We also provide a lot of pre-trained models trained on the ImageNet-1k dataset. For the model list and download address, please refer to the [model library overview](../models/models_intro_en.md).
### 1.3 Resume Training
* If the training process is terminated for some reasons, you can also load the checkpoints to continue training.
If the training process is terminated for some reasons, you can also load the checkpoints to continue training.
```
python tools/train_multi_platform.py \
-c configs/ResNet/ResNet50.yaml \
-o checkpoints="./output/ResNet/0/ppcls"
python tools/train.py \
-c configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
-o checkpoints="./output/MobileNetV3_large_x1_0_gpupaddle/0/ppcls" \
-o last_epoch=5 \
-o use_gpu=True
```
The configuration file does not need to be modified. You only need to add the `checkpoints` parameter during training, which represents the path of the checkpoints. The parameter weights, earning rate, optimizer and other information will be loaded using this parameter.
The configuration file does not need to be modified. You only need to add the `checkpoints` parameter during training, which represents the path of the checkpoints. The parameter weights, learning rate, optimizer and other information will be loaded using this parameter.
**Note**:
* The parameter `-o last_epoch=5` means to record the number of the last training epoch as `5`, that is, the number of this training epoch starts from `6`, , and the parameter defaults to `-1`, which means the number of this training epoch starts from `0`.
* The `-o checkpoints` parameter does not need to include the suffix of the checkpoints. The above training command will generate the checkpoints as shown below during the training process. If you want to continue training from the epoch `0`, Just set the `checkpoints` to `./output/MobileNetV3_large_x1_0_gpupaddle/0/ppcls`, PaddleClas will automatically fill in the `pdopt` and `pdparams` suffixes.
```shell
output/
└── MobileNetV3_large_x1_0
├── 0
│ ├── ppcls.pdopt
│ └── ppcls.pdparams
├── 1
│ ├── ppcls.pdopt
│ └── ppcls.pdparams
.
.
.
```
### 1.4 Model evaluation
* The model evaluation process can be started as follows.
The model evaluation process can be started as follows.
```bash
python tools/eval_multi_platform.py \
python tools/eval.py \
-c ./configs/eval.yaml \
-o ARCHITECTURE.name="ResNet50_vd" \
-o ARCHITECTURE.name="MobileNetV3_large_x1_0" \
-o pretrained_model=path_to_pretrained_models
```
You can modify the `ARCHITECTURE.name` field and `pretrained_model` field in `configs/eval.yaml` to configure the evaluation model, and you also can update the configuration through the -o parameter.
You can modify the `ARCHITECTURE.name` field and `pretrained_model` field in `configs/eval.yaml` to configure the evaluation model, and you also can update the configuration through the `-o` parameter.
**Note:** When loading the pretrained model, you need to specify the prefix of the pretrained model. For example, the pretrained model path is `output/ResNet50_vd/19`, and the pretrained model filename is `output/ResNet50_vd/19/ppcls.pdparams`, the parameter `pretrained_model` needs to be specified as `output/ResNet50_vd/19/ppcls`, PaddleClas will automatically fill in the `.pdparams` suffix.
**Note:** When loading the pre-trained model, you need to specify the prefix of the pretrained model, such as [1.3 Resume Training](#13-resume-training).
<a name="2"></a>
### 2. Training and evaluation on Linux+GPU
If you want to run PaddleClas on Linux with GPU, it is highly recommended to use the model training and evaluation scripts provided by PaddleClas: `tools/train.py` and `tools/eval.py`.
If you want to run PaddleClas on Linux with GPU, it is highly recommended to use `paddle.distributed.launch` to start the model training script(`tools/train.py`) and evaluation script(`tools/eval.py`), which can start on multi-GPU environment more conveniently.
### 2.1 Model training
After preparing the configuration file, The training process can be started in the following way.
After preparing the configuration file, The training process can be started in the following way. `paddle.distributed.launch` specifies the GPU running card number by setting `selected_gpus`:
```bash
# PaddleClas starts multi-card and multi-process training through launch
# Specify the GPU running card number by setting FLAGS_selected_gpus
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch \
--selected_gpus="0,1,2,3" \
tools/train.py \
-c ./configs/ResNet/ResNet50_vd.yaml
-c ./configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
```
The configuration can be updated by adding the `-o` parameter.
......@@ -106,20 +129,20 @@ The configuration can be updated by adding the `-o` parameter.
python -m paddle.distributed.launch \
--selected_gpus="0,1,2,3" \
tools/train.py \
-c ./configs/ResNet/ResNet50_vd.yaml \
-o use_mix=1 \
--vdl_dir=./scalar/
-c ./configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
-o pretrained_model="" \
-o use_gpu=True
```
The format of output log information is the same as above.
The format of output log information is the same as above, see [1.1 Model training](#11-model-training) for details.
### 2.2 Model finetuning
* After configuring the configuration file, you can finetune it by loading the pretrained weights, The command is as shown below.
After configuring the configuration file, you can finetune it by loading the pretrained weights, The command is as shown below.
```
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch \
--selected_gpus="0,1,2,3" \
tools/train.py \
......@@ -129,58 +152,105 @@ python -m paddle.distributed.launch \
Among them, `pretrained_model` is used to set the address to load the pretrained weights. When using it, you need to replace it with your own pretrained weights' path, or you can modify the path directly in the configuration file.
* There contains a lot of examples of model finetuning in [The quick start tutorial](./quick_start_en.md). You can refer to this tutorial to finetune the model on a specific dataset.
There contains a lot of examples of model finetuning in [Quick Start](./quick_start_en.md). You can refer to this tutorial to finetune the model on a specific dataset.
<a name="model_resume"></a>
### 2.3 Resume Training
* If the training process is terminated for some reasons, you can also load the checkpoints to continue training.
If the training process is terminated for some reasons, you can also load the checkpoints to continue training.
```
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch \
--selected_gpus="0,1,2,3" \
tools/train.py \
-c configs/ResNet/ResNet50.yaml \
-c ./configs/quick_start/MobileNetV3_large_x1_0_finetune.yaml \
-o checkpoints="./output/ResNet/0/ppcls"
-o last_epoch=5 \
-o use_gpu=True
```
The configuration file does not need to be modified. You only need to add the `checkpoints` parameter during training, which represents the path of the checkpoints. The parameter weights, learning rate, optimizer and other information will be loaded using this parameter.
The configuration file does not need to be modified. You only need to add the `checkpoints` parameter during training, which represents the path of the checkpoints. The parameter weights, learning rate, optimizer and other information will be loaded using this parameter. About `last_epoch` parameter, please refer [1.3 Resume training](#13-resume-training) for details.
### 2.4 Model evaluation
* The model evaluation process can be started as follows.
The model evaluation process can be started as follows.
```bash
python tools/eval_multi_platform.py \
python tools/eval.py \
-c ./configs/eval.yaml \
-o ARCHITECTURE.name="ResNet50_vd" \
-o ARCHITECTURE.name="MobileNetV3_large_x1_0" \
-o pretrained_model=path_to_pretrained_models
```
You can modify the `ARCHITECTURE.name` field and `pretrained_model` field in `configs/eval.yaml` to configure the evaluation model, and you also can update the configuration through the -o parameter.
You can modify the `ARCHITECTURE.name` field and `pretrained_model` field in `configs/eval.yaml` to configure the evaluation model, and you also can update the configuration through the `-o` parameter.
<a name="model_infer"></a>
## 3. Use the pre-trained model to predict
After the training is completed, you can predict by using the pre-trained model obtained by the training, as follows:
## 3. Model inference
```python
python tools/infer/infer.py \
--i image path \
--m model name \
--p path to persistence model \
--use_gpu True \
--load_static_weights False
```
PaddlePaddle provides three ways to perform model inference. Next, how to use the inference engine to perforance model inference will be introduced.
Among them:
+ `image_file`(i): The path of the image file to be predicted, such as `./test.jpeg`;
+ `model`(m): Model name, such as `MobileNetV3_large_x1_0`;
+ `pretrained_model`(p): Weight file path, such as `./pretrained/MobileNetV3_large_x1_0_pretrained/`;
+ `use_gpu`: Whether to use the GPU, default by `True`;
+ `load_static_weights`: Whether to load the pre-trained model obtained from static image training, default by `False`;
+ `pre_label_image`: Whether to pre-label the image data, default value: `False`;
+ `pre_label_out_idr`: The output path of pre-labeled image data. When `pre_label_image=True`, a lot of subfolders will be generated under the path, each subfolder represent a category, which stores all the images predicted by the model to belong to the category.
About more detailed infomation, you can refer to [infer.py](../../../tools/infer/infer.py).
<a name="model_inference"></a>
## 4. Use the inference model to predict
PaddlePaddle supports inference using prediction engines, which will be introduced next.
Firstly, you should export inference model using `tools/export_model.py`.
```bash
python tools/export_model.py \
--model=model_name \
--pretrained_model=pretrained_model_dir \
--output_path=save_inference_dir
--model=MobileNetV3_large_x1_0 \
--pretrained_model=./output/MobileNetV3_large_x1_0/best_model/ppcls \
--output_path=./exported_model
```
Among them, the `--model` parameter is used to specify the model name, `--pretrained_model` parameter is used to specify the model file path, the path does not need to include the model file suffix name, and `--output_path` is used to specify the storage path of the converted model .
**Note**: In the file `export_model.py:line53`, the `shape` parameter is the shape of the model input image, the default is `224*224`. Please modify it according to the actual situation, as shown below:
```python
50 # Please modify the 'shape' according to actual needs
51 @to_static(input_spec=[
52 paddle.static.InputSpec(
53 shape=[None, 3, 224, 224], dtype='float32')
54 ])
```
Secondly, Inference engine can be started using the following commands.
The above command will generate the model structure file (`__model__`) and the model weight file (`__variables__`), and then the inference engine can be used for inference:
```bash
python tools/infer/predict.py \
-m model_path \
-p params_path \
-i image path \
-m path of __model__ \
-p path of __variables__ \
--use_gpu=1 \
--use_tensorrt=True
```
please refer to [inference](../extension/paddle_inference_en.md) for more details.
Among them:
+ `image_file`(i): The path of the image file to be predicted, such as `./test.jpeg`;
+ `model_file`(m): Model file path, such as `./MobileNetV3_large_x1_0/__model__`;
+ `params_file`(p): Weight file path, such as `./MobileNetV3_large_x1_0/__variables__`;
+ `use_tensorrt`: Whether to use the TesorRT, default by `True`;
+ `use_gpu`: Whether to use the GPU, default by `True`.
If you want to evaluate the speed of the model, it is recommended to use [predict.py](../../../tools/infer/predict.py), and enable TensorRT to accelerate.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册