|
1 | | -# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 |
2 | | -# This file was automatically generated from src/transformers/models/videollama3/modular_videollama3.py. |
3 | | -# Do NOT edit this file manually as any edits will be overwritten by the generation of |
4 | | -# the file from the modular. If any change should be done, please apply the change to the |
5 | | -# modular_videollama3.py file directly. One of our CI enforces this. |
6 | | -# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 |
| 1 | +"""Image processor class for VideoLLaMA3.""" |
| 2 | + |
| 3 | +import math |
7 | 4 | from typing import Optional, Union |
8 | 5 |
|
9 | 6 | import numpy as np |
|
26 | 23 | from ...image_processing_utils import BaseImageProcessor |
27 | 24 | from ...image_transforms import convert_to_rgb, resize, to_channel_dimension_format |
28 | 25 | from ...image_utils import infer_channel_dimension_format, is_scaled_image, make_list_of_images, to_numpy_array |
29 | | -from .image_processing_videollama3_fast import smart_resize |
30 | 26 |
|
31 | 27 |
|
32 | 28 | logger = logging.get_logger(__name__) |
33 | 29 |
|
34 | 30 |
|
| 31 | +def smart_resize( |
| 32 | + height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 |
| 33 | +): |
| 34 | + """Rescales the image so that the following conditions are met: |
| 35 | +
|
| 36 | + 1. Both dimensions (height and width) are divisible by 'factor'. |
| 37 | +
|
| 38 | + 2. The total number of pixels is within the range ['min_pixels', 'max_pixels']. |
| 39 | +
|
| 40 | + 3. The aspect ratio of the image is maintained as closely as possible. |
| 41 | +
|
| 42 | + """ |
| 43 | + if max(height, width) / min(height, width) > 200: |
| 44 | + raise ValueError( |
| 45 | + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" |
| 46 | + ) |
| 47 | + h_bar = round(height / factor) * factor |
| 48 | + w_bar = round(width / factor) * factor |
| 49 | + if h_bar * w_bar > max_pixels: |
| 50 | + beta = math.sqrt((height * width) / max_pixels) |
| 51 | + h_bar = max(factor, math.floor(height / beta / factor) * factor) |
| 52 | + w_bar = max(factor, math.floor(width / beta / factor) * factor) |
| 53 | + elif h_bar * w_bar < min_pixels: |
| 54 | + beta = math.sqrt(min_pixels / (height * width)) |
| 55 | + h_bar = math.ceil(height * beta / factor) * factor |
| 56 | + w_bar = math.ceil(width * beta / factor) * factor |
| 57 | + return h_bar, w_bar |
| 58 | + |
| 59 | + |
35 | 60 | class Videollama3ImageProcessor(BaseImageProcessor): |
36 | 61 | r""" |
37 | | - Constructs a Qwen2-VL image processor that dynamically resizes images based on the original images. |
| 62 | + Constructs a VideoLLaMA3 image processor that dynamically resizes images based on the original images. |
38 | 63 |
|
39 | 64 | Args: |
40 | 65 | do_resize (`bool`, *optional*, defaults to `True`): |
|
0 commit comments