
import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 				lowerCamelCase__							:




			'''simple docstring'''




			@staticmethod
			def      _lowerCamelCase       (      *a	:List[Any]  ,     **a	:List[Any]		)	->				Any:
										pass




@is_pipeline_test
@require_vision
@require_timm
@require_torch
class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	_A			=  MODEL_FOR_OBJECT_DETECTION_MAPPING







	def      _lowerCamelCase       (      self	:Any  ,     a	:str  ,     a	:int  ,     a	:Optional[Any]		)	->				int:
								__UpperCamelCase			:     Dict         =      ObjectDetectionPipeline(model=a  ,     image_processor=a		)
								return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]







	def      _lowerCamelCase       (      self	:int  ,     a	:List[Any]  ,     a	:Optional[int]		)	->				Union[str, Any]:
								__UpperCamelCase			:     Union[str, Any]         =      object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png"  ,     threshold=0.0		)

								self.assertGreater(len(a		)  ,     0		)
								for detected_object in outputs:
															self.assertEqual(
															    a  ,     {
															        "score": ANY(a		),
															        "label": ANY(a		),
															        "box": {"xmin": ANY(a		), "ymin": ANY(a		), "xmax": ANY(a		), "ymax": ANY(a		)},
															    }  ,     )

								import datasets

								__UpperCamelCase			:     Tuple         =      datasets.load_dataset("hf-internal-testing/fixtures_image_utils"  ,     "image"  ,     split="test"		)

								__UpperCamelCase			:     int         =      [
								    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		),
								    "http://images.cocodataset.org/val2017/000000039769.jpg",
								    # RGBA
								    dataset[0]["file"],
								    # LA
								    dataset[1]["file"],
								    # L
								    dataset[2]["file"],
								]
								__UpperCamelCase			:     Optional[Any]         =      object_detector(a  ,     threshold=0.0		)

								self.assertEqual(len(a		)  ,     len(a		)		)
								for outputs in batch_outputs:
															self.assertGreater(len(a		)  ,     0		)
															for detected_object in outputs:
																						self.assertEqual(
																						    a  ,     {
																						        "score": ANY(a		),
																						        "label": ANY(a		),
																						        "box": {"xmin": ANY(a		), "ymin": ANY(a		), "xmax": ANY(a		), "ymax": ANY(a		)},
																						    }  ,     )







	@require_tf
	@unittest.skip("Object detection not implemented in TF"		)
	def      _lowerCamelCase       (      self	:int		)	->				str:
								pass







	@require_torch
	def      _lowerCamelCase       (      self	:List[str]		)	->				Tuple:
								__UpperCamelCase			:     str         =      "hf-internal-testing/tiny-detr-mobilenetsv3"

								__UpperCamelCase			:     Union[str, Any]         =      AutoModelForObjectDetection.from_pretrained(a		)
								__UpperCamelCase			:     Union[str, Any]         =      AutoFeatureExtractor.from_pretrained(a		)
								__UpperCamelCase			:     List[str]         =      ObjectDetectionPipeline(model=a  ,     feature_extractor=a		)

								__UpperCamelCase			:     Union[str, Any]         =      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"  ,     threshold=0.0		)

								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								        {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								    ]  ,     )

								__UpperCamelCase			:     List[str]         =      object_detector(
								    [
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								    ]  ,     threshold=0.0  ,     )

								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        [
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								        ],
								        [
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								            {"score": 0.3376, "label": "LABEL_0", "box": {"xmin": 1_5_9, "ymin": 1_2_0, "xmax": 4_8_0, "ymax": 3_5_9}},
								        ],
								    ]  ,     )







	@require_torch
	@slow
	def      _lowerCamelCase       (      self	:List[str]		)	->				Union[str, Any]:
								__UpperCamelCase			:     Optional[int]         =      "facebook/detr-resnet-50"

								__UpperCamelCase			:     Union[str, Any]         =      AutoModelForObjectDetection.from_pretrained(a		)
								__UpperCamelCase			:     Optional[int]         =      AutoFeatureExtractor.from_pretrained(a		)
								__UpperCamelCase			:     Optional[int]         =      ObjectDetectionPipeline(model=a  ,     feature_extractor=a		)

								__UpperCamelCase			:     str         =      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								        {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								        {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]  ,     )

								__UpperCamelCase			:     Optional[Any]         =      object_detector(
								    [
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								    ]		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								    ]  ,     )







	@require_torch
	@slow
	def      _lowerCamelCase       (      self	:List[str]		)	->				List[str]:
								__UpperCamelCase			:     int         =      "facebook/detr-resnet-50"

								__UpperCamelCase			:     List[str]         =      pipeline("object-detection"  ,     model=a		)

								__UpperCamelCase			:     List[str]         =      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								        {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								        {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]  ,     )

								__UpperCamelCase			:     Optional[Any]         =      object_detector(
								    [
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								    ]		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								        [
								            {"score": 0.9982, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_0, "xmax": 1_7_5, "ymax": 1_1_7}},
								            {"score": 0.9960, "label": "remote", "box": {"xmin": 3_3_3, "ymin": 7_2, "xmax": 3_6_8, "ymax": 1_8_7}},
								            {"score": 0.9955, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_3_9, "ymax": 4_7_3}},
								            {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								            {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								        ],
								    ]  ,     )







	@require_torch
	@slow
	def      _lowerCamelCase       (      self	:int		)	->				int:
								__UpperCamelCase			:     Optional[int]         =      0.9985
								__UpperCamelCase			:     Tuple         =      "facebook/detr-resnet-50"

								__UpperCamelCase			:     Optional[Any]         =      pipeline("object-detection"  ,     model=a		)

								__UpperCamelCase			:     Optional[int]         =      object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"  ,     threshold=a		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        {"score": 0.9988, "label": "cat", "box": {"xmin": 1_3, "ymin": 5_2, "xmax": 3_1_4, "ymax": 4_7_0}},
								        {"score": 0.9987, "label": "cat", "box": {"xmin": 3_4_5, "ymin": 2_3, "xmax": 6_4_0, "ymax": 3_6_8}},
								    ]  ,     )







	@require_torch
	@require_pytesseract
	@slow
	def      _lowerCamelCase       (      self	:List[str]		)	->				Dict:
								__UpperCamelCase			:     int         =      "Narsil/layoutlmv3-finetuned-funsd"
								__UpperCamelCase			:     List[Any]         =      0.9993

								__UpperCamelCase			:     Tuple         =      pipeline("object-detection"  ,     model=a  ,     threshold=a		)

								__UpperCamelCase			:     List[str]         =      object_detector(
								    "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"		)
								self.assertEqual(
								    nested_simplify(a  ,     decimals=4		)  ,     [
								        {"score": 0.9993, "label": "I-ANSWER", "box": {"xmin": 2_9_4, "ymin": 2_5_4, "xmax": 3_4_3, "ymax": 2_6_4}},
								        {"score": 0.9993, "label": "I-ANSWER", "box": {"xmin": 2_9_4, "ymin": 2_5_4, "xmax": 3_4_3, "ymax": 2_6_4}},
								    ]  ,     )

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


