






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




									'''simple docstring'''





									@staticmethod
									def 			UpperCamelCase_					(      *_lowerCAmelCase   ,**_lowerCAmelCase       ):
															pass




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




		'''simple docstring'''





		_UpperCamelCase				  =					MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  pipeline(
								    """zero-shot-object-detection"""   ,model="""hf-internal-testing/tiny-random-owlvit-object-detection"""       )

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






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  object_detector(examples[0]   ,threshold=0.0       )

								lowerCamelCase__         =  len(_lowerCAmelCase       )
								self.assertGreater(_lowerCAmelCase   ,0       )
								self.assertEqual(
								    _lowerCAmelCase   ,[
								        {
								            """score""": ANY(_lowerCAmelCase       ),
								            """label""": ANY(_lowerCAmelCase       ),
								            """box""": {"""xmin""": ANY(_lowerCAmelCase       ), """ymin""": ANY(_lowerCAmelCase       ), """xmax""": ANY(_lowerCAmelCase       ), """ymax""": ANY(_lowerCAmelCase       )},
								        }
								        for i in range(_lowerCAmelCase       )
								    ]   ,)






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






		@require_torch
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  pipeline(
								    """zero-shot-object-detection"""   ,model="""hf-internal-testing/tiny-random-owlvit-object-detection"""       )

								lowerCamelCase__         =  object_detector(
								    """./tests/fixtures/tests_samples/COCO/000000039769.png"""   ,candidate_labels=["""cat""", """remote""", """couch"""]   ,threshold=0.64   ,)

								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[
								        {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								        {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								        {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								        {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								        {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								        {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								        {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
								        {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 2_74, """xmax""": 93, """ymax""": 2_97}},
								        {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
								    ]   ,)

								lowerCamelCase__         =  object_detector(
								    [
								        {
								            """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
								            """candidate_labels""": ["""cat""", """remote""", """couch"""],
								        }
								    ]   ,threshold=0.64   ,)

								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[
								        [
								            {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								            {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								            {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 2_04, """ymin""": 1_67, """xmax""": 2_32, """ymax""": 1_90}},
								            {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								            {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								            {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 5_71, """ymin""": 83, """xmax""": 5_98, """ymax""": 1_03}},
								            {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
								            {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 2_74, """xmax""": 93, """ymax""": 2_97}},
								            {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 4_94, """ymin""": 1_05, """xmax""": 5_21, """ymax""": 1_27}},
								        ]
								    ]   ,)






		@require_torch
		@slow
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  pipeline("""zero-shot-object-detection"""       )

								lowerCamelCase__         =  object_detector(
								    """http://images.cocodataset.org/val2017/000000039769.jpg"""   ,candidate_labels=["""cat""", """remote""", """couch"""]   ,)
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[
								        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
								        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
								        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
								        {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
								        {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_42, """ymax""": 4_76}},
								    ]   ,)

								lowerCamelCase__         =  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(_lowerCAmelCase   ,decimals=4       )   ,[
								        [
								            {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
								            {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
								            {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
								            {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
								            {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_42, """ymax""": 4_76}},
								        ],
								        [
								            {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
								            {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
								            {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
								            {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 3_35, """ymin""": 74, """xmax""": 3_71, """ymax""": 1_87}},
								            {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_42, """ymax""": 4_76}},
								        ],
								    ]   ,)






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






		@require_torch
		@slow
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  0.2
								lowerCamelCase__         =  pipeline("""zero-shot-object-detection"""       )

								lowerCamelCase__         =  object_detector(
								    """http://images.cocodataset.org/val2017/000000039769.jpg"""   ,candidate_labels=["""cat""", """remote""", """couch"""]   ,threshold=_lowerCAmelCase   ,)
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[
								        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
								        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
								        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 3_15, """ymax""": 4_72}},
								    ]   ,)






		@require_torch
		@slow
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  2
								lowerCamelCase__         =  pipeline("""zero-shot-object-detection"""       )

								lowerCamelCase__         =  object_detector(
								    """http://images.cocodataset.org/val2017/000000039769.jpg"""   ,candidate_labels=["""cat""", """remote""", """couch"""]   ,top_k=_lowerCAmelCase   ,)
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[
								        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 3_24, """ymin""": 20, """xmax""": 6_40, """ymax""": 3_73}},
								        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 1_77, """ymax""": 1_15}},
								    ]   ,)









'''simple docstring'''







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


UpperCamelCase    :      Tuple  			=		logging.get_logger(__name__)

UpperCamelCase    :      List[str]  			=		{
    'naver-clova-ix/donut-base': 'https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json',
    # See all Donut models at https://huggingface.co/models?filter=donut-swin
}




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'donut-swin'

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






		def __init__(      self   ,_lowerCAmelCase=2_24   ,_lowerCAmelCase=4   ,_lowerCAmelCase=3   ,_lowerCAmelCase=96   ,_lowerCAmelCase=[2, 2, 6, 2]   ,_lowerCAmelCase=[3, 6, 12, 24]   ,_lowerCAmelCase=7   ,_lowerCAmelCase=4.0   ,_lowerCAmelCase=True   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase="gelu"   ,_lowerCAmelCase=False   ,_lowerCAmelCase=0.02   ,_lowerCAmelCase=1E-5   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )

								lowerCamelCase__         =  image_size
								lowerCamelCase__         =  patch_size
								lowerCamelCase__         =  num_channels
								lowerCamelCase__         =  embed_dim
								lowerCamelCase__         =  depths
								lowerCamelCase__         =  len(_lowerCAmelCase       )
								lowerCamelCase__         =  num_heads
								lowerCamelCase__         =  window_size
								lowerCamelCase__         =  mlp_ratio
								lowerCamelCase__         =  qkv_bias
								lowerCamelCase__         =  hidden_dropout_prob
								lowerCamelCase__         =  attention_probs_dropout_prob
								lowerCamelCase__         =  drop_path_rate
								lowerCamelCase__         =  hidden_act
								lowerCamelCase__         =  use_absolute_embeddings
								lowerCamelCase__         =  layer_norm_eps
								lowerCamelCase__         =  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
								lowerCamelCase__         =  int(embed_dim * 2 ** (len(_lowerCAmelCase       ) - 1)       )

