



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  __magic_name__   :






				@staticmethod
				def SCREAMING_SNAKE_CASE_			(						*UpperCamelCase__      :     int				,		**UpperCamelCase__      :     str      )				->					Any:
										'''simple docstring'''







										pass





@is_pipeline_test
@require_vision
@require_timm
@require_torch
class  __magic_name__   (							unittest.TestCase						):
		lowercase      :       List[str]          =MODEL_FOR_OBJECT_DETECTION_MAPPING






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Optional[int]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =    ObjectDetectionPipeline(model=UpperCamelCase__				,		image_processor=UpperCamelCase__      )
								return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]






		def SCREAMING_SNAKE_CASE_			(						self      :     str				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     Optional[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =    object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png"				,		threshold=0.0      )

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

								import datasets

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

								UpperCAmelCase         =    [
								    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         =    object_detector(UpperCamelCase__				,		threshold=0.0      )

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






		@require_tf
		@unittest.skip("Object detection not implemented in TF"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Any:
								'''simple docstring'''







								pass






		@require_torch
		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =    "hf-internal-testing/tiny-detr-mobilenetsv3"

								UpperCAmelCase         =    AutoModelForObjectDetection.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =    AutoFeatureExtractor.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =    ObjectDetectionPipeline(model=UpperCamelCase__				,		feature_extractor=UpperCamelCase__      )

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

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

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

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






		@require_torch
		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =    "facebook/detr-resnet-50"

								UpperCAmelCase         =    AutoModelForObjectDetection.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =    AutoFeatureExtractor.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =    ObjectDetectionPipeline(model=UpperCamelCase__				,		feature_extractor=UpperCamelCase__      )

								UpperCAmelCase         =    object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__				,		decimals=4      )				,		[
								        {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								        {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								        {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								        {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								        {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								    ]				,		)

								UpperCAmelCase         =    object_detector(
								    [
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								    ]      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__				,		decimals=4      )				,		[
								        [
								            {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								            {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								        ],
								        [
								            {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								            {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								        ],
								    ]				,		)






		@require_torch
		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =    "facebook/detr-resnet-50"

								UpperCAmelCase         =    pipeline("object-detection"				,		model=UpperCamelCase__      )

								UpperCAmelCase         =    object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__				,		decimals=4      )				,		[
								        {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								        {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								        {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								        {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								        {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								    ]				,		)

								UpperCAmelCase         =    object_detector(
								    [
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								        "http://images.cocodataset.org/val2017/000000039769.jpg",
								    ]      )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__				,		decimals=4      )				,		[
								        [
								            {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								            {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								        ],
								        [
								            {"score": 0.99_82, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 1_75, "ymax": 1_17}},
								            {"score": 0.99_60, "label": "remote", "box": {"xmin": 3_33, "ymin": 72, "xmax": 3_68, "ymax": 1_87}},
								            {"score": 0.99_55, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 6_39, "ymax": 4_73}},
								            {"score": 0.99_88, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 3_14, "ymax": 4_70}},
								            {"score": 0.99_87, "label": "cat", "box": {"xmin": 3_45, "ymin": 23, "xmax": 6_40, "ymax": 3_68}},
								        ],
								    ]				,		)






		@require_torch
		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =    0.99_85
								UpperCAmelCase         =    "facebook/detr-resnet-50"

								UpperCAmelCase         =    pipeline("object-detection"				,		model=UpperCamelCase__      )

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







		@require_torch
		@require_pytesseract
		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =    "Narsil/layoutlmv3-finetuned-funsd"
								UpperCAmelCase         =    0.99_93

								UpperCAmelCase         =    pipeline("object-detection"				,		model=UpperCamelCase__				,		threshold=UpperCamelCase__      )

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








import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation





class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("swish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("silu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("mish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.Mish      )

								self.assertEqual(act(torch.tensor(-2_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("gelu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.GELU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )



