inkling-2-small-vision-encoder / native_source /image_processing_inkling2.py
gatilin's picture
Add files using upload-large-folder tool
1f25247 verified
Raw History Blame Contribute Delete
5.01 kB
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# This file was automatically generated from src/transformers/models/inkling2/modular_inkling2.py.
# Do NOT edit this file manually as any edits will be overwritten by the generation of
# the file from the modular. If any change should be done, please apply the change to the
# modular_inkling2.py file directly. One of our CI enforces this.
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 the HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import torch
from torchvision.transforms.v2 import functional as tvF
from ...image_processing_backends import TorchvisionBackend
from ...image_processing_utils import BatchFeature
from ...image_utils import ChannelDimension, ImageInput, ImageType, SizeDict, get_image_type
from ...processing_utils import ImagesKwargs, Unpack
from ...utils import auto_docstring
@auto_docstring(custom_intro="Constructs an Inkling2 image processor.")
class Inkling2ImageProcessor(TorchvisionBackend):
image_mean = [0.5, 0.5, 0.5]
image_std = [0.5, 0.5, 0.5]
do_convert_rgb = True
do_rescale = True
do_normalize = True
size = {"height": 40, "width": 40}
model_input_names = ["pixel_values", "image_grid_thw"]
def process_image(
self,
image: ImageInput,
do_convert_rgb: bool | None = None,
input_data_format: str | ChannelDimension | None = None,
device: "torch.device | None" = None,
**kwargs: Unpack[ImagesKwargs],
) -> "torch.Tensor":
# keep the alpha channel of PIL images so that `convert_to_rgb` composites it onto the checkerboard
if do_convert_rgb and get_image_type(image) == ImageType.PIL:
image = tvF.pil_to_tensor(image.convert("RGBA" if image.has_transparency_data else "RGB"))
input_data_format = ChannelDimension.FIRST
return super().process_image(
image, do_convert_rgb=do_convert_rgb, input_data_format=input_data_format, device=device, **kwargs
)
def convert_to_rgb(
self, image: "torch.Tensor", input_data_format: str | ChannelDimension | None = None
) -> "torch.Tensor":
if image.shape[-3] != 4:
return super().convert_to_rgb(image, input_data_format=input_data_format)
height, width = image.shape[-2:]
rows = torch.arange(height, device=image.device)[:, None] // 8
columns = torch.arange(width, device=image.device)[None, :] // 8
background = torch.where((rows + columns) % 2 == 0, 204, 153)
rgba = image.to(torch.int32)
alpha = rgba[3:4]
return ((rgba[:3] * alpha + background * (255 - alpha) + 127) // 255).to(torch.uint8)
def _preprocess(
self,
images: list["torch.Tensor"],
size: SizeDict,
do_rescale: bool,
rescale_factor: float,
do_normalize: bool,
image_mean: float | list[float] | None,
image_std: float | list[float] | None,
return_tensors: str | None,
**kwargs,
) -> BatchFeature:
patches, image_grid_thw = [], []
for image in images:
channels, height, width = image.shape
rows, columns = math.ceil(height / size.height), math.ceil(width / size.width)
# pad to the grid with -1 on the 0-255 scale, then cut it into one patch per image token
pad_size = SizeDict(height=rows * size.height, width=columns * size.width)
image = self.pad([image.float()], pad_size=pad_size, fill_value=-1.0)[0]
image = self.rescale_and_normalize(image, do_rescale, rescale_factor, do_normalize, image_mean, image_std)
image = image.reshape(channels, rows, size.height, columns, size.width).permute(1, 3, 2, 4, 0)
patches.append(image.flatten(0, 1))
image_grid_thw.append([1, rows, columns])
return BatchFeature(
data={"pixel_values": torch.cat(patches), "image_grid_thw": torch.tensor(image_grid_thw)},
tensor_type=return_tensors,
)
__all__ = ["Inkling2ImageProcessor"]