





"""simple docstring"""





import copy
from typing import Dict, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING
from ..detr import DetrConfig
from ..swin import SwinConfig


_UpperCamelCase					      =   {
    'facebook/maskformer-swin-base-ade': (
        'https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json'
    )
    # See all MaskFormer models at https://huggingface.co/models?filter=maskformer
}

_UpperCamelCase					      =   logging.get_logger(__name__)
class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
				"""simple docstring"""
				__snake_case :							List[Any]									=		"""maskformer"""
				__snake_case :							List[Any]									=		{"""hidden_size""": """mask_feature_size"""}
				__snake_case :							Tuple									=		["""resnet""", """swin"""]
				__snake_case :							Tuple									=		["""detr"""]




				def __init__(  self	:Dict    ,     __lowercase	:int = 256    ,     __lowercase	:int = 256    ,     __lowercase	:float = 0.1    ,     __lowercase	:bool = False    ,     __lowercase	:Optional[Dict] = None    ,     __lowercase	:Optional[Dict] = None    ,     __lowercase	:float = 0.02    ,     __lowercase	:float = 1.0    ,     __lowercase	:float = 1.0    ,     __lowercase	:float = 1.0    ,     __lowercase	:float = 20.0    ,     __lowercase	:Optional[bool] = None    ,     **__lowercase	:int    ,     ):
											if backbone_config is None:
																		# fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
																		__lowerCamelCase   :   Tuple       =SwinConfig(
																		    image_size=384    ,     in_channels=3    ,     patch_size=4    ,     embed_dim=128    ,     depths=[2, 2, 18, 2]    ,     num_heads=[4, 8, 16, 32]    ,     window_size=12    ,     drop_path_rate=0.3    ,     out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4''']    ,     )

											if isinstance(__lowercase    ,     __lowercase      ):
																		__lowerCamelCase   :   Optional[int]       =backbone_config.pop('''model_type'''      )
																		__lowerCamelCase   :   Tuple       =CONFIG_MAPPING[backbone_model_type]
																		__lowerCamelCase   :   Dict       =config_class.from_dict(__lowercase      )

											# verify that the backbone is supported
											if backbone_config.model_type not in self.backbones_supported:
																		logger.warning_once(
																		    f'Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. '
																		    f'Supported model types: {",".join(self.backbones_supported      )}'      )

											if decoder_config is None:
																		# fall back to https://huggingface.co/facebook/detr-resnet-50
																		__lowerCamelCase   :   Tuple       =DetrConfig()
											else:
																		# verify that the decoder is supported
																		__lowerCamelCase   :   Any       =(
																		    decoder_config.pop('''model_type'''      ) if isinstance(__lowercase    ,     __lowercase      ) else decoder_config.model_type
																		)
																		if decoder_type not in self.decoders_supported:
																									raise ValueError(
																									    f'Transformer Decoder {decoder_type} not supported, please use one of'
																									    f' {",".join(self.decoders_supported      )}'      )
																		if isinstance(__lowercase    ,     __lowercase      ):
																									__lowerCamelCase   :   Optional[Any]       =CONFIG_MAPPING[decoder_type]
																									__lowerCamelCase   :   str       =config_class.from_dict(__lowercase      )

											__lowerCamelCase   :   Optional[int]       =backbone_config
											__lowerCamelCase   :   int       =decoder_config
											# main feature dimension for the model
											__lowerCamelCase   :   List[Any]       =fpn_feature_size
											__lowerCamelCase   :   List[Any]       =mask_feature_size
											# initializer
											__lowerCamelCase   :   int       =init_std
											__lowerCamelCase   :   Tuple       =init_xavier_std
											# Hungarian matcher && loss
											__lowerCamelCase   :   List[Any]       =cross_entropy_weight
											__lowerCamelCase   :   Optional[Any]       =dice_weight
											__lowerCamelCase   :   Any       =mask_weight
											__lowerCamelCase   :   Any       =use_auxiliary_loss
											__lowerCamelCase   :   List[Any]       =no_object_weight
											__lowerCamelCase   :   Optional[int]       =output_auxiliary_logits

											__lowerCamelCase   :   str       =self.decoder_config.encoder_attention_heads
											__lowerCamelCase   :   str       =self.decoder_config.num_hidden_layers
											super().__init__(**__lowercase      )




				@classmethod
				def        __lowercase	(  cls	:List[str]    ,     __lowercase	:PretrainedConfig    ,     __lowercase	:PretrainedConfig    ,     **__lowercase	:Optional[int]      ):
											return cls(
											    backbone_config=__lowercase    ,     decoder_config=__lowercase    ,     **__lowercase    ,     )




				def        __lowercase	(  self	:Optional[Any]      ):
											__lowerCamelCase   :   Any       =copy.deepcopy(self.__dict__      )
											__lowerCamelCase   :   int       =self.backbone_config.to_dict()
											__lowerCamelCase   :   Any       =self.decoder_config.to_dict()
											__lowerCamelCase   :   str       =self.__class__.model_type
											return output











"""simple docstring"""





from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
							import torch


_UpperCamelCase					      =   logging.get_logger(__name__)
class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
				"""simple docstring"""
				__snake_case :							Optional[int]									=		["""pixel_values"""]




				def __init__(  self	:Optional[Any]    ,     __lowercase	:bool = True    ,     __lowercase	:Optional[Dict[str, int]] = None    ,     __lowercase	:PILImageResampling = PILImageResampling.BILINEAR    ,     __lowercase	:bool = True    ,     __lowercase	:Dict[str, int] = None    ,     __lowercase	:bool = True    ,     __lowercase	:Union[int, float] = 1 / 255    ,     __lowercase	:bool = True    ,     __lowercase	:Optional[Union[float, List[float]]] = None    ,     __lowercase	:Optional[Union[float, List[float]]] = None    ,     **__lowercase	:Tuple    ,     ):
											super().__init__(**__lowercase      )
											__lowerCamelCase   :   int       =size if size is not None else {'''shortest_edge''': 256}
											__lowerCamelCase   :   int       =get_size_dict(__lowercase    ,     default_to_square=__lowercase      )
											__lowerCamelCase   :   Optional[Any]       =crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
											__lowerCamelCase   :   List[str]       =get_size_dict(__lowercase    ,     param_name='''crop_size'''      )
											__lowerCamelCase   :   List[str]       =do_resize
											__lowerCamelCase   :   Optional[Any]       =size
											__lowerCamelCase   :   Tuple       =resample
											__lowerCamelCase   :   int       =do_center_crop
											__lowerCamelCase   :   Optional[Any]       =crop_size
											__lowerCamelCase   :   int       =do_rescale
											__lowerCamelCase   :   str       =rescale_factor
											__lowerCamelCase   :   int       =do_normalize
											__lowerCamelCase   :   Union[str, Any]       =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
											__lowerCamelCase   :   Tuple       =image_std if image_std is not None else IMAGENET_STANDARD_STD




				def        __lowercase	(  self	:Union[str, Any]    ,     __lowercase	:np.ndarray    ,     __lowercase	:Dict[str, int]    ,     __lowercase	:PILImageResampling = PILImageResampling.BICUBIC    ,     __lowercase	:Optional[Union[str, ChannelDimension]] = None    ,     **__lowercase	:str    ,     ):
											__lowerCamelCase   :   Tuple       =get_size_dict(__lowercase    ,     default_to_square=__lowercase      )
											if "shortest_edge" not in size:
																		raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'      )
											__lowerCamelCase   :   List[Any]       =get_resize_output_image_size(__lowercase    ,     size=size['''shortest_edge''']    ,     default_to_square=__lowercase      )
											return resize(__lowercase    ,     size=__lowercase    ,     resample=__lowercase    ,     data_format=__lowercase    ,     **__lowercase      )




				def        __lowercase	(  self	:Union[str, Any]    ,     __lowercase	:np.ndarray    ,     __lowercase	:Dict[str, int]    ,     __lowercase	:Optional[Union[str, ChannelDimension]] = None    ,     **__lowercase	:int    ,     ):
											__lowerCamelCase   :   List[Any]       =get_size_dict(__lowercase      )
											if "height" not in size or "width" not in size:
																		raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}'      )
											return center_crop(__lowercase    ,     size=(size['''height'''], size['''width'''])    ,     data_format=__lowercase    ,     **__lowercase      )




				def        __lowercase	(  self	:str    ,     __lowercase	:np.ndarray    ,     __lowercase	:float    ,     __lowercase	:Optional[Union[str, ChannelDimension]] = None    ,     **__lowercase	:str      ):
											return rescale(__lowercase    ,     scale=__lowercase    ,     data_format=__lowercase    ,     **__lowercase      )




				def        __lowercase	(  self	:Union[str, Any]    ,     __lowercase	:np.ndarray    ,     __lowercase	:Union[float, List[float]]    ,     __lowercase	:Union[float, List[float]]    ,     __lowercase	:Optional[Union[str, ChannelDimension]] = None    ,     **__lowercase	:Union[str, Any]    ,     ):
											return normalize(__lowercase    ,     mean=__lowercase    ,     std=__lowercase    ,     data_format=__lowercase    ,     **__lowercase      )




				def        __lowercase	(  self	:List[str]    ,     __lowercase	:ImageInput    ,     __lowercase	:Optional[bool] = None    ,     __lowercase	:Dict[str, int] = None    ,     __lowercase	:PILImageResampling = None    ,     __lowercase	:bool = None    ,     __lowercase	:Dict[str, int] = None    ,     __lowercase	:Optional[bool] = None    ,     __lowercase	:Optional[float] = None    ,     __lowercase	:Optional[bool] = None    ,     __lowercase	:Optional[Union[float, List[float]]] = None    ,     __lowercase	:Optional[Union[float, List[float]]] = None    ,     __lowercase	:Optional[Union[str, TensorType]] = None    ,     __lowercase	:Union[str, ChannelDimension] = ChannelDimension.FIRST    ,     **__lowercase	:Optional[Any]    ,     ):
											__lowerCamelCase   :   Optional[Any]       =do_resize if do_resize is not None else self.do_resize
											__lowerCamelCase   :   Optional[Any]       =size if size is not None else self.size
											__lowerCamelCase   :   List[str]       =get_size_dict(__lowercase    ,     default_to_square=__lowercase      )
											__lowerCamelCase   :   List[str]       =resample if resample is not None else self.resample
											__lowerCamelCase   :   Optional[int]       =do_center_crop if do_center_crop is not None else self.do_center_crop
											__lowerCamelCase   :   Any       =crop_size if crop_size is not None else self.crop_size
											__lowerCamelCase   :   Union[str, Any]       =get_size_dict(__lowercase    ,     param_name='''crop_size'''      )
											__lowerCamelCase   :   Any       =do_rescale if do_rescale is not None else self.do_rescale
											__lowerCamelCase   :   List[str]       =rescale_factor if rescale_factor is not None else self.rescale_factor
											__lowerCamelCase   :   List[Any]       =do_normalize if do_normalize is not None else self.do_normalize
											__lowerCamelCase   :   List[Any]       =image_mean if image_mean is not None else self.image_mean
											__lowerCamelCase   :   int       =image_std if image_std is not None else self.image_std

											__lowerCamelCase   :   int       =make_list_of_images(__lowercase      )

											if not valid_images(__lowercase      ):
																		raise ValueError(
																		    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
																		    '''torch.Tensor, tf.Tensor or jax.ndarray.'''      )

											if do_resize and size is None:
																		raise ValueError('''Size must be specified if do_resize is True.'''      )

											if do_center_crop and crop_size is None:
																		raise ValueError('''Crop size must be specified if do_center_crop is True.'''      )

											if do_rescale and rescale_factor is None:
																		raise ValueError('''Rescale factor must be specified if do_rescale is True.'''      )

											if do_normalize and (image_mean is None or image_std is None):
																		raise ValueError('''Image mean and std must be specified if do_normalize is True.'''      )

											# All transformations expect numpy arrays.
											__lowerCamelCase   :   int       =[to_numpy_array(__lowercase      ) for image in images]

											if do_resize:
																		__lowerCamelCase   :   Tuple       =[self.resize(image=__lowercase    ,     size=__lowercase    ,     resample=__lowercase      ) for image in images]

											if do_center_crop:
																		__lowerCamelCase   :   Any       =[self.center_crop(image=__lowercase    ,     size=__lowercase      ) for image in images]

											if do_rescale:
																		__lowerCamelCase   :   Optional[Any]       =[self.rescale(image=__lowercase    ,     scale=__lowercase      ) for image in images]

											if do_normalize:
																		__lowerCamelCase   :   Tuple       =[self.normalize(image=__lowercase    ,     mean=__lowercase    ,     std=__lowercase      ) for image in images]

											__lowerCamelCase   :   List[str]       =[to_channel_dimension_format(__lowercase    ,     __lowercase      ) for image in images]

											__lowerCamelCase   :   Union[str, Any]       ={'''pixel_values''': images}
											return BatchFeature(data=__lowercase    ,     tensor_type=__lowercase      )




				def        __lowercase	(  self	:Dict    ,     __lowercase	:Dict    ,     __lowercase	:List[Tuple] = None      ):
											__lowerCamelCase   :   Dict       =outputs.logits

											# Resize logits and compute semantic segmentation maps
											if target_sizes is not None:
																		if len(__lowercase      ) != len(__lowercase      ):
																									raise ValueError(
																									    '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''      )

																		if is_torch_tensor(__lowercase      ):
																									__lowerCamelCase   :   Optional[int]       =target_sizes.numpy()

																		__lowerCamelCase   :   List[Any]       =[]

																		for idx in range(len(__lowercase      )      ):
																									__lowerCamelCase   :   Optional[int]       =torch.nn.functional.interpolate(
																									    logits[idx].unsqueeze(dim=0      )    ,     size=target_sizes[idx]    ,     mode='''bilinear'''    ,     align_corners=__lowercase      )
																									__lowerCamelCase   :   Union[str, Any]       =resized_logits[0].argmax(dim=0      )
																									semantic_segmentation.append(__lowercase      )
											else:
																		__lowerCamelCase   :   Union[str, Any]       =logits.argmax(dim=1      )
																		__lowerCamelCase   :   Optional[int]       =[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]      )]

											return semantic_segmentation




