• PyTorch 官方库「上新」,TorchMultimodal 助力多模态人工智能


    多模态人工智能是一种新型 AI 范式,是指图像、文本、语音、视频等多种数据类型,与多种智能处理算法相结合,以期实现更高的性能。

    在这里插入图片描述
    近日,PyTorch 官方发布了一个 domain library–TorchMultimodal,用于 SoTA 多任务、多模态模型的大规模训练。

    该库提供了:

    • 可组合的 building block(module、transforms、损失函数)用于加速模型开发

    • 从已发表的研究、训练及评估脚本中提取的 SoTA 模型架构 (FLAVA, MDETR, Omnivore)

    • 用于测试这些模型的 notebook

    TorchMultimodal 库仍在积极开发中,详情请关注:
    https://github.com/facebookresearch/multimodal#installation

    TorchMultimodal 开发背景

    随着技术的进步,能理解多种类型输入(文本、图像、视频和音频信号),并能利用这种理解来生成不同形式的输出(句子、图片、视频)的 AI 模型越来越引发关注。

    FAIR 最近的研究工作(如 FLAVA、 Omnivore 和 data2vec)表明,用于理解的多模态模型与单模态模型相比更有优势,并且在某些情况下正在开创全新的 SOTA。

    类似 Make-a-video 以及 Make-a-scene 这样的生成模型,正在重新定义现代人工智能系统的能力边界。

    为了促进 PyTorch 生态中多模态 AI 的发展, TorchMultimodal 库应运而生,其解决思路为:

    • 提供可组合的 building block, 利用这些 building block,研究人员可以在自己的工作流中加速模型开发和试验。模块化设计也降低了迁移到新模态数据的难度。

    • 提供了用于训练和评估研究中最新模型的端到端示例。 这些示例中用到了一些高阶特性,如集成 FSDP 和用于扩展模型及批尺寸的 activation checkpointing。

    初识 TorchMultimodal

    TorchMultimodal 是一个 PyTorch domain library,用于多任务多模态模型的大规模训练。 它提供:

    1. Building Block

    模块及可组合 building block 集合,如模型、融合层、损失函数、数据集和实用程序,例如:

    • 温度对比损失 (Contrastive Loss with Temperature): 常用于训练模型的函数,如 CLIP 和 FLAVA。此外还包括在 ALBEF 等模型中使用的 ImageTextContrastiveLoss 等变量。

    • Codebook layer: 通过向量空间中的最近邻查找压缩高维数据,它也是 VQVAE 的重要组成部分。

    • Shifted-window Attention: window 基于 multi-head self attention,是 Swin 3D Transformer 等编码器的重要组件。

    • CLIP 组件: 由 OpenAI 发布,是一个在学习文本和图像表征方面非常有效的模型。

    • Multimodal GPT: 与生成程序结合时,可将 OpenAI 的 GPT 架构扩展为更适合多模态生成的抽象。

    • MultiHeadAttention: 基于 attention 的模型的一个关键组件,支持 auto-regressive 和 decoding。

    2. 示例

    一组示例展示了如何将 building block 与 PyTorch 组件和公共基础设施 (Lightning, TorchMetrics) 结合,从而复制文献中发表的 SOTA 模型。目前提供了五个示例,其中包括:

    • FLAVA: CVPR 接收论文的官方代码,包括一个关于 FLAVA 微调的教程。

      查看论文:https://arxiv.org/abs/2112.04482

    • MDETR: 与 NYU 的作者合作提供了一个例子,减轻了 PyTorch 生态系统中互操作性 (interoperability) 痛点,包括一个使用 MDETR 进行 phrase grounding 和可视化问答的 notebook。

      查看论文:https://arxiv.org/abs/2104.12763

    • Omnivore: TorchMultimodal 中处理视频和 3D 数据的模型的第一个例子,包括用于探索模型的 notebook。

      查看论文:https://arxiv.org/abs/2204.08058

    • MUGEN: auto-regressive 生成和检索的基础工作,包括使用 OpenAI coinrun 丰富的大规模合成数据集生成和检索 text-video 的 demo。

      查看论文:https://arxiv.org/abs/2204.08058

    • ALBEF: 模型代码,包括用该模型解决视觉问答问题的 notebook。

      查看论文:https://arxiv.org/abs/2107.07651

    以下代码展示了几个与 CLIP 相关的 TorchMultimodal 组件的用法:

    # instantiate clip transform
    clip_transform = CLIPTransform()
    
    # pass the transform to your dataset. Here we use coco captions
    dataset = CocoCaptions(root= ..., annFile=..., transforms=clip_transform)
    dataloader = DataLoader(dataset, batch_size=16)
    
    # instantiate model. Here we use clip with vit-L as the image encoder
    model= clip_vit_l14()
    
    # define loss and other things needed for training
    clip_loss = ContrastiveLossWithTemperature()
    optim = torch.optim.AdamW(model.parameters(), lr = 1e-5)
    epochs = 1
    
    # write your train loop
    for _ in range(epochs):
      for batch_idx, batch in enumerate(dataloader):
        image, text = batch
        image_embeddings, text_embeddings = model(image, text)
        loss = contrastive_loss_with_temperature(image_embeddings, text_embeddings)
        loss.backward()
        optimizer.step()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    安装 TorchMultimodal

    Python ≥ 3.7,安不安装 CUDA 支持均可。

    以下代码以安装 conda 为例

    前提条件

    1. 安装 conda 环境

    conda create -n torch-multimodal python=\<python_version\>
    conda activate torch-multimodal
    
    • 1
    • 2

    2. 安装 PyTorch、torchvision 以及 torchtext

    参阅 PyTorch 文档:
    https://pytorch.org/get-started/locally/

    # Use the current CUDA version as seen [here](https://pytorch.org/get-started/locally/)
    # Select the nightly Pytorch build, Linux as the OS, and conda. Pick the most recent CUDA version.
    conda install pytorch torchvision torchtext pytorch-cuda=\<cuda_version\> -c pytorch-nightly -c nvidia
    
    # For CPU-only install
    conda install pytorch torchvision torchtext cpuonly -c pytorch-nightly
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    从二进制文件安装

    在 Linux 上,适用于 Python 3.7、3.8 和 3.9 的 Nightly binary 可通过 pip wheels 安装。目前只通过 PyPI 支持 Linux 平台。

    python -m pip install torchmultimodal-nightly
    
    • 1

    源码安装

    开发者也可以通过源码构建并运行示例:

    git clone --recursive https://github.com/facebookresearch/multimodal.git multimodal
    cd multimodal
    
    pip install -e .
    
    • 1
    • 2
    • 3
    • 4

    以上就是关于 TorchMultimodal 的简单介绍。除代码外,PyTorch 官方还发布了一个关于微调多模态模型的基础教程, 以及一篇关于如何使用 PyTorch Distributed PyTorch (FSDP and activation checkpointing) 技术扩展这些模型的 blog。

    后续我们将针对这篇 blog 进行汉化整理。欢迎持续关注 PyTorch 开发者社区公众号!

    —— 完 ——

  • 相关阅读:
    计算机大一新生,想卷却找不到方向,恳请前辈指指路?
    Linux回收内存的时候遇到PageWriteback和PageDirty脏页怎么处理?
    08 | Jackson 注解在实体里面如何应用?常见的死循环问题如何解决?
    【freertos】013-任务通知及其实现细节
    2023-11-rust-struct
    gitea的简单介绍
    Eclipse中集成Tomcat
    java毕业设计融呗智慧金融微资讯移动平台小程序端源码+lw文档+mybatis+系统+mysql数据库+调试
    代码随想录——单词接龙(图论)
    遍历完全二叉树节点
  • 原文地址:https://blog.csdn.net/HyperAI/article/details/127988803