whl_en.md 8.0 KB
Newer Older
C
chenziheng 已提交
1 2 3 4 5 6 7 8
# paddleclas package

## Get started quickly

### install package

install by pypi
```bash
L
littletomatodonkey 已提交
9
pip install paddleclas==2.0.3
C
chenziheng 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23
```

build own whl package and install
```bash
python3 setup.py bdist_wheel
pip3 install dist/paddleclas-x.x.x-py3-none-any.whl
```

### 1. Quick Start

* Assign `image_file='docs/images/whl/demo.jpg'`, Use inference model that Paddle provides `model_name='ResNet50'`

**Here is demo.jpg**

L
littletomatodonkey 已提交
24 25 26
<div align="center">
<img src="../images/whl/demo.jpg"  width = "400" />
</div>
C
chenziheng 已提交
27 28 29

```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
30
clas = PaddleClas(model_name='ResNet50', top_k=5)
C
chenziheng 已提交
31 32 33 34 35 36 37
image_file='docs/images/whl/demo.jpg'
result=clas.predict(image_file)
print(result)
```

```
    >>> result
T
Tingquan Gao 已提交
38 39
    [{'class_ids': array([ 8,  7, 86, 82, 80]), 'scores': array([9.7967714e-01, 2.0280687e-02, 2.7053760e-05, 6.1860351e-06,
       2.6378802e-06], dtype=float32), 'label_names': ['hen', 'cock', 'partridge', 'ruffed grouse, partridge, Bonasa umbellus', 'black grouse'], 'filename': 'docs/images/whl/demo.jpg'}
C
chenziheng 已提交
40 41 42 43
```

* Using command line interactive programming
```bash
T
Tingquan Gao 已提交
44
paddleclas --model_name=ResNet50 --top_k=5 --image_file='docs/images/whl/demo.jpg'
C
chenziheng 已提交
45 46 47 48
```

```
    >>> result
T
Tingquan Gao 已提交
49 50 51
    **********docs/images/whl/demo.jpg**********
    filename: docs/images/whl/demo.jpg; class id: 8, 7, 86, 82, 80; scores: 0.9797, 0.0203, 0.0000, 0.0000, 0.0000; label: hen, cock, partridge, ruffed grouse, partridge, Bonasa umbellus, black grouse
    Predict complete!
C
chenziheng 已提交
52 53 54 55
```

### 2. Definition of Parameters
* model_name(str): model's name. If not assigning `model_file`and`params_file`, you can assign this param. If using inference model based on ImageNet1k provided by Paddle, set as default='ResNet50'.
T
Tingquan Gao 已提交
56
* image_file(str or numpy.ndarray): image's path. Support assigning single local image, internet image and folder containing series of images. Also Support numpy.ndarray, the channel order is [B, G, R].
C
chenziheng 已提交
57 58
* use_gpu(bool): Whether to use GPU or not, defalut=False。
* use_tensorrt(bool): whether to open tensorrt or not. Using it can greatly promote predict preformance, default=False.
T
Tingquan Gao 已提交
59
* is_preprocessed(bool): Assign the image data has been preprocessed or not when the image_file is numpy.ndarray.
C
chenziheng 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
* resize_short(int): resize the minima between height and width into resize_short(int), default=256
* resize(int): resize image into resize(int), default=224.
* normalize(bool): whether normalize image or not, default=True.
* batch_size(int): batch number, default=1.
* model_file(str): path of inference.pdmodel. If not assign this param,you need assign `model_name` for downloading.
* params_file(str): path of inference.pdiparams. If not assign this param,you need assign `model_name` for downloading.
* ir_optim(bool): whether enable IR optimization or not, default=True.
* gpu_mem(int): GPU memory usages,default=8000。
* enable_profile(bool): whether enable profile or not,default=False.
* top_k(int): Assign top_k, default=1.
* enable_mkldnn(bool): whether enable MKLDNN or not, default=False.
* cpu_num_threads(int): Assign number of cpu threads, default=10.
* label_name_path(str): Assign path of label_name_dict you use. If using your own training model, you can assign this param. If using inference model based on ImageNet1k provided by Paddle, you may not assign this param.Defaults take ImageNet1k's label name.
* pre_label_image(bool): whether prelabel or not, default=False.
* pre_label_out_idr(str): If prelabeling, the path of output.

