
ModelScope 多模态 Pipeline 全景解析multi_modal 模块 15 大 Pipeline 的架构与实战指南【免费下载链接】modelscopeModelScope: bring the notion of Model-as-a-Service to life.项目地址: https://gitcode.com/GitHub_Trending/mo/modelscope本文围绕 ModelScope 开源框架中 modelscope/pipelines/multi_modal 模块展开系统讲解该模块对外暴露的 15 个多模态 Pipeline 类图文理解、文生图、跨模态检索、视频理解、语音识别等的任务注册机制、底层模型与预处理器装配方式以及通过统一pipeline()入口快速上手的实战用法。读完本文你将掌握 ModelScope 多模态推理管线的整体架构能够在自己的项目中按任务类型选择并调用合适的 Pipeline。模块定位多模态能力的统一推理入口在 ModelScope 中Pipeline是模型 预处理器 后处理三件套的封装用户只需传入模型 ID 或模型实例即可完成一次推理。modelscope.pipelines.multi_modal正是承载多模态图像、文本、视频、语音任意组合推理能力的子模块其对外 API 索引定义于 docs/source/api/modelscope.pipelines.multi_modal.rst共收录 15 个 Pipeline 类Pipeline 类对应任务Tasks 常量底层模型/预处理器源码确认ImageCaptioningPipelineimage_captioningOFA / MPlug / CLIP-InterrogatorVisualQuestionAnsweringPipelinevisual_question_answeringOFA / MPlugVisualEntailmentPipelinevisual_entailmentOFAVisualGroundingPipelinevisual_groundingOFAStableDiffusionPipelinetext_to_image_synthesisdiffusers 封装ChineseStableDiffusionPipelinetext_to_image_synthesisdiffusers 封装TextToImageSynthesisPipelinetext_to_image_synthesisOFA / MultiStageDiffusionMultiModalEmbeddingPipelinemulti_modal_embedding / image_text_retrievalCLIPGEMMMultiModalEmbeddingPipelinegenerative_multi_modal_embeddingGEMM 系列DocumentVLEmbeddingPipelinedocument_vl_embeddingVLDocVideoMultiModalEmbeddingPipelinevideo_multi_modal_embedding视频多模态编码器MGeoRankingPipelinetext_rankingMGeo 地理文本模型VideoCaptioningPipelinevideo_captioningHiTeAVideoQuestionAnsweringPipelinevideo_question_answering视频问答模型AutomaticSpeechRecognitionPipelineauto_speech_recognitionOFA / MPlug语音从 模块注册表 可以看到该模块实际实现远不止这 15 个类还包括ProSTTextVideoRetrievalPipeline文本-视频检索、SOONetVideoTemporalGroundingPipeline视频时序定位、TextToVideoSynthesisPipeline文生视频、MultimodalDialoguePipeline多模态对话、VideoComposerPipeline、FreeUTextToImagePipeline等而 RST 文档收录的 15 个类是官方 API 层面的核心代表。快速上手pipeline() 统一入口所有 Pipeline 均通过 modelscope.pipelines 暴露的pipeline()工厂函数创建其第一个参数是任务名第二个参数是模型 ID 或模型实例from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks # 以图文生成image captioning为例 model_id damo/cv_clip-interrogator pipeline_ci pipeline(Tasks.image_captioning, modelmodel_id) print(pipeline_ci(test.png))该示例直接取自 ImageCaptioningPipeline 的 docstring是官方推荐的标准用法。pipeline()内部会依据任务名与模型类型通过注册表自动完成 Pipeline 类的查找、模型的加载以及预处理器Preprocessor的自动装配。对于更复杂的输入结构以文档向量嵌入为例取自 DocumentVLEmbeddingPipeline docstringfrom modelscope.models import Model from modelscope.pipelines import pipeline model Model.from_pretrained( damo/multi-modal_convnext-roberta-base_vldoc-embedding) doc_vl_emb_pipeline pipeline(taskdocument-vl-embedding, modelmodel) inp { images: [data/demo.png], ocr_info_paths: [data/demo.json] } result doc_vl_emb_pipeline(inp)要点pipeline()接受任务名字符串或Tasks常量或直接传入已加载的Model实例当传入Model实例时Pipeline 会从model.model_dir中自动推断预处理器。注册机制任务名如何绑定到 Pipeline 类每个 Pipeline 类都通过PIPELINES.register_module(...)装饰器完成注册注册键是任务名 模块名的二元组。例如 MultiModalEmbeddingPipeline 同时注册了两个任务PIPELINES.register_module( Tasks.image_text_retrieval, module_namePipelines.multi_modal_embedding) PIPELINES.register_module( Tasks.multi_modal_embedding, module_namePipelines.multi_modal_embedding) class MultiModalEmbeddingPipeline(Pipeline): ...这说明同一个 Pipeline 类可以服务多个任务名实现跨任务复用。注册表PIPELINES定义于 modelscope/pipelines/builder.pyTasks与Pipelines常量分别定义于 modelscope/utils/constant.py 与 modelscope/metainfo.py。此外模块采用**懒加载LazyImportModule**机制见 modelscope/pipelines/multi_modal/init.py在TYPE_CHECKING分支下声明完整导入列表运行时则通过_import_structure字典按需加载子模块从而显著降低import modelscope的启动开销这是大型框架常用的工程优化手段。图文理解类 PipelineImageCaptioningPipeline图像描述生成注册于Tasks.image_captioning支持三种底层模型族见 image_captioning_pipeline.pyOfaForAllTasks→ 自动装配OfaPreprocessorMPlugForAllTasks→ 自动装配MPlugPreprocessorCLIP_Interrogator→ 自动装配ImageCaptioningClipInterrogatorPreprocessor即上述示例中的damo/cv_clip-interrogator模型。其_batch方法对 MPlug 模型会把图像张量torch.cat拼接、问题 token 封装为BatchEncoding对 OFA 模型则调用batch_process工具完成批处理forward全程在torch.no_grad()下执行。VisualQuestionAnsweringPipeline视觉问答注册于Tasks.visual_question_answering源码底层支持 OFA 与 MPlug 模型族__init__中同样按模型类型自动装配OfaPreprocessor/MPlugPreprocessor并在批处理阶段对 OFA 使用batch_process加速。输入通常为{image: ..., question: ...}输出直接透传模型结果postprocess返回inputs。VisualEntailmentPipeline视觉蕴含判断注册于Tasks.visual_entailment源码用于判断图像是否蕴含给定文本描述底层基于 OFAOfaForAllTasksOfaPreprocessor。该任务与 VQA 共享 OFA 的统一多任务架构测试用例可参考 tests/pipelines/test_ofa_tasks.py。VisualGroundingPipeline视觉定位短语指代注册于Tasks.visual_grounding同样基于 OFA 架构用于根据自然语言短语在图像中定位目标区域输出检测框。该能力在 tests/pipelines/test_visual_grounding.py 中有端到端测试覆盖。文生图与扩散模型类 PipelineStableDiffusionPipeline 与 ChineseStableDiffusionPipeline两者均来自 modelscope/pipelines/multi_modal/diffusers_wrapped/ 子目录是对 HuggingFace diffusers 生态 Stable Diffusion 模型的封装使 ModelScope 用户可以沿用统一pipeline()接口调用。ChineseStableDiffusionPipeline面向中文提示词优化二者均注册于Tasks.text_to_image_synthesis。相关的 Stable Diffusion 推理示例见 examples/pytorch/stable_diffusion训练侧示例见 tests/trainers/test_stable_diffusion_trainer.py。TextToImageSynthesisPipeline统一文生图入口注册于Tasks.text_to_image_synthesis源码是文生图任务的通用封装其forward分支逻辑揭示了不同模型族的调用差异OfaForTextToImageSynthesis/MultiStageDiffusionForTextToImageSynthesis→ 直接调用self.model(input)其他扩散模型 → 调用self.model.generate(input)。postprocess统一把输出包装为{OutputKeys.OUTPUT_IMGS: [img1, img2, ...]}列表形式其中OutputKeys定义于 modelscope/outputs/outputs.py。跨模态检索与向量嵌入类 PipelineMultiModalEmbeddingPipelineCLIP 图文双塔嵌入注册于Tasks.multi_modal_embedding与Tasks.image_text_retrieval两个任务见上文注册代码底层为CLIPForMultiModalEmbeddingmodelscope/models/multi_modal/clip/model.py自动装配CLIPPreprocessor。其forward是极简链路self.model(self.preprocess(input))——输入图像/文本经预处理后直接得到共享语义空间中的向量可用于图文检索、相似度计算等。参考测试 tests/pipelines/test_multi_modal_embedding.py。GEMMMultiModalEmbeddingPipeline生成式多模态嵌入注册于Tasks.generative_multi_modal_embedding源码采用生成式范式学习多模态表示preprocess直接透传输入return inputforward调用self.model(input)。它支持图像、文本、视频等多种模态的统一嵌入输出适合跨模态检索与零样本分类场景。DocumentVLEmbeddingPipeline文档图像向量化注册于Tasks.document_vl_embedding源码底层为VLDocForDocVLEmbeddingmodelscope/models/multi_modal/vldoc/model.py自动装配VLDocPreprocessor。其独特之处在于输入同时包含图像与OCR 信息ocr_info_paths将视觉版式信息与文字内容融合为文档向量可用于文档检索、版面理解等场景。forward中会把编码结果显式搬运到self.device。参考测试 tests/pipelines/test_document_vl_embedding.py。VideoMultiModalEmbeddingPipeline视频-文本联合嵌入注册于Tasks.video_multi_modal_embedding将视频帧序列与文本映射到统一嵌入空间支持视频检索与视频-文本匹配。相关算法如 HICOSSL、CMDSSL 自监督视频嵌入的端到端测试见 tests/pipelines/test_hicossl_video_embedding.py 与 tests/pipelines/test_cmdssl_video_embedding.py。MGeoRankingPipeline地理文本相关性排序注册于Tasks.text_ranking源码是面向地理信息POI、地址等的多模态文本排序模型。其构造器提供sequence_length128、devicegpu、auto_collateTrue等可调参数postprocess中对 logits 施加sigmoid得到 0~1 的相关性分数并输出为{OutputKeys.SCORES: [...]}。其核心亮点是get_gis/_collate_fn方法把每条文本的 GIS 结构化特征几何 ID、相对/绝对位置、省市区编码等组装成gis_list并区分 6 维全球版与 9 维中国版含 prov/city/dist ID两种输入形态——这是 MGeo 区别于普通文本排序模型的关键实现细节。视频理解类 PipelineVideoCaptioningPipeline视频描述生成注册于Tasks.video_captioning源码底层基于 HiTeAHiTeAForAllTasks自动装配HiTeAPreprocessor。批处理时对视频张量torch.cat拼接、问题 token 封装为BatchEncodingforward在torch.no_grad()下执行。HiTeA 一族的完整任务测试见 tests/pipelines/test_hitea_tasks.py。VideoQuestionAnsweringPipeline视频问答注册于Tasks.video_question_answering接受视频 问题输入并输出答案与 OFA/MPlug 家族的图文问答架构同源是视频理解与多模态推理结合的代表性管线。语音类 PipelineAutomaticSpeechRecognitionPipeline自动语音识别注册于Tasks.auto_speech_recognition源码是 OFA/MPlug 统一多模态架构在语音方向的延伸模型既可以理解图像也可以直接接收音频特征完成语音转写。该 Pipeline 的构造器对模型加载做了显式校验model must be a single str or OfaForAllTasks支持字符串模型 ID 或模型实例两种传入方式。注意本模块内的 ASR Pipeline 基于 OFA 统一架构仓库中还有更完整的专用 ASR 管线族见 modelscope/pipelines/audio其示例脚本位于 examples/pytorch/auto_speech_recognition。源码级共性设计三层流水线与自动装配纵观全部 15 个 Pipeline可以归纳出 ModelScope 多模态推理的三条共性设计生命周期固定所有 Pipeline 继承自 modelscope/pipelines/base.py 中的Pipeline基类统一实现preprocess → forward → postprocess三段式流程推理类 Pipeline 普遍在forward中包裹torch.no_grad()且__init__末尾调用self.model.eval()切换到评估模式。预处理器自动装配绝大多数 Pipeline 支持preprocessorNone时的自动装配——根据模型实例类型isinstance判断从model.model_dir加载对应 Preprocessor如OfaPreprocessor、MPlugPreprocessor、CLIPPreprocessor、VLDocPreprocessor、HiTeAPreprocessor等均定义于 modelscope/preprocessors。这也意味着用户完全不需要手动管理 tokenizer 与图像/视频预处理细节。批处理友好OFA、MPlug、HiTeA 模型族通过 modelscope/pipelines/util.py 的batch_process工具或自定义_batch拼接逻辑将样本级 dict 组装为模型可消费的 batch 输入为批量推理提供基础设施。若需在项目中验证这些管线可直接运行仓库内对应测试例如 tests/pipelines/test_ofa_tasks.py、tests/pipelines/test_mplug_tasks.py、tests/pipelines/test_document_vl_embedding.py想要自定义新任务管线则可以仿照任一 Pipeline 类通过PIPELINES.register_module(Tasks.xxx, module_name...)接入注册表。【免费下载链接】modelscopeModelScope: bring the notion of Model-as-a-Service to life.项目地址: https://gitcode.com/GitHub_Trending/mo/modelscope创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考