Views
No views yet

GME-Qwen2VL series of unified multimodal embedding models,
which are based on the advanced Qwen2-VL multimodal large language models (MLLMs).GME models support three types of input: text, image, and image-text pair, all of which can produce universal vector representations and have powerful retrieval performance.Qwen2-VL and our training data, GME models support dynamic resolution image input.| Models | Model Size | Max Seq. Length | Dimension | MTEB-en | MTEB-zh | UMRB |
|---|---|---|---|---|---|---|
gme-Qwen2-VL-2B | 2.21B | 32768 | 1536 | 65.27 | 66.92 | 64.45 |
gme-Qwen2-VL-7B | 8.29B | 32768 | 3584 | 67.48 | 69.73 | 67.44 |
transformers>=4.52.0, please downgrade or use sentence_transformers1from transformers import AutoModel
2from transformers.utils.versions import require_version
3
4
5require_version(
6 "transformers<4.52.0",
7 "The remote code has some issues with transformers>=4.52.0, please downgrade: pip install transformers==4.51.3"
8)
9
10
11t2i_prompt = 'Find an image that matches the given text.'
12texts = [
13 "The Tesla Cybertruck is a battery electric pickup truck built by Tesla, Inc. since 2023.",
14 "Alibaba office.",
15]
16images = [
17 'https://upload.wikimedia.org/wikipedia/commons/e/e9/Tesla_Cybertruck_damaged_window.jpg',
18 'https://upload.wikimedia.org/wikipedia/commons/e/e0/TaobaoCity_Alibaba_Xixi_Park.jpg',
19]
20
21
22gme = AutoModel.from_pretrained(
23 "Alibaba-NLP/gme-Qwen2-VL-2B-Instruct",
24 torch_dtype="float16", device_map='cuda', trust_remote_code=True
25)
26
27
28# Single-modal embedding
29e_text = gme.get_text_embeddings(texts=texts)
30e_image = gme.get_image_embeddings(images=images)
31print('Single-modal', (e_text @ e_image.T).tolist())
32## Single-modal [[0.359619140625, 0.0655517578125], [0.04180908203125, 0.374755859375]]
33
34# How to set embedding instruction
35e_query = gme.get_text_embeddings(texts=texts, instruction=t2i_prompt)
36# If is_query=False, we always use the default instruction.
37e_corpus = gme.get_image_embeddings(images=images, is_query=False)
38print('Single-modal with instruction', (e_query @ e_corpus.T).tolist())
39## Single-modal with instruction [[0.429931640625, 0.11505126953125], [0.049835205078125, 0.409423828125]]
40
41# Fused-modal embedding
42e_fused = gme.get_fused_embeddings(texts=texts, images=images)
43print('Fused-modal', (e_fused @ e_fused.T).tolist())
44## Fused-modal [[1.0, 0.05511474609375], [0.05511474609375, 1.0]]encode function accept str or dict with key(s) in {'text', 'image', 'prompt'}.prompt as the argument to encode, pass as the input as a dict with a prompt key.1from sentence_transformers import SentenceTransformer
2
3
4t2i_prompt = 'Find an image that matches the given text.'
5texts = [
6 "The Tesla Cybertruck is a battery electric pickup truck built by Tesla, Inc. since 2023.",
7 "Alibaba office.",
8]
9images = [
10 'https://upload.wikimedia.org/wikipedia/commons/e/e9/Tesla_Cybertruck_damaged_window.jpg',
11 'https://upload.wikimedia.org/wikipedia/commons/e/e0/TaobaoCity_Alibaba_Xixi_Park.jpg',
12]
13
14
15gme_st = SentenceTransformer("Alibaba-NLP/gme-Qwen2-VL-2B-Instruct")
16
17# Single-modal embedding
18e_text = gme_st.encode(texts, convert_to_tensor=True)
19e_image = gme_st.encode([dict(image=i) for i in images], convert_to_tensor=True)
20print('Single-modal', (e_text @ e_image.T).tolist())
21## Single-modal [[0.356201171875, 0.06536865234375], [0.041717529296875, 0.37890625]]
22
23# How to set embedding instruction
24e_query = gme_st.encode([dict(text=t, prompt=t2i_prompt) for t in texts], convert_to_tensor=True)
25# If no prompt, we always use the default instruction.
26e_corpus = gme_st.encode([dict(image=i) for i in images], convert_to_tensor=True)
27print('Single-modal with instruction', (e_query @ e_corpus.T).tolist())
28## Single-modal with instruction [[0.425537109375, 0.1158447265625], [0.049835205078125, 0.413818359375]]
29
30# Fused-modal embedding
31e_fused = gme_st.encode([dict(text=t, image=i) for t, i in zip(texts, images)], convert_to_tensor=True)
32print('Fused-modal', (e_fused @ e_fused.T).tolist())
33## Fused-modal [[0.99951171875, 0.0556640625], [0.0556640625, 0.99951171875]]| Single-modal | Cross-modal | Fused-modal | Avg. | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| T→T (16) | I→I (1) | T→I (4) | T→VD (10) | I→T (4) | T→IT (2) | IT→T (5) | IT→I (2) | IT→IT (3) | (47) | ||
| VISTA | 0.2B | 55.15 | 31.98 | 32.88 | 10.12 | 31.23 | 45.81 | 53.32 | 8.97 | 26.26 | 37.32 |
| CLIP-SF | 0.4B | 39.75 | 31.42 | 59.05 | 24.09 | 62.95 | 66.41 | 53.32 | 34.9 | 55.65 | 43.66 |
| One-Peace | 4B | 43.54 | 31.27 | 61.38 | 42.9 | 65.59 | 42.72 | 28.29 | 6.73 | 23.41 | 42.01 |
| DSE | 4.2B | 48.94 | 27.92 | 40.75 | 78.21 | 52.54 | 49.62 | 35.44 | 8.36 | 40.18 | 50.04 |
| E5-V | 8.4B | 52.41 | 27.36 | 46.56 | 41.22 | 47.95 | 54.13 | 32.9 | 23.17 | 7.23 | 42.52 |
| GME-Qwen2-VL-2B | 2.2B | 55.93 | 29.86 | 57.36 | 87.84 | 61.93 | 76.47 | 64.58 | 37.02 | 66.47 | 64.45 |
| GME-Qwen2-VL-7B | 8.3B | 58.19 | 31.89 | 61.35 | 89.92 | 65.83 | 80.94 | 66.18 | 42.56 | 73.62 | 67.44 |
pip install ms-swift -U1# MAX_PIXELS settings to reduce memory usage
2# check: https://swift.readthedocs.io/en/latest/BestPractices/Embedding.html
3nproc_per_node=8
4MAX_PIXELS=1003520 \
5USE_HF=1 \
6NPROC_PER_NODE=$nproc_per_node \
7swift sft \
8 --model Alibaba-NLP/gme-Qwen2-VL-2B-Instruct \
9 --train_type lora \
10 --dataset 'HuggingFaceM4/TextCaps:emb' \
11 --torch_dtype bfloat16 \
12 --num_train_epochs 1 \
13 --per_device_train_batch_size 2 \
14 --per_device_eval_batch_size 2 \
15 --gradient_accumulation_steps $(expr 64 / $nproc_per_node) \
16 --eval_steps 100 \
17 --save_steps 100 \
18 --eval_strategy steps \
19 --save_total_limit 5 \
20 --logging_steps 5 \
21 --output_dir output \
22 --lazy_tokenize true \
23 --warmup_ratio 0.05 \
24 --learning_rate 5e-6 \
25 --deepspeed zero3 \
26 --dataloader_num_workers 4 \
27 --task_type embedding \
28 --loss_type infonce \
29 --dataloader_drop_last trueQwen2-VL, an image could be converted into a very large number of visual tokens. We limit the number of visual tokens to 1024 to obtain a good training efficiency.
Due to the lack of relevant data, our models and evaluations retain one single image.Qwen2-VL models are multilingual, the multilingual-multimodal embedding performance are not guaranteed.Built with GME on your website, user interface, blog post, About page, or product documentation.GME.multimodal-embedding-v1 model service is available.@misc{zhang2024gme,
title={GME: Improving Universal Multimodal Retrieval by Multimodal LLMs},
author={Zhang, Xin and Zhang, Yanzhao and Xie, Wen and Li, Mingxin and Dai, Ziqi and Long, Dingkun and Xie, Pengjun and Zhang, Meishan and Li, Wenjie and Zhang, Min},
year={2024},
eprint={2412.16855},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={http://arxiv.org/abs/2412.16855},
}