OpenFlamingo 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
OpenFlamingo 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
OpenFlamingo 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 未知
开发语言 Python
操作系统 跨平台
软件类型 开源软件
开源组织
地区 不详
投 递 者 罗奇奇
适用人群 未知
收录时间 2023-03-30

软件简介

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 寻求使用交叉注意力层来融合预训练的视觉编码器和语言模型。模型架构如下图所示。

展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣 发布并加入讨论🔥
发表了资讯
03/30 12:53

LAION-AI 推出 OpenFlamingo,有望成为 GPT-4 开源替代品

非营利组织 LAION-AI 发布了 OpenFlamingo,这是一个用于训练和评估大型多模态模型 (LMM)的框架,属于 DeepMind 的 Flamingo 模型(一种能够处理和推理图像、视频和文本的等多模态内容的框架)的开源复制品。 其数据集 OpenFlamingo-9B 的 Demo 页面展示了训练结果,用户可以上传图片让该模型进行识别。 小编浅试了一下,一张简单的图片耗时 11 秒,可以相当精准地描述图片的主体: 对于另一张内容较多的图片,识别的时间则上到...

0
5
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
6 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部