当前位置: 首页 > news >正文

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

文章目录

  • 一、前言
  • 二、Computer vision
    • 2.1 Image classification
    • 2.2 Object detection
    • 2.3 Image segmentation
    • 2.4 Depth estimation
  • 三、NLP
    • 3.1 Text classification
    • 3.2 Token classification
    • 3.3 Question answering
    • 3.4 Summarization
    • 3.5 Translation
    • 3.6 Language modeling
      • 3.6.1 预测序列中的下一个单词
      • 3.6.2 预测一个序列中的一个被屏蔽的token

一、前言

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

将一种语言的转换为另一种语言。

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")
http://www.lryc.cn/news/65800.html

相关文章:

  • HTMLCSS
  • 【安装Nginx】
  • VSCode作业1:猜数字游戏和简单计数器(包含完整代码)
  • NANK OE骨传导开放式蓝牙耳机发布,极致体验拉满!
  • 看完这篇文章你就彻底懂啦{保姆级讲解}-----(I.MX6U驱动GPIO中断《包括时钟讲解》) 2023.5.9
  • MySql -- 事务
  • 关于大模型对未来影响的一点看法
  • Android - 约束布局 ConstraintLayout
  • Addictive Multiplicative in NN
  • LeetCode 1206. 实现跳表
  • 离散数学_九章:关系(2)
  • [ubuntu][原创]通过apt方式去安装libnccl库
  • YonLinker连接集成平台构建新一代产业互联根基
  • 泛型的详解
  • 用科技创造未来!流辰信息技术助您实现高效办公
  • 基于R语言APSIM模型
  • 块状链表实现BigString大字符串操作(golang)
  • 项目问题记录(持续更新)
  • Linux的进程
  • 与其焦虑被 AI 取代或猜测前端是否已死, 不如看看 vertical-align 扎实你的基础!!!
  • 路由、交换机、集线器、DNS服务器、广域网/局域网、端口、MTU
  • 在全志V851S开发板上进行屏幕触摸适配
  • 字符串拷贝时的内存重叠问题
  • 告别PPT手残党!这6款AI神器,让你秒变PPT王者!
  • JVM配置与优化
  • 电力系统储能调峰、调频模型研究(Matlab代码实现)
  • C++基础之类、对象一(类的定义,作用域、this指针)
  • javaScript---设计模式-封装与对象
  • 【消息中间件】kafka高性能设计之内存池
  • 创建型模式——单例(singleton)