LAVIS - 语音-视觉智能一站式库
项目描述
AMMICO-LAVIS
这是LAVIS(版本1.0.2)的一个分支,支持ARM M1、M2和M3 Mac。在MacOS上,它依赖于eva-decord,而不是其他系统上的decord。支持transformers>=4.25.0<4.27。
LAVIS - 语言-视觉智能库
新增内容:🎉
一个简单而有效的跨模态框架,建立在冻结的LLM之上,允许整合各种模态(图像、视频、音频、3D)而无需进行广泛的模态特定定制。
一个比 DreamBooth 训练快20倍的可用于文本到图像生成的模型。同时也促进了零样本主题驱动生成和编辑。
使用 BLIP-2 模型的新视觉-语言指令微调框架,在广泛的视觉-语言任务上实现了最先进的零样本泛化性能。
一个通用的、高效的预训练策略,可以轻松地利用预训练视觉模型和大型语言模型(LLM)的发展来执行视觉-语言预训练。BLIP-2 在零样本 VQAv2 上击败了 Flamingo(65.0 对 56.3),在零样本标题生成(在 NoCaps 上 121.6 CIDEr 分数与之前最佳 113.2)上建立了新的记录。此外,配备强大的LLM(例如OPT、FlanT5),BLIP-2 还解锁了针对各种有趣应用的零样本指令视觉到语言生成的新能力!
- 2023年1月,LAVIS 现已可在 PyPI 上安装!
- [模型发布] 2022年12月,发布了 Img2LLM-VQA 的实现(CVPR 2023,"从图像到文本提示:使用冻结的大型语言模型进行零样本 VQA"(Guo Jiaxian 等人))
论文,项目页面,
一个即插即用的模块,允许即用型使用大型语言模型(LLM)进行视觉问答(VQA)。Img2LLM-VQA 在 VQAv2 上的零样本 VQA 超过了 Flamingo(61.9 对 56.3),而无需进行端到端训练!
- [模型发布] 2022年10月,发布了 PNP-VQA 的实现(EMNLP Findings 2022,"即插即用 VQA:通过结合大型预训练模型和零训练进行零样本 VQA"(Anthony T.M.H. 等人)),
论文,项目页面,)
一个模块化的零样本VQA框架,无需训练PLMs,实现了SOTA零样本VQA性能。
技术报告和引用LAVIS
您可以在我们的技术报告中找到更多详细信息。
如果您在研究或应用中使用LAVIS,请使用此BibTeX引用它:
@inproceedings{li-etal-2023-lavis,
title = "{LAVIS}: A One-stop Library for Language-Vision Intelligence",
author = "Li, Dongxu and
Li, Junnan and
Le, Hung and
Wang, Guangsen and
Savarese, Silvio and
Hoi, Steven C.H.",
booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 3: System Demonstrations)",
month = jul,
year = "2023",
address = "Toronto, Canada",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2023.acl-demo.3",
pages = "31--41",
abstract = "We introduce LAVIS, an open-source deep learning library for LAnguage-VISion research and applications. LAVIS aims to serve as a one-stop comprehensive library that brings recent advancements in the language-vision field accessible for researchers and practitioners, as well as fertilizing future research and development. It features a unified interface to easily access state-of-the-art image-language, video-language models and common datasets. LAVIS supports training, evaluation and benchmarking on a rich variety of tasks, including multimodal classification, retrieval, captioning, visual question answering, dialogue and pre-training. In the meantime, the library is also highly extensible and configurable, facilitating future development and customization. In this technical report, we describe design principles, key components and functionalities of the library, and also present benchmarking results across common language-vision tasks.",
}
目录
引言
LAVIS是一个用于LAnguage-and-VISion智能研究和应用的Python深度学习库。该库旨在为工程师和研究人员提供一站式解决方案,以快速开发针对特定多模态场景的模型,并在标准和定制数据集上对其进行基准测试。它具有统一的接口设计,可访问
- 10+个任务(检索、字幕、视觉问答、多模态分类等);
- 20+个数据集(COCO、Flickr、Nocaps、Conceptual Commons、SBU等);
- 30+个最先进的基础语言-视觉模型的预训练权重及其任务特定调整,包括ALBEF、BLIP、ALPRO、CLIP。
LAVIS的关键特性包括
-
统一和模块化接口:便于轻松利用和重新使用现有模块(数据集、模型、预处理程序),也可以添加新模块。
-
易于现成推理和特征提取:可用的预训练模型让您能够利用最先进的多模态理解和生成能力处理您的数据。
-
可复制的模型库和训练配方:轻松复制和扩展现有任务和新任务上的最先进模型。
-
数据集库和自动下载工具:准备许多语言-视觉数据集可能很麻烦。LAVIS提供自动下载脚本,以帮助准备各种数据集及其注释。
以下表格显示了库中支持的任务、数据集和模型。这是一个持续的工作,我们正在努力扩大列表。
任务 | 支持的模型 | 支持的数据集 |
---|---|---|
图像-文本预训练 | ALBEF, BLIP | COCO, VisualGenome, SBU ConceptualCaptions |
图像-文本检索 | ALBEF, BLIP, CLIP | COCO, Flickr30k |
文本-图像检索 | ALBEF, BLIP, CLIP | COCO, Flickr30k |
视觉问答 | ALBEF, BLIP | VQAv2, OKVQA, A-OKVQA |
图像字幕 | BLIP | COCO, NoCaps |
图像分类 | CLIP | ImageNet |
自然语言视觉推理(NLVR) | ALBEF, BLIP | NLVR2 |
视觉蕴涵(VE) | ALBEF | SNLI-VE |
视觉对话 | BLIP | VisDial |
视频-文本检索 | BLIP, ALPRO | MSRVTT, DiDeMo |
文本-视频检索 | BLIP, ALPRO | MSRVTT, DiDeMo |
视频问答(VideoQA) | BLIP, ALPRO | MSRVTT, MSVD |
视频对话 | VGD-GPT | AVSD |
多模态特征提取 | ALBEF, CLIP, BLIP, ALPRO | 定制 |
文本到图像生成 | (即将推出) |
安装
- (可选)创建conda环境
conda create -n lavis python=3.8
conda activate lavis
- 从PyPI安装
pip install salesforce-lavis
- 或者,对于开发,您可以从源代码构建
git clone https://github.com/salesforce/LAVIS.git
cd LAVIS
pip install -e .
入门
模型库
模型库总结了LAVIS中支持的模型,要查看
from lavis.models import model_zoo
print(model_zoo)
# ==================================================
# Architectures Types
# ==================================================
# albef_classification ve
# albef_feature_extractor base
# albef_nlvr nlvr
# albef_pretrain base
# albef_retrieval coco, flickr
# albef_vqa vqav2
# alpro_qa msrvtt, msvd
# alpro_retrieval msrvtt, didemo
# blip_caption base_coco, large_coco
# blip_classification base
# blip_feature_extractor base
# blip_nlvr nlvr
# blip_pretrain base
# blip_retrieval coco, flickr
# blip_vqa vqav2, okvqa, aokvqa
# clip_feature_extractor ViT-B-32, ViT-B-16, ViT-L-14, ViT-L-14-336, RN50
# clip ViT-B-32, ViT-B-16, ViT-L-14, ViT-L-14-336, RN50
# gpt_dialogue base
让我们看看如何使用LAVIS中的模型对示例数据进行推理。我们首先从本地加载一个示例图像。
import torch
from PIL import Image
# setup device to use
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# load sample image
raw_image = Image.open("docs/_static/merlion.png").convert("RGB")
本示例图像展示了新加坡的地标建筑——鱼尾狮公园(《来源》:来源)。
图像字幕
在此示例中,我们使用BLIP模型为图像生成标题。为了使推理更加简单,我们还通过load_model_and_preprocess()
将每个预训练模型与其预处理器(变换)关联。
import torch
from lavis.models import load_model_and_preprocess
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# loads BLIP caption base model, with finetuned checkpoints on MSCOCO captioning dataset.
# this also loads the associated image processors
model, vis_processors, _ = load_model_and_preprocess(name="blip_caption", model_type="base_coco", is_eval=True, device=device)
# preprocess the image
# vis_processors stores image transforms for "train" and "eval" (validation / testing / inference)
image = vis_processors["eval"](raw_image).unsqueeze(0).to(device)
# generate caption
model.generate({"image": image})
# ['a large fountain spewing water into the air']
视觉问答(VQA)
BLIP模型能够用自然语言回答关于图像的自由形式问题。要访问VQA模型,只需替换传递给load_model_and_preprocess()
的name
和model_type
参数。
from lavis.models import load_model_and_preprocess
model, vis_processors, txt_processors = load_model_and_preprocess(name="blip_vqa", model_type="vqav2", is_eval=True, device=device)
# ask a random question.
question = "Which city is this photo taken?"
image = vis_processors["eval"](raw_image).unsqueeze(0).to(device)
question = txt_processors["eval"](question)
model.predict_answers(samples={"image": image, "text_input": question}, inference_method="generate")
# ['singapore']
统一特征提取接口
LAVIS提供了一个统一的接口,用于从每个架构中提取特征。为了提取特征,我们加载每个模型的特征提取器变体。多模态特征可用于多模态分类。低维单模态特征可用于计算跨模态相似度。
from lavis.models import load_model_and_preprocess
model, vis_processors, txt_processors = load_model_and_preprocess(name="blip_feature_extractor", model_type="base", is_eval=True, device=device)
caption = "a large fountain spewing water into the air"
image = vis_processors["eval"](raw_image).unsqueeze(0).to(device)
text_input = txt_processors["eval"](caption)
sample = {"image": image, "text_input": [text_input]}
features_multimodal = model.extract_features(sample)
print(features_multimodal.multimodal_embeds.shape)
# torch.Size([1, 12, 768]), use features_multimodal[:,0,:] for multimodal classification tasks
features_image = model.extract_features(sample, mode="image")
features_text = model.extract_features(sample, mode="text")
print(features_image.image_embeds.shape)
# torch.Size([1, 197, 768])
print(features_text.text_embeds.shape)
# torch.Size([1, 12, 768])
# low-dimensional projected features
print(features_image.image_embeds_proj.shape)
# torch.Size([1, 197, 256])
print(features_text.text_embeds_proj.shape)
# torch.Size([1, 12, 256])
similarity = features_image.image_embeds_proj[:,0,:] @ features_text.text_embeds_proj[:,0,:].t()
print(similarity)
# tensor([[0.2622]])
加载数据集
LAVIS通过提供自动下载工具来支持下载和整理各种常见的语言-视觉数据集。下载后,使用以下代码加载数据集:
from lavis.datasets.builders import dataset_zoo
dataset_names = dataset_zoo.get_names()
print(dataset_names)
# ['aok_vqa', 'coco_caption', 'coco_retrieval', 'coco_vqa', 'conceptual_caption_12m',
# 'conceptual_caption_3m', 'didemo_retrieval', 'flickr30k', 'imagenet', 'laion2B_multi',
# 'msrvtt_caption', 'msrvtt_qa', 'msrvtt_retrieval', 'msvd_caption', 'msvd_qa', 'nlvr',
# 'nocaps', 'ok_vqa', 'sbu_caption', 'snli_ve', 'vatex_caption', 'vg_caption', 'vg_vqa']
下载图像后,我们可以使用load_dataset()
获取数据集。
from lavis.datasets.builders import load_dataset
coco_dataset = load_dataset("coco_caption")
print(coco_dataset.keys())
# dict_keys(['train', 'val', 'test'])
print(len(coco_dataset["train"]))
# 566747
print(coco_dataset["train"][0])
# {'image': <PIL.Image.Image image mode=RGB size=640x480>,
# 'text_input': 'A woman wearing a net on her head cutting a cake. ',
# 'image_id': 0}
如果您已经托管了数据集的本地副本,您可以通过传递vis_path
参数来更改默认的图像加载位置。
coco_dataset = load_dataset("coco_caption", vis_path=YOUR_LOCAL_PATH)
Jupyter Notebook 示例
请参阅示例以获取更多推理示例,例如标题生成、特征提取、VQA、GradCam、零样本分类。
资源和工具
- 基准测试:请参阅基准测试以获取评估和训练支持模型的说明。
- 数据集下载和浏览:请参阅数据集下载以获取有关下载常见语言-视觉数据集的说明和自动工具。
- GUI演示:要本地运行演示,请运行
bash run_scripts/run_demo.sh
,然后按照提示在浏览器中查看。Web演示即将推出。
文档
有关更多详细信息和高阶用法,请参阅文档。
道德和负责任的使用
请注意,LAVIS中的模型对其多模态能力不提供保证;可能会观察到错误或具有偏见的预测。特别是,LAVIS中使用的 datasets 和预训练模型可能包含社会经济偏见,可能导致错误分类和其他不受欢迎的行为,如冒犯性或不恰当的言论。我们强烈建议用户在采用前先审查LAVIS中的预训练模型和整个系统。我们计划通过调查和减轻这些潜在偏见和不适当行为来改进库。
联系我们
如果您有任何问题、评论或建议,请随时通过lavis@salesforce.com与我们联系。