CoolFace
Modelpublic

zhibinlan/UME-R1-7B

sourceHugging Faceapache-2.0updated 11mo agoView on Hugging Face
5likes550downloads
README.md328 linesDownload Raw Back to root
1---2language:3- en4library_name: transformers5license: apache-2.06pipeline_tag: image-text-to-text7tags:8- Sentence Similarity9- Embedding10- zero-shot-image-classification11- video-text-to-text12---13 14# UME-R1-7B15 16## Model Summary17 18The model has undergone a cold-start SFT stage and an RL stage of training, and is capable of embedding text, images, multiple images, and videos. In particular, UME-R1 can generate either discriminative or generative embeddings as needed, and the generative embeddings possess the potential for test-time scaling.19 20- **Repository:** [UME-R1](https://github.com/XMUDeepLIT/UME-R1)21- **Paper:** [UME-R1](https://arxiv.org/abs/2511.00405)22 23## Train/Eval Data24 - Train data: https://huggingface.co/datasets/zhibinlan/UME-sft-train25 - Eval data: https://huggingface.co/datasets/TIGER-Lab/MMEB-V226 27 28 29 30## Model Performance31UME-R1 significantly outperforms discriminative embeddings and can provide discriminative or generative representations as needed. Its oracle performance—selecting the best between discriminative and generative—far exceeds using either mode alone.32 33 34<img src="./figures/main_result.png" alt="MMEB-V2" width="1200" height="auto">35<!-- ![MMEB-V2](./figures/main_result.png) -->36 37In addition, UME-R1 can produce improved embedding representations through repeated sampling, indicating that generative embeddings also hold strong promise for inference-time scaling. 38 39<img src="./figures/scaling.png" alt="pass@k" width="1200" height="auto">40 41### Quick Start42 43First clone our github44```bash45git clone https://github.com/DeepLearnXMU/UME-R146cd UME-R147bash setup.sh48```49 50Below, we provide simple examples to show how to use UME-R1 with 🤗 Transformers.51 52Example of obtaining generative embeddings:53 54```python55from transformers import Qwen2VLForConditionalGeneration,AutoProcessor56from qwen_vl_utils import process_vision_info57import torch58 59model = Qwen2VLForConditionalGeneration.from_pretrained(60    "zhibinlan/UME-R1-7B",61    torch_dtype=torch.bfloat16,62    attn_implementation="flash_attention_2",63    device_map="cuda:0",64)65 66processor = AutoProcessor.from_pretrained("zhibinlan/UME-R1-7B")67 68prompt = '''Represent the above input text, images, videos, or any combination of the three as embeddings. 69First output the thinking process in <think> </think> tags and then summarize the entire input in a word or sentence. 70Finally, use the <gen_emb> tag to represent the entire input.'''71 72 73 74messages = [75    {76        "role": "user",77        "content": [78            {79                "type": "image",80                "image": "assets/example.jpg",81            },82            {"type": "text", "text": "Represent the given image with the following question: What is in the image?\n<disc_emb>\n" + prompt},83        ],84    }85]86 87# Preparation for inference88text = processor.apply_chat_template(89    messages, tokenize=False, add_generation_prompt=True90)91 92image_inputs, video_inputs = process_vision_info(messages)93inputs = processor(94    text=[text],95    images=image_inputs,96    videos=video_inputs,97    padding=True,98    return_tensors="pt",99)100inputs = inputs.to(model.device)101 102# Inference: Generation of the output103generated_output = model.generate(**inputs, max_new_tokens=8192, output_hidden_states=True, return_dict_in_generate=True, use_cache=True)104# Post-process the output105generated_ids = generated_output.sequences106hidden_states = generated_output.hidden_states107 108generated_ids_trimmed = [109    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)110]111 112def get_embedding_idx(generated_ids_trimmed, EMBEDDING_TOKEN_ID):113 114    embedding_idx = []115    for i, out_ids in enumerate(generated_ids_trimmed):116        embed_exist = False117        for j in range(len(out_ids) - 1, -1, -1):118            if out_ids[j] == EMBEDDING_TOKEN_ID:119                embedding_idx.append(j + 1)120                embed_exist = True121                break122        if not embed_exist:123            embedding_idx.append(-1)124 125    return embedding_idx126 127def normalize_reps(reps):128    reps = torch.nn.functional.normalize(reps, p=2, dim=-1)129    return reps130 131# Get the last hidden state of the <gen_emb> token132embedding_idx = get_embedding_idx(generated_ids_trimmed, processor.tokenizer.get_vocab()["<gen_emb>"])133embedding_reps = hidden_states[embedding_idx[0]][-1].squeeze(1)134 135# Normalize the representations136embedding_reps = normalize_reps(embedding_reps)137 138output_text = processor.batch_decode(139    generated_ids_trimmed, skip_special_tokens=False, clean_up_tokenization_spaces=False140)141```142 143<details>144<summary>Example of obtaining discriminative embeddings</summary>145 146```python147from transformers import Qwen2VLForConditionalGeneration,AutoProcessor148from qwen_vl_utils import process_vision_info149import torch150 151pretrained_path = "zhibinlan/UME-R1-7B"152 153# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.154model = Qwen2VLForConditionalGeneration.from_pretrained(155    pretrained_path,156    torch_dtype=torch.bfloat16,157    attn_implementation="flash_attention_2",158    device_map="cuda:0",159)160 161# default processor162processor = AutoProcessor.from_pretrained(pretrained_path)163 164messages = [165    {166        "role": "user",167        "content": [168            {169                "type": "image",170                "image": "UME-R1/assets/example.jpg",171            },172            {"type": "text", "text": "Represent the given image with the following question: What is in the image?\n<disc_emb>\n"},173        ],174    }175]176 177# Preparation for inference178text = processor.apply_chat_template(179    messages, tokenize=False, add_generation_prompt=True180)181 182image_inputs, video_inputs = process_vision_info(messages)183inputs = processor(184    text=[text],185    images=image_inputs,186    videos=video_inputs,187    padding=True,188    return_tensors="pt",189)190inputs = inputs.to(model.device)191 192def get_embedding_idx(generated_ids_trimmed, EMBEDDING_TOKEN_ID):193 194    embedding_idx = []195    # Search from the last token forward196    for i, out_ids in enumerate(generated_ids_trimmed):197        embed_exist = False198        for j in range(len(out_ids) - 1, -1, -1):199            if out_ids[j] == EMBEDDING_TOKEN_ID:200                embedding_idx.append(j)201                embed_exist = True202                break203        if not embed_exist:204            embedding_idx.append(-1)205 206    return embedding_idx207 208def normalize_reps(reps):209    # Normalize the representations210    reps = torch.nn.functional.normalize(reps, p=2, dim=-1)211    return reps212 213output = model(**inputs, output_hidden_states=True, return_dict=True)214hidden_states = output.hidden_states[-1][0]215# print("output.hidden_states shape: ", hidden_states.shape)216embedding_idx = get_embedding_idx(inputs['input_ids'], processor.tokenizer.get_vocab()["<disc_emb>"])217 218# Get the last hidden state of the <gen_emb> token219embedding_reps = hidden_states[embedding_idx[0]]220 221# Normalize the representations222embedding_reps = normalize_reps(embedding_reps)223```224 225</details>226 227<details>228<summary>Multi image inference</summary>229 230```python231# Messages containing multiple images and a text query232messages = [233    {234        "role": "user",235        "content": [236            {"type": "image", "image": "file:///path/to/image1.jpg"},237            {"type": "image", "image": "file:///path/to/image2.jpg"},238            {"type": "text", "text": "Represent the given images."},239        ],240    }241]242```243 244</details>245 246<details>247<summary>Video inference</summary>248 249```python250# Messages containing a images list as a video and a text query251messages = [252    {253        "role": "user",254        "content": [255            {256                "type": "video",257                "video": [258                    "file:///path/to/frame1.jpg",259                    "file:///path/to/frame2.jpg",260                    "file:///path/to/frame3.jpg",261                    "file:///path/to/frame4.jpg",262                ],263            },264            {"type": "text", "text": "Represent this video."},265        ],266    }267]268 269# Messages containing a local video path and a text query270messages = [271    {272        "role": "user",273        "content": [274            {275                "type": "video",276                "video": "file:///path/to/video1.mp4",277                "max_pixels": 360 * 420,278                "fps": 1.0,279            },280            {"type": "text", "text": "Represent this video."},281        ],282    }283]284 285# Messages containing a video url and a text query286messages = [287    {288        "role": "user",289        "content": [290            {291                "type": "video",292                "video": "https://path/to/video.mp4",293                "min_pixels": 4 * 28 * 28,294                "max_pixels": 256 * 28 * 28,295                "total_pixels": 20480 * 28 * 28,296            },297            {"type": "text", "text": "Represent this video."},298        ],299    }300]301image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)302inputs = processor(303    text=[text],304    images=image_inputs,305    videos=video_inputs,306    fps=fps,307    padding=True,308    return_tensors="pt",309    **video_kwargs,310)311```312 313</details>314 315 316For more usage tips, please refer to our [Github page](https://github.com/DeepLearnXMU/UME-R1).317 318 319## Citation320If you find our work useful, please consider citing it.321```322@article{lan2025ume,323  title={UME-R1: Exploring Reasoning-Driven Generative Multimodal Embeddings},324  author={Lan, Zhibin and Niu, Liqiang and Meng, Fandong and Zhou, Jie and Su, Jinsong},325  journal={arXiv preprint arXiv:2511.00405},326  year={2025}327}328```