2022-05-24 17:14:58 +08:00
|
|
|
|
# Pipeline使用教程
|
2022-07-01 17:35:56 +08:00
|
|
|
|
本文简单介绍如何使用`pipeline`函数加载模型进行推理。`pipeline`函数支持按照任务类型、模型名称从模型仓库拉取模型进行进行推理,包含以下几个方面:
|
2022-05-24 17:14:58 +08:00
|
|
|
|
* 使用pipeline()函数进行推理
|
|
|
|
|
|
* 指定特定预处理、特定模型进行推理
|
|
|
|
|
|
* 不同场景推理任务示例
|
2022-05-30 11:53:53 +08:00
|
|
|
|
## 环境准备
|
|
|
|
|
|
详细步骤可以参考 [快速开始](../quick_start.md)
|
2022-05-24 17:14:58 +08:00
|
|
|
|
## Pipeline基本用法
|
2022-07-01 17:35:56 +08:00
|
|
|
|
下面以中文分词任务为例,说明pipeline函数的基本用法
|
2022-05-24 17:14:58 +08:00
|
|
|
|
|
2022-07-01 17:35:56 +08:00
|
|
|
|
1. pipeline函数支持指定特定任务名称,加载任务默认模型,创建对应pipeline对象
|
2022-05-30 11:53:53 +08:00
|
|
|
|
执行如下python代码
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```python
|
2022-07-01 17:35:56 +08:00
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
|
word_segmentation = pipeline('word-segmentation')
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2022-07-01 17:35:56 +08:00
|
|
|
|
2. 输入文本
|
2022-05-24 17:14:58 +08:00
|
|
|
|
``` python
|
2022-07-01 17:35:56 +08:00
|
|
|
|
input = '今天天气不错,适合出去游玩'
|
|
|
|
|
|
print(word_segmentation(input))
|
|
|
|
|
|
{'output': '今天 天气 不错 , 适合 出去 游玩'}
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2022-07-01 17:35:56 +08:00
|
|
|
|
3. 输入多条样本
|
|
|
|
|
|
|
|
|
|
|
|
pipeline对象也支持传入多个样本列表输入,返回对应输出列表,每个元素对应输入样本的返回结果
|
2022-05-24 17:14:58 +08:00
|
|
|
|
|
|
|
|
|
|
```python
|
2022-07-01 17:35:56 +08:00
|
|
|
|
inputs = ['今天天气不错,适合出去游玩','这本书很好,建议你看看']
|
|
|
|
|
|
print(word_segmentation(inputs))
|
|
|
|
|
|
[{'output': '今天 天气 不错 , 适合 出去 游玩'}, {'output': '这 本 书 很 好 , 建议 你 看看'}]
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
## 指定预处理、模型进行推理
|
|
|
|
|
|
pipeline函数支持传入实例化的预处理对象、模型对象,从而支持用户在推理过程中定制化预处理、模型。
|
|
|
|
|
|
|
2022-07-01 17:35:56 +08:00
|
|
|
|
1. 首先,创建预处理方法和模型
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```python
|
2022-07-01 17:35:56 +08:00
|
|
|
|
from modelscope.models import Model
|
2022-07-05 20:40:48 +08:00
|
|
|
|
from modelscope.preprocessors import TokenClassificationPreprocessor
|
2022-07-01 17:35:56 +08:00
|
|
|
|
model = Model.from_pretrained('damo/nlp_structbert_word-segmentation_chinese-base')
|
2022-07-05 20:40:48 +08:00
|
|
|
|
tokenizer = TokenClassificationPreprocessor(model.model_dir)
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2022-07-01 17:35:56 +08:00
|
|
|
|
2. 使用tokenizer和模型对象创建pipeline
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```python
|
2022-07-01 17:35:56 +08:00
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
|
word_seg = pipeline('word-segmentation', model=model, preprocessor=tokenizer)
|
|
|
|
|
|
input = '今天天气不错,适合出去游玩'
|
|
|
|
|
|
print(word_seg(input))
|
|
|
|
|
|
{'output': '今天 天气 不错 , 适合 出去 游玩'}
|
2022-05-24 17:14:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
## 不同场景任务推理示例
|
2022-07-01 17:35:56 +08:00
|
|
|
|
下面以一个图像任务:人像抠图('image-matting')为例,进一步说明pipeline的用法
|
|
|
|
|
|
```python
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
|
img_matting = pipeline('image-matting')
|
2022-07-04 12:11:12 +08:00
|
|
|
|
result = img_matting('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_matting.png')
|
2022-07-01 17:35:56 +08:00
|
|
|
|
cv2.imwrite('result.png', result['output_png'])
|
|
|
|
|
|
```
|