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

G
gaotingquan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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.

---

## Contents

- [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 40
## 2. Quick Start
* Using the `ResNet50` model provided by PaddleClas, the following image(`'docs/images/whl/demo.jpg'`) as an example.
C
chenziheng 已提交
41

L
littletomatodonkey 已提交
42 43 44
<div align="center">
<img src="../images/whl/demo.jpg"  width = "400" />
</div>
C
chenziheng 已提交
45

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

T
Tingquan Gao 已提交
55 56
**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 已提交
57
```
T
Tingquan Gao 已提交
58 59
>>> 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 已提交
60 61
```

T
Tingquan Gao 已提交
62
* CLI
C
chenziheng 已提交
63
```bash
T
Tingquan Gao 已提交
64 65 66 67 68 69 70 71 72
paddleclas --model_name=ResNet50  --infer_imgs="docs/images/whl/demo.jpg"
```

```
>>> result
filename: docs/images/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']
Predict complete!
```

G
gaotingquan 已提交
73
<a name="3"></a>
T
Tingquan Gao 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
## 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 已提交
90
* pre_label_image(bool): whether prelabel or not, default=False.
T
Tingquan Gao 已提交
91
* 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 已提交
92

T
Tingquan Gao 已提交
93
**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 已提交
94

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

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

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

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

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

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

G
gaotingquan 已提交
122
<a name="4.2"></a>
T
Tingquan Gao 已提交
123 124
### 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 已提交
125

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

T
Tingquan Gao 已提交
135
* CLI
C
chenziheng 已提交
136
```bash
T
Tingquan Gao 已提交
137
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/whl/demo.jpg'
C
chenziheng 已提交
138 139
```

G
gaotingquan 已提交
140
<a name="4.3"></a>
T
Tingquan Gao 已提交
141 142 143 144
### 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 已提交
145 146
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
147 148 149 150
clas = PaddleClas(inference_model_dir='./inference/')
infer_imgs = 'docs/images/whl/demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
151 152
```

T
Tingquan Gao 已提交
153
* CLI
C
chenziheng 已提交
154
```bash
T
Tingquan Gao 已提交
155
paddleclas --inference_model_dir='./inference/' --infer_imgs='docs/images/whl/demo.jpg'
C
chenziheng 已提交
156 157
```

G
gaotingquan 已提交
158
<a name="4.4"></a>
T
Tingquan Gao 已提交
159 160
### 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 已提交
161

T
Tingquan Gao 已提交
162
* Python
C
chenziheng 已提交
163 164
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
165 166 167 168 169
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 已提交
170 171
```

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

G
gaotingquan 已提交
177
<a name="4.5"></a>
T
Tingquan Gao 已提交
178 179 180 181
### 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 已提交
182 183
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
184
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
185 186 187
infer_imgs = 'https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/whl/demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
188 189
```

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

G
gaotingquan 已提交
195
<a name="4.6"></a>
T
Tingquan Gao 已提交
196 197 198 199
### 4.6 Prediction of NumPy.array format image
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 image data must be 3 channels.

* python
C
chenziheng 已提交
200
```python
T
Tingquan Gao 已提交
201
import cv2
C
chenziheng 已提交
202
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
203 204 205 206
clas = PaddleClas(model_name='ResNet50')
infer_imgs = cv2.imread("docs/images/whl/demo.jpg")
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
207 208
```

G
gaotingquan 已提交
209
<a name="4.7"></a>
T
Tingquan Gao 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222
### 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/')
infer_imgs = 'docs/images/whl/' # it can be infer_imgs folder path which contains all of images you want to predict.
result=clas.predict(infer_imgs)
print(next(result))
```

* CLI
C
chenziheng 已提交
223
```bash
T
Tingquan Gao 已提交
224
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/whl/' --save_dir='./output_pre_label/'
C
chenziheng 已提交
225 226
```

G
gaotingquan 已提交
227
<a name="4.8"></a>
T
Tingquan Gao 已提交
228 229 230 231
### 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 已提交
232 233

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

T
Tingquan Gao 已提交
237
For example:
C
chenziheng 已提交
238 239

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

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

T
Tingquan Gao 已提交
255
* CLI
C
chenziheng 已提交
256
```bash
T
Tingquan Gao 已提交
257
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/whl/demo.jpg' --class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt'
C
chenziheng 已提交
258
```