



"""simple docstring"""





from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
  from PIL import Image

  from ..image_utils import load_image

if is_torch_available():
  import torch

  from transformers.modeling_outputs import BaseModelOutput

  from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCamelCase  			=		logging.get_logger(__name__)
@add_end_docstrings(SCREAMING_SNAKE_CASE )
class  lowercase__  (       SCREAMING_SNAKE_CASE ):







       '''simple docstring'''


       def __init__(				self     :    Union[str, Any]						,							**_UpperCAmelCase     :    Optional[int]       )					->		str:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase       )

             if self.framework == "tf":
                   raise ValueError(F"""The {self.__class__} is only available in PyTorch."""       )

             requires_backends(self						,							"vision"       )
             self.check_model_type(_UpperCAmelCase       )



       def __call__(				self     :    int						,							_UpperCAmelCase     :    Union[str, "Image.Image", List[Dict[str, Any]]]						,							_UpperCAmelCase     :    Union[str, List[str]] = None						,							**_UpperCAmelCase     :    Optional[int]						,							)					->		List[Any]:


             '''simple docstring'''




             if "text_queries" in kwargs:
                   UpperCAmelCase_     					=				kwargs.pop("text_queries"       )

             if isinstance(_UpperCAmelCase						,							(str, Image.Image)       ):
                   UpperCAmelCase_     					=				{"image": image, "candidate_labels": candidate_labels}
             else:
                   UpperCAmelCase_     					=				image
             UpperCAmelCase_     					=				super().__call__(_UpperCAmelCase						,							**_UpperCAmelCase       )
             return results



       def 	lowercase__							(				self     :    str						,							**_UpperCAmelCase     :    List[Any]       )					->		List[str]:


             '''simple docstring'''




             UpperCAmelCase_     					=				{}
             if "threshold" in kwargs:
                   UpperCAmelCase_     					=				kwargs["threshold"]
             if "top_k" in kwargs:
                   UpperCAmelCase_     					=				kwargs["top_k"]
             return {}, {}, postprocess_params



       def 	lowercase__							(				self     :    int						,							_UpperCAmelCase     :    int       )					->		Any:


             '''simple docstring'''




             UpperCAmelCase_     					=				load_image(inputs["image"]       )
             UpperCAmelCase_     					=				inputs["candidate_labels"]
             if isinstance(_UpperCAmelCase						,							_UpperCAmelCase       ):
                   UpperCAmelCase_     					=				candidate_labels.split(","       )

             UpperCAmelCase_     					=				torch.tensor([[image.height, image.width]]						,							dtype=torch.intaa       )
             for i, candidate_label in enumerate(_UpperCAmelCase       ):
                   UpperCAmelCase_     					=				self.tokenizer(_UpperCAmelCase						,							return_tensors=self.framework       )
                   UpperCAmelCase_     					=				self.image_processor(_UpperCAmelCase						,							return_tensors=self.framework       )
                   yield {
                       "is_last": i == len(_UpperCAmelCase       ) - 1,
                       "target_size": target_size,
                       "candidate_label": candidate_label,
                       **text_inputs,
                       **image_features,
                   }



       def 	lowercase__							(				self     :    int						,							_UpperCAmelCase     :    List[Any]       )					->		int:


             '''simple docstring'''




             UpperCAmelCase_     					=				model_inputs.pop("target_size"       )
             UpperCAmelCase_     					=				model_inputs.pop("candidate_label"       )
             UpperCAmelCase_     					=				model_inputs.pop("is_last"       )

             UpperCAmelCase_     					=				self.model(**_UpperCAmelCase       )

             UpperCAmelCase_     					=				{"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
             return model_outputs



       def 	lowercase__							(				self     :    str						,							_UpperCAmelCase     :    List[str]						,							_UpperCAmelCase     :    Any=0.1						,							_UpperCAmelCase     :    List[str]=None       )					->		int:


             '''simple docstring'''




             UpperCAmelCase_     					=				[]
             for model_output in model_outputs:
                   UpperCAmelCase_     					=				model_output["candidate_label"]
                   UpperCAmelCase_     					=				BaseModelOutput(_UpperCAmelCase       )
                   UpperCAmelCase_     					=				self.image_processor.post_process_object_detection(
                       outputs=_UpperCAmelCase						,							threshold=_UpperCAmelCase						,							target_sizes=model_output["target_size"]       )[0]

                   for index in outputs["scores"].nonzero():
                         UpperCAmelCase_     					=				outputs["scores"][index].item()
                         UpperCAmelCase_     					=				self._get_bounding_box(outputs["boxes"][index][0]       )

                         UpperCAmelCase_     					=				{"score": score, "label": label, "box": box}
                         results.append(_UpperCAmelCase       )

             UpperCAmelCase_     					=				sorted(_UpperCAmelCase						,							key=lambda _UpperCAmelCase       : x["score"]						,							reverse=_UpperCAmelCase       )
             if top_k:
                   UpperCAmelCase_     					=				results[:top_k]

             return results







       def 	lowercase__							(				self     :    str						,							_UpperCAmelCase     :    "torch.Tensor"       )					->		Dict[str, int]:


             '''simple docstring'''




             if self.framework != "pt":
                   raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch."       )
             UpperCAmelCase_					,       UpperCAmelCase_					,       UpperCAmelCase_					,       UpperCAmelCase_     					=				box.int().tolist()
             UpperCAmelCase_     					=				{
                 "xmin": xmin,
                 "ymin": ymin,
                 "xmax": xmax,
                 "ymax": ymax,
             }
             return bbox










'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase_							:	int	     =		logging.get_logger(__name__)

UpperCAmelCase_							:	Dict	     =		{
    '''microsoft/swin-tiny-patch4-window7-224''': (
        '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'''
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}





class      lowerCAmelCase (			__lowerCAmelCase					, __lowerCAmelCase):
       __lowercase	:   List[Any]										=      '''swin'''

       __lowercase	:   str										=      {
           '''num_attention_heads''': '''num_heads''',
           '''num_hidden_layers''': '''num_layers''',
       }


       def __init__( self     ,	__SCREAMING_SNAKE_CASE=224     ,	__SCREAMING_SNAKE_CASE=4     ,	__SCREAMING_SNAKE_CASE=3     ,	__SCREAMING_SNAKE_CASE=96     ,	__SCREAMING_SNAKE_CASE=[2, 2, 6, 2]     ,	__SCREAMING_SNAKE_CASE=[3, 6, 12, 24]     ,	__SCREAMING_SNAKE_CASE=7     ,	__SCREAMING_SNAKE_CASE=4.0     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE="gelu"     ,	__SCREAMING_SNAKE_CASE=False     ,	__SCREAMING_SNAKE_CASE=0.02     ,	__SCREAMING_SNAKE_CASE=1E-5     ,	__SCREAMING_SNAKE_CASE=32     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE     ,	)   ->	int:



            '''simple docstring'''




            super().__init__(**__SCREAMING_SNAKE_CASE	)

            __snake_case            =	image_size
            __snake_case            =	patch_size
            __snake_case            =	num_channels
            __snake_case            =	embed_dim
            __snake_case            =	depths
            __snake_case            =	len(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	num_heads
            __snake_case            =	window_size
            __snake_case            =	mlp_ratio
            __snake_case            =	qkv_bias
            __snake_case            =	hidden_dropout_prob
            __snake_case            =	attention_probs_dropout_prob
            __snake_case            =	drop_path_rate
            __snake_case            =	hidden_act
            __snake_case            =	use_absolute_embeddings
            __snake_case            =	layer_norm_eps
            __snake_case            =	initializer_range
            __snake_case            =	encoder_stride
            # 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
            __snake_case            =	int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE	) - 1)	)
            __snake_case            =	['''stem'''] + [F'''stage{idx}''' for idx in range(1     ,	len(__SCREAMING_SNAKE_CASE	) + 1	)]
            __snake_case       ,     __snake_case            =	get_aligned_output_features_output_indices(
                out_features=__SCREAMING_SNAKE_CASE     ,	out_indices=__SCREAMING_SNAKE_CASE     ,	stage_names=self.stage_names	)





class      lowerCAmelCase (			__lowerCAmelCase):
       __lowercase	:   Optional[int]										=      version.parse('''1.11''')


       @property
       def    lowerCAmelCase   ( self	)   ->	Mapping[str, Mapping[int, str]]:



            '''simple docstring'''




            return OrderedDict(
                [
                    ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                ]	)




       @property
       def    lowerCAmelCase   ( self	)   ->	float:



            '''simple docstring'''




            return 1E-4






