Chapter 02
查看Pipeline支持的任务类型
Notebooktransformers41 cells
查看Pipeline支持的任务类型
In [ ]python · cell 2
python
from transformers.pipelines import SUPPORTED_TASKSIn [ ]python · cell 3
python
from pprint import pprint
pprint(SUPPORTED_TASKS.keys())In [ ]python · cell 4
python
for k, v in SUPPORTED_TASKS.items():
print(k, v)Pipeline的创建与使用方式
In [ ]python · cell 6
python
from transformers import pipeline, QuestionAnsweringPipeline根据任务类型直接创建Pipeline, 默认都是英文的模型
In [ ]python · cell 8
python
pipe = pipeline("text-classification")In [ ]python · cell 9
python
pipe(["very good!", "vary bad!"])指定任务类型,再指定模型,创建基于指定模型的Pipeline
In [ ]python · cell 11
python
# https://huggingface.co/models
pipe = pipeline("text-classification", model="uer/roberta-base-finetuned-dianping-chinese")In [ ]python · cell 12
python
pipe("我觉得不太行!")预先加载模型,再创建Pipeline
In [ ]python · cell 14
python
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# 这种方式,必须同时指定model和tokenizer
model = AutoModelForSequenceClassification.from_pretrained("uer/roberta-base-finetuned-dianping-chinese")
tokenizer = AutoTokenizer.from_pretrained("uer/roberta-base-finetuned-dianping-chinese")
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)In [ ]python · cell 15
python
pipe("我觉得不太行!")In [ ]python · cell 16
python
pipe.model.deviceIn [ ]python · cell 17
python
import torch
import time
times = []
for i in range(100):
torch.cuda.synchronize()
start = time.time()
pipe("我觉得不太行!")
torch.cuda.synchronize()
end = time.time()
times.append(end - start)
print(sum(times) / 100)使用GPU进行推理
In [ ]python · cell 19
python
pipe = pipeline("text-classification", model="uer/roberta-base-finetuned-dianping-chinese", device=0)In [ ]python · cell 20
python
pipe.model.deviceIn [ ]python · cell 21
python
import torch
import time
times = []
for i in range(100):
torch.cuda.synchronize()
start = time.time()
pipe("我觉得不太行!")
torch.cuda.synchronize()
end = time.time()
times.append(end - start)
print(sum(times) / 100)确定Pipeline参数
In [ ]python · cell 23
python
qa_pipe = pipeline("question-answering", model="uer/roberta-base-chinese-extractive-qa")In [ ]python · cell 24
python
qa_pipeIn [ ]python · cell 25
python
QuestionAnsweringPipelineIn [ ]python · cell 26
python
qa_pipe(question="中国的首都是哪里?", context="中国的首都是北京", max_answer_len=1)其他Pipeline示例
In [ ]python · cell 28
python
checkpoint = "google/owlvit-base-patch32"
detector = pipeline(model=checkpoint, task="zero-shot-object-detection")In [ ]python · cell 29
python
import requests
from PIL import Image
url = "https://unsplash.com/photos/oj0zeY2Ltk4/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTR8fHBpY25pY3xlbnwwfHx8fDE2Nzc0OTE1NDk&force=true&w=640"
im = Image.open(requests.get(url, stream=True).raw)
imIn [ ]python · cell 30
python
predictions = detector(
im,
candidate_labels=["hat", "sunglasses", "book"],
)
predictionsIn [ ]python · cell 31
python
from PIL import ImageDraw
draw = ImageDraw.Draw(im)
for prediction in predictions:
box = prediction["box"]
label = prediction["label"]
score = prediction["score"]
xmin, ymin, xmax, ymax = box.values()
draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1)
draw.text((xmin, ymin), f"{label}: {round(score,2)}", fill="red")
imPipeline背后的实现
In [ ]python · cell 33
python
from transformers import *
import torchIn [ ]python · cell 34
python
tokenizer = AutoTokenizer.from_pretrained("uer/roberta-base-finetuned-dianping-chinese")
model = AutoModelForSequenceClassification.from_pretrained("uer/roberta-base-finetuned-dianping-chinese")In [ ]python · cell 35
python
input_text = "我觉得不太行!"
inputs = tokenizer(input_text, return_tensors="pt")
inputsIn [ ]python · cell 36
python
res = model(**inputs)
resIn [ ]python · cell 37
python
logits = res.logits
logits = torch.softmax(logits, dim=-1)
logitsIn [ ]python · cell 38
python
pred = torch.argmax(logits).item()
predIn [ ]python · cell 39
python
model.config.id2labelIn [ ]python · cell 40
python
result = model.config.id2label.get(pred)
resultIn [ ]python · cell 41
python
