BLIP3技术小结(xGen-MM (BLIP-3): A Family of Open Large Multimodal Models)

paperhttps://www.arxiv.org/abs/2408.08872
githubhttps://github.com/salesforce/LAVIS/tree/xgen-mm
Org.Salesforce AI Research
个人博客地址http://myhz0606.com/article/blip3

前置阅读:BLIP系列总结

核心思路

虽然过去BLIP系列对LMM发展起到至关重要的作用,但从效果上来说,已经远落后于当下的SOTA模型,主要有一下3点原因:
1)数据上,训练数据数量少、质量不高、多样性不强。
2)训练策略上,多个stage(ITMITCITG)训练流程冗长,up scale的训练开销大
3)模型架构上,BLIP系列仅支持单图输入,应用范围相对较窄

BLIP3针对以上3个方面进行改进:
1)数据上,构造了更大的、质量更高、多样性更强的数据集。
2)训练策略上,提出3 stage 的训练范式,并统一用next token prediction作为训练目标目标,提升训练效率和模型效果。
3)模型架构上,支持交错图文输入。

在这里插入图片描述

方法

模型架构

多模态两种主流架构:

  • cross-attention style;通过给LLM引入交叉注意力机制,融合不同模态的信息;代表方法:FlamingoLlama 3.1
  • decoder-only style;通过VL-connector,将visual token转为LLM可以“理解”的token,无需改动LLM的架构。代表方法:BLIP-2LLAVAQwen-vlPali-xKosmos-2phi3-vision

BLIP3也是decoder-only style。区别于早先的BLIP2BLIP3支持交错式的图文输入。

训练阶段:图片用visual tokenizer转为visual token,文本用text tokenizer转为text token,最后按序拼接起来送入LLM,用causal mask做并行,仅对文本token处计算自回归损失。

推理阶段:图片用visual tokenizer转为visual token,文本用text tokenizer转为text token,最后按序拼接起来送入LLM,按照next token prediction的范式做生成。

BLIP3架构只能解决多模态图文交错输入,单模态文本输出。

在这里插入图片描述

BLIP3的架构包含3部分

image transforme(image encoder)

这个部分不参与训练,所用模型为SigLIPViT-SO400M-14-SigLIP-384),用于提取图片embedding。

VL-Connector

BLIP3中没有沿用BLIP2QFormer,而是用了FlamingoPerceiver Resampler。二者核心思路其实都差不多——以learnable queries的方式,将image encoder提取的image embedding转为固定长度的image token。

Perceiver Reampler的核心计算逻辑如图所示:

代码位置:https://github.com/salesforce/LAVIS/blob/xgen-mm/open_flamingo/src/helpers.py#L105

在这里插入图片描述

LLM

BLIP3中,LLM也参与训练。所用的LLMphi3-mini

Any-Resolution Vision Token Sampling

为了强化模型对细粒度信息的捕获能力。BLIP3也引入了Llava next中的Any-Resolution Vision Token Sampling 策略,具体过程如下:

step1: 找到最优分辨率

预设了一些模版,通过下面的目标找到输入图片最适合的分辨率

O b j e c t i o n : A r g min ⁡ t ( w a s t e d _ r e s o l u t i o n ) , t = 1 , 2 , ⋯ N \begin{aligned} &\mathrm{Objection:} \mathrm{Arg} \min_{t} (\mathrm{wasted\_resolution}), t=1,2, \cdots N \\ \end{aligned} Objection:Argtmin(wasted_resolution),t=1,2,N

其中t为模版的索引,一共有 N N N个预设模版

r t = min ⁡ ( w t w o r i , h t h o r i ) w a s t e d _ r e s o l u t i o n = w t ∗ h t − min ⁡ ( w o r i ∗ h o r i , I N T ( w o r i ∗ r t ) ∗ I N T ( h o r i ∗ r t ) ) \begin{aligned} &r_{t} = \min( \frac{w_t} {w_{\rm ori}}, \frac{h_t} {h_{\rm ori}}) \\ &\mathrm{wasted\_resolution} = w_t * h_t - \min (w_{\rm ori} * h_{\rm ori}, \mathrm{INT}(w_{\rm ori} * r_{t}) * \mathrm{INT} (h_{\rm ori} * r_{t}) ) \\\end{aligned} rt=min(woriwt,horiht)wasted_resolution=wthtmin(worihori,INT(worirt)INT(horirt))


