|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 The HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Fast Image processor class for Swin2SR.""" |
| 16 | + |
| 17 | +from typing import List, Optional, Union |
| 18 | + |
| 19 | +from ...image_processing_utils import ( |
| 20 | + BatchFeature, |
| 21 | + ChannelDimension, |
| 22 | + get_image_size, |
| 23 | +) |
| 24 | +from ...image_processing_utils_fast import ( |
| 25 | + BASE_IMAGE_PROCESSOR_FAST_DOCSTRING, |
| 26 | + BASE_IMAGE_PROCESSOR_FAST_DOCSTRING_PREPROCESS, |
| 27 | + BaseImageProcessorFast, |
| 28 | + DefaultFastImageProcessorKwargs, |
| 29 | + group_images_by_shape, |
| 30 | + reorder_images, |
| 31 | +) |
| 32 | +from ...image_utils import ImageInput |
| 33 | +from ...processing_utils import Unpack |
| 34 | +from ...utils import ( |
| 35 | + TensorType, |
| 36 | + add_start_docstrings, |
| 37 | + is_torch_available, |
| 38 | + is_torchvision_available, |
| 39 | + is_torchvision_v2_available, |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +if is_torch_available(): |
| 44 | + import torch |
| 45 | + |
| 46 | +if is_torchvision_available(): |
| 47 | + if is_torchvision_v2_available(): |
| 48 | + from torchvision.transforms.v2 import functional as F |
| 49 | + else: |
| 50 | + from torchvision.transforms import functional as F |
| 51 | + |
| 52 | + |
| 53 | +class Swin2SRFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): |
| 54 | + do_pad: Optional[bool] |
| 55 | + pad_size: Optional[int] |
| 56 | + |
| 57 | + |
| 58 | +@add_start_docstrings( |
| 59 | + "Constructs a fast Swin2SR image processor.", |
| 60 | + BASE_IMAGE_PROCESSOR_FAST_DOCSTRING, |
| 61 | + """ |
| 62 | + do_pad (`bool`, *optional*, defaults to `True`): |
| 63 | + Whether to pad the image to make the height and width divisible by `window_size`. |
| 64 | + pad_size (`int`, *optional*, defaults to `8`): |
| 65 | + The size of the sliding window for the local attention. |
| 66 | + """, |
| 67 | +) |
| 68 | +class Swin2SRImageProcessorFast(BaseImageProcessorFast): |
| 69 | + do_rescale = True |
| 70 | + rescale_factor = 1 / 255 |
| 71 | + do_pad = True |
| 72 | + pad_size = 8 |
| 73 | + valid_kwargs = Swin2SRFastImageProcessorKwargs |
| 74 | + |
| 75 | + def __init__(self, **kwargs: Unpack[Swin2SRFastImageProcessorKwargs]): |
| 76 | + super().__init__(**kwargs) |
| 77 | + |
| 78 | + @add_start_docstrings( |
| 79 | + BASE_IMAGE_PROCESSOR_FAST_DOCSTRING_PREPROCESS, |
| 80 | + """ |
| 81 | + do_pad (`bool`, *optional*, defaults to `True`): |
| 82 | + Whether to pad the image to make the height and width divisible by `window_size`. |
| 83 | + pad_size (`int`, *optional*, defaults to `8`): |
| 84 | + The size of the sliding window for the local attention. |
| 85 | + """, |
| 86 | + ) |
| 87 | + def preprocess(self, images: ImageInput, **kwargs: Unpack[Swin2SRFastImageProcessorKwargs]) -> BatchFeature: |
| 88 | + return super().preprocess(images, **kwargs) |
| 89 | + |
| 90 | + def pad(self, images: "torch.Tensor", size: int) -> "torch.Tensor": |
| 91 | + """ |
| 92 | + Pad an image to make the height and width divisible by `size`. |
| 93 | +
|
| 94 | + Args: |
| 95 | + images (`torch.Tensor`): |
| 96 | + Images to pad. |
| 97 | + size (`int`): |
| 98 | + The size to make the height and width divisible by. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + `torch.Tensor`: The padded images. |
| 102 | + """ |
| 103 | + height, width = get_image_size(images, ChannelDimension.FIRST) |
| 104 | + pad_height = (height // size + 1) * size - height |
| 105 | + pad_width = (width // size + 1) * size - width |
| 106 | + |
| 107 | + return F.pad( |
| 108 | + images, |
| 109 | + (0, 0, pad_width, pad_height), |
| 110 | + padding_mode="symmetric", |
| 111 | + ) |
| 112 | + |
| 113 | + def _preprocess( |
| 114 | + self, |
| 115 | + images: List["torch.Tensor"], |
| 116 | + do_rescale: bool, |
| 117 | + rescale_factor: float, |
| 118 | + do_pad: bool, |
| 119 | + pad_size: int, |
| 120 | + return_tensors: Optional[Union[str, TensorType]], |
| 121 | + interpolation: Optional["F.InterpolationMode"], |
| 122 | + **kwargs, |
| 123 | + ) -> BatchFeature: |
| 124 | + grouped_images, grouped_images_index = group_images_by_shape(images) |
| 125 | + processed_image_grouped = {} |
| 126 | + for shape, stacked_images in grouped_images.items(): |
| 127 | + if do_rescale: |
| 128 | + stacked_images = self.rescale(stacked_images, scale=rescale_factor) |
| 129 | + if do_pad: |
| 130 | + stacked_images = self.pad(stacked_images, size=pad_size) |
| 131 | + processed_image_grouped[shape] = stacked_images |
| 132 | + processed_images = reorder_images(processed_image_grouped, grouped_images_index) |
| 133 | + processed_images = torch.stack(processed_images, dim=0) if return_tensors else processed_images |
| 134 | + |
| 135 | + return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) |
| 136 | + |
| 137 | + |
| 138 | +__all__ = ["Swin2SRImageProcessorFast"] |
0 commit comments