Instructions to use BiliSakura/AnyUp-transformers with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use BiliSakura/AnyUp-transformers with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="BiliSakura/AnyUp-transformers")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("BiliSakura/AnyUp-transformers", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download anyup-multi-backbone/image_processing_anyup.py from BiliSakura/AnyUp-transformers: direct link, hf CLI and curl.
- Browser
- Download file 5.65 kB
-
https://huggingface.co/BiliSakura/AnyUp-transformers/resolve/main/anyup-multi-backbone/image_processing_anyup.py
- Command line
-
hf download hf://BiliSakura/AnyUp-transformers/anyup-multi-backbone/image_processing_anyup.py
-
curl -L -o image_processing_anyup.py https://huggingface.co/BiliSakura/AnyUp-transformers/resolve/main/anyup-multi-backbone/image_processing_anyup.py
5.65 kB
| # Copyright 2026 The AnyUp authors and The HuggingFace Inc. team. | |
| """Image processor for AnyUp (ImageNet-normalized RGB guide, no default resize).""" | |
| from typing import Optional, Union | |
| import numpy as np | |
| from transformers.image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict | |
| from transformers.image_transforms import to_channel_dimension_format | |
| from transformers.image_utils import ( | |
| IMAGENET_DEFAULT_MEAN, | |
| IMAGENET_DEFAULT_STD, | |
| ChannelDimension, | |
| ImageInput, | |
| PILImageResampling, | |
| infer_channel_dimension_format, | |
| make_flat_list_of_images, | |
| to_numpy_array, | |
| valid_images, | |
| ) | |
| from transformers.utils import TensorType, filter_out_non_signature_kwargs, logging | |
| logger = logging.get_logger(__name__) | |
| def _maybe_rescale(image: np.ndarray, do_rescale: bool, rescale_factor: float) -> np.ndarray: | |
| if not do_rescale: | |
| return image.astype(np.float32, copy=False) | |
| if np.nanmax(image) > 1.5: | |
| return image.astype(np.float32) * rescale_factor | |
| return image.astype(np.float32) | |
| class AnyUpImageProcessor(BaseImageProcessor): | |
| """ | |
| Processor for AnyUp's high-resolution RGB guide. | |
| Images are rescaled to ``[0, 1]`` and ImageNet-normalized. Resize is **off** by default so the | |
| guide keeps its native ``(H, W)``; AnyUp is resolution-agnostic. Low-resolution features are | |
| passed through as ``features`` and are not transformed. | |
| """ | |
| model_input_names = ["pixel_values", "features"] | |
| def __init__( | |
| self, | |
| do_resize: bool = False, | |
| size: Optional[dict[str, int]] = None, | |
| resample: PILImageResampling = PILImageResampling.BILINEAR, | |
| do_rescale: bool = True, | |
| rescale_factor: float = 1 / 255.0, | |
| do_normalize: bool = True, | |
| image_mean: Optional[Union[float, list[float]]] = None, | |
| image_std: Optional[Union[float, list[float]]] = None, | |
| do_convert_rgb: bool = True, | |
| **kwargs, | |
| ): | |
| super().__init__(**kwargs) | |
| size = size if size is not None else {"height": 224, "width": 224} | |
| self.do_resize = do_resize | |
| self.size = size | |
| self.resample = resample | |
| self.do_rescale = do_rescale | |
| self.rescale_factor = rescale_factor | |
| self.do_normalize = do_normalize | |
| self.image_mean = IMAGENET_DEFAULT_MEAN if image_mean is None else image_mean | |
| self.image_std = IMAGENET_DEFAULT_STD if image_std is None else image_std | |
| self.do_convert_rgb = do_convert_rgb | |
| def preprocess( | |
| self, | |
| images: ImageInput, | |
| features=None, | |
| do_resize: Optional[bool] = None, | |
| size: Optional[dict[str, int]] = None, | |
| resample: Optional[PILImageResampling] = None, | |
| do_rescale: Optional[bool] = None, | |
| rescale_factor: Optional[float] = None, | |
| do_normalize: Optional[bool] = None, | |
| image_mean: Optional[Union[float, list[float]]] = None, | |
| image_std: Optional[Union[float, list[float]]] = None, | |
| do_convert_rgb: Optional[bool] = None, | |
| return_tensors: Optional[Union[str, TensorType]] = None, | |
| data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, | |
| input_data_format: Optional[Union[str, ChannelDimension]] = None, | |
| ) -> BatchFeature: | |
| do_resize = self.do_resize if do_resize is None else do_resize | |
| size = get_size_dict(self.size if size is None else size, default_to_square=True) | |
| resample = self.resample if resample is None else resample | |
| do_rescale = self.do_rescale if do_rescale is None else do_rescale | |
| rescale_factor = self.rescale_factor if rescale_factor is None else rescale_factor | |
| do_normalize = self.do_normalize if do_normalize is None else do_normalize | |
| image_mean = self.image_mean if image_mean is None else image_mean | |
| image_std = self.image_std if image_std is None else image_std | |
| do_convert_rgb = self.do_convert_rgb if do_convert_rgb is None else do_convert_rgb | |
| image_list = make_flat_list_of_images(images) | |
| if not valid_images(image_list): | |
| raise ValueError("Invalid image type. Must be PIL, numpy, or torch tensor.") | |
| processed_images = [] | |
| for image in image_list: | |
| image = to_numpy_array(image) | |
| if do_convert_rgb and image.ndim == 2: | |
| image = np.stack([image, image, image], axis=-1) | |
| if input_data_format is None: | |
| try: | |
| channel_format = infer_channel_dimension_format(image) | |
| except ValueError: | |
| channel_format = ChannelDimension.LAST | |
| else: | |
| channel_format = input_data_format | |
| if do_resize: | |
| image = self.resize( | |
| image, | |
| size={"height": size["height"], "width": size["width"]}, | |
| resample=resample, | |
| input_data_format=channel_format, | |
| ) | |
| image = _maybe_rescale(image, do_rescale, rescale_factor) | |
| if do_normalize: | |
| image = self.normalize( | |
| image=image, mean=image_mean, std=image_std, input_data_format=channel_format | |
| ) | |
| image = to_channel_dimension_format(image, data_format, input_channel_dim=channel_format) | |
| processed_images.append(image) | |
| data = {"pixel_values": processed_images} | |
| if features is not None: | |
| data["features"] = features | |
| return BatchFeature(data=data, tensor_type=return_tensors) | |
| __all__ = ["AnyUpImageProcessor"] | |