L
littletomatodonkey 已提交
76
**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` when building a `PaddleClas` object. The following is a demo.
L
littletomatodonkey 已提交
77 78 79 80

```python
clas = PaddleClas(model_name='ViT_base_patch16_384', top_k=5, resize_short=384, resize=384)
```
T
Tingquan Gao 已提交
81

C
chenziheng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
### 3. Different Usages of Codes

**We provide two ways to use: 1. Python interative programming 2. Bash command line programming**

* check `help` information
```bash
paddleclas -h
```

* Use user-specified model, you need to assign model's path `model_file` and parameters's path`params_file`

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
96 97 98
clas = PaddleClas(model_file='the path of model file',
    params_file='the path of params file')
image_file = 'docs/images/whl/demo.jpg'
C
chenziheng 已提交
99 100 101 102 103 104
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
105
paddleclas --model_file='user-specified model path' --params_file='parmas path' --image_file='docs/images/whl/demo.jpg'
C
chenziheng 已提交
106 107
```

T
Tingquan Gao 已提交
108
* Use inference model which PaddlePaddle provides to predict, you need to choose one of model proviede by PaddleClas to assign `model_name`. So there's no need to assign `model_file`. And the model you chosen will be download in `~/.paddleclas/`, which will be saved in folder named by `model_name`.
C
chenziheng 已提交
109 110 111 112

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
113 114
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/demo.jpg'
C
chenziheng 已提交
115 116 117 118 119 120
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
121
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/demo.jpg'
C
chenziheng 已提交
122 123
```

T
Tingquan Gao 已提交
124
* You can assign input as format `numpy.ndarray` which has been preprocessed `image_file=np.ndarray`. Note that the image data must be three channel. If need To preprocess the image, the image channels order must be [B, G, R].
C
chenziheng 已提交
125 126 127

###### python
```python
T
Tingquan Gao 已提交
128
import cv2
C
chenziheng 已提交
129
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
130 131
clas = PaddleClas(model_name='ResNet50')
image_file = cv2.imread("docs/images/whl/demo.jpg")
C
chenziheng 已提交
132 133 134
result=clas.predict(image_file)
```

T
Tingquan Gao 已提交
135
* You can assign `image_file` as a folder path containing series of images.
C
chenziheng 已提交
136 137 138 139

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
140 141
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/' # it can be image_file folder path which contains all of images you want to predict.
C
chenziheng 已提交
142 143 144 145 146 147
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
148
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/'
C
chenziheng 已提交
149 150
```

T
Tingquan Gao 已提交
151
* You can assign `--pre_label_image=True`, `--pre_label_out_idr= './output_pre_label/'`. Then images will be copied into folder named by top-1 class_id.
C
chenziheng 已提交
152 153 154 155

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
156 157
clas = PaddleClas(model_name='ResNet50', pre_label_image=True, pre_label_out_idr='./output_pre_label/')
image_file = 'docs/images/whl/' # it can be image_file folder path which contains all of images you want to predict.
C
chenziheng 已提交
158 159 160 161 162 163
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
164
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/' --pre_label_image=True --pre_label_out_idr='./output_pre_label/'
C
chenziheng 已提交
165 166 167 168 169 170 171 172 173 174 175
```

* You can assign `--label_name_path` as your own label_dict_file, format should be as(class_id<space>class_name<\n>).

```
0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
```

T
Tingquan Gao 已提交
176
* If you use inference model that PaddleClas provides, you do not need assign `label_name_path`. Program will take `ppcls/utils/imagenet1k_label_list.txt` as defaults. If you hope using your own training model, you can provide `label_name_path` outputing 'label_name' and scores, otherwise no 'label_name' in output information.
C
chenziheng 已提交
177 178 179 180

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
181 182
clas = PaddleClas(model_file= 'the path of model file', params_file = 'the path of params file', label_name_path='./ppcls/utils/imagenet1k_label_list.txt')
image_file = 'docs/images/whl/demo.jpg' # it can be image_file folder path which contains all of images you want to predict.
C
chenziheng 已提交
183 184 185 186 187 188
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
189
paddleclas --model_file='the path of model file' --params_file='the path of params file' --image_file='docs/images/whl/demo.jpg' --label_name_path='./ppcls/utils/imagenet1k_label_list.txt'
C
chenziheng 已提交
190 191 192 193 194
```

###### python
```python
from paddleclas import PaddleClas
T
Tingquan Gao 已提交
195 196
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/' # it can be directory which contains all of images you want to predict.
C
chenziheng 已提交
197 198 199 200 201 202
result=clas.predict(image_file)
print(result)
```

###### bash
```bash
T
Tingquan Gao 已提交
203
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/'
C
chenziheng 已提交
204
```