Image Feature Extraction
PerceptionEncoder
Safetensors
inkling2
vision-encoder
multimodal
extracted-component
modelopt
Instructions to use gatilin/inkling-2-small-vision-encoder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PerceptionEncoder
How to use gatilin/inkling-2-small-vision-encoder with PerceptionEncoder:
# Use any PE model as a vision encoder import core.vision_encoder.pe as pe model = pe.VisionTransformer.from_config("gatilin/inkling-2-small-vision-encoder", pretrained=True) - Notebooks
- Google Colab
- Kaggle
Download native_source/image_processing_inkling2.py from gatilin/inkling-2-small-vision-encoder: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/gatilin/inkling-2-small-vision-encoder/resolve/main/native_source/image_processing_inkling2.py
- Command line
-
hf download hf://gatilin/inkling-2-small-vision-encoder/native_source/image_processing_inkling2.py
-
curl -L -o image_processing_inkling2.py https://huggingface.co/gatilin/inkling-2-small-vision-encoder/resolve/main/native_source/image_processing_inkling2.py
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 | |
| 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"] | |