OpenFlamingo大型 LMM 训练框架

联合创作 · 2023-09-25 22:53

OpenFlamingo 的核心是一个支持大型多模态模型 (LMM) 训练和评估的框架,DeepMind 的 Flamingo 模型的开源复制品。


主要包含如下内容:



  • 一个用于训练 Flamingo 风格 LMM 的 Python 框架(基于 Lucidrains 的 flamingo 实现和 David Hansmair 的 flamingo-mini 存储库)。

  • 具有交错图像和文本序列的大规模多模态数据集。

  • 视觉语言任务的上下文学习评估基准。

  • ​​ OpenFlamingo-9B 模型(基于 LLaMA )的第一个版本


OpenFlamingo 架构如下图,使用交叉注意力层来融合预训练的视觉编码器和语言模型。



安装


要在现有环境中安装包,请运行



pip install open-flamingo 

或者创建运行 OpenFlamingo 的 conda 环境,运行



conda env create -f environment.yml 

用法


我们使用 CLIP ViT-Large 视觉编码器和 LLaMA-7B 语言模型提供初始OpenFlamingo 9B 模型。一般来说,我们支持任何CLIP 视觉编码器。对于语言模型,我们支持LLaMA OPT GPT-Neo GPT-JPythia模型。


注意:要使用 LLaMA 模型,您需要通过以下方式安装最新版本的变压器



pip install git+https://github.com/huggingface/transformers 

使用此脚本将 LLaMA 权重转换为 HuggingFace 格式。


初始化 OpenFlamingo 模型



from open_flamingo import create_model_and_transforms

model, image_processor, tokenizer = create_model_and_transforms( clip_vision_encoder_path="ViT-L-14", clip_vision_encoder_pretrained="openai", lang_encoder_path="", tokenizer_path="", cross_attn_every_n_layers=4 )


grab model checkpoint from huggingface hub


from huggingface_hub import hf_hub_download import torch


checkpoint_path = hf_hub_download("openflamingo/OpenFlamingo-9B", "checkpoint.pt") model.load_state_dict(torch.load(checkpoint_path), strict=False)


 


生成文本


这是一个以交错图像/文本为条件生成文本的示例,在这种情况下将进行少镜头图像字幕。



from PIL import Image
import requests

""" Step 1: Load images """ demo_image_one = Image.open( requests.get( "http://images.cocodataset.org/val2017/000000039769.jpg", stream=True ).raw )


demo_image_two = Image.open( requests.get( "http://images.cocodataset.org/test-stuff2017/000000028137.jpg", stream=True ).raw )


query_image = Image.open( requests.get( "http://images.cocodataset.org/test-stuff2017/000000028352.jpg", stream=True ).raw )


""" Step 2: Preprocessing images Details: For OpenFlamingo, we expect the image to be a torch tensor of shape batch_size x num_media x num_frames x channels x height x width. In this case batch_size = 1, num_media = 3, num_frames = 1 (this will always be one expect for video which we don't support yet), channels = 3, height = 224, width = 224. """ vision_x = [image_processor(demo_image_one).unsqueeze(0), image_processor(demo_image_two).unsqueeze(0), image_processor(query_image).unsqueeze(0)] vision_x = torch.cat(vision_x, dim=0) vision_x = vision_x.unsqueeze(1).unsqueeze(0)


""" Step 3: Preprocessing text Details: In the text we expect an special token to indicate where an image is. We also expect an <|endofchunk|> special token to indicate the end of the text portion associated with an image. """ tokenizer.padding_side = "left" # For generation padding tokens should be on the left lang_x = tokenizer( ["An image of two cats.<|endofchunk|>An image of a bathroom sink.<|endofchunk|>An image of"], return_tensors="pt", )


""" Step 4: Generate text """ generated_text = model.generate( vision_x=vision_x, lang_x=lang_x["input_ids"], attention_mask=lang_x["attention_mask"], max_new_tokens=20, num_beams=3, )


print("Generated text: ", tokenizer.decode(generated_text[0]))


 


方法


OpenFlamingo 是一种多模态语言模型,可用于多种任务。它在大型多模态数据集(例如 Multimodal C4)上进行训练,可用于生成以交错图像/文本为条件的文本。 例如,OpenFlamingo 可用于为图像生成标题,或根据图像和文本段落生成问题。这种方法的好处是我们能够使用上下文训练快速适应新任务。


模型架构


OpenFlamingo 寻求使用交叉注意力层来融合预训练的视觉编码器和语言模型。模型架构如下图所示。






浏览 40
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报