





'''simple docstring'''


from collections import UserDict
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_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

lowerCAmelCase :					str 							=logging.get_logger(__name__)
@add_end_docstrings(_a							)
class 				a_       (    _a							):





     def __init__(			self		: int     ,				**lowercase		: int						):





        """simple docstring"""




        super().__init__(**__lowerCAmelCase						)

        requires_backends(self     ,				"vision"						)
        self.check_model_type(
            TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
            if self.framework == "tf"
            else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING						)





     def __call__(			self		: Dict     ,				lowercase		: Optional[int]     ,				**lowercase		: Optional[Any]						):





        """simple docstring"""




        return super().__call__(__lowerCAmelCase     ,				**__lowerCAmelCase						)





     def 							lowercase__				(			self		: Optional[Any]     ,				**lowercase		: Tuple						):





        """simple docstring"""




        lowercase_      :List[Any]         =			{}
        if "candidate_labels" in kwargs:
           lowercase_      :Optional[Any]         =			kwargs["candidate_labels"]
        if "hypothesis_template" in kwargs:
           lowercase_      :str         =			kwargs["hypothesis_template"]

        return preprocess_params, {}, {}





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: str     ,				lowercase		: Any=None     ,				lowercase		: Union[str, Any]="This is a photo of {}."						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			load_image(__lowerCAmelCase						)
        lowercase_      :Union[str, Any]         =			self.image_processor(images=[image]     ,				return_tensors=self.framework						)
        lowercase_      :Union[str, Any]         =			candidate_labels
        lowercase_      :str         =			[hypothesis_template.format(__lowerCAmelCase						) for x in candidate_labels]
        lowercase_      :Tuple         =			self.tokenizer(__lowerCAmelCase     ,				return_tensors=self.framework     ,				padding=__lowerCAmelCase						)
        lowercase_      :Optional[int]         =			[text_inputs]
        return inputs





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: List[Any]						):





        """simple docstring"""




        lowercase_      :Dict         =			model_inputs.pop("candidate_labels"						)
        lowercase_      :Union[str, Any]         =			model_inputs.pop("text_inputs"						)
        if isinstance(text_inputs[0]     ,				__lowerCAmelCase						):
           lowercase_      :int         =			text_inputs[0]
        else:
           # Batching case.
           lowercase_      :int         =			text_inputs[0][0]

        lowercase_      :str         =			self.model(**__lowerCAmelCase     ,				**__lowerCAmelCase						)

        lowercase_      :Optional[Any]         =			{
            "candidate_labels": candidate_labels,
            "logits": outputs.logits_per_image,
        }
        return model_outputs







     def 							lowercase__				(			self		: List[str]     ,				lowercase		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :List[Any]         =			model_outputs.pop("candidate_labels"						)
        lowercase_      :Tuple         =			model_outputs["logits"][0]
        if self.framework == "pt":
           lowercase_      :int         =			logits.softmax(dim=-1						).squeeze(-1						)
           lowercase_      :Dict         =			probs.tolist()
           if not isinstance(__lowerCAmelCase     ,				__lowerCAmelCase						):
              lowercase_      :Union[str, Any]         =			[scores]
        elif self.framework == "tf":
           lowercase_      :List[str]         =			stable_softmax(__lowerCAmelCase     ,				axis=-1						)
           lowercase_      :Dict         =			probs.numpy().tolist()
        else:
           raise ValueError(F'Unsupported framework: {self.framework}'						)

        lowercase_      :int         =			[
            {"score": score, "label": candidate_label}
            for score, candidate_label in sorted(zip(__lowerCAmelCase     ,				__lowerCAmelCase						)     ,				key=lambda lowercase						: -x[0]						)
        ]
        return result









'''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


lowerCAmelCase :					Any 							={
    '''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
}

lowerCAmelCase :					int 							=logging.get_logger(__name__)
class 				a_       (    _lowerCAmelCase							):
     __A								=      "maskformer"
     __A								=      {"hidden_size": "mask_feature_size"}
     __A								=      ["resnet", "swin"]
     __A								=      ["detr"]





     def __init__(			self		: List[Any]     ,				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		: Any     ,				):





        """simple docstring"""




        if backbone_config is None:
           # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
           lowercase_      :Any         =			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						):
           lowercase_      :Optional[int]         =			backbone_config.pop("model_type"						)
           lowercase_      :Optional[int]         =			CONFIG_MAPPING[backbone_model_type]
           lowercase_      :int         =			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
           lowercase_      :Optional[Any]         =			DetrConfig()
        else:
           # verify that the decoder is supported
           lowercase_      :Tuple         =			(
               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						):
              lowercase_      :str         =			CONFIG_MAPPING[decoder_type]
              lowercase_      :List[str]         =			config_class.from_dict(lowercase						)

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

        lowercase_      :Optional[Any]         =			self.decoder_config.encoder_attention_heads
        lowercase_      :int         =			self.decoder_config.num_hidden_layers
        super().__init__(**lowercase						)





     @classmethod
     def 							lowercase__				(			cls		: Tuple     ,				lowercase		: PretrainedConfig     ,				lowercase		: PretrainedConfig     ,				**lowercase		: Union[str, Any]						):





        """simple docstring"""




        return cls(
            backbone_config=lowercase     ,				decoder_config=lowercase     ,				**lowercase     ,				)







     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




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


