2024-3-4 🚀🚀 We have released the BGE-VL-MLLM models on Huggingface: BGE-VL-MLLM-S1 and BGE-VL-MLLM-S2. BGE-VL-MLLM-S1 is trained exclusively on our MegaPairs dataset, achieving outstanding performance in composed image retrieval, with an 8.1% improvement on the CIRCO benchmark (mAP@5) over the previous state-of-the-art. BGE-VL-MLLM-S2 builds on BGE-VL-MLLM-S1 with an additional epoch of fine-tuning on the MMEB benchmark training set, delivering enhanced performance across a broader range of multimodal embedding tasks.
In this work, we introduce MegaPairs, a novel data synthesis method that leverages open-domain images to create heterogeneous KNN triplets for universal multimodal retrieval. Our MegaPairs dataset contains over 26 million triplets, and we have trained a series of multimodal retrieval models, BGE-VL, including BGE-VL-CLIP (base and large) and BGE-VL-MLLM.
BGE-VL achieve state-of-the-art performance on four popular zero-shot composed image retrieval benchmarks and the massive multimodal embedding benchmark (MMEB). Extensive experiments demonstrate the efficiency, scalability, and generalization features of MegaPairs. Please refer to our paper for more details.
Model Usage
Using Sentence Transformers
Install Sentence Transformers:
pip install sentence_transformers[image]
python
1from sentence_transformers import SentenceTransformer
23model = SentenceTransformer("BAAI/BGE-VL-base", trust_remote_code=True)45query_image ="https://huggingface.co/BAAI/BGE-VL-base/resolve/main/assets/cir_query.png"6candidate_1 ="https://huggingface.co/BAAI/BGE-VL-base/resolve/main/assets/cir_candi_1.png"7candidate_2 ="https://huggingface.co/BAAI/BGE-VL-base/resolve/main/assets/cir_candi_2.png"89# Encode text10text_embeddings = model.encode(["A dog sitting on a bench","A cat sleeping on a couch"])11print(text_embeddings.shape)12# (2, 512)1314# Encode images15image_embeddings = model.encode([query_image, candidate_1])16print(image_embeddings.shape)17# (2, 512)1819# Compute similarities20similarities = model.similarity(text_embeddings, image_embeddings)21print(similarities)22# tensor([[0.1050, 0.0871],23# [0.0010, 0.0355]])2425# Composed image retrieval: encode image+text query, compare with image candidates26query_embeddings = model.encode([{27"image": query_image,28"text":"Make the background dark, as if the camera has taken the photo at night",29}])30candidate_embeddings = model.encode([candidate_1, candidate_2])31scores = model.similarity(query_embeddings, candidate_embeddings)32print(scores)33# tensor([[0.2645, 0.1251]])
You can pass string texts, images as PIL Images, local paths, URLs, or a combination of text and images (with a dictionary format) to the model's encode function. The model will automatically process the inputs and return the corresponding embeddings. You can then compute cosine similarities or perform retrieval tasks based on these embeddings.
Using transformers
You can easily use BGE-VL-CLIP models based on transformers
python
1import torch
2from transformers import AutoModel
34MODEL_NAME ="BAAI/BGE-VL-base"# or "BAAI/BGE-VL-large"56model = AutoModel.from_pretrained(MODEL_NAME, trust_remote_code=True)# You must set trust_remote_code=True7model.set_processor(MODEL_NAME)8model.eval()910with torch.no_grad():11 query = model.encode(12 images ="./assets/cir_query.png",13 text ="Make the background dark, as if the camera has taken the photo at night"14)1516 candidates = model.encode(17 images =["./assets/cir_candi_1.png","./assets/cir_candi_2.png"]18)1920 scores = query @ candidates.T
21print(scores)
See the demo for a complete example of using BGE-VL for multimodel retrieval.
2. BGE-VL-MLLM Models
python
1import torch
2from transformers import AutoModel
3from PIL import Image
45MODEL_NAME="BAAI/BGE-VL-MLLM-S1"67model = AutoModel.from_pretrained(MODEL_NAME, trust_remote_code=True)8model.eval()9model.cuda()1011with torch.no_grad():12 model.set_processor(MODEL_NAME)1314 query_inputs = model.data_process(15 text="Make the background dark, as if the camera has taken the photo at night",16 images="./assets/cir_query.png",17 q_or_c="q",18 task_instruction="Retrieve the target image that best meets the combined criteria by using both the provided image and the image retrieval instructions: "19)2021 candidate_inputs = model.data_process(22 images=["./assets/cir_candi_1.png","./assets/cir_candi_2.png"],23 q_or_c="c",24)2526 query_embs = model(**query_inputs, output_hidden_states=True)[:,-1,:]27 candi_embs = model(**candidate_inputs, output_hidden_states=True)[:,-1,:]2829 query_embs = torch.nn.functional.normalize(query_embs, dim=-1)30 candi_embs = torch.nn.functional.normalize(candi_embs, dim=-1)3132 scores = torch.matmul(query_embs, candi_embs.T)33print(scores)
Model Performance
Zero-Shot Composed Image Retrieval
BGE-VL sets a new performance benchmark in zero-shot composed image retrieval tasks. On the CIRCO benchmark, our BGE-VL-base model, with only 149 million parameters, surpasses all previous models, including those with 50 times more parameters. Additionally, BGE-VL-MLLM achieves an 8.1% improvement over the previous state-of-the-art model.
Zero-Shot Performance on MMEB
BGE-VL-MLLM achieves state-of-the-art zero-shot performance on the Massive Multimodal Embedding Benchmark (MMEB), despite being trained only on the ImageText-to-Image paradigm. This demonstrates the excellent generalization capability of MegaPairs for multimodal embedding.
Fine-Tuning Performance on MMEB
After fine-tuning on downstream tasks, BGE-VL-MLLM maintains its leading performance. Notably, it surpasses the previous state-of-the-art by 7.1% on the MMEB out-of-distribution (OOD) set. These results demonstrate the robust generalization capability of BGE-VL-MLLM and highlight the potential of MegaPairs as foundational training data for universal multimodal embedding.
Performance Scaling
MegaPairs showcases scalability: BGE-VL-base improves as training data increases. It also demonstrates efficiency: with just 0.5M training samples, BGE-VL-base significantly outperforms MagicLens, which uses the same CLIP-base backbone and was trained on 36.7M samples.
License
The annotations for MegaPairs and the BGE-VL models are released under the MIT License. The images in MegaPairs originate from the Recap-Datacomp, which is released under the CC BY 4.0 license.
Citation
If you find this repository useful, please consider giving a star ⭐ and citation
@article{zhou2024megapairs,
title={MegaPairs: Massive Data Synthesis For Universal Multimodal Retrieval},
author={Zhou, Junjie and Liu, Zheng and Liu, Ze and Xiao, Shitao and Wang, Yueze and Zhao, Bo and Zhang, Chen Jason and Lian, Defu and Xiong, Yongping},
journal={arXiv preprint arXiv:2412.14475},
year={2024}
}