【人工智能】Transformers之Pipeline(概述):30w+大模型极简应用

​​​​​​​

目录

一、引言 

二、pipeline库

2.1 概述

2.2 使用task实例化pipeline对象

2.2.1 基于task实例化“自动语音识别”

2.2.2 task列表

2.2.3 task默认模型

2.3 使用model实例化pipeline对象

2.3.1 基于model实例化“自动语音识别”

 2.3.2 查看model与task的对应关系

三、总结


一、引言 

 pipeline(管道)是huggingface transformers库中一种极简方式使用大模型推理的抽象,将所有大模型分为语音(Audio)、计算机视觉(Computer vision)、自然语言处理(NLP)、多模态(Multimodal)等4大类,28小类任务(tasks)。共计覆盖32万个模型

本文对pipeline进行整体介绍,之后本专栏以每个task为主题,分别介绍各种task使用方法。

二、pipeline库

2.1 概述

管道是一种使用模型进行推理的简单而好用的方法。这些管道是从库中抽象出大部分复杂代码的对象,提供了专用于多项任务的简单 API,包括命名实体识别、掩码语言建模、情感分析、特征提取和问答。在使用上,主要有2种方法

  • 使用task实例化pipeline对象
  • 使用model实例化pipeline对象

2.2 使用task实例化pipeline对象

2.2.1 基于task实例化“自动语音识别”

自动语音识别的task为automatic-speech-recognition:

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"from transformers import pipelinespeech_file = "./output_video_enhanced.mp3"
pipe = pipeline(task="automatic-speech-recognition")
result = pipe(speech_file)
print(result)

2.2.2 task列表

task共计28类,按首字母排序,列表如下,直接替换2.2.1代码中的pipeline的task即可应用:

  • "audio-classification":将返回一个AudioClassificationPipeline。
  • "automatic-speech-recognition":将返回一个AutomaticSpeechRecognitionPipeline。
  • "depth-estimation":将返回一个DepthEstimationPipeline。
  • "document-question-answering":将返回一个DocumentQuestionAnsweringPipeline。
  • "feature-extraction":将返回一个FeatureExtractionPipeline。
  • "fill-mask":将返回一个FillMaskPipeline:。
  • "image-classification":将返回一个ImageClassificationPipeline。
  • "image-feature-extraction":将返回一个ImageFeatureExtractionPipeline。
  • "image-segmentation":将返回一个ImageSegmentationPipeline。
  • "image-to-image":将返回一个ImageToImagePipeline。
  • "image-to-text":将返回一个ImageToTextPipeline。
  • "mask-generation":将返回一个MaskGenerationPipeline。
  • "object-detection":将返回一个ObjectDetectionPipeline。
  • "question-answering":将返回一个QuestionAnsweringPipeline。
  • "summarization":将返回一个SummarizationPipeline。
  • "table-question-answering":将返回一个TableQuestionAnsweringPipeline。
  • "text2text-generation":将返回一个Text2TextGenerationPipeline。
  • "text-classification"("sentiment-analysis"可用别名):将返回一个 TextClassificationPipeline。
  • "text-generation":将返回一个TextGenerationPipeline:。
  • "text-to-audio""text-to-speech"可用别名):将返回一个TextToAudioPipeline:。
  • "token-classification"("ner"可用别名):将返回一个TokenClassificationPipeline。
  • "translation":将返回一个TranslationPipeline。
  • "translation_xx_to_yy":将返回一个TranslationPipeline。
  • "video-classification":将返回一个VideoClassificationPipeline。
  • "visual-question-answering":将返回一个VisualQuestionAnsweringPipeline。
  • "zero-shot-classification":将返回一个ZeroShotClassificationPipeline。
  • "zero-shot-image-classification":将返回一个ZeroShotImageClassificationPipeline。
  • "zero-shot-audio-classification":将返回一个ZeroShotAudioClassificationPipeline。
  • "zero-shot-object-detection":将返回一个ZeroShotObjectDetectionPipeline。

2.2.3 task默认模型

针对每一个task,pipeline默认配置了模型,可以通过pipeline源代码查看:

