






"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase_					=      logging.get_logger(__name__)







class 			UpperCamelCase_       (__A				,						__A     ):
	__magic_name__	      =				'''maskformer-swin'''

	__magic_name__	      =				{
	    '''num_attention_heads''': '''num_heads''',
	    '''num_hidden_layers''': '''num_layers''',
	}





	def __init__(      self					: Any	,	lowerCAmelCase_					: Dict=224	,	lowerCAmelCase_					: Any=4	,	lowerCAmelCase_					: Optional[Any]=3	,	lowerCAmelCase_					: List[str]=96	,	lowerCAmelCase_					: List[Any]=[2, 2, 6, 2]	,	lowerCAmelCase_					: str=[3, 6, 12, 24]	,	lowerCAmelCase_					: str=7	,	lowerCAmelCase_					: Union[str, Any]=4.0	,	lowerCAmelCase_					: List[str]=True	,	lowerCAmelCase_					: Union[str, Any]=0.0	,	lowerCAmelCase_					: Union[str, Any]=0.0	,	lowerCAmelCase_					: List[Any]=0.1	,	lowerCAmelCase_					: Union[str, Any]="gelu"	,	lowerCAmelCase_					: Optional[int]=False	,	lowerCAmelCase_					: Tuple=0.0_2	,	lowerCAmelCase_					: str=1e-5	,	lowerCAmelCase_					: List[str]=None	,	lowerCAmelCase_					: Dict=None	,	**lowerCAmelCase_					: List[Any]	,	)						->			Tuple:
				super().__init__(**lowerCAmelCase_						)

				UpperCAmelCase_     : int			     =					image_size
				UpperCAmelCase_     : Tuple			     =					patch_size
				UpperCAmelCase_     : str			     =					num_channels
				UpperCAmelCase_     : Optional[int]			     =					embed_dim
				UpperCAmelCase_     : List[Any]			     =					depths
				UpperCAmelCase_     : Dict			     =					len(lowerCAmelCase_						)
				UpperCAmelCase_     : Optional[Any]			     =					num_heads
				UpperCAmelCase_     : List[str]			     =					window_size
				UpperCAmelCase_     : Any			     =					mlp_ratio
				UpperCAmelCase_     : int			     =					qkv_bias
				UpperCAmelCase_     : Tuple			     =					hidden_dropout_prob
				UpperCAmelCase_     : Tuple			     =					attention_probs_dropout_prob
				UpperCAmelCase_     : Optional[Any]			     =					drop_path_rate
				UpperCAmelCase_     : Any			     =					hidden_act
				UpperCAmelCase_     : Optional[int]			     =					use_absolute_embeddings
				UpperCAmelCase_     : Dict			     =					layer_norm_eps
				UpperCAmelCase_     : Optional[Any]			     =					initializer_range
				# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
				# this indicates the channel dimension after the last stage of the model
				UpperCAmelCase_     : Union[str, Any]			     =					int(embed_dim * 2 ** (len(lowerCAmelCase_						) - 1)						)
				UpperCAmelCase_     : Union[str, Any]			     =					["stem"] + [f"""stage{idx}""" for idx in range(1	,	len(lowerCAmelCase_						) + 1						)]
				UpperCAmelCase_     ,	UpperCAmelCase_     : List[str]			     =					get_aligned_output_features_output_indices(
				    out_features=lowerCAmelCase_	,	out_indices=lowerCAmelCase_	,	stage_names=self.stage_names						)











"""simple docstring"""
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
			from PIL import Image

			from ..image_utils import load_image

if is_tf_available():
			import tensorflow as tf

			from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
			from ..tf_utils import stable_softmax

if is_torch_available():
			from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

lowerCamelCase_					=      logging.get_logger(__name__)







@add_end_docstrings(__A     )
class 			UpperCamelCase_       (__A     ):





	def __init__(      self					: int	,	*lowerCAmelCase_					: Tuple	,	**lowerCAmelCase_					: List[str]						)						->			Optional[Any]:
				super().__init__(*lowerCAmelCase_	,	**lowerCAmelCase_						)
				requires_backends(self	,	"vision"						)
				self.check_model_type(
				    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
				    if self.framework == "tf"
				    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING						)





	def      _SCREAMING_SNAKE_CASE      (      self					: str	,	lowerCAmelCase_					: Optional[int]=None						)						->			List[Any]:
				UpperCAmelCase_     : str			     =					{}
				if top_k is not None:
							UpperCAmelCase_     : List[str]			     =					top_k
				return {}, {}, postprocess_params





	def __call__(      self					: str	,	lowerCAmelCase_					: Union[str, List[str], "Image.Image", List["Image.Image"]]	,	**lowerCAmelCase_					: Any						)						->			Tuple:
				return super().__call__(lowerCAmelCase_	,	**lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: str	,	lowerCAmelCase_					: str						)						->			Any:
				UpperCAmelCase_     : Tuple			     =					load_image(lowerCAmelCase_						)
				UpperCAmelCase_     : Dict			     =					self.image_processor(images=lowerCAmelCase_	,	return_tensors=self.framework						)
				return model_inputs





	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]	,	lowerCAmelCase_					: Dict						)						->			str:
				UpperCAmelCase_     : Any			     =					self.model(**lowerCAmelCase_						)
				return model_outputs





	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: Optional[int]=5						)						->			Any:
				if top_k > self.model.config.num_labels:
							UpperCAmelCase_     : int			     =					self.model.config.num_labels

				if self.framework == "pt":
							UpperCAmelCase_     : str			     =					model_outputs.logits.softmax(-1						)[0]
							UpperCAmelCase_     ,	UpperCAmelCase_     : Tuple			     =					probs.topk(lowerCAmelCase_						)
				elif self.framework == "tf":
							UpperCAmelCase_     : str			     =					stable_softmax(model_outputs.logits	,	axis=-1						)[0]
							UpperCAmelCase_     : Union[str, Any]			     =					tf.math.top_k(lowerCAmelCase_	,	k=lowerCAmelCase_						)
							UpperCAmelCase_     ,	UpperCAmelCase_     : List[Any]			     =					topk.values.numpy(), topk.indices.numpy()
				else:
							raise ValueError(f"""Unsupported framework: {self.framework}"""						)

				UpperCAmelCase_     : int			     =					scores.tolist()
				UpperCAmelCase_     : Optional[Any]			     =					ids.tolist()
				return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCAmelCase_	,	lowerCAmelCase_						)]



