whl_deploy_en.md 12.6 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

G
gaotingquan 已提交
28
* **[Recommended]** Installing from PyPI:
C
chenziheng 已提交
29 30

```bash
G
gaotingquan 已提交
31
pip3 install paddleclas
C
chenziheng 已提交
32 33
```

G
gaotingquan 已提交
34
* Please build and install locally if you need to use the develop branch of PaddleClas to experience the latest functions, or need to redevelop based on PaddleClas. The command is as follows:
T
Tingquan Gao 已提交
35

C
chenziheng 已提交
36
```bash
G
gaotingquan 已提交
37
python3 setup.py install
C
chenziheng 已提交
38 39
```

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

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

<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 已提交
49

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

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

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

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

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

G
gaotingquan 已提交
79 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
<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 已提交
141
<a name="3"></a>
G
gaotingquan 已提交
142

T
Tingquan Gao 已提交
143 144 145 146 147 148
## 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 已提交
149 150 151 152 153 154 155 156 157 158 159 160
* 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 已提交
161

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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