whl_deploy_en.md 9.1 KB
Newer Older
T
Tingquan Gao 已提交
1
# PaddleClas wheel package
C
chenziheng 已提交
2

G
gaotingquan 已提交
3 4 5 6
Paddleclas supports Python WHL package for prediction. At present, WHL package only supports image classification, but does not support subject detection, feature extraction and vector retrieval.

---

G
gaotingquan 已提交
7
## Catalogue
G
gaotingquan 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

- [1. Installation](#1)
- [2. Quick Start](#2)
- [3. Definition of Parameters](#3)
- [4. Usage](#4)
   - [4.1 View help information](#4.1)
   - [4.2 Prediction using inference model provide by PaddleClas](#4.2)
   - [4.3 Prediction using local model files](#4.3)
   - [4.4 Prediction by batch](#4.4)
   - [4.5 Prediction of Internet image](#4.5)
   - [4.6 Prediction of `NumPy.array` format image](#4.6)
   - [4.7 Save the prediction result(s)](#4.7)
   - [4.8 Specify the mapping between class id and label name](#4.8)

<a name="1"></a>
T
Tingquan Gao 已提交
23
## 1. Installation
C
chenziheng 已提交
24

T
Tingquan Gao 已提交
25
* installing from pypi
C
chenziheng 已提交
26 27

```bash
28
pip3 install paddleclas==2.2.1
C
chenziheng 已提交
29 30
```

T
Tingquan Gao 已提交
31 32
* build own whl package and install

C
chenziheng 已提交
33 34
```bash
python3 setup.py bdist_wheel
T
Tingquan Gao 已提交
35
pip3 install dist/*
C
chenziheng 已提交
36 37
```

G
gaotingquan 已提交
38
<a name="2"></a>
T
Tingquan Gao 已提交
39
## 2. Quick Start
T
Tingquan Gao 已提交
40
* Using the `ResNet50` model provided by PaddleClas, the following image(`'docs/images/inference_deployment/whl_demo.jpg'`) as an example.
C
chenziheng 已提交
41

S
sibo2rr 已提交
42
![](../../images/inference_deployment/whl_demo.jpg)
C
chenziheng 已提交
43

T
Tingquan Gao 已提交
44
* Python
C
chenziheng 已提交
45 46
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
47
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
48
infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
49 50
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
51 52
```

T
Tingquan Gao 已提交
53 54
**Note**: `PaddleClas.predict()` is a `generator`. Therefore you need to use `next()` or `for` call it iteratively. It will perform a prediction by `batch_size` and return the prediction result(s) when called. Examples of returned results are as follows:

C
chenziheng 已提交
55
```
T
Tingquan Gao 已提交
56 57
>>> result
[{'class_ids': [8, 7, 136, 80, 84], 'scores': [0.79368, 0.16329, 0.01853, 0.00959, 0.00239], 'label_names': ['hen', 'cock', 'European gallinule, Porphyrio porphyrio', 'black grouse', 'peacock']}]
C
chenziheng 已提交
58 59
```

T
Tingquan Gao 已提交
60
* CLI
C
chenziheng 已提交
61
```bash
T
Tingquan Gao 已提交
62
paddleclas --model_name=ResNet50  --infer_imgs="docs/images/inference_deployment/whl_demo.jpg"
T
Tingquan Gao 已提交
63 64 65 66
```

```
>>> result
T
Tingquan Gao 已提交
67
filename: docs/images/inference_deployment/whl_demo.jpg, top-5, class_ids: [8, 7, 136, 80, 84], scores: [0.79368, 0.16329, 0.01853, 0.00959, 0.00239], label_names: ['hen', 'cock', 'European gallinule, Porphyrio porphyrio', 'black grouse', 'peacock']
T
Tingquan Gao 已提交
68 69 70
Predict complete!
```

G
gaotingquan 已提交
71
<a name="3"></a>
T
Tingquan Gao 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
## 3. Definition of Parameters

The following parameters can be specified in Command Line or used as parameters of the constructor when instantiating the PaddleClas object in Python.
* model_name(str): If using inference model based on ImageNet1k provided by Paddle, please specify the model's name by the parameter.
* inference_model_dir(str): Local model files directory, which is valid when `model_name` is not specified. The directory should contain `inference.pdmodel` and `inference.pdiparams`.
* infer_imgs(str): The path of image to be predicted, or the directory containing the image files, or the URL of the image from Internet.
* use_gpu(bool): Whether to use GPU or not, default by `True`.
* gpu_mem(int): GPU memory usages,default by `8000`
* use_tensorrt(bool): Whether to open TensorRT or not. Using it can greatly promote predict preformance, default by `False`.
* enable_mkldnn(bool): Whether enable MKLDNN or not, default `False`.
* cpu_num_threads(int): Assign number of cpu threads, valid when `--use_gpu` is `False` and `--enable_mkldnn` is `True`, default by `10`.
* batch_size(int): Batch size, default by `1`.
* resize_short(int): Resize the minima between height and width into `resize_short`, default by `256`.
* crop_size(int): Center crop image to `crop_size`, default by `224`.
* topk(int): Print (return) the `topk` prediction results, default by `5`.
* class_id_map_file(str): The mapping file between class ID and label, default by `ImageNet1K` dataset's mapping.
C
chenziheng 已提交
88
* pre_label_image(bool): whether prelabel or not, default=False.
T
Tingquan Gao 已提交
89
* save_dir(str): The directory to save the prediction results that can be used as pre-label, default by `None`, that is, not to save.
C
chenziheng 已提交
90

T
Tingquan Gao 已提交
91
**Note**: If you want to use `Transformer series models`, such as `DeiT_***_384`, `ViT_***_384`, etc., please pay attention to the input size of model, and need to set `resize_short=384`, `resize=384`. The following is a demo.
L
littletomatodonkey 已提交
92

T
Tingquan Gao 已提交
93 94 95
* CLI:
```bash
from paddleclas import PaddleClas, get_default_confg
T
Tingquan Gao 已提交
96
paddleclas --model_name=ViT_base_patch16_384 --infer_imgs='docs/images/inference_deployment/whl_demo.jpg' --resize_short=384 --crop_size=384
T
Tingquan Gao 已提交
97
```
T
Tingquan Gao 已提交
98 99

* Python:
T
Tingquan Gao 已提交
100 101 102 103 104
```python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ViT_base_patch16_384', resize_short=384, crop_size=384)
```

G
gaotingquan 已提交
105
<a name="4"></a>
T
Tingquan Gao 已提交
106
## 4. Usage
T
Tingquan Gao 已提交
107

T
Tingquan Gao 已提交
108 109 110
PaddleClas provides two ways to use:
1. Python interative programming;
2. Bash command line programming.
C
chenziheng 已提交
111

G
gaotingquan 已提交
112
<a name="4.1"></a>
T
Tingquan Gao 已提交
113
### 4.1 View help information
C
chenziheng 已提交
114

T
Tingquan Gao 已提交
115
* CLI
C
chenziheng 已提交
116 117 118 119
```bash
paddleclas -h
```

G
gaotingquan 已提交
120
<a name="4.2"></a>
T
Tingquan Gao 已提交
121 122
### 4.2 Prediction using inference model provide by PaddleClas
You can use the inference model provided by PaddleClas to predict, and only need to specify `model_name`. In this case, PaddleClas will automatically download files of specified model and save them in the directory `~/.paddleclas/`.
C
chenziheng 已提交
123

T
Tingquan Gao 已提交
124
* Python
C
chenziheng 已提交
125 126
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
127
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
128
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
129 130
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
131 132
```

T
Tingquan Gao 已提交
133
* CLI
C
chenziheng 已提交
134
```bash
T
Tingquan Gao 已提交
135
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
136 137
```

G
gaotingquan 已提交
138
<a name="4.3"></a>
T
Tingquan Gao 已提交
139 140 141 142
### 4.3 Prediction using local model files
You can use the local model files trained by yourself to predict, and only need to specify `inference_model_dir`. Note that the directory must contain `inference.pdmodel` and `inference.pdiparams`.

* Python
C
chenziheng 已提交
143 144
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
145
clas = PaddleClas(inference_model_dir='./inference/')
T
Tingquan Gao 已提交
146
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
147 148
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
149 150
```

T
Tingquan Gao 已提交
151
* CLI
C
chenziheng 已提交
152
```bash
T
Tingquan Gao 已提交
153
paddleclas --inference_model_dir='./inference/' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
154 155
```

G
gaotingquan 已提交
156
<a name="4.4"></a>
T
Tingquan Gao 已提交
157 158
### 4.4 Prediction by batch
You can predict by batch, only need to specify `batch_size` when `infer_imgs` is direcotry contain image files.
C
chenziheng 已提交
159

T
Tingquan Gao 已提交
160
* Python
C
chenziheng 已提交
161 162
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
163 164 165 166 167
clas = PaddleClas(model_name='ResNet50', batch_size=2)
infer_imgs = 'docs/images/'
result=clas.predict(infer_imgs)
for r in result:
    print(r)
C
chenziheng 已提交
168 169
```

T
Tingquan Gao 已提交
170 171 172 173
* CLI
```bash
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --batch_size 2
```
C
chenziheng 已提交
174

G
gaotingquan 已提交
175
<a name="4.5"></a>
T
Tingquan Gao 已提交
176 177 178 179
### 4.5 Prediction of Internet image
You can predict the Internet image, only need to specify URL of Internet image by `infer_imgs`. In this case, the image file will be downloaded and saved in the directory `~/.paddleclas/images/`.

* Python
C
chenziheng 已提交
180 181
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
182
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
183
infer_imgs = 'https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
184 185
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
186 187
```

T
Tingquan Gao 已提交
188
* CLI
C
chenziheng 已提交
189
```bash
T
Tingquan Gao 已提交
190
paddleclas --model_name='ResNet50' --infer_imgs='https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
191 192
```

G
gaotingquan 已提交
193
<a name="4.6"></a>
T
Tingquan Gao 已提交
194
### 4.6 Prediction of NumPy.array format image
T
Tingquan Gao 已提交
195
In Python code, you can predict the `NumPy.array` format image, only need to use the `infer_imgs` to transfer variable of image data. Note that the models in PaddleClas only support to predict 3 channels image data, and channels order is `RGB`.
T
Tingquan Gao 已提交
196 197

* python
C
chenziheng 已提交
198
```python
T
Tingquan Gao 已提交
199
import cv2
C
chenziheng 已提交
200
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
201
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
202
infer_imgs = cv2.imread("docs/en/inference_deployment/whl_deploy_en.md")[:, :, ::-1]
T
Tingquan Gao 已提交
203 204
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
205 206
```

G
gaotingquan 已提交
207
<a name="4.7"></a>
T
Tingquan Gao 已提交
208 209 210 211 212 213 214
### 4.7 Save the prediction result(s)
You can save the prediction result(s) as pre-label, only need to use `pre_label_out_dir` to specify the directory to save.

* python
```python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', save_dir='./output_pre_label/')
G
gaotingquan 已提交
215
infer_imgs = 'docs/images/' # it can be infer_imgs folder path which contains all of images you want to predict.
T
Tingquan Gao 已提交
216 217 218 219 220
result=clas.predict(infer_imgs)
print(next(result))
```

* CLI
C
chenziheng 已提交
221
```bash
G
gaotingquan 已提交
222
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --save_dir='./output_pre_label/'
C
chenziheng 已提交
223 224
```

G
gaotingquan 已提交
225
<a name="4.8"></a>
T
Tingquan Gao 已提交
226 227 228 229
### 4.8 Specify the mapping between class id and label name
You can specify the mapping between class id and label name, only need to use `class_id_map_file` to specify the mapping file. PaddleClas uses ImageNet1K's mapping by default.

The content format of mapping file shall be:
C
chenziheng 已提交
230 231

```
T
Tingquan Gao 已提交
232
class_id<space>class_name<\n>
C
chenziheng 已提交
233 234
```

T
Tingquan Gao 已提交
235
For example:
C
chenziheng 已提交
236 237

```
T
Tingquan Gao 已提交
238 239 240 241
0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
C
chenziheng 已提交
242 243
```

T
Tingquan Gao 已提交
244
* Python
C
chenziheng 已提交
245 246
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
247
clas = PaddleClas(model_name='ResNet50', class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt')
T
Tingquan Gao 已提交
248
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
249 250
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
251 252
```

T
Tingquan Gao 已提交
253
* CLI
C
chenziheng 已提交
254
```bash
T
Tingquan Gao 已提交
255
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg' --class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt'
C
chenziheng 已提交
256
```