





"""simple docstring"""






import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 				__lowercase			:







							'''simple docstring'''




							@staticmethod
							def  _lowerCamelCase		(						*_UpperCAmelCase	,			**_UpperCAmelCase     ):
											pass




@is_pipeline_test
@require_vision
@require_torch
class 				__lowercase			(			unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[Any]			       =  pipeline(
									    '''zero-shot-object-detection'''	,			model='''hf-internal-testing/tiny-random-owlvit-object-detection'''     )

									__a :						int			       =  [
									    {
									        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
									        '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
									    }
									]
									return object_detector, examples







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[str]			       =  object_detector(examples[0]	,			threshold=0.0     )

									__a :						Optional[int]			       =  len(_UpperCAmelCase     )
									self.assertGreater(_UpperCAmelCase	,			0     )
									self.assertEqual(
									    _UpperCAmelCase	,			[
									        {
									            '''score''': ANY(_UpperCAmelCase     ),
									            '''label''': ANY(_UpperCAmelCase     ),
									            '''box''': {'''xmin''': ANY(_UpperCAmelCase     ), '''ymin''': ANY(_UpperCAmelCase     ), '''xmax''': ANY(_UpperCAmelCase     ), '''ymax''': ANY(_UpperCAmelCase     )},
									        }
									        for i in range(_UpperCAmelCase     )
									    ]	,			)







					@require_tf
					@unittest.skip('''Zero Shot Object Detection not implemented in TF'''     )
					def  _lowerCamelCase		(						self     ):
									pass







					@require_torch
					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  pipeline(
									    '''zero-shot-object-detection'''	,			model='''hf-internal-testing/tiny-random-owlvit-object-detection'''     )

									__a :						Optional[int]			       =  object_detector(
									    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''	,			candidate_labels=['''cat''', '''remote''', '''couch''']	,			threshold=0.6_4	,			)

									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        {'''score''': 0.7_2_3_5, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									        {'''score''': 0.7_2_1_8, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									        {'''score''': 0.7_1_8_4, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									        {'''score''': 0.6_7_4_8, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									        {'''score''': 0.6_6_5_6, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									        {'''score''': 0.6_6_1_4, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									        {'''score''': 0.6_4_5_6, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
									        {'''score''': 0.6_4_2, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
									        {'''score''': 0.6_4_1_9, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
									    ]	,			)

									__a :						Optional[int]			       =  object_detector(
									    [
									        {
									            '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
									            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
									        }
									    ]	,			threshold=0.6_4	,			)

									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        [
									            {'''score''': 0.7_2_3_5, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									            {'''score''': 0.7_2_1_8, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									            {'''score''': 0.7_1_8_4, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
									            {'''score''': 0.6_7_4_8, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									            {'''score''': 0.6_6_5_6, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									            {'''score''': 0.6_6_1_4, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
									            {'''score''': 0.6_4_5_6, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
									            {'''score''': 0.6_4_2, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
									            {'''score''': 0.6_4_1_9, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
									        ]
									    ]	,			)







					@require_torch
					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  pipeline('''zero-shot-object-detection'''     )

									__a :						Dict			       =  object_detector(
									    '''http://images.cocodataset.org/val2017/000000039769.jpg'''	,			candidate_labels=['''cat''', '''remote''', '''couch''']	,			)
									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        {'''score''': 0.2_8_6_8, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
									        {'''score''': 0.2_7_7, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
									        {'''score''': 0.2_5_3_7, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
									        {'''score''': 0.1_4_7_4, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
									        {'''score''': 0.1_2_0_8, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
									    ]	,			)

									__a :						Dict			       =  object_detector(
									    [
									        {
									            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
									        },
									        {
									            '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
									            '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
									        },
									    ]	,			)
									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        [
									            {'''score''': 0.2_8_6_8, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
									            {'''score''': 0.2_7_7, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
									            {'''score''': 0.2_5_3_7, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
									            {'''score''': 0.1_4_7_4, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
									            {'''score''': 0.1_2_0_8, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
									        ],
									        [
									            {'''score''': 0.2_8_6_8, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
									            {'''score''': 0.2_7_7, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
									            {'''score''': 0.2_5_3_7, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
									            {'''score''': 0.1_4_7_4, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
									            {'''score''': 0.1_2_0_8, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
									        ],
									    ]	,			)







					@require_tf
					@unittest.skip('''Zero Shot Object Detection not implemented in TF'''     )
					def  _lowerCamelCase		(						self     ):
									pass







					@require_torch
					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  0.2
									__a :						List[str]			       =  pipeline('''zero-shot-object-detection'''     )

									__a :						Any			       =  object_detector(
									    '''http://images.cocodataset.org/val2017/000000039769.jpg'''	,			candidate_labels=['''cat''', '''remote''', '''couch''']	,			threshold=_UpperCAmelCase	,			)
									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        {'''score''': 0.2_8_6_8, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
									        {'''score''': 0.2_7_7, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
									        {'''score''': 0.2_5_3_7, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
									    ]	,			)







					@require_torch
					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  2
									__a :						str			       =  pipeline('''zero-shot-object-detection'''     )

									__a :						List[Any]			       =  object_detector(
									    '''http://images.cocodataset.org/val2017/000000039769.jpg'''	,			candidate_labels=['''cat''', '''remote''', '''couch''']	,			top_k=_UpperCAmelCase	,			)
									self.assertEqual(
									    nested_simplify(_UpperCAmelCase	,			decimals=4     )	,			[
									        {'''score''': 0.2_8_6_8, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
									        {'''score''': 0.2_7_7, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
									    ]	,			)






"""simple docstring"""






from typing import List, Optional

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


A 			=  logging.get_logger(__name__)

A 			=  {
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}




class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''autoformer'''
					__lowerCAmelCase  =  {
					    '''hidden_size''': '''d_model''',
					    '''num_attention_heads''': '''encoder_attention_heads''',
					    '''num_hidden_layers''': '''encoder_layers''',
					}







					def __init__(						self	,			_UpperCAmelCase = None	,			_UpperCAmelCase = None	,			_UpperCAmelCase = "student_t"	,			_UpperCAmelCase = "nll"	,			_UpperCAmelCase = 1	,			_UpperCAmelCase = [1, 2, 3, 4, 5, 6, 7]	,			_UpperCAmelCase = True	,			_UpperCAmelCase = 0	,			_UpperCAmelCase = 0	,			_UpperCAmelCase = 0	,			_UpperCAmelCase = 0	,			_UpperCAmelCase = None	,			_UpperCAmelCase = None	,			_UpperCAmelCase = 64	,			_UpperCAmelCase = 2	,			_UpperCAmelCase = 2	,			_UpperCAmelCase = 2	,			_UpperCAmelCase = 2	,			_UpperCAmelCase = 32	,			_UpperCAmelCase = 32	,			_UpperCAmelCase = "gelu"	,			_UpperCAmelCase = 0.1	,			_UpperCAmelCase = 0.1	,			_UpperCAmelCase = 0.1	,			_UpperCAmelCase = 0.1	,			_UpperCAmelCase = 0.1	,			_UpperCAmelCase = 100	,			_UpperCAmelCase = 0.0_2	,			_UpperCAmelCase = True	,			_UpperCAmelCase=True	,			_UpperCAmelCase = 10	,			_UpperCAmelCase = 25	,			_UpperCAmelCase = 3	,			**_UpperCAmelCase	,			):
									# time series specific configuration
									__a :						Optional[int]			       =  prediction_length
									__a :						Optional[int]			       =  context_length if context_length is not None else prediction_length
									__a :						Union[str, Any]			       =  distribution_output
									__a :						str			       =  loss
									__a :						Optional[Any]			       =  input_size
									__a :						str			       =  num_time_features
									__a :						Optional[Any]			       =  lags_sequence
									__a :						Any			       =  scaling
									__a :						Optional[Any]			       =  num_dynamic_real_features
									__a :						str			       =  num_static_real_features
									__a :						Any			       =  num_static_categorical_features
									if cardinality is not None and num_static_categorical_features > 0:
													if len(_UpperCAmelCase     ) != num_static_categorical_features:
																	raise ValueError(
																	    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''     )
													__a :						List[str]			       =  cardinality
									else:
													__a :						Any			       =  [0]
									if embedding_dimension is not None and num_static_categorical_features > 0:
													if len(_UpperCAmelCase     ) != num_static_categorical_features:
																	raise ValueError(
																	    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''     )
													__a :						Any			       =  embedding_dimension
									else:
													__a :						Tuple			       =  [min(50	,			(cat + 1) // 2     ) for cat in self.cardinality]
									__a :						Union[str, Any]			       =  num_parallel_samples

									# Transformer architecture configuration
									__a :						Union[str, Any]			       =  input_size * len(self.lags_sequence     ) + self._number_of_features
									__a :						List[Any]			       =  d_model
									__a :						List[str]			       =  encoder_attention_heads
									__a :						Union[str, Any]			       =  decoder_attention_heads
									__a :						Optional[int]			       =  encoder_ffn_dim
									__a :						List[str]			       =  decoder_ffn_dim
									__a :						List[str]			       =  encoder_layers
									__a :						Optional[int]			       =  decoder_layers

									__a :						Union[str, Any]			       =  dropout
									__a :						Union[str, Any]			       =  attention_dropout
									__a :						Optional[Any]			       =  activation_dropout
									__a :						List[str]			       =  encoder_layerdrop
									__a :						Any			       =  decoder_layerdrop

									__a :						Union[str, Any]			       =  activation_function
									__a :						Union[str, Any]			       =  init_std

									__a :						List[str]			       =  use_cache

									# Autoformer
									__a :						int			       =  label_length
									__a :						List[Any]			       =  moving_average
									__a :						str			       =  autocorrelation_factor

									super().__init__(is_encoder_decoder=_UpperCAmelCase	,			**_UpperCAmelCase     )







					@property
					def  _lowerCamelCase		(						self     ):
									return (
									    sum(self.embedding_dimension     )
									    + self.num_dynamic_real_features
									    + self.num_time_features
									    + self.num_static_real_features
									    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
									)