快速上手transformers库之pipeline
pipeline简介
在 Hugging Face 的 Transformers 库中,pipeline是一个高级接口,它将预处理、模型推理和后处理封装在一起,从而简化了使用预训练模型执行各种任务的流程。具体来说,pipeline 包的特点包括:
- 易用性:用户只需指定任务类型(例如情感分析、文本生成、命名实体识别、翻译、摘要等)和输入数据,pipeline 会自动加载相应的预训练模型和分词器,并完成所有处理步骤。
- 自动化处理: pipeline 封装了数据的预处理、模型推理和结果的后处理,免去了用户编写大量底层代码的麻烦。
- 多任务支持: 它支持多种 NLP 任务,并且在每个任务中都采用了最佳实践,使得快速原型开发和部署变得非常方便。
!pip install transformers
pipeline支持的任务类型
现阶段pipeline接口支持的任务类型有29种,可以通过以下代码查看:
from transformers.pipelines import SUPPORTED_TASKS
for index,(k,v) in enumerate(SUPPORTED_TASKS.items()):
print(k, v)
audio-classification {'impl': <class 'transformers.pipelines.audio_classification.AudioClassificationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForAudioClassification'>,), 'default': {'model': {'pt': ('superb/wav2vec2-base-superb-ks', '372e048')}}, 'type': 'audio'}
automatic-speech-recognition {'impl': <class 'transformers.pipelines.automatic_speech_recognition.AutomaticSpeechRecognitionPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForCTC'>, <class 'transformers.models.auto.modeling_auto.AutoModelForSpeechSeq2Seq'>), 'default': {'model': {'pt': ('facebook/wav2vec2-base-960h', '22aad52')}}, 'type': 'multimodal'}
text-to-audio {'impl': <class 'transformers.pipelines.text_to_audio.TextToAudioPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForTextToWaveform'>, <class 'transformers.models.auto.modeling_auto.AutoModelForTextToSpectrogram'>), 'default': {'model': {'pt': ('suno/bark-small', '1dbd7a1')}}, 'type': 'text'}
feature-extraction {'impl': <class 'transformers.pipelines.feature_extraction.FeatureExtractionPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModel'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModel'>,), 'default': {'model': {'pt': ('distilbert/distilbert-base-cased', '6ea8117'), 'tf': ('distilbert/distilbert-base-cased', '6ea8117')}}, 'type': 'multimodal'}
text-classification {'impl': <class 'transformers.pipelines.text_classification.TextClassificationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForSequenceClassification'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForSequenceClassification'>,), 'default': {'model': {'pt': ('distilbert/distilbert-base-uncased-finetuned-sst-2-english', '714eb0f'), 'tf': ('distilbert/distilbert-base-uncased-finetuned-sst-2-english', '714eb0f')}}, 'type': 'text'}
token-classification {'impl': <class 'transformers.pipelines.token_classification.TokenClassificationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForTokenClassification'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForTokenClassification'>,), 'default': {'model': {'pt': ('dbmdz/bert-large-cased-finetuned-conll03-english', '4c53496'), 'tf': ('dbmdz/bert-large-cased-finetuned-conll03-english', '4c53496')}}, 'type': 'text'}
question-answering {'impl': <class 'transformers.pipelines.question_answering.QuestionAnsweringPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForQuestionAnswering'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForQuestionAnswering'>,), 'default': {'model': {'pt': ('distilbert/distilbert-base-cased-distilled-squad', '564e9b5'), 'tf': ('distilbert/distilbert-base-cased-distilled-squad', '564e9b5')}}, 'type': 'text'}
table-question-answering {'impl': <class 'transformers.pipelines.table_question_answering.TableQuestionAnsweringPipeline'>, 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForTableQuestionAnswering'>,), 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForTableQuestionAnswering'>,), 'default': {'model': {'pt': ('google/tapas-base-finetuned-wtq', 'e3dde19'), 'tf': ('google/tapas-base-finetuned-wtq', 'e3dde19')}}, 'type': 'text'}
visual-question-answering {'impl': <class 'transformers.pipelines.visual_question_answering.VisualQuestionAnsweringPipeline'>, 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForVisualQuestionAnswering'>,), 'tf': (), 'default': {'model': {'pt': ('dandelin/vilt-b32-finetuned-vqa', 'd0a1f6a')}}, 'type': 'multimodal'}
document-question-answering {'impl': <class 'transformers.pipelines.document_question_answering.DocumentQuestionAnsweringPipeline'>, 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForDocumentQuestionAnswering'>,), 'tf': (), 'default': {'model': {'pt': ('impira/layoutlm-document-qa', 'beed3c4')}}, 'type': 'multimodal'}
fill-mask {'impl': <class 'transformers.pipelines.fill_mask.FillMaskPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForMaskedLM'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForMaskedLM'>,), 'default': {'model': {'pt': ('distilbert/distilroberta-base', 'fb53ab8'), 'tf': ('distilbert/distilroberta-base', 'fb53ab8')}}, 'type': 'text'}
summarization {'impl': <class 'transformers.pipelines.text2text_generation.SummarizationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForSeq2SeqLM'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForSeq2SeqLM'>,), 'default': {'model': {'pt': ('sshleifer/distilbart-cnn-12-6', 'a4f8f3e'), 'tf': ('google-t5/t5-small', 'df1b051')}}, 'type': 'text'}
translation {'impl': <class 'transformers.pipelines.text2text_generation.TranslationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForSeq2SeqLM'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForSeq2SeqLM'>,), 'default': {('en', 'fr'): {'model': {'pt': ('google-t5/t5-base', 'a9723ea'), 'tf': ('google-t5/t5-base', 'a9723ea')}}, ('en', 'de'): {'model': {'pt': ('google-t5/t5-base', 'a9723ea'), 'tf': ('google-t5/t5-base', 'a9723ea')}}, ('en', 'ro'): {'model': {'pt': ('google-t5/t5-base', 'a9723ea'), 'tf': ('google-t5/t5-base', 'a9723ea')}}}, 'type': 'text'}
text2text-generation {'impl': <class 'transformers.pipelines.text2text_generation.Text2TextGenerationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForSeq2SeqLM'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForSeq2SeqLM'>,), 'default': {'model': {'pt': ('google-t5/t5-base', 'a9723ea'), 'tf': ('google-t5/t5-base', 'a9723ea')}}, 'type': 'text'}
text-generation {'impl': <class 'transformers.pipelines.text_generation.TextGenerationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForCausalLM'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForCausalLM'>,), 'default': {'model': {'pt': ('openai-community/gpt2', '607a30d'), 'tf': ('openai-community/gpt2', '607a30d')}}, 'type': 'text'}
zero-shot-classification {'impl': <class 'transformers.pipelines.zero_shot_classification.ZeroShotClassificationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForSequenceClassification'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForSequenceClassification'>,), 'default': {'model': {'pt': ('facebook/bart-large-mnli', 'd7645e1'), 'tf': ('FacebookAI/roberta-large-mnli', '2a8f12d')}, 'config': {'pt': ('facebook/bart-large-mnli', 'd7645e1'), 'tf': ('FacebookAI/roberta-large-mnli', '2a8f12d')}}, 'type': 'text'}
zero-shot-image-classification {'impl': <class 'transformers.pipelines.zero_shot_image_classification.ZeroShotImageClassificationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForZeroShotImageClassification'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForZeroShotImageClassification'>,), 'default': {'model': {'pt': ('openai/clip-vit-base-patch32', '3d74acf'), 'tf': ('openai/clip-vit-base-patch32', '3d74acf')}}, 'type': 'multimodal'}
zero-shot-audio-classification {'impl': <class 'transformers.pipelines.zero_shot_audio_classification.ZeroShotAudioClassificationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModel'>,), 'default': {'model': {'pt': ('laion/clap-htsat-fused', 'cca9e28')}}, 'type': 'multimodal'}
image-classification {'impl': <class 'transformers.pipelines.image_classification.ImageClassificationPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForImageClassification'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForImageClassification'>,), 'default': {'model': {'pt': ('google/vit-base-patch16-224', '3f49326'), 'tf': ('google/vit-base-patch16-224', '3f49326')}}, 'type': 'image'}
image-feature-extraction {'impl': <class 'transformers.pipelines.image_feature_extraction.ImageFeatureExtractionPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModel'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModel'>,), 'default': {'model': {'pt': ('google/vit-base-patch16-224', '3f49326'), 'tf': ('google/vit-base-patch16-224', '3f49326')}}, 'type': 'image'}
image-segmentation {'impl': <class 'transformers.pipelines.image_segmentation.ImageSegmentationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForImageSegmentation'>, <class 'transformers.models.auto.modeling_auto.AutoModelForSemanticSegmentation'>), 'default': {'model': {'pt': ('facebook/detr-resnet-50-panoptic', 'd53b52a')}}, 'type': 'multimodal'}
image-to-text {'impl': <class 'transformers.pipelines.image_to_text.ImageToTextPipeline'>, 'tf': (<class 'transformers.models.auto.modeling_tf_auto.TFAutoModelForVision2Seq'>,), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForVision2Seq'>,), 'default': {'model': {'pt': ('ydshieh/vit-gpt2-coco-en', '5bebf1e'), 'tf': ('ydshieh/vit-gpt2-coco-en', '5bebf1e')}}, 'type': 'multimodal'}
image-text-to-text {'impl': <class 'transformers.pipelines.image_text_to_text.ImageTextToTextPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForImageTextToText'>,), 'default': {'model': {'pt': ('llava-hf/llava-onevision-qwen2-0.5b-ov-hf', '2c9ba3b')}}, 'type': 'multimodal'}
object-detection {'impl': <class 'transformers.pipelines.object_detection.ObjectDetectionPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForObjectDetection'>,), 'default': {'model': {'pt': ('facebook/detr-resnet-50', '1d5f47b')}}, 'type': 'multimodal'}
zero-shot-object-detection {'impl': <class 'transformers.pipelines.zero_shot_object_detection.ZeroShotObjectDetectionPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForZeroShotObjectDetection'>,), 'default': {'model': {'pt': ('google/owlvit-base-patch32', 'cbc355f')}}, 'type': 'multimodal'}
depth-estimation {'impl': <class 'transformers.pipelines.depth_estimation.DepthEstimationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForDepthEstimation'>,), 'default': {'model': {'pt': ('Intel/dpt-large', 'bc15f29')}}, 'type': 'image'}
video-classification {'impl': <class 'transformers.pipelines.video_classification.VideoClassificationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForVideoClassification'>,), 'default': {'model': {'pt': ('MCG-NJU/videomae-base-finetuned-kinetics', '488eb9a')}}, 'type': 'video'}
mask-generation {'impl': <class 'transformers.pipelines.mask_generation.MaskGenerationPipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForMaskGeneration'>,), 'default': {'model': {'pt': ('facebook/sam-vit-huge', '87aecf0')}}, 'type': 'multimodal'}
image-to-image {'impl': <class 'transformers.pipelines.image_to_image.ImageToImagePipeline'>, 'tf': (), 'pt': (<class 'transformers.models.auto.modeling_auto.AutoModelForImageToImage'>,), 'default': {'model': {'pt': ('caidas/swin2SR-classical-sr-x2-64', 'cee1c92')}}, 'type': 'image'}
pipeline的3种创建方法
- 根据任务类型直接创建pipeline
from transformers import pipeline
pipe = pipeline("text-classification")
pipe("You must always have faith in yourself.")
- 创建基于指定模型的pipeline
model_path = '/content/drive/MyDrive/Colab Notebooks/bert-base-cased'
pipe = pipeline("text-classification", model = model_path)
pipe("You must always have faith in yourself.")
- 先加载模型再创建pipeline
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
pipe = pipeline("text-classification", model = model, tokenizer = tokenizer)
pipe("You must always have faith in yourself.")
pipeline的几个任务实例
- sentiment-analysis 情感分析
pipe = pipeline("sentiment-analysis")
pipe("Love is like a piece of shit.")
pipe(["Love is like a piece of shit.", "I love you three thousand times!"])
- zero-shot-classification 零样本训练分类
pipe = pipeline("zero-shot-classification")
pipe("This is a course about how to make machine know human.",
candidate_labels = ["education", "politics", "business"]
)
- text-generation 文本生成
pipe = pipeline("text-generation")
pipe("In this course, we will teach you how to")
pipe("In this course, we will teach you how to",
num_return_sequences = 2,
max_length = 50
)
- fill-mask 遮盖词填充
pipe = pipeline("fill-mask")
pipe("This course will teach you all about <mask> models.", top_k = 2)
- ner 命名实体识别
pipe = pipeline("ner", grouped_entities = True)
pipe("My name is LeBron James and I work at Lakers in LA.")
- question-answering 自动问答
pipe = pipeline("question-answering")
pipe(question = "Where do I work?",
context = "My name is LeBron James and I work at Lakers in LA.")
- summarization 自动摘要
pipe = pipeline("summarization")
pipe("""
Once upon a time, in a small village nestled among rolling hills, there lived a curious young girl named Aria.
Every evening, Aria would gaze at the night sky, enchanted by a twinkling star that seemed to whisper her name.
One crisp autumn night, as the star shone brighter than ever, Aria decided to follow its light.
Packing a small bag with a loaf of bread, a flask of water, and her favorite book of stories, she stepped into the unknown.
The path led her into a mysterious forest, where the trees whispered secrets and the gentle wind hummed ancient tunes.
Deep in the forest, Aria encountered a wise old owl named Orion.
With soft, knowing eyes, Orion explained that the star was not merely a beacon in the sky—it was a guide to a long-forgotten treasure that held the power to bring prosperity and joy to her village.
Inspired by his words, Aria embarked on her quest, her heart filled with courage and wonder.
As she journeyed through enchanted glades and over bubbling streams, Aria faced challenges that tested her resolve.
She solved riddles posed by playful sprites, navigated winding paths illuminated only by the star's glow, and even comforted a lonely creature lost in the dark.
With each trial, Aria grew wiser and more determined.
After days of travel, she arrived at the foot of a towering ancient tree, its branches stretching high into the heavens.
There, nestled in the crook of its mighty trunk, was a small, ornate door.
The whispering star hovered above, as if inviting her to unlock its secret.
With a deep breath, Aria opened the door and discovered a hidden chamber filled with glowing crystals and weathered scrolls—treasures of knowledge, hope, and healing.
Realizing that the true treasure was the wisdom contained in these ancient texts, Aria carefully gathered them and returned to her village.
She shared the magical insights with her neighbors, teaching them how to nurture the land and care for one another.
The village soon blossomed into a place of harmony and prosperity, and Aria was celebrated not just as a brave adventurer, but as a guardian of hope.
Every night thereafter, as the whispering star lit up the sky, the villagers remembered Aria’s journey—a reminder that sometimes the greatest treasures are found within ourselves, and even the smallest spark can light up the darkest night.
""")
使用cpu/gpu进行pipeline推理
! pip install torch
import torch
- cpu推理
pipe = pipeline("text-classification")
pipe("I don't love her any more.")
print(pipe.model.device)
- gpu推理
pipe = pipeline("text-classification", device = 0)
pipe("I don't love her any more.")
print(pipe.model.device)
pipeline实现原理
步骤:
- 预处理(Preprocessing):
输入数据首先经过预处理步骤,通常是利用分词器(Tokenizer)进行分词、编码、截断和填充等操作,将原始数据转换成模型可以接受的格式。 - 模型推理(Model Inference):
预处理后的数据传递给预训练模型进行前向传播(forward pass)。此时,模型会根据内部参数计算出原始的输出(如 logits、隐藏状态等)。 - 后处理(Postprocessing):
最后,pipeline 会将模型输出进行解码或格式转换,比如将 logits 转换为概率、提取预测标签或生成自然语言文本,从而形成最终的易读结果。
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
input_text = "You must always have faith in yourself."
inputs = tokenizer(input_text, return_tensors="pt")
print('inputs = ', inputs)
res = model(**inputs)
print('res = ', res)
logits = res.logits
logits = torch.softmax(logits, dim=-1)
print('logits = ', logits)
pred = torch.argmax(logits).item()
result = model.config.id2label.get(pred)
print('result = ', result)