SUPPORTED_TASKS = {"audio-classification": {"impl": AudioClassificationPipeline,"tf": (),"pt": (AutoModelForAudioClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("superb/wav2vec2-base-superb-ks", "372e048")}},"type": "audio",},"automatic-speech-recognition": {"impl": AutomaticSpeechRecognitionPipeline,"tf": (),"pt": (AutoModelForCTC, AutoModelForSpeechSeq2Seq) if is_torch_available() else (),"default": {"model": {"pt": ("facebook/wav2vec2-base-960h", "55bb623")}},"type": "multimodal",},"text-to-audio": {"impl": TextToAudioPipeline,"tf": (),"pt": (AutoModelForTextToWaveform, AutoModelForTextToSpectrogram) if is_torch_available() else (),"default": {"model": {"pt": ("suno/bark-small", "645cfba")}},"type": "text",},"feature-extraction": {"impl": FeatureExtractionPipeline,"tf": (TFAutoModel,) if is_tf_available() else (),"pt": (AutoModel,) if is_torch_available() else (),"default": {"model": {"pt": ("distilbert/distilbert-base-cased", "935ac13"),"tf": ("distilbert/distilbert-base-cased", "935ac13"),}},"type": "multimodal",},"text-classification": {"impl": TextClassificationPipeline,"tf": (TFAutoModelForSequenceClassification,) if is_tf_available() else (),"pt": (AutoModelForSequenceClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("distilbert/distilbert-base-uncased-finetuned-sst-2-english", "af0f99b"),"tf": ("distilbert/distilbert-base-uncased-finetuned-sst-2-english", "af0f99b"),},},"type": "text",},"token-classification": {"impl": TokenClassificationPipeline,"tf": (TFAutoModelForTokenClassification,) if is_tf_available() else (),"pt": (AutoModelForTokenClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("dbmdz/bert-large-cased-finetuned-conll03-english", "f2482bf"),"tf": ("dbmdz/bert-large-cased-finetuned-conll03-english", "f2482bf"),},},"type": "text",},"question-answering": {"impl": QuestionAnsweringPipeline,"tf": (TFAutoModelForQuestionAnswering,) if is_tf_available() else (),"pt": (AutoModelForQuestionAnswering,) if is_torch_available() else (),"default": {"model": {"pt": ("distilbert/distilbert-base-cased-distilled-squad", "626af31"),"tf": ("distilbert/distilbert-base-cased-distilled-squad", "626af31"),},},"type": "text",},"table-question-answering": {"impl": TableQuestionAnsweringPipeline,"pt": (AutoModelForTableQuestionAnswering,) if is_torch_available() else (),"tf": (TFAutoModelForTableQuestionAnswering,) if is_tf_available() else (),"default": {"model": {"pt": ("google/tapas-base-finetuned-wtq", "69ceee2"),"tf": ("google/tapas-base-finetuned-wtq", "69ceee2"),},},"type": "text",},"visual-question-answering": {"impl": VisualQuestionAnsweringPipeline,"pt": (AutoModelForVisualQuestionAnswering,) if is_torch_available() else (),"tf": (),"default": {"model": {"pt": ("dandelin/vilt-b32-finetuned-vqa", "4355f59")},},"type": "multimodal",},"document-question-answering": {"impl": DocumentQuestionAnsweringPipeline,"pt": (AutoModelForDocumentQuestionAnswering,) if is_torch_available() else (),"tf": (),"default": {"model": {"pt": ("impira/layoutlm-document-qa", "52e01b3")},},"type": "multimodal",},"fill-mask": {"impl": FillMaskPipeline,"tf": (TFAutoModelForMaskedLM,) if is_tf_available() else (),"pt": (AutoModelForMaskedLM,) if is_torch_available() else (),"default": {"model": {"pt": ("distilbert/distilroberta-base", "ec58a5b"),"tf": ("distilbert/distilroberta-base", "ec58a5b"),}},"type": "text",},"summarization": {"impl": SummarizationPipeline,"tf": (TFAutoModelForSeq2SeqLM,) if is_tf_available() else (),"pt": (AutoModelForSeq2SeqLM,) if is_torch_available() else (),"default": {"model": {"pt": ("sshleifer/distilbart-cnn-12-6", "a4f8f3e"), "tf": ("google-t5/t5-small", "d769bba")}},"type": "text",},# This task is a special case as it's parametrized by SRC, TGT languages."translation": {"impl": TranslationPipeline,"tf": (TFAutoModelForSeq2SeqLM,) if is_tf_available() else (),"pt": (AutoModelForSeq2SeqLM,) if is_torch_available() else (),"default": {("en", "fr"): {"model": {"pt": ("google-t5/t5-base", "686f1db"), "tf": ("google-t5/t5-base", "686f1db")}},("en", "de"): {"model": {"pt": ("google-t5/t5-base", "686f1db"), "tf": ("google-t5/t5-base", "686f1db")}},("en", "ro"): {"model": {"pt": ("google-t5/t5-base", "686f1db"), "tf": ("google-t5/t5-base", "686f1db")}},},"type": "text",},"text2text-generation": {"impl": Text2TextGenerationPipeline,"tf": (TFAutoModelForSeq2SeqLM,) if is_tf_available() else (),"pt": (AutoModelForSeq2SeqLM,) if is_torch_available() else (),"default": {"model": {"pt": ("google-t5/t5-base", "686f1db"), "tf": ("google-t5/t5-base", "686f1db")}},"type": "text",},"text-generation": {"impl": TextGenerationPipeline,"tf": (TFAutoModelForCausalLM,) if is_tf_available() else (),"pt": (AutoModelForCausalLM,) if is_torch_available() else (),"default": {"model": {"pt": ("openai-community/gpt2", "6c0e608"), "tf": ("openai-community/gpt2", "6c0e608")}},"type": "text",},"zero-shot-classification": {"impl": ZeroShotClassificationPipeline,"tf": (TFAutoModelForSequenceClassification,) if is_tf_available() else (),"pt": (AutoModelForSequenceClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("facebook/bart-large-mnli", "c626438"),"tf": ("FacebookAI/roberta-large-mnli", "130fb28"),},"config": {"pt": ("facebook/bart-large-mnli", "c626438"),"tf": ("FacebookAI/roberta-large-mnli", "130fb28"),},},"type": "text",},"zero-shot-image-classification": {"impl": ZeroShotImageClassificationPipeline,"tf": (TFAutoModelForZeroShotImageClassification,) if is_tf_available() else (),"pt": (AutoModelForZeroShotImageClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("openai/clip-vit-base-patch32", "f4881ba"),"tf": ("openai/clip-vit-base-patch32", "f4881ba"),}},"type": "multimodal",},"zero-shot-audio-classification": {"impl": ZeroShotAudioClassificationPipeline,"tf": (),"pt": (AutoModel,) if is_torch_available() else (),"default": {"model": {"pt": ("laion/clap-htsat-fused", "973b6e5"),}},"type": "multimodal",},"image-classification": {"impl": ImageClassificationPipeline,"tf": (TFAutoModelForImageClassification,) if is_tf_available() else (),"pt": (AutoModelForImageClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("google/vit-base-patch16-224", "5dca96d"),"tf": ("google/vit-base-patch16-224", "5dca96d"),}},"type": "image",},"image-feature-extraction": {"impl": ImageFeatureExtractionPipeline,"tf": (TFAutoModel,) if is_tf_available() else (),"pt": (AutoModel,) if is_torch_available() else (),"default": {"model": {"pt": ("google/vit-base-patch16-224", "3f49326"),"tf": ("google/vit-base-patch16-224", "3f49326"),}},"type": "image",},"image-segmentation": {"impl": ImageSegmentationPipeline,"tf": (),"pt": (AutoModelForImageSegmentation, AutoModelForSemanticSegmentation) if is_torch_available() else (),"default": {"model": {"pt": ("facebook/detr-resnet-50-panoptic", "fc15262")}},"type": "multimodal",},"image-to-text": {"impl": ImageToTextPipeline,"tf": (TFAutoModelForVision2Seq,) if is_tf_available() else (),"pt": (AutoModelForVision2Seq,) if is_torch_available() else (),"default": {"model": {"pt": ("ydshieh/vit-gpt2-coco-en", "65636df"),"tf": ("ydshieh/vit-gpt2-coco-en", "65636df"),}},"type": "multimodal",},"object-detection": {"impl": ObjectDetectionPipeline,"tf": (),"pt": (AutoModelForObjectDetection,) if is_torch_available() else (),"default": {"model": {"pt": ("facebook/detr-resnet-50", "2729413")}},"type": "multimodal",},"zero-shot-object-detection": {"impl": ZeroShotObjectDetectionPipeline,"tf": (),"pt": (AutoModelForZeroShotObjectDetection,) if is_torch_available() else (),"default": {"model": {"pt": ("google/owlvit-base-patch32", "17740e1")}},"type": "multimodal",},"depth-estimation": {"impl": DepthEstimationPipeline,"tf": (),"pt": (AutoModelForDepthEstimation,) if is_torch_available() else (),"default": {"model": {"pt": ("Intel/dpt-large", "e93beec")}},"type": "image",},"video-classification": {"impl": VideoClassificationPipeline,"tf": (),"pt": (AutoModelForVideoClassification,) if is_torch_available() else (),"default": {"model": {"pt": ("MCG-NJU/videomae-base-finetuned-kinetics", "4800870")}},"type": "video",},"mask-generation": {"impl": MaskGenerationPipeline,"tf": (),"pt": (AutoModelForMaskGeneration,) if is_torch_available() else (),"default": {"model": {"pt": ("facebook/sam-vit-huge", "997b15")}},"type": "multimodal",},"image-to-image": {"impl": ImageToImagePipeline,"tf": (),"pt": (AutoModelForImageToImage,) if is_torch_available() else (),"default": {"model": {"pt": ("caidas/swin2SR-classical-sr-x2-64", "4aaedcb")}},"type": "image",},
}

