MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / MaxLongEdgeMinShortEdgeResize

Class MaxLongEdgeMinShortEdgeResize

data/transforms.py:34–106  ·  view source on GitHub ↗

Resize the input image so that its longest side and shortest side are within a specified range, ensuring that both sides are divisible by a specified stride. Args: max_size (int): Maximum size for the longest edge of the image. min_size (int): Minimum size for the shortest e

Source from the content-addressed store, hash-verified

32from modeling.qwen2vl.image_processing_qwen2_vl import Qwen2VLImageProcessor
33
34class MaxLongEdgeMinShortEdgeResize(torch.nn.Module):
35 """Resize the input image so that its longest side and shortest side are within a specified range,
36 ensuring that both sides are divisible by a specified stride.
37
38 Args:
39 max_size (int): Maximum size for the longest edge of the image.
40 min_size (int): Minimum size for the shortest edge of the image.
41 stride (int): Value by which the height and width of the image must be divisible.
42 max_pixels (int): Maximum pixels for the full image.
43 interpolation (InterpolationMode): Desired interpolation enum defined by
44 :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.
45 If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.NEAREST_EXACT``,
46 ``InterpolationMode.BILINEAR``, and ``InterpolationMode.BICUBIC`` are supported.
47 The corresponding Pillow integer constants, e.g., ``PIL.Image.BILINEAR`` are also accepted.
48 antialias (bool, optional): Whether to apply antialiasing (default is True).
49 """
50
51 def __init__(
52 self,
53 max_size: int,
54 min_size: int,
55 stride: int,
56 max_pixels: int,
57 interpolation=InterpolationMode.BICUBIC,
58 antialias=True
59 ):
60 super().__init__()
61 self.max_size = max_size
62 self.min_size = min_size
63 self.stride = stride
64 self.max_pixels = max_pixels
65 self.interpolation = interpolation
66 self.antialias = antialias
67
68 def _make_divisible(self, value, stride):
69 """Ensure the value is divisible by the stride."""
70 return max(stride, int(round(value / stride) * stride))
71
72 def _apply_scale(self, width, height, scale):
73 new_width = round(width * scale)
74 new_height = round(height * scale)
75 new_width = self._make_divisible(new_width, self.stride)
76 new_height = self._make_divisible(new_height, self.stride)
77 return new_width, new_height
78
79 def forward(self, img, img_num=1):
80 """
81 Args:
82 img (PIL Image): Image to be resized.
83 img_num (int): Number of images, used to change max_tokens.
84 Returns:
85 PIL Image or Tensor: Rescaled image with divisible dimensions.
86 """
87 if isinstance(img, torch.Tensor):
88 height, width = img.shape[-2:]
89 else:
90 width, height = img.size
91

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected