ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Transformers 快速上手:3 行代码跑通文本、视觉与语音模型推理,附完整避坑清单

Transformers 快速上手:3 行代码跑通文本、视觉与语音模型推理,附完整避坑清单 Transformers 快速上手3 行代码跑通文本、视觉与语音模型推理附完整避坑清单【免费下载链接】transformers Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.项目地址: https://gitcode.com/GitHub_Trending/tra/transformers模型推理的痛点在于加载权重、写预处理、整理输出每一步都要自己搭。Transformers 把这些步骤收进一个pipeline调用覆盖文本、视觉、音频、多模态四类任务。本文带你从安装到跑通第一个示例再给一份排错清单。安装与最小可运行示例 先在虚拟环境里带 torch 扩展安装README 要求 Python 3.10、PyTorch 2.5pip install transformers[torch]最小可运行示例来自官方 READMEfrom transformers import pipeline pipe pipeline(tasktext-generation, modelQwen/Qwen2.5-1.5B) print(pipe(the secret to baking a really good cake is ))首次运行会自动下载模型并缓存返回值是字典列表续写文本在generated_text键下。 提示换task和model两个参数同样的三行代码就能切到图像分类、语音识别等任务。Transformers Pipeline 核心能力与返回结果一览pipeline背后是 28 个任务实现都在 src/transformers/pipelines/ 下每个任务一个文件。最常用的四类任务task 参数返回内容来源模块README 中的测试用例文本生成text-generationgenerated_text续写文本pipelines/text_generation.pyQwen2.5-1.5B 续写蛋糕配方图像分类image-classification前 5 名 label 与 scorepipelines/image_classification.pydinov2-small 把鹦鹉图判为 macawscore 0.9978语音识别automatic-speech-recognitiontext转写文本pipelines/automatic_speech_recognition.pywhisper-large-v3 转写一段 mlk.flac 音频视觉问答visual-question-answeringanswerpipelines/image_text_to_text.pyblip-vqa-base 回答 statue of liberty仓库 tests 目录里就备好了可直接喂给图像任务的两张 COCO 测试图。第一张是白底红苹果适合图像分类回归另一张是厨房场景厨师正在切披萨常用作目标检测回归实用能力清单命令行直接对话不写代码。运行transformers chat Qwen/Qwen2.5-0.5B-Instruct即可在终端与模型对话实现见 src/transformers/cli/chat.py无需自己搭服务。离线语音转写。pipeline(automatic-speech-recognition, modelopenai/whisper-large-v3)后直接传本地音频路径返回转写文本适合不联网的转写场景。大模型自动多卡分布。构造 pipeline 时加dtypetorch.bfloat16, device_mapauto单卡装不下的大模型会被自动分层放置这是 README 聊天示例里的标准写法。零样本任务即开即用。目录名即任务参数zero_shot_image_classification.py、zero_shot_object_detection.py、zero_shot_audio_classification.py都已在 pipelines 目录里传参即可调用。主要调用方式对比调用方式关键参数适用场景pipeline(task, model)task / model / dtype / device_mapPython 进程内推理最常用transformers chat model模型名终端交互式对话免写代码transformers serve模型名本地起服务chat 子命令默认连 localhost:8000扩展点在哪新任务实现放 src/transformers/pipelines/一任务一文件命令行入口在 src/transformers/cli/六类常见报错及处理集中在 docs/source/en/troubleshooting.md。避坑清单这些报错都有标准解法 ️别裸装 transformers。不带 extras 的pip install transformers没有 torch 依赖import 即崩用 README 给的pip install transformers[torch]并满足 Python 3.10、PyTorch 2.5。首次运行卡在下载。模型第一次调用才从 Hub 拉取并缓存防火墙环境参考排障文档 Firewalled environments 一节配置代理。CUDA out of memory 最常见。单卡装不下大模型按文档 CUDA out of memory 一节降精度、减分辨率或用device_mapauto拆层。批量输出错乱多因 padding。不等长序列成批推理时padding token 没被 mask 会让模型看到填充位、输出跑偏处理见文档 Incorrect output when padding tokens arent masked 一节。Auto 类与配置类不匹配。报 Unrecognized configuration class XYZ for this kind of AutoModel 时检查配置与 Auto 类是否配套或显式实例化对应模型类。examples 不能照抄。README 明确 examples 目录只是参考示例在你的数据集上未必开箱即用需自行适配数据与参数。 提示报错后先翻 troubleshooting 文档的章节标题六类高频错误已按原因分节不用盲调。小结Transformers 把用一个模型从加载、预处理、后处理三件事压缩成一次pipeline调用。装好依赖跑通上面三行示例剩下按避坑清单对号入座即可。【免费下载链接】transformers Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.项目地址: https://gitcode.com/GitHub_Trending/tra/transformers创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表