2.3 使用model实例化pipeline对象

2.3.1 基于model实例化“自动语音识别”

如果不想使用task中默认的模型,可以指定huggingface中的模型:

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"from transformers import pipelinespeech_file = "./output_video_enhanced.mp3"
#transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-medium")
pipe = pipeline(model="openai/whisper-medium")
result = pipe(speech_file)
print(result)

 2.3.2 查看model与task的对应关系

可以登录https://huggingface.co/tasks查看

三、总结

本文为transformers之pipeline专栏的第0篇,后面会以每个task为一篇,共计讲述28+个tasks的用法,通过28个tasks的pipeline使用学习,可以掌握语音、计算机视觉、自然语言处理、多模态乃至强化学习等30w+个huggingface上的开源大模型。让你成为大模型领域的专家!

期待您的3连+关注,如何还有时间,欢迎阅读我的其他文章:

《AI—工程篇》

AI智能体研发之路-工程篇(一):Docker助力AI智能体开发提效

AI智能体研发之路-工程篇(二):Dify智能体开发平台一键部署

AI智能体研发之路-工程篇(三):大模型推理服务框架Ollama一键部署

AI智能体研发之路-工程篇(四):大模型推理服务框架Xinference一键部署

AI智能体研发之路-工程篇(五):大模型推理服务框架LocalAI一键部署

