# Copyright 2022 The HuggingFace Inc. 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. """Image processor class for MobileViT.""" from typing import Union import torch from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_outputs import SemanticSegmentationPostProcessorOutput from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, logging, requires_backends, ) logger = logging.get_logger(__name__) class MobileVitImageProcessorKwargs(ImagesKwargs, total=False): """ do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): Whether to flip the color channels from RGB to BGR or vice versa. do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255. """ do_flip_channel_order: bool do_reduce_labels: bool @auto_docstring class MobileViTImageProcessor(TorchvisionBackend): """Torchvision backend for MobileViT with flip_channel_order and reduce_label support.""" valid_kwargs = MobileVitImageProcessorKwargs resample = PILImageResampling.BICUBIC image_mean = IMAGENET_STANDARD_MEAN image_std = IMAGENET_STANDARD_STD size = {"shortest_edge": 224} default_to_square = False crop_size = {"height": 256, "width": 256} do_resize = True do_center_crop = True do_rescale = True do_normalize = None do_convert_rgb = None do_flip_channel_order = True do_reduce_labels = False def __init__(self, **kwargs: Unpack[MobileVitImageProcessorKwargs]): super().__init__(**kwargs) @auto_docstring def preprocess( self, images: ImageInput, segmentation_maps: ImageInput | None = None, **kwargs: Unpack[MobileVitImageProcessorKwargs], ) -> BatchFeature: r""" segmentation_maps (`ImageInput`, *optional*): The segmentation maps to preprocess. """ return super().preprocess(images, segmentation_maps, **kwargs) def _preprocess_image_like_inputs( self, images: ImageInput, segmentation_maps: ImageInput | None, do_convert_rgb: bool, input_data_format: ChannelDimension, return_tensors: str | TensorType | None, device: Union[str, "torch.device"] | None = None, **kwargs, ) -> BatchFeature: """Handle extra inputs beyond images.""" images = self._prepare_image_like_inputs( images=images, do_convert_rgb=do_convert_rgb, input_data_format=input_data_format, device=device ) images_kwargs = kwargs.copy() images_kwargs["do_reduce_labels"] = False data = {} data["pixel_values"] = self._preprocess(images, **images_kwargs) if segmentation_maps is not None: processed_segmentation_maps = self._prepare_image_like_inputs( images=segmentation_maps, expected_ndims=2, do_convert_rgb=False, input_data_format=ChannelDimension.FIRST, ) segmentation_maps_kwargs = kwargs.copy() segmentation_maps_kwargs.update( { "do_rescale": False, "do_flip_channel_order": False, # Nearest interpolation is used for segmentation maps instead of BICUBIC. "resample": PILImageResampling.NEAREST, } ) processed_segmentation_maps = self._preprocess( images=processed_segmentation_maps, **segmentation_maps_kwargs ) processed_segmentation_maps = [ processed_segmentation_map.squeeze(0).to(torch.int64) for processed_segmentation_map in processed_segmentation_maps ] data["labels"] = processed_segmentation_maps return BatchFeature(data=data, tensor_type=return_tensors) def reduce_label(self, labels: list["torch.Tensor"]) -> list["torch.Tensor"]: """Reduce label values by 1, replacing 0 with 255.""" for idx in range(len(labels)): label = labels[idx] label = torch.where(label == 0, torch.tensor(255, dtype=label.dtype, device=label.device), label) label = label - 1 label = torch.where(label == 254, torch.tensor(255, dtype=label.dtype, device=label.device), label) labels[idx] = label return labels def flip_channel_order(self, images: "torch.Tensor") -> "torch.Tensor": """Flip RGB to BGR or vice versa.""" if images.ndim == 3: # Single image: (C, H, W) flipped = images.clone() flipped[0:3] = images[[2, 1, 0]] return flipped elif images.ndim == 4: # Batched images: (B, C, H, W) flipped = images.clone() flipped[:, 0:3] = images[:, [2, 1, 0]] return flipped return images def _preprocess( self, images: list["torch.Tensor"], do_resize: bool, size: SizeDict, resample: "PILImageResampling | tvF.InterpolationMode | int | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, rescale_factor: float, disable_grouping: bool | None, do_reduce_labels: bool = False, do_flip_channel_order: bool = True, **kwargs, ) -> list["torch.Tensor"]: """Custom preprocessing for MobileViT.""" if do_reduce_labels: images = self.reduce_label(images) grouped_images, grouped_images_index = group_images_by_shape(images, disable_grouping=disable_grouping) resized_images_grouped = {} for shape, stacked_images in grouped_images.items(): if do_resize: stacked_images = self.resize(stacked_images, size, resample) resized_images_grouped[shape] = stacked_images resized_images = reorder_images(resized_images_grouped, grouped_images_index) grouped_images, grouped_images_index = group_images_by_shape(resized_images, disable_grouping=disable_grouping) processed_images_grouped = {} for shape, stacked_images in grouped_images.items(): if do_center_crop: stacked_images = self.center_crop(stacked_images, crop_size) if do_rescale: stacked_images = self.rescale(stacked_images, rescale_factor) if do_flip_channel_order: stacked_images = self.flip_channel_order(stacked_images) processed_images_grouped[shape] = stacked_images processed_images = reorder_images(processed_images_grouped, grouped_images_index) return processed_images def post_process_semantic_segmentation( self, outputs, target_sizes: list[tuple[int, int]] | None = None, return_segmentation_scores: bool = False, ) -> "list[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]": """ Converts the output of [`MobileViTForSemanticSegmentation`] into semantic segmentation maps. Args: outputs ([`MobileViTForSemanticSegmentation`]): Raw outputs of the model. target_sizes (`list[tuple[int, int]]`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. return_segmentation_scores (`bool`, *optional*, defaults to `False`): Whether to return segmentation scores alongside the segmentation map. When `True`, each element of the returned list is a [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`). Returns: `list[torch.Tensor]` or `list[SemanticSegmentationPostProcessorOutput]`: When `return_segmentation_scores=False` (default), a list of length `batch_size` where each item is a segmentation map of shape `(height, width)` with class IDs. When `return_segmentation_scores=True`, a list of [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`). In both cases, `(height, width)` corresponds to the target size (if `target_sizes` is specified). """ requires_backends(self, "torch") logits = outputs.logits batch_size = len(logits) if target_sizes is not None: if len(logits) != len(target_sizes): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) if isinstance(target_sizes, torch.Tensor): target_sizes = target_sizes.tolist() semantic_segmentation = [] for idx in range(batch_size): resized_logits = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0), size=target_sizes[idx], mode="bilinear", align_corners=False ) semantic_segmentation.append( SemanticSegmentationPostProcessorOutput( data={ "segmentation": resized_logits[0].argmax(dim=0), "segmentation_scores": resized_logits[0], } ) ) else: semantic_segmentation = [ SemanticSegmentationPostProcessorOutput( data={"segmentation": logits[i].argmax(dim=0), "segmentation_scores": logits[i]} ) for i in range(batch_size) ] if not return_segmentation_scores: semantic_segmentation = [item.segmentation for item in semantic_segmentation] return semantic_segmentation __all__ = ["MobileViTImageProcessor"]