lowercase						:      Optional[Any]									= logging.get_logger(__name__)

lowercase						:      List[str]									= {
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}




class 				lowerCamelCase__							(       __lowercase  ,		__lowercase):




	'''simple docstring'''




	_A			=  'nat'

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







	def __init__(      self	:Optional[Any]  ,     a	:Any=4  ,     a	:Any=3  ,     a	:int=6_4  ,     a	:Dict=[3, 4, 6, 5]  ,     a	:Dict=[2, 4, 8, 1_6]  ,     a	:Optional[Any]=7  ,     a	:Any=3.0  ,     a	:Optional[int]=True  ,     a	:int=0.0  ,     a	:Union[str, Any]=0.0  ,     a	:List[Any]=0.1  ,     a	:str="gelu"  ,     a	:Union[str, Any]=0.02  ,     a	:Tuple=1E-5  ,     a	:str=0.0  ,     a	:Optional[int]=None  ,     a	:Dict=None  ,     **a	:Optional[Any]  ,     )	->				int:
								super().__init__(**a		)

								__UpperCamelCase			:     Any         =      patch_size
								__UpperCamelCase			:     str         =      num_channels
								__UpperCamelCase			:     List[Any]         =      embed_dim
								__UpperCamelCase			:     str         =      depths
								__UpperCamelCase			:     str         =      len(a		)
								__UpperCamelCase			:     Optional[Any]         =      num_heads
								__UpperCamelCase			:     Dict         =      kernel_size
								__UpperCamelCase			:     Union[str, Any]         =      mlp_ratio
								__UpperCamelCase			:     Union[str, Any]         =      qkv_bias
								__UpperCamelCase			:     List[str]         =      hidden_dropout_prob
								__UpperCamelCase			:     Any         =      attention_probs_dropout_prob
								__UpperCamelCase			:     Any         =      drop_path_rate
								__UpperCamelCase			:     Any         =      hidden_act
								__UpperCamelCase			:     Tuple         =      layer_norm_eps
								__UpperCamelCase			:     Dict         =      initializer_range
								# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								__UpperCamelCase			:     int         =      int(embed_dim * 2 ** (len(a		) - 1)		)
								__UpperCamelCase			:     List[Any]         =      layer_scale_init_value
								__UpperCamelCase			:     Optional[Any]         =      ["stem"] + [f'stage{idx}' for idx in range(1  ,     len(a		) + 1		)]
								__UpperCamelCase     ,		__UpperCamelCase			:     Any         =      get_aligned_output_features_output_indices(
								    out_features=a  ,     out_indices=a  ,     stage_names=self.stage_names		)