




"""simple docstring"""




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 			__magic_name__					(*SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



																"""simple docstring"""




																pass



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


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     MODEL_FOR_OBJECT_DETECTION_MAPPING



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     ObjectDetectionPipeline(model=snake_case_     , image_processor=snake_case_					)
											return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     , threshold=0.0					)

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

											import datasets

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

											SCREAMING_SNAKE_CASE__		: Optional[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"""],
											]
											SCREAMING_SNAKE_CASE__		: Any     =     object_detector(snake_case_     , threshold=0.0					)

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



				@require_tf
				@unittest.skip("""Object detection not implemented in TF"""					)
				def 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											pass



				@require_torch
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     """hf-internal-testing/tiny-detr-mobilenetsv3"""

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     AutoModelForObjectDetection.from_pretrained(snake_case_					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     AutoFeatureExtractor.from_pretrained(snake_case_					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     ObjectDetectionPipeline(model=snake_case_     , feature_extractor=snake_case_					)

											SCREAMING_SNAKE_CASE__		: int     =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""     , threshold=0.0					)

											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											        {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											    ]     , )

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

											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        [
											            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											        ],
											        [
											            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											            {"""score""": 0.3376, """label""": """LABEL_0""", """box""": {"""xmin""": 1_59, """ymin""": 1_20, """xmax""": 4_80, """ymax""": 3_59}},
											        ],
											    ]     , )



				@require_torch
				@slow
				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """facebook/detr-resnet-50"""

											SCREAMING_SNAKE_CASE__		: List[Any]     =     AutoModelForObjectDetection.from_pretrained(snake_case_					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     AutoFeatureExtractor.from_pretrained(snake_case_					)
											SCREAMING_SNAKE_CASE__		: Any     =     ObjectDetectionPipeline(model=snake_case_     , feature_extractor=snake_case_					)

											SCREAMING_SNAKE_CASE__		: int     =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											    ]     , )

											SCREAMING_SNAKE_CASE__		: Tuple     =     object_detector(
											    [
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    ]					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        [
											            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											        ],
											        [
											            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											        ],
											    ]     , )



				@require_torch
				@slow
				def 			__magic_name__					(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """facebook/detr-resnet-50"""

											SCREAMING_SNAKE_CASE__		: List[Any]     =     pipeline("""object-detection"""     , model=snake_case_					)

											SCREAMING_SNAKE_CASE__		: Dict     =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											        {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											        {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											    ]     , )

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     object_detector(
											    [
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    ]					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        [
											            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											        ],
											        [
											            {"""score""": 0.9982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 1_75, """ymax""": 1_17}},
											            {"""score""": 0.9960, """label""": """remote""", """box""": {"""xmin""": 3_33, """ymin""": 72, """xmax""": 3_68, """ymax""": 1_87}},
											            {"""score""": 0.9955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 6_39, """ymax""": 4_73}},
											            {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											            {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											        ],
											    ]     , )



				@require_torch
				@slow
				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     0.9985
											SCREAMING_SNAKE_CASE__		: List[str]     =     """facebook/detr-resnet-50"""

											SCREAMING_SNAKE_CASE__		: int     =     pipeline("""object-detection"""     , model=snake_case_					)

											SCREAMING_SNAKE_CASE__		: Dict     =     object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""     , threshold=snake_case_					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        {"""score""": 0.9988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 3_14, """ymax""": 4_70}},
											        {"""score""": 0.9987, """label""": """cat""", """box""": {"""xmin""": 3_45, """ymin""": 23, """xmax""": 6_40, """ymax""": 3_68}},
											    ]     , )



				@require_torch
				@require_pytesseract
				@slow
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     """Narsil/layoutlmv3-finetuned-funsd"""
											SCREAMING_SNAKE_CASE__		: str     =     0.9993

											SCREAMING_SNAKE_CASE__		: Dict     =     pipeline("""object-detection"""     , model=snake_case_     , threshold=snake_case_					)

											SCREAMING_SNAKE_CASE__		: Tuple     =     object_detector(
											    """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"""					)
											self.assertEqual(
											    nested_simplify(snake_case_     , decimals=4					)     , [
											        {"""score""": 0.9993, """label""": """I-ANSWER""", """box""": {"""xmin""": 2_94, """ymin""": 2_54, """xmax""": 3_43, """ymax""": 2_64}},
											        {"""score""": 0.9993, """label""": """I-ANSWER""", """box""": {"""xmin""": 2_94, """ymin""": 2_54, """xmax""": 3_43, """ymax""": 2_64}},
											    ]     , )






"""simple docstring"""




from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput



class 			lowerCAmelCase_		(nn.Module						):


				"""simple docstring"""



				def __init__(self     , SCREAMING_SNAKE_CASE__ = 16     , SCREAMING_SNAKE_CASE__ = 88     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = 1     , SCREAMING_SNAKE_CASE__ = 0.0     , SCREAMING_SNAKE_CASE__ = 32     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = False     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = "geglu"     , SCREAMING_SNAKE_CASE__ = None     , )      ->	Optional[Any]:



											"""simple docstring"""




											super().__init__()
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     nn.ModuleList(
											    [
											        TransformeraDModel(
											            num_attention_heads=SCREAMING_SNAKE_CASE__     , attention_head_dim=SCREAMING_SNAKE_CASE__     , in_channels=SCREAMING_SNAKE_CASE__     , num_layers=SCREAMING_SNAKE_CASE__     , dropout=SCREAMING_SNAKE_CASE__     , norm_num_groups=SCREAMING_SNAKE_CASE__     , cross_attention_dim=SCREAMING_SNAKE_CASE__     , attention_bias=SCREAMING_SNAKE_CASE__     , sample_size=SCREAMING_SNAKE_CASE__     , num_vector_embeds=SCREAMING_SNAKE_CASE__     , activation_fn=SCREAMING_SNAKE_CASE__     , num_embeds_ada_norm=SCREAMING_SNAKE_CASE__     , )
											        for _ in range(2					)
											    ]					)

											# Variables that can be set by a pipeline:

											# The ratio of transformer1 to transformer2's output states to be combined during inference
											SCREAMING_SNAKE_CASE__		: Tuple     =     0.5

											# The shape of `encoder_hidden_states` is expected to be
											# `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     [77, 2_57]

											# Which transformer to use to encode which condition.
											# E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
											SCREAMING_SNAKE_CASE__		: int     =     [1, 0]



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__ = True     , )      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     hidden_states

											SCREAMING_SNAKE_CASE__		: Tuple     =     []
											SCREAMING_SNAKE_CASE__		: List[str]     =     0
											# attention_mask is not used yet
											for i in range(2					):
																		# for each of the two transformers, pass the corresponding condition tokens
																		SCREAMING_SNAKE_CASE__		: str     =     encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
																		SCREAMING_SNAKE_CASE__		: Any     =     self.transformer_index_for_condition[i]
																		SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.transformers[transformer_index](
																		    SCREAMING_SNAKE_CASE__     , encoder_hidden_states=SCREAMING_SNAKE_CASE__     , timestep=SCREAMING_SNAKE_CASE__     , cross_attention_kwargs=SCREAMING_SNAKE_CASE__     , return_dict=SCREAMING_SNAKE_CASE__     , )[0]
																		encoded_states.append(encoded_state - input_states					)
																		tokens_start += self.condition_lengths[i]

											SCREAMING_SNAKE_CASE__		: Tuple     =     encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     output_states + input_states

											if not return_dict:
																		return (output_states,)

											return TransformeraDModelOutput(sample=SCREAMING_SNAKE_CASE__					)




