【深度学习 | Transformer】Transformers 教程:pipeline一键预测

这篇具有很好参考价值的文章主要介绍了【深度学习 | Transformer】Transformers 教程:pipeline一键预测。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、前言

Transformers 是用于自然语言处理 (NLP)、计算机视觉以及音频和语音处理任务的预训练最先进模型库。该库不仅包含 Transformer 模型,还包含非 Transformer 模型,例如用于计算机视觉任务的现代卷积网络。

pipeline()可以加载多个模型让进行推理变得简单,即使没有使用特定模态的经验或不熟悉模型背后的底层代码,仍然可以使用它们通过pipeline()进行推理。

二、Computer vision

2.1 Image classification

从一组预定义的类中标记图像。

from transformers import pipeline
classifier = pipeline(task="image-classification")
preds = classifier(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)

preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]

输出结果为:

{'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.2 Object detection

目标检测识别图像对象以及对象在图像中的位置。

from transformers import pipeline
detector = pipeline(task="object-detection")
preds = detector(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)

preds = [{"score": round(pred["score"], 4), "label": pred["label"], "box": pred["box"]} for pred in preds]

输出结果为:

[{'score': 0.9865,
  'label': 'cat',
  'box': {'xmin': 178, 'ymin': 154, 'xmax': 882, 'ymax': 598}}]

2.3 Image segmentation

图像分割是一项像素级任务,它将图像中的每个像素分配给一个类别。

from transformers import pipeline
segmenter = pipeline(task="image-segmentation")
preds = segmenter(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)

preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]

输出结果为:

{'score': 0.9879, 'label': 'LABEL_184'}
{'score': 0.9973, 'label': 'snow'}
{'score': 0.9972, 'label': 'cat'}

2.4 Depth estimation

预测图像中每个像素与相机的距离。

from transformers import pipeline
depth_estimator = pipeline(task="depth-estimation")
preds = depth_estimator(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)

三、NLP

3.1 Text classification

从一组预定义的类中标记一系列文本。

from transformers import pipeline
classifier = pipeline(task="sentiment-analysis")
preds = classifier("Hugging Face is the best thing since sliced bread!")

3.2 Token classification

为每个token分配定义类别中的标签。

from transformers import pipeline
classifier = pipeline(task="ner")
preds = classifier("Hugging Face is a French company based in New York City.")

3.3 Question answering

返回问题的答案,有时有上下文(开放域),有时没有上下文(封闭域)。

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",
)

3.4 Summarization

从较长的文本创建较短的版本,同时试图保留原始文档的大部分含义。

from transformers import pipeline
summarizer = pipeline(task="summarization")
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."
)

3.5 Translation

将一种语言的转换为另一种语言。文章来源地址https://www.toymoban.com/news/detail-452194.html

from transformers import pipeline
text = "translate English to French: Hugging Face is a community-based open-source platform for machine learning."
translator = pipeline(task="translation", model="t5-small")

3.6 Language modeling

3.6.1 预测序列中的下一个单词

from transformers import pipeline
prompt = "Hugging Face is a community-based open-source platform for machine learning."
generator = pipeline(task="text-generation")

3.6.2 预测一个序列中的一个被屏蔽的token

text = "Hugging Face is a community-based open-source <mask> for machine learning."
fill_mask = pipeline(task="fill-mask")