《AI—模型篇》

AI智能体研发之路-模型篇(一):大模型训练框架LLaMA-Factory在国内网络环境下的安装、部署及使用

AI智能体研发之路-模型篇(二):DeepSeek-V2-Chat 训练与推理实战

AI智能体研发之路-模型篇(三):中文大模型开、闭源之争

AI智能体研发之路-模型篇(四):一文入门pytorch开发

AI智能体研发之路-模型篇(五):pytorch vs tensorflow框架DNN网络结构源码级对比

AI智能体研发之路-模型篇(六):【机器学习】基于tensorflow实现你的第一个DNN网络

AI智能体研发之路-模型篇(七):【机器学习】基于YOLOv10实现你的第一个视觉AI大模型

AI智能体研发之路-模型篇(八):【机器学习】Qwen1.5-14B-Chat大模型训练与推理实战

AI智能体研发之路-模型篇(九):【机器学习】GLM4-9B-Chat大模型/GLM-4V-9B多模态大模型概述、原理及推理实战

《AI—Transformers应用》

【AI大模型】Transformers大模型库(一):Tokenizer

【AI大模型】Transformers大模型库(二):AutoModelForCausalLM

【AI大模型】Transformers大模型库(三):特殊标记(special tokens)

【AI大模型】Transformers大模型库(四):AutoTokenizer

