当前位置:网站首页>抱抱脸(hugging face)教程-中文翻译-基于pipeline的推理
抱抱脸(hugging face)教程-中文翻译-基于pipeline的推理
2022-08-09 14:56:00 【wwlsm_zql】
基于pipeline的推理
Pipeline ()使得使用 Model Hub 中的任何模型来推断各种任务变得非常简单,比如文本生成、图像分割和音频分类。即使您没有特定模式的经验或者不了解支持模型的代码,您仍然可以将它们与pipeline一起使用() !本教程将教你:
- 使用 pipeline ()进行推理。
- 使用特定的标记器或模型。
- 使用pipeline()执行音频和视觉任务。
查看pipeline()文档,以获得所支持的 taska 的完整列表。
pipeline的使用
虽然每个任务都有一个关联的pipeline(),但是使用包含所有特定任务pipeline的通用pipeline()抽象更简单。Pipeline ()自动加载一个默认模型和标记器,能够推断您的任务。
- 首先创建一个 pipeline ()并指定一个推理任务:
from transformers import pipeline
generator = pipeline(task="text-generation")
- 将输入文本传递给pipeline():
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
如果你有一个以上的输入,把你的输入作为一个列表传递:
generator(
... [
... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone",
... "Nine for Mortal Men, doomed to die, One for the Dark Lord on his dark throne",
... ]
... )
任务的任何其他参数也可以包含在pipeline()中。文本生成任务具有一个具有多个参数的 generate ()方法来控制输出。例如,如果您想生成多个输出,请设置 num_return_sequences 参数:
generator(
... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone",
... num_return_sequences=2,
... )
- 选择一个模型和标记器
Pipeline ()接受来自 modelhub 的任何模型。Model Hub 上有一些标记,它们允许您过滤您希望用于任务的模型。一旦你选择了一个合适的模型,加载相应的 autoodelfor 和[‘ AutoTokenizer’]类。例如,为一个因果语言建模任务加载 automatodelforcausallm 类:
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
为您的任务创建一个 pipeline () ,并指定您加载的模型和标记器:
from transformers import pipeline
generator = pipeline(task="text-generation", model=model, tokenizer=tokenizer)
将输入文本传递给 pipeline ()以生成一些文本:
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
[{
'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Seven for the Dragon-lords (for them to rule in a world ruled by their rulers, and all who live within the realm'}]
音频pipeline
流水线()的灵活性意味着它也可以扩展到音频任务。
例如,让我们从约翰·f·肯尼迪(johnf.Kennedy)著名的“我们选择登月”演讲的一个短片中来分类情绪。在 Model Hub 上找到一个用于情感识别的音频分类模型,并将其加载到pipeline中() :
from transformers import pipeline
audio_classifier = pipeline(
... task="audio-classification", model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"... )
将音频文件传递给pipeline() :
audio_classifier("jfk_moon_speech.wav")
[{
'label': 'calm', 'score': 0.13856211304664612},
{
'label': 'disgust', 'score': 0.13148026168346405},
{
'label': 'happy', 'score': 0.12635163962841034},
{
'label': 'angry', 'score': 0.12439591437578201},
{
'label': 'fearful', 'score': 0.12404385954141617}]
愿景pipeline
最后,使用pipeline()执行视觉任务实际上是相同的。
指定您的视觉任务并将图像传递给分类器。图像映像可以是图像的链接或本地路径。例如,下面显示的是哪种猫科动物?
from transformers import pipeline
vision_classifier = pipeline(task="image-classification")
vision_classifier(
... images="
"... )
[{
'label': 'lynx, catamount', 'score': 0.4403027892112732},
{
'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',
'score': 0.03433405980467796},
{
'label': 'snow leopard, ounce, Panthera uncia',
'score': 0.032148055732250214},
{
'label': 'Egyptian cat', 'score': 0.02353910356760025},
{
'label': 'tiger cat', 'score': 0.023034192621707916}]
本文是对英文内容的翻译学习,仅学习使用
边栏推荐
猜你喜欢
随机推荐
pyspark explode时增加序号
smote 采样
回收站一直显示未清空的图标问题
PathMeasure 轨迹动画神器
Dagger2从0基础使用,到单例注入的正确姿势
仪表盘
spark shuffle
cheerio根据多个class匹配
关于初级程序员职场如何提升技能的几点建议?
数字图像处理的基本原理和常用方法
你知道亚马逊代运营的成本是多少吗?
scala 内部类使用小细节
单例模式-五种方式 不要被克隆
LNK1123: Failed during transition to COFF: invalid or corrupt file
关于亚马逊的坑你知道几个?
bin document read and write
利用qrcode组件实现图片转二维码
微信小程序自定义日期选择器(带标题的)
深度神经网络中的多任务学习研究综述
NoUniqueBeanDefinitionException和JSON乱码处理出现异常









