NLP-transformer学习:(2)transformer的 pipeline

NLP-transformer学习:(2)transformer的 pipeline

在这里插入图片描述

基于 NLP-transformer学习:(1),这里对transformer 更近一步,学习尝试使用pipeline

学习内容:

  1. pipeline 知识基础:
  2. pipeline 实战:
  3. gpu 上跑模型:

1 pipeline 基础基础知识:

pipeline:字面意思就是流水线:包括数据预处理+模型调用+结果后处理。pipeline() 提供了在任何语言、计算机视觉、音频和多模态任务上使用 Hub 中的任何模型进行推理的简单方法, 如图

text: line2 this is flowchart
tokenizer
input POS: [101,2018,2107,2000,557]
Model
Logits: 4.3459, 5,67890
post processing
postive: 99.1234%, negative: 0.8766%

pipeline 的huggingface 参考教程:https://transformers-doc.100sta.com/docs/transformers/v4.31.0/zh/pipeline_tutorial#pipeline
pipeline 支持的任务:

Notasktypedetail
1text-clasification(sentiment-analysis: )text文本分类,情感分析
2token-clasification(ner )text识别
3quesion-answeringtext问答
4fill-masktext掩码填充
5summarizationtext摘要生成,阅读理解
6translationtext
7text-2-text generationtextsequence to sequence
8text-generationtext
9quesion-answeringtext问答
10conversationaltext对话
11table-question, answeringtext表格问答
12zero-shot-classificationtext0样本分类
13automatic-speech-recognitionmultimodal语音识别
14feature-extractionmultimodal特征抽取
15audio-classificationaudio
16visual-question-answeringmultimodal视觉问答
17document-question-answeringmultimodal文档问答
18zero-shot-image-classificationmultimodal图像0样本分类
19zero-shot-audio-classificationmultimodal音频0样本分类
20image-classificationimage
21zero-shot-object-classificationmultimodal音频样本分类
22video-classificationmultimodal视频分类

通过代码也可以看支持哪些任务:

# print the support task
from transformers.pipelines import SUPPORTED_TASKS
for k, v in SUPPORTED_TASKS.items():
   print("---------------------------")
   print(k, v)

运行结果

2 pipeline 实战 :

代码:

from transformers import pipeline

# case 1 text-classification
model_id = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
text_pipe = pipeline("text-classification", model=model_id)
print("case1:")
print(text_pipe('good!'))

# case 2 sentiment
model_id = "facebook/detr-resnet-50"
sentiment_pipe = pipeline("object-detection", model=model_id)
print("case2:")
print(sentiment_pipe('/home/mex/Desktop/learn_objdetect/datasets/coco128/images/train2017/000000000025.jpg'))

在这里插入图片描述
注:其中,第一次运行时没有模型,需要下载

运行结果:
在这里插入图片描述
可以看到对于情感判断的 case1 ,我写的是好的积极的判断正确,
同时case 2 是一张来自与目标检测coco数据集的图片,是一只长颈鹿,这个模型也是运行正确
在这里插入图片描述
还有一点要注意:
就是之前的写法可以不写明模型id(model_id),但是目前我用的 transformer 需要增加,如果不增加就会报出如下错误
在这里插入图片描述
model id 怎么看? 在章节1 中的打印可以看到
例如:
在这里插入图片描述

在这里插入图片描述

当我们登录 huggingface 后,选择界面上的 models

在这里插入图片描述
比如我们选择 uer
在这里插入图片描述
选择 uer/roberta-base-finetuned-cluener2020-chinese
进入进去后,我们看到的开头就是 model_id就是我们想要的
在这里插入图片描述

3 gpu 运行 :

代码:

# case 3 
import torch
import time

model_id = "facebook/detr-resnet-50"
objdct_pipe = pipeline("object-detection", model=model_id)
start = time.time()
for i in range(30):
   objdct_pipe('/home/mex/Desktop/learn_objdetect/datasets/coco128/images/train2017/000000000025.jpg')
end = time.time()
print("case 3:")
print("cpu time:" + str((end - start)))


model_id = "facebook/detr-resnet-50"
objdct_pipe = pipeline("object-detection", model=model_id, device=0) # chose gpu 0
objdct_pipe.model.device
torch.cuda.synchronize()
start = time.time()
for i in range(30):
   objdct_pipe('/home/mex/Desktop/learn_objdetect/datasets/coco128/images/train2017/000000000025.jpg')
torch.cuda.synchronize()
end = time.time()
print("gpu time:" + str((end - start)))

运行结果:
在这里插入图片描述
可以看到gpu 明显运行快很多,但是不要比运行一次的,因为gpu开始和结束需要同步,比较耗时。

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值