Skip to content

Commit

Permalink
[MIEB] Fix get_fused_emebddings (#1612)
Browse files Browse the repository at this point in the history
* Fix fused

* fix vlm2vec

* Fix lint
  • Loading branch information
izhx authored Dec 22, 2024
1 parent 74cb6e6 commit 6740207
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 80 deletions.
7 changes: 2 additions & 5 deletions mteb/models/align_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -114,10 +111,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
9 changes: 3 additions & 6 deletions mteb/models/blip2_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -188,10 +185,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand All @@ -202,7 +199,7 @@ def get_fused_embeddings(
fused_embeddings = text_embeddings + image_embeddings
elif fusion_mode == "multimodal":
fused_embeddings = self.get_multimodal_embeddings(
texts, images, batch_size
texts, images, kwargs.get("batch_size", 32)
)
else:
# to do: add other fusion mode
Expand Down
7 changes: 2 additions & 5 deletions mteb/models/blip_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -134,10 +131,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
7 changes: 2 additions & 5 deletions mteb/models/clip_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -118,10 +115,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
7 changes: 2 additions & 5 deletions mteb/models/cohere_v.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -158,10 +155,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
7 changes: 2 additions & 5 deletions mteb/models/dino_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -110,10 +107,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
raise ValueError("DINO models only support image encoding.")
Expand Down
8 changes: 3 additions & 5 deletions mteb/models/e5_v.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,14 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 8,
**kwargs: Any,
):
if texts is None and images is None:
raise ValueError("Either texts or images must be provided")

all_fused_embeddings = []
kwargs.update(batch_size=batch_size)

if texts is not None and images is not None:
with torch.no_grad():
Expand Down Expand Up @@ -168,10 +166,10 @@ def get_fused_embeddings(
all_fused_embeddings.append(outputs.cpu())
return torch.cat(all_fused_embeddings, dim=0)
elif texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)
return text_embeddings
elif images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)
return image_embeddings


Expand Down
8 changes: 2 additions & 6 deletions mteb/models/evaclip_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -143,10 +139,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
14 changes: 2 additions & 12 deletions mteb/models/jina_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -116,18 +112,12 @@ def get_fused_embeddings(

if texts is not None:
text_embeddings = self.get_text_embeddings(
texts,
batch_size=batch_size,
convert_to_numpy=False,
convert_to_tensor=True,
texts, convert_to_numpy=False, convert_to_tensor=True, **kwargs
)

if images is not None:
image_embeddings = self.get_image_embeddings(
images,
batch_size=batch_size,
convert_to_numpy=False,
convert_to_tensor=True,
images, convert_to_numpy=False, convert_to_tensor=True, **kwargs
)

if text_embeddings is not None and image_embeddings is not None:
Expand Down
8 changes: 2 additions & 6 deletions mteb/models/nomic_models_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -142,10 +138,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
8 changes: 2 additions & 6 deletions mteb/models/openclip_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -128,10 +124,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
8 changes: 2 additions & 6 deletions mteb/models/siglip_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
fusion_mode="sum",
**kwargs: Any,
):
Expand All @@ -137,10 +133,10 @@ def get_fused_embeddings(
image_embeddings = None

if texts is not None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)

if images is not None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)

if text_embeddings is not None and image_embeddings is not None:
if len(text_embeddings) != len(image_embeddings):
Expand Down
1 change: 0 additions & 1 deletion mteb/models/vista_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def get_fused_embeddings(
self,
texts: list[str] = None,
images: list[Image.Image] | DataLoader = None,
*,
task_name: str | None = None,
prompt_type: PromptType | None = None,
batch_size: int = 32,
Expand Down
7 changes: 5 additions & 2 deletions mteb/models/vlm2vec_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,16 @@ def get_fused_embeddings(

text_embeddings = None
image_embeddings = None
kwargs.update(
task_name=task_name, prompt_type=prompt_type, batch_size=batch_size
)

if texts is not None and images is None:
text_embeddings = self.get_text_embeddings(texts, batch_size)
text_embeddings = self.get_text_embeddings(texts, **kwargs)
return text_embeddings

if images is not None and texts is None:
image_embeddings = self.get_image_embeddings(images, batch_size)
image_embeddings = self.get_image_embeddings(images, **kwargs)
return image_embeddings

# text_embeddings is not None and image_embeddings is not None
Expand Down
7 changes: 2 additions & 5 deletions mteb/models/voyage_v.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from __future__ import annotations

import logging
import os
from functools import partial
from typing import Any

import logging
import torch
from PIL import Image
from torch.utils.data import DataLoader
from torchvision import transforms
from tqdm import tqdm

import mteb
from mteb.model_meta import ModelMeta
from mteb.encoder_interface import PromptType
from mteb.model_meta import ModelMeta

Expand All @@ -23,9 +22,7 @@
def downsample_image(
image: Image.Image, max_pixels: int = 16000000, target_longest_side: int = 4000
) -> Image.Image:
"""
if image pixel > max_pixels, downsample it to target_longest_side while keeping the width height ratio.
"""
"""If image pixel > max_pixels, downsample it to target_longest_side while keeping the width height ratio."""
width, height = image.size
pixels = width * height

Expand Down

0 comments on commit 6740207

Please sign in to comment.