File size: 16,330 Bytes
c0b5fc9 6435505 c0b5fc9 6435505 c0b5fc9 6435505 c0b5fc9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
import math
from typing import ClassVar, List, Optional, Tuple, Union
import torch
from PIL import Image, ImageOps
from transformers import BatchFeature, LlavaNextProcessor
def round_by_factor(number: float, factor: int) -> int:
"""Returns the closest integer to 'number' that is divisible by 'factor'."""
return round(number / factor) * factor
def ceil_by_factor(number: float, factor: int) -> int:
"""Returns the smallest integer greater than or equal to 'number' that is divisible by 'factor'."""
return math.ceil(number / factor) * factor
def floor_by_factor(number: float, factor: int) -> int:
"""Returns the largest integer less than or equal to 'number' that is divisible by 'factor'."""
return math.floor(number / factor) * factor
class ColGraniteVisionProcessor(LlavaNextProcessor):
"""
Processor for ColPali.
"""
visual_prompt_prefix: ClassVar[str] = "<|user|>\n<image>\nDescribe the image.\n"
system_message: ClassVar[
str] = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions."
query_prefix: ClassVar[str] = "Query: "
query_start: ClassVar[str] = "<|user|>\n"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.factor = 14
self.min_size = 384
self.max_size = 384 * 2
self.suffix_len = 10
self.patch_size = 14
@property
def query_augmentation_token(self) -> str:
"""
Return the query augmentation token.
Query augmentation buffers are used as reasoning buffers during inference.
"""
return self.tokenizer.pad_token
@staticmethod
def smart_resize_helper(
width: int,
height: int,
factor: int,
min_size: int,
max_size: int
) -> Tuple[int, int]:
"""
Returns the resized image dimensions such that:
1. The smaller dimension is set to 'min_size'.
2. The larger dimension is scaled proportionally to maintain aspect ratio.
3. If the larger dimension exceeds 'max_size', it is clipped to 'max_size',
and the smaller dimension is adjusted accordingly to maintain aspect ratio.
4. Both dimensions are divisible by 'factor'.
"""
# Determine scale factor based on min_size
if height < width:
scale_factor = min_size / height
else:
scale_factor = min_size / width
new_width = round(width * scale_factor)
new_height = round(height * scale_factor)
# If the longer dimension exceeds max_size, adjust accordingly
if max(new_width, new_height) > max_size:
clip_factor = max_size / max(new_width, new_height)
new_width = round(new_width * clip_factor)
new_height = round(new_height * clip_factor)
# Ensure dimensions are divisible by factor
# new_width = round_by_factor(new_width, factor)
# new_height = round_by_factor(new_height, factor)
return new_width, new_height
@staticmethod
def pad_image_center(image: Image.Image,
target_width: int,
target_height: int,
fill_color=(0, 0, 0)) -> Image.Image:
"""
Pads the given image to be centered within the target dimensions.
:param image: PIL Image to be padded.
:param target_width: The desired width after padding.
:param target_height: The desired height after padding.
:param fill_color: Background color (default is black).
:return: Padded image with centered content.
"""
# Get original image size
img_width, img_height = image.size
# Compute padding values
pad_left = (target_width - img_width) // 2
pad_top = (target_height - img_height) // 2
pad_right = target_width - img_width - pad_left
pad_bottom = target_height - img_height - pad_top
# Apply padding
padded_image = ImageOps.expand(image, (pad_left, pad_top, pad_right, pad_bottom), fill_color).convert("RGB")
return padded_image
def smart_resize(self, image: Image.Image) -> Image.Image:
"""
Resize and convert the image to the required format.
"""
image_size = image.size
resized_height, resized_width = self.smart_resize_helper(
width=image_size[0],
height=image_size[1],
factor=self.factor,
min_size=self.min_size,
max_size=self.max_size
)
return image.convert("RGB").resize((resized_width, resized_height))
def smart_resize_and_pad(self, image: Image.Image) -> Image.Image:
"""
Resize and pad the image to the required format.
"""
return self.resize_and_pad_centered_to_long_side(
image=image,
factor=self.factor,
min_size=self.min_size,
max_size=self.max_size,
fill_color=0
)
def resize_and_pad_centered_to_long_side(
self,
image: Image.Image,
factor: int,
min_size: int,
max_size: int,
fill_color=0
) -> Image.Image:
"""
Resizes and pads an image such that:
- The long side is set to `max_size`.
- The short side is scaled proportionally but not below `min_size`.
- The image is centered within the final padded area.
:param image: PIL Image
:param factor: Factor to make dimensions divisible by
:param min_size: Minimum allowed size for the short side
:param max_size: Target size for the long side
:param fill_color: Background padding color (default black)
:return: Resized and padded image
"""
# Get original size
width, height = image.size
if min_size == -1 or max_size == -1:
return image.convert("RGB")
# Step 1: scale long side to max_size, keep aspect ratio
if width > height:
scale_factor = max_size / width
target_width = max_size
max_scale_factor = max(min_size / height, scale_factor)
target_height = round(height * max_scale_factor)
else:
scale_factor = max_size / height
target_height = max_size
max_scale_factor = max(min_size / width, scale_factor)
target_width = round(width * max_scale_factor)
# Resize the image
resized_image = image.resize((target_width, target_height), Image.LANCZOS)
final_image =resized_image.convert("RGB")
return final_image
def resize_and_pad_centered(self,
image: Image.Image,
factor: int,
min_size: int,
max_size: int,
fill_color=0
) -> Image.Image:
"""
Resizes and pads an image such that:
- The short side is set to `min_size`.
- The long side is scaled proportionally but clipped to `max_size`.
- The image is centered within the final padded area.
:param image: PIL Image
:param factor: Factor to make dimensions divisible by
:param min_size: Minimum size for the short side
:param max_size: Maximum allowed size for the long side
:param fill_color: Background padding color (default black)
:return: Resized and padded image
"""
# Get original size
width, height = image.size
if min_size == -1 or max_size == -1:
return image.convert("RGB")
# Determine scale factor based on the short side (min_size)
if width < height:
scale_factor = min_size / width
target_width = min_size
max_scale_factor = min(max_size / height, scale_factor)
target_height = round(height * max_scale_factor)
else:
scale_factor = min_size / height
target_height = min_size
max_scale_factor = min(max_size / width, scale_factor)
target_width = round(width * max_scale_factor)
# Ensure the longer side does not exceed max_size
# if max(target_width, target_height) > max_size:
# clip_factor = max_size / max(target_width, target_height)
# target_width = round(target_width * clip_factor)
# target_height = round(target_height * clip_factor)
# Ensure dimensions are divisible by factor
# target_width = round_by_factor(target_width, factor)
# target_height = round_by_factor(target_height, factor)
# Resize the image
resized_image = image.resize((target_width, target_height), Image.LANCZOS)
# Determine final padded dimensions (aligned to short side)
if width < height:
final_width, final_height = min_size, max_size
else:
final_width, final_height = max_size, min_size
# Compute padding to center the image
pad_left = (final_width - target_width) // 2
pad_top = (final_height - target_height) // 2
pad_right = final_width - target_width - pad_left
pad_bottom = final_height - target_height - pad_top
# Apply centered padding
# final_image = ImageOps.expand(resized_image, (pad_left, pad_top, pad_right, pad_bottom), fill_color).convert("RGB")
final_image = resized_image.convert("RGB")
return final_image
def format_data(self, question, image):
return [
{
"role": "system",
"content": [{"type": "text", "text": self.system_message}],
},
{
"role": "user",
"content": [
{
"type": "image",
"image": image,
},
{
"type": "text",
"text": question,
},
],
}
]
def format_data_wo_role(self, question, image=None):
return [
{
"role": "user",
"content": [
{
"type": "image",
"image": image,
},
{
"type": "text",
"text": question,
},
],
}
]
def process_images(
self,
images: List[Image.Image],
) -> BatchFeature:
"""
Process images for ColPali.
"""
# texts_doc = [self.apply_chat_template(self.format_data_wo_role(self.visual_prompt_prefix, img),tokenize=False ) for img in images]
texts_doc = [self.visual_prompt_prefix for _ in images]
images = [self.smart_resize_and_pad(image) for image in images]
batch_doc = self(
text=texts_doc,
images=images,
return_tensors="pt",
padding="longest",
)
return batch_doc
def process_queries(self, queries, max_length=2048, suffix=None):
if suffix is None:
suffix = self.query_augmentation_token * self.suffix_len
processed = []
for q in queries:
q = self.query_start + self.query_prefix + q
# truncate before it eats actual query content
if len(q) + len(suffix) > max_length:
q = q[: max_length - len(suffix) - 1]
q += suffix + "\n"
processed.append(q)
return self(
text=processed,
images=None,
return_tensors="pt",
padding="longest",
truncation=True,
max_length=max_length,
)
def score(
self,
qs: List[torch.Tensor],
ps: List[torch.Tensor],
device: Optional[Union[str, torch.device]] = None,
**kwargs,
) -> torch.Tensor:
"""
Compute the MaxSim score (ColBERT-like) for the given multi-vector query and passage embeddings.
"""
return self.score_multi_vector(qs, ps, device=device, **kwargs)
def get_n_patches(
self,
image_size: Tuple[int, int],
patch_size: int,
) -> Tuple[int, int]:
n_patches_x = self.image_processor.size["width"] // patch_size
n_patches_y = self.image_processor.size["height"] // patch_size
return n_patches_x, n_patches_y
def get_image_mask(self, batch_images: BatchFeature) -> torch.Tensor:
return batch_images.input_ids == self.image_token_id
@staticmethod
def score_single_vector(
qs: List[torch.Tensor],
ps: List[torch.Tensor],
device: Optional[Union[str, torch.device]] = None,
) -> torch.Tensor:
"""
Compute the dot product score for the given single-vector query and passage embeddings.
"""
if len(qs) == 0:
raise ValueError("No queries provided")
if len(ps) == 0:
raise ValueError("No passages provided")
qs_stacked = torch.stack(qs).to(device)
ps_stacked = torch.stack(ps).to(device)
scores = torch.einsum("bd,cd->bc", qs_stacked, ps_stacked)
assert scores.shape[0] == len(qs), f"Expected {len(qs)} scores, got {scores.shape[0]}"
scores = scores.to(torch.float32)
return scores
@staticmethod
def score_multi_vector(
qs: Union[torch.Tensor, List[torch.Tensor]],
ps: Union[torch.Tensor, List[torch.Tensor]],
batch_size: int = 128,
device: Optional[Union[str, torch.device]] = None,
) -> torch.Tensor:
"""
Compute the late-interaction/MaxSim score (ColBERT-like) for the given multi-vector
query embeddings (`qs`) and passage embeddings (`ps`). For ColPali, a passage is the
image of a document page.
Because the embedding tensors are multi-vector and can thus have different shapes, they
should be fed as:
(1) a list of tensors, where the i-th tensor is of shape (sequence_length_i, embedding_dim)
(2) a single tensor of shape (n_passages, max_sequence_length, embedding_dim) -> usually
obtained by padding the list of tensors.
Args:
qs (`Union[torch.Tensor, List[torch.Tensor]`): Query embeddings.
ps (`Union[torch.Tensor, List[torch.Tensor]`): Passage embeddings.
batch_size (`int`, *optional*, defaults to 128): Batch size for computing scores.
device (`Union[str, torch.device]`, *optional*): Device to use for computation. If not
provided, uses `get_torch_device("auto")`.
Returns:
`torch.Tensor`: A tensor of shape `(n_queries, n_passages)` containing the scores. The score
tensor is saved on the "cpu" device.
"""
if len(qs) == 0:
raise ValueError("No queries provided")
if len(ps) == 0:
raise ValueError("No passages provided")
scores_list: List[torch.Tensor] = []
for i in range(0, len(qs), batch_size):
scores_batch = []
qs_batch = torch.nn.utils.rnn.pad_sequence(qs[i: i + batch_size], batch_first=True, padding_value=0).to(
device
)
for j in range(0, len(ps), batch_size):
ps_batch = torch.nn.utils.rnn.pad_sequence(
ps[j: j + batch_size], batch_first=True, padding_value=0
).to(device)
scores_batch.append(torch.einsum("bnd,csd->bcns", qs_batch, ps_batch).max(dim=3)[0].sum(dim=2))
scores_batch = torch.cat(scores_batch, dim=1).cpu()
scores_list.append(scores_batch)
scores = torch.cat(scores_list, dim=0)
assert scores.shape[0] == len(qs), f"Expected {len(qs)} scores, got {scores.shape[0]}"
scores = scores.to(torch.float32)
return scores |