【AI大模型】Transformers大模型库(五):AutoModel、Model Head及查看模型结构

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/44403.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何通过Java操作Redis?——Jedis!

简介 在redis命令行客户端中操作redis是否可行?可行,但不方便且不是主流的方式。最终还是要通过Java代码来操作~ Redis的底层通信是遵守RESP协议的,一些第三方的库就实现了这些协议,然后封装好API,程序猿通过封装好的…

IEC62056标准体系简介-4.IEC62056-53 COSEM应用层

为在通信介质中传输COSEM对象模型,IEC62056参照OSI参考模型,制定了简化的三层通信模型,包括应用层、数据链路层(或中间协议层)和物理层,如图6所示。COSEM应用层完成对COSEM对象的属性和方法的访问&#xff…

01MFC建立单个文件类型——画线

文章目录 选择模式初始化文件作用解析各初始化文件解析类导向创建鼠标按键按下抬起操作函数添加一个变量记录起始位置注意事项代码实现效果图虚实/颜色线选择模式 初始化文件作用解析 运行: 各初始化文件解析 MFC(Microsoft Foundation Classes)是一个C++类库,用于在Win…

昇思25天学习打卡营第16天|基于MindSpore通过GPT实现情感分类

今天的这个代码几乎没有任何解释,结合之前GPT生成文本摘要的代码。 大概记录一下 import numpy as np # 导入NumPy库def process_dataset(dataset, tokenizer, max_seq_len512, batch_size4, shuffleFalse): # 判断当前设备是否为Ascend,如果是ascen的…

防御课综合实验

实验拓扑: 实验要求: 1、DMZ区内的服务器,办公区仅能在办公时间内(9点到18点)可以访问,生产区的设备全天可以访问 2、生产区不允许访问互联网,办公区和游客区允许访问互联网 3、办公区设备10…

二叉平衡树(左单旋,右单旋,左右双旋、右左双旋)

