VLM2Vec-Full / README.md
wenhu's picture
Update README.md
c326e8a verified
|
raw
history blame
4.17 kB
metadata
license: apache-2.0
datasets:
  - TIGER-Lab/MMEB-train
language:
  - en
metrics:
  - accuracy
base_model:
  - microsoft/Phi-3.5-vision-instruct
library_name: transformers
tags:
  - Embedding

VLM2Vec

This repo contains the code and data for VLM2Vec: Training Vision-Language Models for Massive Multimodal Embedding Tasks. In this paper, we aimed at building a unified multimodal embedding model for any tasks. Our model is based on converting an existing well-trained VLM (Phi-3.5-V) into an embedding model. The basic idea is to add an [EOS] token in the end of the sequence, which will be used as the representation of the multimodal inputs.

abs

Release

Our model is being trained on MMEB-train and evaluated on MMEB-eval with contrastive learning. We only use in-batch negatives for training. Our best results were based on Lora training with batch size of 1024. We also have checkpoint with full training with batch size of 2048. Our results on 36 evaluation datasets are:

Train/Eval Data

VLM2Vec Checkpoints

Github

Experimental Results

Our model can outperform the existing baselines by a huge margin. abs

How to use VLM2Vec

First you can clone our github

git clone https://github.com/TIGER-AI-Lab/VLM2Vec.git

Then you can enter the directory to run the following command.

from src.model import MMEBModel
from src.arguments import ModelArguments
import torch
from transformers import HfArgumentParser, AutoProcessor
from PIL import Image
import numpy as np

model_args = ModelArguments(
    model_name='microsoft/Phi-3.5-vision-instruct', 
    pooling='last',
    normalize=True,
    lora=True,
    checkpoint_path='TIGER-Lab/VLM2Vec-LoRA')

model = MMEBModel.load(model_args)
model.eval()
model = model.to('cuda', dtype=torch.bfloat16)

processor = AutoProcessor.from_pretrained(
    model_args.model_name,
    trust_remote_code=True,
    num_crops=4,
)

inputs = processor(
    '<|image_1|> Represent the given image with the following question: What is in the image',
    [Image.open('figures/example.jpg')])
inputs = {key: value.to('cuda') for key, value in inputs.items()}
qry_output = model(qry=inputs)["qry_reps"]

# Compute the similarity;
string = 'A cat and a dog'
inputs = processor(string, None, return_tensors="pt")
inputs = {key: value.to('cuda') for key, value in inputs.items()}
tgt_output = model(tgt=inputs)["tgt_reps"]
print(string, '=', model.compute_similarity(qry_output, tgt_output))

string = 'A cat and a tiger'
inputs = processor(string, None, return_tensors="pt")
inputs = {key: value.to('cuda') for key, value in inputs.items()}
tgt_output = model(tgt=inputs)["tgt_reps"]
print(string, '=', model.compute_similarity(qry_output, tgt_output))

string = 'A pig'
inputs = processor(string, None, return_tensors="pt")
inputs = {key: value.to('cuda') for key, value in inputs.items()}
tgt_output = model(tgt=inputs)["tgt_reps"]
print(string, '=', model.compute_similarity(qry_output, tgt_output))

string = 'a flight'
inputs = processor(string, None, return_tensors="pt")
inputs = {key: value.to('cuda') for key, value in inputs.items()}
tgt_output = model(tgt=inputs)["tgt_reps"]
print(string, '=', model.compute_similarity(qry_output, tgt_output))

Citation

@article{jiang2024vlm2vec,
  title={VLM2Vec: Training Vision-Language Models for Massive Multimodal Embedding Tasks},
  author={Jiang, Ziyan and Meng, Rui and Yang, Xinyi and Yavuz, Semih and Zhou, Yingbo and Chen, Wenhu},
  journal={arXiv preprint arXiv:2410.05160},
  year={2024}
}