python_use_hub_en.md 12.9 KB
Newer Older
D
Daniel Yang 已提交
1
# Using PaddleHub through Python Code Execution
W
wuzewu 已提交
2

D
Daniel Yang 已提交
3
The codes/commands on this page can run online on [AIStudio](https://aistudio.baidu.com/aistudio/projectdetail/635335). It is similar to the notebook environment, which can be accessed through a browser, without environment preparation. This is a quick and easy experiences for developers.
W
wuzewu 已提交
4

D
Daniel Yang 已提交
5
## PaddleHub example for computer vision tasks
W
wuzewu 已提交
6

D
Daniel Yang 已提交
7
Taking the computer vision task as an example, we choose a test image test.jpg to implement the following four functions:
W
wuzewu 已提交
8

D
Daniel Yang 已提交
9
* Portrait Cutout ([deeplabv3p\_xception65\_humanseg](https://www.paddlepaddle.org.cn/hubdetail?name=deeplabv3p_xception65_humanseg&en_category=ImageSegmentation))
W
wuzewu 已提交
10

D
Daniel Yang 已提交
11
* Body Part Segmentation ([ace2p](https://www.paddlepaddle.org.cn/hubdetail?name=ace2p&en_category=ImageSegmentation))
W
wuzewu 已提交
12

D
Daniel Yang 已提交
13
* Face Detection ([ultra\_light\_fast\_generic\_face\_detector\_1mb\_640](https://www.paddlepaddle.org.cn/hubdetail?name=ultra_light_fast_generic_face_detector_1mb_640&en_category=FaceDetection))
W
wuzewu 已提交
14

D
Daniel Yang 已提交
15 16 17 18 19
* Key Point Detection ([human\_pose\_estimation\_resnet50\_mpii](https://www.paddlepaddle.org.cn/hubdetail?name=human_pose_estimation_resnet50_mpii&en_category=KeyPointDetection))

> Note: If you need to find out which pre-training models can be executed from PaddleHub, get the model name (for example, deeplabv3p\_xception65\_humanseg, by which the model will be executed in subsequent codes). For details, see official website document. In the documents, it is easy to search because models are classified according to model categories, with providing a detailed description of the models.

### Before experience, install the PaddleHub.
W
wuzewu 已提交
20 21

```shell
D
Daniel Yang 已提交
22
# install the latest version
W
wuzewu 已提交
23 24 25
$ pip install paddlehub --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```

D
Daniel Yang 已提交
26
### Original Image Display
W
wuzewu 已提交
27 28

```shell
D
Daniel Yang 已提交
29
# Download a picture
W
wuzewu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
$ wget https://paddlehub.bj.bcebos.com/resources/test_image.jpg
```

    --2020-07-22 12:22:19--  https://paddlehub.bj.bcebos.com/resources/test_image.jpg
    Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
    Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 967120 (944K) [image/jpeg]
    Saving to: ‘test_image.jpg.1’

    test_image.jpg.1    100%[===================>] 944.45K  5.51MB/s    in 0.2s  

    2020-07-22 12:22:19 (5.51 MB/s) - ‘test_image.jpg.1’ saved [967120/967120]

D
Daniel Yang 已提交
44
![png](../../imgs/humanseg_test.png)
W
wuzewu 已提交
45

D
Daniel Yang 已提交
46
### Portrait Cutout
W
wuzewu 已提交
47

D
Daniel Yang 已提交
48
PaddleHub adopts the model-based software design concept. All pre-training models are similar to Python packages, with the concept of version. You can install, upgrade, and remove the model conveniently by running the commands `hub install` and `hub uninstall`:
W
wuzewu 已提交
49

D
Daniel Yang 已提交
50
> By default, you can download the model of the latest version by running the following command. If you want to specify the version, you can add the version number after the `==1.1.1` command.
W
wuzewu 已提交
51 52

```shell
D
Daniel Yang 已提交
53
#intall pre-trained models
W
wuzewu 已提交
54 55 56 57 58 59 60 61 62 63
$ hub install deeplabv3p_xception65_humanseg
```

    Downloading deeplabv3p_xception65_humanseg
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpo32jeve0/deeplabv3p_xception65_humanseg
    [==================================================] 100.00%
    Successfully installed deeplabv3p_xception65_humanseg-1.1.1

```python
D
Daniel Yang 已提交
64
# import paddlehub
W
wuzewu 已提交
65 66 67 68 69 70 71 72
import paddlehub as hub
module = hub.Module(name="deeplabv3p_xception65_humanseg")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='humanseg_output')
```

    [32m[2020-07-22 12:22:49,474] [    INFO] - Installing deeplabv3p_xception65_humanseg module [0m


D
Daniel Yang 已提交
73

W
wuzewu 已提交
74 75 76 77 78 79
    Downloading deeplabv3p_xception65_humanseg
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpzrrl1duq/deeplabv3p_xception65_humanseg
    [==================================================] 100.00%


D
Daniel Yang 已提交
80

W
wuzewu 已提交
81 82
    [32m[2020-07-22 12:23:11,811] [    INFO] - Successfully installed deeplabv3p_xception65_humanseg-1.1.1 [0m

D
Daniel Yang 已提交
83
![png](../../imgs/output_8_3.png)
W
wuzewu 已提交
84

D
Daniel Yang 已提交
85
As you can see, the execution of PaddleHub with Python codes requires only three lines of codes:
W
wuzewu 已提交
86 87

```
D
Daniel Yang 已提交
88 89 90
import paddlehub as hub  
module = hub.Module(name="deeplabv3p_xception65_humanseg")  
res = module.segmentation(paths = ["./test.jpg"], visualization=True, output_dir='humanseg_output')  
W
wuzewu 已提交
91 92
```

D
Daniel Yang 已提交
93 94 95
* Model names are specified through the `hub.Module` API.
* `module.segmentation` is used to execute the image segmentation prediction tasks. Different prediction APIs are designed for different types of tasks. For example, the face detection task uses the `face_detection` function. It is recommended to view the corresponding model introduction document before the pre-training model is executed.
* The prediction results are saved in `output_dir='humanseg_output'` directory, and you can view the output images in this directory.
W
wuzewu 已提交
96

D
Daniel Yang 已提交
97
For the implementation of other tasks, refer to this pattern. Let's see how the next few tasks are implemented.
W
wuzewu 已提交
98

D
Daniel Yang 已提交
99
### Body Part Segmentation
W
wuzewu 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

```shell
$ hub install ace2p
```

    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    Downloading ace2p
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpfsovt3f8/ace2p
    [==================================================] 100.00%
    Successfully installed ace2p-1.1.0

```python
import paddlehub as hub
module = hub.Module(name="ace2p")
res = module.segmentation(paths = ["./test_image.jpg"], visualization=True, output_dir='ace2p_output')
```

    [32m[2020-07-22 12:23:58,027] [    INFO] - Installing ace2p module [0m


D
Daniel Yang 已提交
122

W
wuzewu 已提交
123 124 125 126 127 128
    Downloading ace2p
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmptrogpj6j/ace2p
    [==================================================] 100.00%


D
Daniel Yang 已提交
129

W
wuzewu 已提交
130 131
    [32m[2020-07-22 12:24:22,575] [    INFO] - Successfully installed ace2p-1.1.0 [0m

D
Daniel Yang 已提交
132
![png](../../imgs/output_12_3.png)
W
wuzewu 已提交
133

D
Daniel Yang 已提交
134
### Face Detection
W
wuzewu 已提交
135 136

```shell
D
Daniel Yang 已提交
137
#install pre-trained model
W
wuzewu 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
$ hub install ultra_light_fast_generic_face_detector_1mb_640
```

    Downloading ultra_light_fast_generic_face_detector_1mb_640
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpz82xnmy6/ultra_light_fast_generic_face_detector_1mb_640
    [==================================================] 100.00%
    Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2

```python
import paddlehub as hub
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
res = module.face_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='face_detection_output')
```

    [32m[2020-07-22 12:25:12,948] [    INFO] - Installing ultra_light_fast_generic_face_detector_1mb_640 module [0m


D
Daniel Yang 已提交
156

W
wuzewu 已提交
157 158 159 160 161 162
    Downloading ultra_light_fast_generic_face_detector_1mb_640
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpw44mo56p/ultra_light_fast_generic_face_detector_1mb_640
    [==================================================] 100.00%


D
Daniel Yang 已提交
163

W
wuzewu 已提交
164 165
    [32m[2020-07-22 12:25:14,698] [    INFO] - Successfully installed ultra_light_fast_generic_face_detector_1mb_640-1.1.2

D
Daniel Yang 已提交
166
![png](../../imgs/output_15_3.png)
W
wuzewu 已提交
167

D
Daniel Yang 已提交
168
### Key Point Detection
W
wuzewu 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

```shell
$ hub install human_pose_estimation_resnet50_mpii
```

    /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import imp
    Downloading human_pose_estimation_resnet50_mpii
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmpn_ppwkzq/human_pose_estimation_resnet50_mpii
    [========                                          ] 17.99%

```python
import paddlehub as hub
module = hub.Module(name="human_pose_estimation_resnet50_mpii")
res = module.keypoint_detection(paths = ["./test_image.jpg"], visualization=True, output_dir='keypoint_output')
```

    [32m[2020-07-23 11:27:33,989] [    INFO] - Installing human_pose_estimation_resnet50_mpii module [0m
    [32m[2020-07-23 11:27:33,992] [    INFO] - Module human_pose_estimation_resnet50_mpii already installed in /home/aistudio/.paddlehub/modules/human_pose_estimation_resnet50_mpii [0m


D
Daniel Yang 已提交
191

W
wuzewu 已提交
192 193
    image saved in keypoint_output/test_imagetime=1595474855.jpg

D
Daniel Yang 已提交
194
![png](../../imgs/output_18_2.png)
W
wuzewu 已提交
195

D
Daniel Yang 已提交
196
## PaddleHub Example for Natural Language Processing Tasks
W
wuzewu 已提交
197

D
Daniel Yang 已提交
198
Let's look at two more examples of natural language processing tasks: Chinese word segmentation and sentiment classification tasks.
W
wuzewu 已提交
199

D
Daniel Yang 已提交
200 201
* Chinese word segmentation ([lac](https://www.paddlepaddle.org.cn/hubdetail?name=lac&en_category=LexicalAnalysis))
* Sentiment analysis ([senta\_bilstm](https://www.paddlepaddle.org.cn/hubdetail?name=senta_bilstm&en_category=SentimentAnalysis))
W
wuzewu 已提交
202

D
Daniel Yang 已提交
203
### Chinese word segmentation
W
wuzewu 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

```shell
$ hub install lac
```

    2020-07-22 10:03:09,866-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
    2020-07-22 10:03:10,208-INFO: generated new fontManager
    Downloading lac
    [==================================================] 100.00%
    Uncompress /home/aistudio/.paddlehub/tmp/tmp8ukaz690/lac
    [==================================================] 100.00%
    Successfully installed lac-2.1.1

```python
import paddlehub as hub
lac = hub.Module(name="lac")
test_text = ["1996年,曾经是微软员工的加布·纽维尔和麦克·哈灵顿一同创建了Valve软件公司。他们在1996年下半年从id software取得了雷神之锤引擎的使用许可,用来开发半条命系列。"]
res = lac.lexical_analysis(texts = test_text)
D
Daniel Yang 已提交
222
print("The resuls are: ", res)
W
wuzewu 已提交
223 224 225 226 227 228
```

    [32m[2020-07-22 10:03:18,439] [    INFO] - Installing lac module[0m
    [32m[2020-07-22 10:03:18,531] [    INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m


D
Daniel Yang 已提交
229 230

    The resuls are: [{'word': ['1996年', ',', '曾经', '是', '微软', '员工', '的', '加布·纽维尔', '和', '麦克·哈灵顿', '一同', '创建', '了', 'Valve软件公司', '。', '他们', '在', '1996年下半年', '从', 'id', ' ', 'software', '取得', '了', '雷神之锤', '引擎', '的', '使用', '许可', ',', '用来', '开发', '半条命', '系列', '。'], 'tag': ['TIME', 'w', 'd', 'v', 'ORG', 'n', 'u', 'PER', 'c', 'PER', 'd', 'v', 'u', 'ORG', 'w', 'r', 'p', 'TIME', 'p', 'nz', 'w', 'n', 'v', 'u', 'n', 'n', 'u', 'vn', 'vn', 'w', 'v', 'v', 'n', 'n', 'w']}]
W
wuzewu 已提交
231

D
Daniel Yang 已提交
232
As you can see, compared to computer vision tasks, there are differences between the input and output interfaces (where you need to enter text, executed as the function parameters), because it depends on the task types. For details, see the API description for the corresponding pre-training model.
W
wuzewu 已提交
233

D
Daniel Yang 已提交
234
### Sentiment classification
W
wuzewu 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

```shell
$ hub install senta_bilstm
```

    Module senta_bilstm-1.1.0 already installed in /home/aistudio/.paddlehub/modules/senta_bilstm

```python
import paddlehub as hub
senta = hub.Module(name="senta_bilstm")
test_text = ["味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。"]
res = senta.sentiment_classify(texts = test_text)

print("情感分析结果:", res)
```

    [32m[2020-07-22 10:34:06,922] [    INFO] - Installing senta_bilstm module [0m
    [32m[2020-07-22 10:34:06,984] [    INFO] - Module senta_bilstm already installed in /home/aistudio/.paddlehub/modules/senta_bilstm
    [32m[2020-07-22 10:34:08,937] [    INFO] - Installing lac module[0m
    [32m[2020-07-22 10:34:08,939] [    INFO] - Module lac already installed in /home/aistudio/.paddlehub/modules/lac [0m


D
Daniel Yang 已提交
257

W
wuzewu 已提交
258 259
    情感分析结果: [{'text': '味道不错,确实不算太辣,适合不能吃辣的人。就在长江边上,抬头就能看到长江的风景。鸭肠、黄鳝都比较新鲜。', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9771, 'negative_probs': 0.0229}]

D
Daniel Yang 已提交
260
## Summary
W
wuzewu 已提交
261

D
Daniel Yang 已提交
262
PaddleHub provides a rich set of pre-training models, including image classification, semantic model, video classification, image generation, image segmentation, text review, key point detection and other mainstream models. These can be executed quickly and easily with only 3 lines of Python codes, with the instant output of prediction results. You can try it out by selecting some models from the Pre-training Model List.