一、AVL树(二叉平衡树:高度平衡的二叉搜索树) 0、二叉平衡树 左右子树高度差不超过1的二叉搜索树。 public class AVLTree{static class AVLTreeNode {public TreeNode left null; // 节点的左孩子public TreeNode right null; // 节点的…

Java 中的异常处理机制是如何工作的?请解释 try-catch-finally 的基本用法?

Java中的异常处理机制是确保程序稳健性的重要组成部分,它允许程序在遇到错误或异常情况时,能够优雅地处理问题,而不是直接崩溃。 这一机制的核心在于使用try-catch-finally结构,以及通过throw和throws关键字来抛出和声明异常。 …

基于Transformer的端到端的目标检测 | 读论文

本文正在参加 人工智能创作者扶持计划 提及到计算机视觉的目标检测,我们一般会最先想到卷积神经网络(CNN),因为这算是目标检测领域的开山之作了,在很长的一段时间里人们都折服于卷积神经网络在图像处理领域的优势&…

Vue2.0和Vue3.0的区别?

Vue.js 3.0 相较于 Vue.js 2.0 在多个方面进行了改进和优化,主要包括以下几点: 性能提升: Vue 3.0 使用了新的响应式系统,称为“Proxy-based”,相比于 Vue 2.0 的“Object.defineProperty”,更加高效。 编…

【深度学习基础】安装包报错——MAC M3-MAX芯片安装scikit-learn库报错。

目录 一、问题描述二、解决方法 一、问题描述 首先想安装scikit-learn库在mac终端显示顺利安装完成,但是测试的时候报错如下所示: /opt/anaconda3/envs/dtc/bin/python /Users/chenfaquan/PycharmProjects/TimeSeries/data_create.py Traceback (most…

论文 | REACT: SYNERGIZING REASONING AND ACTING INLANGUAGE MODELS

本文首先认为,到目前为止,LLM 在语言理解方面令人印象深刻,它们已被用来生成 CoT(思想链)来解决一些问题,它们也被用于执行和计划生成。 尽管这两者是分开研究的,但本文旨在以交错的方式将推理…

JSP入门基础

JSP入门基础 软件开发环境这门课程的复习资料 Web开发技术概述 URL的组成部分 协议、主机DNS名或IP地址和文件名 Tomcat服务器 Tomcat服务器的默认端口号是8080 概念 软件开发环境是围绕着软件开发的一定目标而组织在一起的一组相关软件工具的有机集合 JSP和HTML的区别…

range()用法

range(n):是Python中的函数,作用是可以生成 [0, n)之间的正数range(a,b) :生成[a,b)之间的正数数字,不包含brange(start, end, step):生成的数值规则--- [start, end) 步长是 step,默认 1 详见:http://t.csdnimg.cn/7…

科研入门笔记

自学参考: 沐神论文精读系列 如何读论文 通常,一篇论文的结构为: title标题abstract摘要introduction介绍method方法experiments实验conclusion结论 一篇论文可以考虑读1~3遍 第一遍 海选:标题、摘要、结论,选读方…

SPE连接器技术革新汽车制造业

概述 新的SPE标准在汽车制造业中的应用正日益受到重视,它不仅推动了汽车通信技术的革新,还对汽车性能测试方法产生了深远影响。本文将详细探讨SPE标准在汽车制造业中的应用案例分析,以及它对供应链的挑战与机遇。 SPE标准在汽车制造业中的应…

[leetcode]subarray-product-less-than-k 乘积小于K的子数组

. - 力扣&#xff08;LeetCode&#xff09; class Solution { public:int numSubarrayProductLessThanK(vector<int>& nums, int k) {if (k 0) {return 0;}int n nums.size();vector<double> logPrefix(n 1);for (int i 0; i < n; i) {logPrefix[i 1] …

揭秘!chatGPT核心技术应用

2022年11月30日&#xff0c;可能将成为一个改变人类历史的日子——美国人工智能开发机构OpenAI推出了聊天机器人ChatGPT-3.5&#xff0c;将人工智能的发展推向了一个新的高度。2023年11月7日&#xff0c;OpenAI首届开发者大会被称为“科技界的春晚”&#xff0c;吸引了全球广大…

js 时间单位秒转时分秒方法封装。00:00:00

1.方法封装 /*** 秒数转时分秒*/ function formatSeconds(t) {let h parseInt(${t / 60 / 60})let m parseInt(${t / 60 % 60})let s parseInt(${t % 60})//三元表达式 补零 如果小于10 则在前边进行补零 如果大于10 则不需要补零h h < 10 ? 0 h : hm m < 10 ?…

prometheus回顾(2)--如何使用Grafana对接Prometheus数据源的详细过程,清晰易懂。

文章目录 Grafana简介什么是GrafanaGrafana 能做什么&#xff1f;什么时候我们会用到Grafana?Prometheus有图形化展示&#xff0c;为什么我们还要用Grafana? 环境操作步骤一、Grafana安装二、Grafana数据源Prometheus添加三、Grafana添加数据仪表盘补充、如何查找仪表盘 Graf…

在Linux下直接修改磁盘镜像文件的内容

背景 嵌入式Linux系统通常在调试稳定后&#xff0c;会对磁盘&#xff08;SSD、NVME、SD卡、TF卡&#xff09;做个镜像&#xff0c;通常是.img后缀的文件&#xff0c;以后组装新设备时&#xff0c;就将镜像文件烧录到新磁盘即可&#xff0c;非常简单。 这种方法有个不便之处&a…