possible_resolutions = [[384, 768], [768, 384], [768, 768], [1152, 384], [384, 1152]]

def select_best_resolution(original_size, possible_resolutions):
    """
    Args:
        original_size (tuple): The original size of the image in the format (width, height).
        possible_resolutions (list): A list of possible resolutions in the format [(width1, height1), (width2, height2), ...].

    Returns:
        tuple: The best fit resolution in the format (width, height).
    """
    original_width, original_height = original_size
    best_fit = None
    max_effective_resolution = 0
    min_wasted_resolution = float('inf')

    for width, height in possible_resolutions:
        scale = min(width / original_width, height / original_height)
        downscaled_width, downscaled_height = int(original_width * scale), int(original_height * scale)
        effective_resolution = min(downscaled_width * downscaled_height, original_width * original_height)
        wasted_resolution = (width * height) - effective_resolution

        if effective_resolution > max_effective_resolution or (effective_resolution == max_effective_resolution and wasted_resolution < min_wasted_resolution):
            max_effective_resolution = effective_resolution
            min_wasted_resolution = wasted_resolution
            best_fit = (width, height)

    return best_fi

step2: 切分patch

每个patch的size为vision transformer的输入size,blip3所用的ViT-SO400M-14-SigLIP-384 的输入size为(384x384)。假设输入图片所映射的模板size为(768x768),将图片resize到(768x768)后进行切分,得到5个patch,4个切分patch加上一个包含全局信息的patch,如下图所示。

在这里插入图片描述

step3: 计算每个patch的image embedding。

一个384x384的图片经过ViT-SO400M-14-SigLIP-384后得到576个token ( 384 / 16 ) 2 (384/16)^2 384/16)2

在这里插入图片描述

step4: 分别提取每个patch的vision token再拼接

从下图可见,不同模版分辨率的image token是不同的。通常来说,token数越多,包含的细粒度信息就越多。对于下游感知密集型的推理任务会有更大优势(如DocQA)。

在这里插入图片描述

训练recipe

training-stage训练模块训练目标datasetdataset size训练时长是否采用any-resolution Vision Token Sampling 的策略
stage1: pre-trainingVL connector, LLMnext token prediction交错图文数据,caption类数据-100 billion multi-modal token
stage2-part1. 常规instruct-following SFTVL connector, LLMnext token prediction开源数据:1)多模态会话
2) image caption;3)VQA;4) document QA;5)science and math understand等1M1 epoch
stage2-part2. multi-image instruction tuningVL connector, LLMnext token predictionMANTIS,Mmdu,及stage 2.1中的交错图文数据--
post-training—Improving Truthfulness by Direct Preference OptimizationLLM-backbone Lora 2.5%参数。DPOVLFeedback62.6k1 epoch
post-training—Improving Harmlessness by Safety Fine-tuningLLM-backbone Lora 2.5%参数next token predictionVLGuard-2k,5k additional examples from the instruction fine-tuning dataset2k3 epoch

从模型架构来看,BLIP3需要训练模块有两个:1)VL-connector (perceiver sampler);2) LLM

BLIP3的训练分为了3个stage

  • pre-training
  • SFT
  • post-training

stage1: pre-training

在这里插入图片描述

数据集格式如:

在这里插入图片描述

stage1训练了约100 billion 多模态token。此外,stage1没有用Any-Resolution Vision Token Sampling的策略。

简单介绍BLIP3自建的3个数据集

(一) BLIP3-KALE

论文暂时未给出细节。

论文原话:Details will be discussed in another paper, and the dataset will be made public very soon

(二) BLIP3-OCR-200M

作者从Datacomp-1B中搜集了200M张高分辨率图片。用paddleOCR提取里面的文字信息。虽然BLIP3中并没有用到文本的bounding box,作者说可以尝试,会提升OCR QA类任务的效果。

在这里插入图片描述

(三) BLIP3-GROUNDING-50M

作者从Datacomp-1B中搜集50M图片(图文信息都要)。用开源的open-set目标检测模型Grounding-DINORecognize Anything进行识别。将caption中的对应的object替换为包含位置信息的object。

在这里插入图片描述

stage2: SFT

part1. 常规instruct-following SFT

