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

G
gaotingquan 已提交
3
PaddleClas supports Python wheel package for prediction. At present, PaddleClas wheel supports image classification including ImagetNet1k models and PULC models, but does not support mainbody detection, feature extraction and vector retrieval.
G
gaotingquan 已提交
4 5 6

---

G
gaotingquan 已提交
7
## Catalogue
G
gaotingquan 已提交
8 9 10

- [1. Installation](#1)
- [2. Quick Start](#2)
G
gaotingquan 已提交
11 12
   - [2.1 ImageNet1k models](#2.1)
   - [2.2 PULC models](#2.2)
G
gaotingquan 已提交
13
- [3. Definition of Parameters](#3)
G
gaotingquan 已提交
14
- [4. More usage](#4)
G
gaotingquan 已提交
15 16 17 18 19 20 21 22 23 24
   - [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>
G
gaotingquan 已提交
25

T
Tingquan Gao 已提交
26
## 1. Installation
C
chenziheng 已提交
27

T
Tingquan Gao 已提交
28
* installing from pypi
C
chenziheng 已提交
29 30

```bash
31
pip3 install paddleclas==2.2.1
C
chenziheng 已提交
32 33
```

T
Tingquan Gao 已提交
34 35
* build own whl package and install

C
chenziheng 已提交
36 37
```bash
python3 setup.py bdist_wheel
T
Tingquan Gao 已提交
38
pip3 install dist/*
C
chenziheng 已提交
39 40
```

G
gaotingquan 已提交
41
<a name="2"></a>
G
gaotingquan 已提交
42

T
Tingquan Gao 已提交
43
## 2. Quick Start
G
gaotingquan 已提交
44 45 46 47 48 49

<a name="2.1"></a>

### 2.1 ImageNet1k models

Using the `ResNet50` model provided by PaddleClas, the following image(`'docs/images/inference_deployment/whl_demo.jpg'`) as an example.
C
chenziheng 已提交
50

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

T
Tingquan Gao 已提交
53
* Python
C
chenziheng 已提交
54 55
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
56
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
57
infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
58 59
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
60 61
```

T
Tingquan Gao 已提交
62 63
**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 已提交
64
```
T
Tingquan Gao 已提交
65 66
>>> 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 已提交
67 68
```

T
Tingquan Gao 已提交
69
* CLI
C
chenziheng 已提交
70
```bash
T
Tingquan Gao 已提交
71
paddleclas --model_name=ResNet50  --infer_imgs="docs/images/inference_deployment/whl_demo.jpg"
T
Tingquan Gao 已提交
72 73 74 75
```

```
>>> result
T
Tingquan Gao 已提交
76
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 已提交
77 78 79
Predict complete!
```

G
gaotingquan 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<a name="2.2"></a>

### 2.2 PULC models

PULC integrates various state-of-the-art algorithms such as backbone network, data augmentation and distillation, etc., and finally can automatically obtain a lightweight and high-precision image classification model.

PaddleClas provides a series of test cases, which contain demos of different scenes about people, cars, OCR, etc. Click [here](https://paddleclas.bj.bcebos.com/data/PULC/pulc_demo_imgs.zip) to download the data.

Prection using the PULC "Human Exists Classification" model provided by PaddleClas:

* Python

```python
import paddleclas
model = paddleclas.PaddleClas(model_name="person_exists")
result = model.predict(input_data="pulc_demo_imgs/person_exists/objects365_01780782.jpg")
print(next(result))
```

```
>>> result
[{'class_ids': [0], 'scores': [0.9955421453341842], 'label_names': ['nobody'], 'filename': 'pulc_demo_imgs/person_exists/objects365_01780782.jpg'}]
```

`Nobody` means there is no one in the image, `someone` means there is someone in the image. Therefore, the prediction result indicates that there is no one in the figure.

**Note**: `model.predict()` is a generator, so `next()` or `for` is needed to call it. This would to predict by batch that length is `batch_size`, default by 1. You can specify the argument `batch_size` and `model_name` when instantiating PaddleClas object, for example: `model = paddleclas.PaddleClas(model_name="person_exists",  batch_size=2)`. Please refer to [Supported Model List](#PULC_Models) for the supported model list.

* CLI

```bash
paddleclas --model_name=person_exists --infer_imgs=pulc_demo_imgs/person_exists/objects365_01780782.jpg
```

```
>>> result
class_ids: [0], scores: [0.9955421453341842], label_names: ['nobody'], filename: pulc_demo_imgs/person_exists/objects365_01780782.jpg
Predict complete!
```

**Note**: The "--infer_imgs" argument specify the image(s) to be predict, and you can also specify a directoy contains images. If use other model, you can specify the `--model_name` argument. Please refer to [Supported Model List](#PULC_Models) for the supported model list.

<a name="PULC_Models"></a>

**Supported Model List**

The name of PULC series models are as follows:

| Name | Intro |
| --- | --- |
| person_exists | Human Exists Classification |
| person_attribute | Pedestrian Attribute Classification |
| safety_helmet | Classification of Wheather Wearing Safety Helmet |
| traffic_sign | Traffic Sign Classification |
| vehicle_attribute | Vehicle Attribute Classification |
| car_exists | Car Exists Classification |
| text_image_orientation | Text Image Orientation Classification |
| textline_orientation | Text-line Orientation Classification |
| language_classification | Language Classification |

Please refer to [Human Exists Classification](../PULC/PULC_person_exists_en.md)[Pedestrian Attribute Classification](../PULC/PULC_person_attribute_en.md)[Classification of Wheather Wearing Safety Helmet](../PULC/PULC_safety_helmet_en.md)[Traffic Sign Classification](../PULC/PULC_traffic_sign_en.md)[Vehicle Attribute Classification](../PULC/PULC_vehicle_attribute_en.md)[Car Exists Classification](../PULC/PULC_car_exists_en.md)[Text Image Orientation Classification](../PULC/PULC_text_image_orientation_en.md)[Text-line Orientation Classification](../PULC/PULC_textline_orientation_en.md)[Language Classification](../PULC/PULC_language_classification_en.md) for more information about different scenarios.

G
gaotingquan 已提交
142
<a name="3"></a>
G
gaotingquan 已提交
143

T
Tingquan Gao 已提交
144 145 146 147 148 149
## 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.
G
gaotingquan 已提交
150 151 152 153 154 155 156 157 158 159 160 161
* use_gpu(bool): Whether to use GPU or not.
* gpu_mem(int): GPU memory usages.
* use_tensorrt(bool): Whether to open TensorRT or not. Using it can greatly promote predict preformance.
* enable_mkldnn(bool): Whether enable MKLDNN or not.
* cpu_num_threads(int): Assign number of cpu threads, valid when `--use_gpu` is `False` and `--enable_mkldnn` is `True`.
* batch_size(int): Batch size.
* resize_short(int): Resize the minima between height and width into `resize_short`.
* crop_size(int): Center crop image to `crop_size`.
* topk(int): Print (return) the `topk` prediction results when Topk postprocess is used.
* threshold(float): The threshold of ThreshOutput when postprocess is used.
* class_id_map_file(str): The mapping file between class ID and label.
* save_dir(str): The directory to save the prediction results that can be used as pre-label.
C
chenziheng 已提交
162

T
Tingquan Gao 已提交
163
**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 已提交
164

T
Tingquan Gao 已提交
165 166 167
* CLI:
```bash
from paddleclas import PaddleClas, get_default_confg
T
Tingquan Gao 已提交
168
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 已提交
169
```
T
Tingquan Gao 已提交
170 171

* Python:
T
Tingquan Gao 已提交
172 173 174 175 176
```python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ViT_base_patch16_384', resize_short=384, crop_size=384)
```

G
gaotingquan 已提交
177
<a name="4"></a>
G
gaotingquan 已提交
178

T
Tingquan Gao 已提交
179
## 4. Usage
T
Tingquan Gao 已提交
180

T
Tingquan Gao 已提交
181 182 183
PaddleClas provides two ways to use:
1. Python interative programming;
2. Bash command line programming.
C
chenziheng 已提交
184

G
gaotingquan 已提交
185
<a name="4.1"></a>
G
gaotingquan 已提交
186

T
Tingquan Gao 已提交
187
### 4.1 View help information
C
chenziheng 已提交
188

T
Tingquan Gao 已提交
189
* CLI
C
chenziheng 已提交
190 191 192 193
```bash
paddleclas -h
```

G
gaotingquan 已提交
194
<a name="4.2"></a>
G
gaotingquan 已提交
195

T
Tingquan Gao 已提交
196 197
### 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 已提交
198

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

T
Tingquan Gao 已提交
208
* CLI
C
chenziheng 已提交
209
```bash
T
Tingquan Gao 已提交
210
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
211 212
```

G
gaotingquan 已提交
213
<a name="4.3"></a>
G
gaotingquan 已提交
214

T
Tingquan Gao 已提交
215 216 217 218
### 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 已提交
219 220
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
221
clas = PaddleClas(inference_model_dir='./inference/')
T
Tingquan Gao 已提交
222
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
223 224
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
225 226
```

T
Tingquan Gao 已提交
227
* CLI
C
chenziheng 已提交
228
```bash
T
Tingquan Gao 已提交
229
paddleclas --inference_model_dir='./inference/' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
230 231
```

G
gaotingquan 已提交
232
<a name="4.4"></a>
G
gaotingquan 已提交
233

T
Tingquan Gao 已提交
234 235
### 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 已提交
236

T
Tingquan Gao 已提交
237
* Python
C
chenziheng 已提交
238 239
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
240 241 242 243 244
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 已提交
245 246
```

T
Tingquan Gao 已提交
247 248 249 250
* CLI
```bash
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --batch_size 2
```
C
chenziheng 已提交
251

G
gaotingquan 已提交
252
<a name="4.5"></a>
G
gaotingquan 已提交
253

T
Tingquan Gao 已提交
254 255 256 257
### 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 已提交
258 259
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
260
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
261
infer_imgs = 'https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
262 263
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
264 265
```

T
Tingquan Gao 已提交
266
* CLI
C
chenziheng 已提交
267
```bash
T
Tingquan Gao 已提交
268
paddleclas --model_name='ResNet50' --infer_imgs='https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'
C
chenziheng 已提交
269 270
```

G
gaotingquan 已提交
271
<a name="4.6"></a>
G
gaotingquan 已提交
272

T
Tingquan Gao 已提交
273
### 4.6 Prediction of NumPy.array format image
T
Tingquan Gao 已提交
274
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 已提交
275 276

* python
C
chenziheng 已提交
277
```python
T
Tingquan Gao 已提交
278
import cv2
C
chenziheng 已提交
279
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
280
clas = PaddleClas(model_name='ResNet50')
T
Tingquan Gao 已提交
281
infer_imgs = cv2.imread("docs/en/inference_deployment/whl_deploy_en.md")[:, :, ::-1]
T
Tingquan Gao 已提交
282 283
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
284 285
```

G
gaotingquan 已提交
286
<a name="4.7"></a>
G
gaotingquan 已提交
287

T
Tingquan Gao 已提交
288 289 290 291 292 293 294
### 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 已提交
295
infer_imgs = 'docs/images/' # it can be infer_imgs folder path which contains all of images you want to predict.
T
Tingquan Gao 已提交
296 297 298 299 300
result=clas.predict(infer_imgs)
print(next(result))
```

* CLI
C
chenziheng 已提交
301
```bash
G
gaotingquan 已提交
302
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --save_dir='./output_pre_label/'
C
chenziheng 已提交
303 304
```

G
gaotingquan 已提交
305
<a name="4.8"></a>
G
gaotingquan 已提交
306

T
Tingquan Gao 已提交
307 308 309 310
### 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 已提交
311 312

```
T
Tingquan Gao 已提交
313
class_id<space>class_name<\n>
C
chenziheng 已提交
314 315
```

T
Tingquan Gao 已提交
316
For example:
C
chenziheng 已提交
317 318

```
T
Tingquan Gao 已提交
319 320 321 322
0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
C
chenziheng 已提交
323 324
```

T
Tingquan Gao 已提交
325
* Python
C
chenziheng 已提交
326 327
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
328
clas = PaddleClas(model_name='ResNet50', class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt')
T
Tingquan Gao 已提交
329
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
T
Tingquan Gao 已提交
330 331
result=clas.predict(infer_imgs)
print(next(result))
C
chenziheng 已提交
332 333
```

T
Tingquan Gao 已提交
334
* CLI
C
chenziheng 已提交
335
```bash
T
Tingquan Gao 已提交
336
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 已提交
337
```