OpenFlamingo大型 LMM 訓(xùn)練框架
OpenFlamingo 的核心是一個(gè)支持大型多模態(tài)模型 (LMM) 訓(xùn)練和評(píng)估的框架,DeepMind 的 Flamingo 模型的開(kāi)源復(fù)制品。
主要包含如下內(nèi)容:
- 一個(gè)用于訓(xùn)練 Flamingo 風(fēng)格 LMM 的 Python 框架(基于 Lucidrains 的 flamingo 實(shí)現(xiàn)和 David Hansmair 的 flamingo-mini 存儲(chǔ)庫(kù))。
- 具有交錯(cuò)圖像和文本序列的大規(guī)模多模態(tài)數(shù)據(jù)集。
- 視覺(jué)語(yǔ)言任務(wù)的上下文學(xué)習(xí)評(píng)估基準(zhǔn)。
- ?? OpenFlamingo-9B 模型(基于 LLaMA )的第一個(gè)版本
OpenFlamingo 架構(gòu)如下圖,使用交叉注意力層來(lái)融合預(yù)訓(xùn)練的視覺(jué)編碼器和語(yǔ)言模型。
安裝
要在現(xiàn)有環(huán)境中安裝包,請(qǐng)運(yùn)行
pip install open-flamingo
或者創(chuàng)建運(yùn)行 OpenFlamingo 的 conda 環(huán)境,運(yùn)行
conda env create -f environment.yml
用法
我們使用 CLIP ViT-Large 視覺(jué)編碼器和 LLaMA-7B 語(yǔ)言模型提供初始OpenFlamingo 9B 模型。一般來(lái)說(shuō),我們支持任何CLIP 視覺(jué)編碼器。對(duì)于語(yǔ)言模型,我們支持LLaMA 、OPT 、GPT-Neo 、GPT-J和Pythia模型。
注意:要使用 LLaMA 模型,您需要通過(guò)以下方式安裝最新版本的變壓器
pip install git+https://github.com/huggingface/transformers
使用此腳本將 LLaMA 權(quán)重轉(zhuǎn)換為 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)
生成文本
這是一個(gè)以交錯(cuò)圖像/文本為條件生成文本的示例,在這種情況下將進(jìn)行少鏡頭圖像字幕。
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 是一種多模態(tài)語(yǔ)言模型,可用于多種任務(wù)。它在大型多模態(tài)數(shù)據(jù)集(例如 Multimodal C4)上進(jìn)行訓(xùn)練,可用于生成以交錯(cuò)圖像/文本為條件的文本。 例如,OpenFlamingo 可用于為圖像生成標(biāo)題,或根據(jù)圖像和文本段落生成問(wèn)題。這種方法的好處是我們能夠使用上下文訓(xùn)練快速適應(yīng)新任務(wù)。
模型架構(gòu)
OpenFlamingo 尋求使用交叉注意力層來(lái)融合預(yù)訓(xùn)練的視覺(jué)編碼器和語(yǔ)言模型。模型架構(gòu)如下圖所示。