作者搜集了一批开源数据集,从里面挑选了1M数据用Any-Resolution Vision Token Sampling的策略微调模型1个epoch。开源数据涉及的领域包括:

  • 多模态会话
  • image caption
  • VQA
  • document QA
  • science and math understand等

part2. multi-image instruction tuning

为了提升模型对多图交错图文场景的理解能力,作者用多图交错图文数据集MANTISMmdu结合2.1的单图交错图片数据对模型进行进一步微调。

stage3: post-training

stage3主要是为了提升模型的helpfulness,harmlessness

part1. 通过DPO提升模型的Truthfulness

训练数据:该阶段利用了开源的VLFeedback数据集的指令,VLFeedback是一个用GPT4-v构造多模态偏好数据集,总计80K。构造方式:给定指令,让多个VLM模型做生成,随后GPT4-v从helpfulness, visual faithfulness, and ethics对生成的结果进行打分。分值高的输出作为preferred responses,分值低的输出作为dispreferred responses。BLIP3进一步过滤掉首选响应得分较低的sample,最终得到62.6K数据。

训练方式BLIP3采用DPO作为训练目标,用LORA微调LLM 2.5%参数,总计训练1个epoch。

part2. 通过Safty-SFT提升模型的Harmlessness

训练数据:用VLGuard数据集+随机5K SFT数据集对BLIP3再次进行微调,在保留helpfulness的同时提升harmlessness。

训练方式LORA微调LLM 2.5%参数

结果

Pre-Training模型的few-shot能力

在这里插入图片描述

SFT 模型评估

单图benchmark评估

在这里插入图片描述

多图benchmark评估

在这里插入图片描述

在这里插入图片描述

Post-training 模型评估

在这里插入图片描述

一些关键的消融实验

Pre-Training阶段消融实验

scaling pre-training data

caption类任务在训练token数达到60B后精度增长放缓;相对复杂的text VQA类任务精度还有较大提升。

在这里插入图片描述

visual backbone & number of visual token

SigLip模型作为backbone有较大的优势,尤其在OCR任务上。

在这里插入图片描述

作者方向将visual token从128降到64后,精度并没有明显下降。

在这里插入图片描述

SFT训练阶段消融实验

Any-Resolution Vision Token Sampling

base resolution pipeline

在这里插入图片描述

anyres-fixed-sampling pipeline

在这里插入图片描述

anyres-patch-sampling pipeline

在这里插入图片描述

从消融实验看,Any-Resolution Vision Token Sampling 对强感知类任务增益很大。

在这里插入图片描述

小结

BLIP3从dataset curation, training recipe, model architectures对BLIP2进行了一系列改进,使BLIP系列达到目前开源SOTA水平。

### Hugging Face BLIP Model Usage The BLIP (Bootstrap Language-Image Pre-training) model from Salesforce is a powerful tool designed to understand both images and text, making it suitable for tasks like image captioning, visual question answering, and more[^1]. To use the BLIP model via Hugging Face&#39;s Transformers library, one must first install necessary packages. #### Installation of Required Libraries To begin with, ensure that `transformers` and `torchvision` are installed within your Python environment: ```bash pip install transformers torchvision ``` After installation, proceed by importing required modules and loading the pre-trained model along with its tokenizer or processor specifically tailored for handling multimodal data involving images and texts. #### Loading the Model and Processor For utilizing BLIP effectively through Hugging Face’s API, import specific classes provided under the `transformers` package as shown below: ```python from PIL import Image import requests from transformers import BlipProcessor, BlipForConditionalGeneration processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") ``` With these components ready, prepare an input sample consisting of either local file paths pointing towards images or URLs where images can be fetched online. Then process this input using the previously instantiated processor object before feeding into the model for inference purposes. #### Performing Inference on Images Once everything has been set up correctly, perform operations such as generating captions based on given images easily: ```python url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) inputs = processor(images=image, return_tensors="pt") generated_ids = model.generate(**inputs) captions = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] print(captions) ``` This code snippet demonstrates how straightforwardly one could leverage BLIP alongside Hugging Face APIs to generate meaningful descriptions about unseen pictures automatically without much effort involved in coding complex algorithms manually. --related questions-- 1. What other models does Hugging Face offer besides BLIP? 2. How do I fine-tune the BLIP model for custom datasets? 3. Can you provide examples of applications built using BLIP beyond image captioning? 4. Is there any difference between base and large versions of the BLIP model when performing inference?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值