到了这里,关于【深度学习 | Transformer】Transformers 教程:pipeline一键预测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • OpenCV与AI深度学习 | 实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程)

    本文来源公众号 “OpenCV与AI深度学习” ,仅用于学术分享,侵权删,干货满满。 原文链接:实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程)     本文将手把手教你用YoloV8训练自己的数据集并实现手势识别。 【1】安装torch, torchvision对应版本,这里先

    2024年04月23日
    浏览(63)
  • Hugging Face 的 Transformers 库快速入门 (一)开箱即用的 pipelines

    注:本系列教程仅供学习使用, 由原作者授权, 均转载自小昇的博客 。 Transformers 是由 Hugging Face 开发的一个 NLP 包,支持加载目前绝大部分的预训练模型。随着 BERT、GPT 等大规模语言模型的兴起,越来越多的公司和研究者采用 Transformers 库来构建 NLP 应用,因此熟悉 Transformer

    2023年04月27日
    浏览(39)
  • 学习笔记:基于Transformer的时间序列预测模型

    为了便于读者理解,笔者将采取一个盾构机掘进参数预测的实际项目进行Transformer模型的说明。此外,该贴更多用于本人的学习记录,适合于对Transformer模型已经有一定了解的读者。此此次外,不定期更新中。 一些参考与图片来源: Transformer论文链接 transformer的细节到底是怎

    2024年02月03日
    浏览(52)
  • Hugging Face使用Stable diffusion Diffusers Transformers Accelerate Pipelines VAE

    A library that offers an implementation of various diffusion models, including text-to-image models. 提供不同扩散模型的实现的库,代码上最简洁,国内的问题是 huggingface 需要翻墙。 A Hugging Face library that provides pre-trained deep learning models for natural language processing tasks. 提供了预训练深度学习模型,

    2024年02月07日
    浏览(40)
  • [深度学习论文笔记]UNETR: Transformers for 3D Medical Image Segmentation

    UNETR: Transformers for 3D Medical Image Segmentation UNETR:用于三维医学图像分割的Transformer Published: Oct 2021 Published in: IEEE Winter Conference on Applications of Computer Vision (WACV) 2022 论文:https://arxiv.org/abs/2103.10504 代码:https://monai.io/research/unetr 摘要:   过去十年以来,具有收缩路径和扩展路径

    2024年01月24日
    浏览(41)
  • 深入理解深度学习——BERT(Bidirectional Encoder Representations from Transformers):基础知识

    分类目录:《深入理解深度学习》总目录 相关文章: · BERT(Bidirectional Encoder Representations from Transformers):基础知识 · BERT(Bidirectional Encoder Representations from Transformers):BERT的结构 · BERT(Bidirectional Encoder Representations from Transformers):MLM(Masked Language Model) · BERT(Bidirect

    2024年02月11日
    浏览(40)
  • 基于transformer的Seq2Seq机器翻译模型训练、预测教程

    机器翻译(Machine Translation, MT)是一类将某种语言(源语言,source language)的句子 x x x 翻译成另一种语言(目标语言,target language)的句子 y y y 的任务。机器翻译的相关研究早在上世纪50年代美苏冷战时期就开始了,当时的机器翻译系统是基于规则的,利用两种语言的单词、

    2024年02月03日
    浏览(32)
  • 深入理解深度学习——BERT(Bidirectional Encoder Representations from Transformers):BERT的结构

    分类目录:《深入理解深度学习》总目录 相关文章: · BERT(Bidirectional Encoder Representations from Transformers):基础知识 · BERT(Bidirectional Encoder Representations from Transformers):BERT的结构 · BERT(Bidirectional Encoder Representations from Transformers):MLM(Masked Language Model) · BERT(Bidirect

    2024年02月11日
    浏览(40)
  • 深度学习11:Transformer

    目录 什么是 Transformer? Encoder Decoder Attention Self-Attention Context-Attention 什么是 Transformer(微软研究院笨笨) RNN和Transformer区别 Universal Transformer和Transformer 区别   ​ 和经典的 seq2seq 模型一样,Transformer 模型中也采用了 encoer-decoder  架构。上图的左半边用 NX 框出来的,就代表

    2024年02月11日
    浏览(28)
  • 深度学习——Transformer的理解整理

    transformer刚被提出的时候就是被用于处理机器翻译的。在transformer架构中的不同位置Q,K,V指代的变量是不一样的。 假设现在处理的是英文-德文的翻译任务。 在encoder的输入端,这里执行的是 self-attention , Q、K、V 都是指代英 文的embedding 。 在decoder的输入端,这里执行的是

    2024年04月28日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包