HuggingFace入门教程:多种任务入门教程
文章目录
一、pipelines简介
Hugging Face 的 pipeline 模块是 transformers 库的核心抽象层,它将复杂的模型推理流程简化为端到端的处理管道,让用户无需深入底层实现即可快速调用预训练模型。

Pipelines(管道)抽象了 transformers 库中大部分复杂代码的对象,提供了一个专门用于多种任务的简单API,包括命名实体识别、掩码语言建模、情感分析、特征提取和问答等。
| Modality | Task | Description | Pipeline API |
|---|---|---|---|
| Audio | Audio classification | 为音频文件分配一个标签 | pipeline(task=“audio-classification”) |
| Automatic speech recognition | 将音频文件中的语音提取为文本 | pipeline(task=“automatic-speech-recognition”) | |
| Computer vision | Image classification | 为图像分配一个标签 | pipeline(task=“image-classification”) |
| Object detection | 预测图像中目标对象的边界框和类别 | pipeline(task=“object-detection”) | |
| Image segmentation | 为图像中每个独立的像素分配标签(支持语义、全景和实例分割) | pipeline(task=“image-segmentation”) | |
| Natural language processing | Text classification | 为给定的文本序列分配一个标签 | pipeline(task=“sentiment-analysis”) |
| Token classification | 为序列里的每个 token 分配一个标签(人, 组织, 地址等等) | pipeline(task=“ner”) | |
| Question answering | 通过给定的上下文和问题, 在文本中提取答案 | pipeline(task=“question-answering”) | |
| Summarization | 为文本序列或文档生成总结 | pipeline(task=“summarization”) | |
| Translation | 将文本从一种语言翻译为另一种语言 | pipeline(task=“translation”) | |
| Multimodal | Document question answering | 根据给定的文档和问题回答一个关于该文档的问题。 | pipeline(task=“document-question-answering”) |
| Visual Question Answering | 给定一个图像和一个问题,正确地回答有关图像的问题 | pipeline(task=“vqa”) |
pipelines 支持所有任务情况:点我
二、情感分类任务
Text classification(文本分类)将一个文本序列(可以是句子级别、段落或者整篇文章)标记为预定义的类别。文本分类有许多实际应用,其中包括:
- 情感分析:根据某种极性(如积极或消极)对文本进行标记,以在政治、金融和市场等领域支持决策制定。
- 内容分类:根据某个主题对文本进行标记,以帮助组织和过滤新闻和社交媒体信息流中的信息(天气、体育、金融等)。
使用huggingface中的distilbert-base-uncased-finetuned-sst-2-english模型进行情感分析。
1、自定义模型下载的路径
在transformers自定义模型下载的路径:
import os
# unbuntu
os.environ['HF_HOME'] = '/mnt/new_volume/hf'
os.environ['HF_HUB_CACHE'] = '/mnt/new_volume/hf/hub'
# windows
os.environ['HF_HOME'] ='./hf/'
os.environ['HF_HUB_CACHE'] = './hf/hub/'
2、情感分类示例程序
from transformers import pipeline
# 仅指定任务时,使用默认模型(不推荐)
pipe = pipeline(task = "sentiment-analysis")
pipe("今儿上海可真冷啊")
注意:未指定模型,huggingface默认使用distilbert-base-uncased-finetuned-sst-2-english模型。第一运行该程序时,程序会将模型下载到本地,速度相对比较慢。下载完成后,模型会调用本地的资源进行计算。
输出:
[{'label': 'NEGATIVE', 'score': 0.8957214951515198}]
输入:
pipe("我觉得这家店蒜泥白肉的味道一般")
输出:
[{'label': 'NEGATIVE', 'score': 0.9238728880882263}]
distilbert-base-uncased-finetuned-sst-2-english模型并未对中文进行专门的训练,所以对中文文本分类任务表现一般:
输入:
pipe("你学东西真的好快,理论课一讲就明白了")
输出(结果不太准确):
[{'label': 'NEGATIVE', 'score': 0.8578686118125916}]
替换为英文后,文本分类任务的表现立刻改善:
pipe("You learn things really quickly. You understand the theory class as soon as it is taught.")
pipe("Today Shanghai is really cold.")
输出
[{'label': 'POSITIVE', 'score': 0.9961802959442139}]
[{'label': 'NEGATIVE', 'score': 0.9995032548904419}]
批处理调用模型推理
text_list = [
"Today Shanghai is really cold.",
"I think the taste of the garlic mashed pork in this store is average.",
"You learn things really quickly. You understand the theory class as soon as it is taught."
]
print(pipe(text_list))
输出:
[{'label': 'NEGATIVE', 'score': 0.9995032548904419},
{'label': 'NEGATIVE', 'score': 0.9984821677207947},
{'label': 'POSITIVE', 'score': 0.9961802959442139}]
三、分词任务
在任何NLP任务中,文本都经过预处理,将文本序列分成单个单词或子词。这些被称为tokens。
Token Classification(Token分类)将每个token分配一个来自预定义类别集的标签。
两种常见的 Token 分类是:
- 命名实体识别(NER):根据实体类别(如组织、人员、位置或日期)对token进行标记。NER在生物医学设置中特别受欢迎,可以标记基因、蛋白质和药物名称。
- 词性标注(POS):根据其词性(如名词、动词或形容词)对标记进行标记。POS对于帮助翻译系统了解两个相同的单词如何在语法上不同很有用(作为名词的银行与作为动词的银行)。
分词任务huggingface默认使用的是bert-large-cased-finetuned-conll03-english模型:
示例程序:
from transformers import pipeline
classifier = pipeline(task="ner")
preds = classifier("Hugging Face is a French company based in New York City.")
preds = [
{
"entity": pred["entity"],
"score": round(pred["score"], 4),
"index": pred["index"],
"word": pred["word"],
"start": pred["start"],
"end": pred["end"],
}
for pred in preds
]
print(*preds, sep="\n")
输出:
{'entity': 'I-ORG', 'score': 0.9968, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2}
{'entity': 'I-ORG', 'score': 0.9293, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7}
{'entity': 'I-ORG', 'score': 0.9763, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12}
{'entity': 'I-MISC', 'score': 0.9983, 'index': 6, 'word': 'French', 'start': 18, 'end': 24}
{'entity': 'I-LOC', 'score': 0.999, 'index': 10, 'word': 'New', 'start': 42, 'end': 45}
{'entity': 'I-LOC', 'score': 0.9987, 'index': 11, 'word': 'York', 'start': 46, 'end': 50}
{'entity': 'I-LOC', 'score': 0.9992, 'index': 12, 'word': 'City', 'start': 51, 'end': 55}
上面示例,将文本分成了一个一个的单词。如果需要将单个单词合并成实体,加入grouped_entities=True参数:
classifier = pipeline(task="ner", grouped_entities=True)
classifier("Hugging Face is a French company based in New York City.")
输出:
[{'entity_group': 'ORG', 'score': 0.9674638, 'word': 'Hugging Face', 'start': 0, 'end': 12},
{'entity_group': 'MISC', 'score': 0.99828726, 'word': 'French', 'start': 18, 'end': 24},
{'entity_group': 'LOC', 'score': 0.99896103, 'word': 'New York City', 'start': 42, 'end': 55}]
四、智能问答任务
Question Answering(问答)是另一个token-level的任务,返回一个问题的答案,有时带有上下文(开放领域),有时不带上下文(封闭领域)。每当我们向虚拟助手提出问题时,例如询问一家餐厅是否营业,就会发生这种情况。它还可以提供客户或技术支持,并帮助搜索引擎检索您要求的相关信息。
有两种常见的问答类型:
- 提取式:给定一个问题和一些上下文,模型必须从上下文中提取出一段文字作为答案
- 生成式:给定一个问题和一些上下文,答案是根据上下文生成的;这种方法由
Text2TextGenerationPipeline处理,而不是下面展示的QuestionAnsweringPipeline
智能问答任务huggingface默认使用的是distilbert-base-cased-distilled-squad模型:
from transformers import pipeline
question_answerer = pipeline(task="question-answering")
preds = question_answerer(
question="What is the name of the repository?",
context="The name of the repository is huggingface/transformers",
)
print(
f"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}"
)
输出:
score: 0.9327, start: 30, end: 54, answer: huggingface/transformers
输入:
preds = question_answerer(
question="What is the capital of China?",
context="On 1 October 1949, CCP Chairman Mao Zedong formally proclaimed the People's Republic of China in Tiananmen Square, Beijing.",
)
print(
f"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}"
)
输出:
score: 0.9458, start: 115, end: 122, answer: Beijing
五、文本摘要任务
Summarization(文本摘要)从较长的文本中创建一个较短的版本,同时尽可能保留原始文档的大部分含义。摘要是一个序列到序列的任务;它输出比输入更短的文本序列。有许多长篇文档可以进行摘要,以帮助读者快速了解主要要点。法案、法律和财务文件、专利和科学论文等文档可以摘要,以节省读者的时间并作为阅读辅助工具。
与问答类似,摘要有两种类型:
- 提取式:从原始文本中识别和提取最重要的句子
- 生成式:从原始文本中生成目标摘要(可能包括输入文件中没有的新单词);
SummarizationPipeline使用生成式方法
文本任务huggingface默认使用的是t5-base模型:
from transformers import pipeline
summarizer = pipeline(task="summarization",
model="t5-base",
min_length=8,
max_length=32,
)
summarizer(
"""
In this work, we presented the Transformer, the first sequence transduction model based entirely on attention,
replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention.
For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers.
On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art.
In the former task our best model outperforms even all previously reported ensembles.
"""
)
输出:
[{'summary_text': 'the Transformer is the first sequence transduction model based entirely on attention . it replaces recurrent layers commonly used in encoder-decode'}]
输入:
summarizer(
'''
Large language models (LLM) are very large deep learning models that are pre-trained on vast amounts of data.
The underlying transformer is a set of neural networks that consist of an encoder and a decoder with self-attention capabilities.
The encoder and decoder extract meanings from a sequence of text and understand the relationships between words and phrases in it.
Transformer LLMs are capable of unsupervised training, although a more precise explanation is that transformers perform self-learning.
It is through this process that transformers learn to understand basic grammar, languages, and knowledge.
Unlike earlier recurrent neural networks (RNN) that sequentially process inputs, transformers process entire sequences in parallel.
This allows the data scientists to use GPUs for training transformer-based LLMs, significantly reducing the training time.
'''
)
输出:
[{'summary_text': 'large language models (LLMs) are very large deep learning models pre-trained on vast amounts of data . transformers are capable of un'}]
六、音频处理任务
音频和语音处理任务与其他模态略有不同,主要是因为音频作为输入是一个连续的信号。与文本不同,原始音频波形不能像句子可以被划分为单词那样被整齐地分割成离散的块。为了解决这个问题,通常在固定的时间间隔内对原始音频信号进行采样。如果在每个时间间隔内采样更多样本,采样率就会更高,音频更接近原始音频源。
以前的方法是预处理音频以从中提取有用的特征。现在更常见的做法是直接将原始音频波形输入到特征编码器中,以提取音频表示。这样可以简化预处理步骤,并允许模型学习最重要的特征。
Audio classification(音频分类)是一项将音频数据从预定义的类别集合中进行标记的任务。这是一个广泛的类别,具有许多具体的应用,其中一些包括:
- 声学场景分类:使用场景标签(“办公室”、“海滩”、“体育场”)对音频进行标记。
- 声学事件检测:使用声音事件标签(“汽车喇叭声”、“鲸鱼叫声”、“玻璃破碎声”)对音频进行标记。
- 标记:对包含多种声音的音频进行标记(鸟鸣、会议中的说话人识别)。
- 音乐分类:使用流派标签(“金属”、“嘻哈”、“乡村”)对音乐进行标记。
模型主页:https://hf-mirror.com/superb/hubert-base-superb-er
数据集主页:https://hf-mirror.com/datasets/superb#er
1、语音情感识别
情感识别(ER)为每个话语预测一个情感类别。我们采用了最广泛使用的ER数据集IEMOCAP,并遵循传统的评估协议:删除不平衡的情感类别,只保留最后四个具有相似数量数据点的类别,并在标准分割的五折交叉验证上进行评估。评估指标是准确率(ACC)。
前置依赖包安装库:ffmpeg
$apt update & apt upgrade
$apt install -y ffmpeg
$pip install ffmpeg ffmpeg-python
示例程序:
from transformers import pipeline
classifier = pipeline(task="audio-classification", model="superb/hubert-base-superb-er")
# 使用 Hugging Face Datasets 上的测试文件
preds = classifier("https://hf-mirror.com/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
# preds = classifier("data/audio/mlk.flac")
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
print(preds)
输出:
[{'score': 0.4532, 'label': 'hap'},
{'score': 0.3622, 'label': 'sad'},
{'score': 0.0943, 'label': 'neu'},
{'score': 0.0903, 'label': 'ang'}]
2、语音识别
Automatic speech recognition(自动语音识别)将语音转录为文本。这是最常见的音频任务之一,部分原因是因为语音是人类交流的自然形式。如今,ASR系统嵌入在智能技术产品中,如扬声器、电话和汽车。
但是,Transformer架构帮助解决的一个关键挑战是低资源语言。通过在大量语音数据上进行预训练,仅在一个低资源语言的一小时标记语音数据上进行微调,仍然可以产生与以前在100倍更多标记数据上训练的ASR系统相比高质量的结果。
模型主页:https://hf-mirror.com/openai/whisper-small
下面展示使用 OpenAI Whisper Small 模型实现 ASR 的 Pipeline API 示例:
from transformers import pipeline
# 使用 `model` 参数指定模型
transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-small")
text = transcriber("data/audio/mlk.flac")
print(text)
输出:
{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'}
七、 计算机视觉
Computer Vision(计算机视觉)任务中最早成功之一是使用卷积神经网络(CNN)识别邮政编码数字图像。图像由像素组成,每个像素都有一个数值。这使得将图像表示为像素值矩阵变得容易。每个像素值组合描述了图像的颜色。
计算机视觉任务可以通过以下两种通用方式解决:
- 使用卷积来学习图像的层次特征,从低级特征到高级抽象特征。
- 将图像分成块,并使用Transformer逐步学习每个图像块如何相互关联以形成图像。与CNN偏好的自底向上方法不同,这种方法有点像从一个模糊的图像开始,然后逐渐将其聚焦清晰。
1、图像分类
Image Classificaiton(图像分类)将整个图像从预定义的类别集合中进行标记。像大多数分类任务一样,图像分类有许多实际用例,其中一些包括:
- 医疗保健:标记医学图像以检测疾病或监测患者健康状况
- 环境:标记卫星图像以监测森林砍伐、提供野外管理信息或检测火灾
- 农业:标记农作物图像以监测植物健康或用于土地使用监测的卫星图像
- 生态学:标记动物或植物物种的图像以监测野生动物种群或跟踪濒危物种
模型主页:https://hf-mirror.com/google/vit-base-patch16-224
from transformers import pipeline
classifier = pipeline(task="image-classification")
preds = classifier(
"https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)
# preds = classifier("data/image/cat-chonk.jpeg") # 已下载到本地
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
print(*preds, sep="\n")
输出:
{'score': 0.4335, 'label': 'lynx, catamount'}
{'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}
{'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}
{'score': 0.0239, 'label': 'Egyptian cat'}
{'score': 0.0229, 'label': 'tiger cat'}

2、目标检测
与图像分类不同,目标检测在图像中识别多个对象以及这些对象在图像中的位置(由边界框定义)。目标检测的一些示例应用包括:
- 自动驾驶车辆:检测日常交通对象,如其他车辆、行人和红绿灯
- 遥感:灾害监测、城市规划和天气预报
- 缺陷检测:检测建筑物中的裂缝或结构损坏,以及制造业产品缺陷
模型主页:https://hf-mirror.com/facebook/detr-resnet-50
前置依赖包安装:
pip install timm
示例:
from transformers import pipeline
detector = pipeline(task="object-detection")
preds = detector(
"https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)
# preds = detector("data/image/cat_dog.jpg")
preds = [{"score": round(pred["score"], 4), "label": pred["label"], "box": pred["box"]} for pred in preds]
preds
输出:
[{'score': 0.9864,
'label': 'cat',
'box': {'xmin': 178, 'ymin': 154, 'xmax': 882, 'ymax': 598}}]

更多推荐



所有评论(0)