



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

UpperCamelCase__ =						logging.get_logger(__name__)
@add_end_docstrings(__lowerCamelCase			)
class 			_lowerCAmelCase   ( __lowerCamelCase			):






			"""simple docstring"""





			def __init__(				self	:       str			,     **lowercase	:       Union[str, Any]   )						->					Any:
									"""simple docstring"""







									super().__init__(**SCREAMING_SNAKE_CASE_   )

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

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





			def __call__(				self	:       str			,     lowercase	:       Tuple			,     lowercase	:       Dict = None			,     **lowercase	:       Any			,     )						->					Union[str, Any]:
									"""simple docstring"""







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

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





			def 		snake_case__			(				self	:       Any			,     **lowercase	:       List[str]   )						->					Optional[Any]:
									"""simple docstring"""







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





			def 		snake_case__			(				self	:       List[Any]			,     lowercase	:       int   )						->					int:
									"""simple docstring"""







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

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





			def 		snake_case__			(				self	:       Union[str, Any]			,     lowercase	:       Dict   )						->					Dict:
									"""simple docstring"""







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

									__lowercase					=				self.model(**SCREAMING_SNAKE_CASE_   )

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





			def 		snake_case__			(				self	:       Optional[Any]			,     lowercase	:       str			,     lowercase	:       int=0.1			,     lowercase	:       List[Any]=None   )						->					List[str]:
									"""simple docstring"""







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

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

																					__lowercase					=				{"""score""": score, """label""": label, """box""": box}
																					results.append(SCREAMING_SNAKE_CASE_   )

									__lowercase					=				sorted(SCREAMING_SNAKE_CASE_			,     key=lambda lowercase   : x["score"]			,     reverse=SCREAMING_SNAKE_CASE_   )
									if top_k:
															__lowercase					=				results[:top_k]

									return results







			def 		snake_case__			(				self	:       Any			,     lowercase	:       Dict   )						->					str:
									"""simple docstring"""







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










import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ =						logging.get_logger(__name__)

UpperCamelCase__ =						{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			lowercase__      :  Union[str, Any]      =			"""sew"""





			def __init__(				self	:       List[Any]			,     lowercase	:       int=32			,     lowercase	:       List[str]=768			,     lowercase	:       Dict=12			,     lowercase	:       str=12			,     lowercase	:       str=3_072			,     lowercase	:       Optional[int]=2			,     lowercase	:       List[str]="gelu"			,     lowercase	:       List[str]=0.1			,     lowercase	:       Tuple=0.1			,     lowercase	:       Dict=0.1			,     lowercase	:       Any=0.0			,     lowercase	:       Dict=0.1			,     lowercase	:       Optional[int]=0.1			,     lowercase	:       List[str]=0.02			,     lowercase	:       Dict=1E-5			,     lowercase	:       Tuple="group"			,     lowercase	:       int="gelu"			,     lowercase	:       Any=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)			,     lowercase	:       Optional[int]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)			,     lowercase	:       Any=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)			,     lowercase	:       List[str]=False			,     lowercase	:       Tuple=128			,     lowercase	:       int=16			,     lowercase	:       Union[str, Any]=True			,     lowercase	:       List[str]=0.05			,     lowercase	:       Optional[int]=10			,     lowercase	:       Any=2			,     lowercase	:       Optional[Any]=0.0			,     lowercase	:       Optional[Any]=10			,     lowercase	:       int=0			,     lowercase	:       Optional[int]="mean"			,     lowercase	:       List[Any]=False			,     lowercase	:       str=False			,     lowercase	:       int=256			,     lowercase	:       str=0			,     lowercase	:       List[Any]=1			,     lowercase	:       List[Any]=2			,     **lowercase	:       List[Any]			,     )						->					Optional[Any]:
									"""simple docstring"""







									super().__init__(**lowercase			,     pad_token_id=lowercase			,     bos_token_id=lowercase			,     eos_token_id=lowercase   )
									__lowercase					=				hidden_size
									__lowercase					=				feat_extract_norm
									__lowercase					=				feat_extract_activation
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				conv_bias
									__lowercase					=				num_conv_pos_embeddings
									__lowercase					=				num_conv_pos_embedding_groups
									__lowercase					=				len(self.conv_dim   )
									__lowercase					=				num_hidden_layers
									__lowercase					=				intermediate_size
									__lowercase					=				squeeze_factor
									__lowercase					=				hidden_act
									__lowercase					=				num_attention_heads
									__lowercase					=				hidden_dropout
									__lowercase					=				attention_dropout
									__lowercase					=				activation_dropout
									__lowercase					=				feat_proj_dropout
									__lowercase					=				final_dropout
									__lowercase					=				layerdrop
									__lowercase					=				layer_norm_eps
									__lowercase					=				initializer_range
									__lowercase					=				vocab_size

									if (
									    (len(self.conv_stride   ) != self.num_feat_extract_layers)
									    or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
									    or (len(self.conv_dim   ) != self.num_feat_extract_layers)
									):
															raise ValueError(
															    """Configuration for convolutional layers is incorrect."""
															    """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
															    F"but is `len(config.conv_dim) = {len(self.conv_dim   )}`, `len(config.conv_stride)"
															    F"= {len(self.conv_stride   )}`, `len(config.conv_kernel) = {len(self.conv_kernel   )}`."   )

									# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
									__lowercase					=				apply_spec_augment
									__lowercase					=				mask_time_prob
									__lowercase					=				mask_time_length
									__lowercase					=				mask_time_min_masks
									__lowercase					=				mask_feature_prob
									__lowercase					=				mask_feature_length
									__lowercase					=				mask_feature_min_masks

									# ctc loss
									__lowercase					=				ctc_loss_reduction
									__lowercase					=				ctc_zero_infinity

									# sequence classification
									__lowercase					=				use_weighted_layer_sum
									__lowercase					=				classifier_proj_size







			@property
			def 		snake_case__			(				self	:       Dict   )						->					str:
									"""simple docstring"""







									return functools.reduce(operator.mul			,     self.conv_stride			,     1   )





