



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


     '''simple docstring'''

     @staticmethod
     def    a__				(					*__lowerCamelCase     :     List[Any]				,     **__lowerCamelCase     :     Any     )							->							Union[str, Any]:

      '''simple docstring'''




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


    '''simple docstring'''

    lowerCAmelCase__      				=							MODEL_FOR_OBJECT_DETECTION_MAPPING

    def    a__				(					self     :     Optional[Any]				,     __lowerCamelCase     :     Any				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     List[str]     )							->							List[Any]:

     '''simple docstring'''




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

    def    a__				(					self     :     List[str]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     Optional[Any]     )							->							Any:

     '''simple docstring'''




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

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

     import datasets

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

     lowerCamelCase__  =     [
         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"""],
     ]
     lowerCamelCase__  =     object_detector(__lowerCamelCase				,     threshold=0.0     )

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

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

     '''simple docstring'''




     pass

    @require_torch
    def    a__				(					self     :     Union[str, Any]     )							->							Optional[Any]:

     '''simple docstring'''




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

     lowerCamelCase__  =     AutoModelForObjectDetection.from_pretrained(__lowerCamelCase     )
     lowerCamelCase__  =     AutoFeatureExtractor.from_pretrained(__lowerCamelCase     )
     lowerCamelCase__  =     ObjectDetectionPipeline(model=__lowerCamelCase				,     feature_extractor=__lowerCamelCase     )

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

     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
             {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
         ]				,     )

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

     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             [
                 {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
                 {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
             ],
             [
                 {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
                 {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}},
             ],
         ]				,     )

    @require_torch
    @slow
    def    a__				(					self     :     Union[str, Any]     )							->							Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__  =     """facebook/detr-resnet-50"""

     lowerCamelCase__  =     AutoModelForObjectDetection.from_pretrained(__lowerCamelCase     )
     lowerCamelCase__  =     AutoFeatureExtractor.from_pretrained(__lowerCamelCase     )
     lowerCamelCase__  =     ObjectDetectionPipeline(model=__lowerCamelCase				,     feature_extractor=__lowerCamelCase     )

     lowerCamelCase__  =     object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
             {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
             {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
             {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
             {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
         ]				,     )

     lowerCamelCase__  =     object_detector(
         [
             "http://images.cocodataset.org/val2017/000000039769.jpg",
             "http://images.cocodataset.org/val2017/000000039769.jpg",
         ]     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             [
                 {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
                 {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
                 {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
                 {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
                 {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
             ],
             [
                 {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
                 {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
                 {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
                 {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
                 {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
             ],
         ]				,     )

    @require_torch
    @slow
    def    a__				(					self     :     int     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     """facebook/detr-resnet-50"""

     lowerCamelCase__  =     pipeline("object-detection"				,     model=__lowerCamelCase     )

     lowerCamelCase__  =     object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
             {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
             {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
             {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
             {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
         ]				,     )

     lowerCamelCase__  =     object_detector(
         [
             "http://images.cocodataset.org/val2017/000000039769.jpg",
             "http://images.cocodataset.org/val2017/000000039769.jpg",
         ]     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             [
                 {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
                 {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
                 {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
                 {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
                 {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
             ],
             [
                 {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}},
                 {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}},
                 {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}},
                 {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
                 {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
             ],
         ]				,     )

    @require_torch
    @slow
    def    a__				(					self     :     int     )							->							List[Any]:

     '''simple docstring'''




     lowerCamelCase__  =     0.9_9_8_5
     lowerCamelCase__  =     """facebook/detr-resnet-50"""

     lowerCamelCase__  =     pipeline("object-detection"				,     model=__lowerCamelCase     )

     lowerCamelCase__  =     object_detector("http://images.cocodataset.org/val2017/000000039769.jpg"				,     threshold=__lowerCamelCase     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}},
             {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}},
         ]				,     )




    @require_torch
    @require_pytesseract
    @slow
    def    a__				(					self     :     Dict     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  =     """Narsil/layoutlmv3-finetuned-funsd"""
     lowerCamelCase__  =     0.9_9_9_3

     lowerCamelCase__  =     pipeline("object-detection"				,     model=__lowerCamelCase				,     threshold=__lowerCamelCase     )

     lowerCamelCase__  =     object_detector(
         "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"     )
     self.assertEqual(
         nested_simplify(__lowerCamelCase				,     decimals=4     )				,     [
             {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
             {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}},
         ]				,     )









'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A							:							Optional[int]  							=		logging.get_logger(__name__)

__A							:							Tuple  							=		{
    """google/mobilenet_v1_1.0_224""": """https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json""",
    """google/mobilenet_v1_0.75_192""": """https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json""",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							"mobilenet_v1"

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     Tuple=3				,     __lowerCamelCase     :     Any=224				,     __lowerCamelCase     :     Tuple=1.0				,     __lowerCamelCase     :     Optional[int]=8				,     __lowerCamelCase     :     str="relu6"				,     __lowerCamelCase     :     Any=True				,     __lowerCamelCase     :     Union[str, Any]=0.9_9_9				,     __lowerCamelCase     :     List[Any]=0.0_2				,     __lowerCamelCase     :     str=0.0_0_1				,     **__lowerCamelCase     :     str				,     )							->							Dict:

     '''simple docstring'''




     super().__init__(**__lowerCamelCase     )

     if depth_multiplier <= 0:
      raise ValueError("depth_multiplier must be greater than zero."     )

     lowerCamelCase__  =     num_channels
     lowerCamelCase__  =     image_size
     lowerCamelCase__  =     depth_multiplier
     lowerCamelCase__  =     min_depth
     lowerCamelCase__  =     hidden_act
     lowerCamelCase__  =     tf_padding
     lowerCamelCase__  =     classifier_dropout_prob
     lowerCamelCase__  =     initializer_range
     lowerCamelCase__  =     layer_norm_eps
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							version.parse("1.11"      )

    @property
    def    a__				(					self     :     List[str]     )							->							Mapping[str, Mapping[int, str]]:

     '''simple docstring'''




     return OrderedDict([("pixel_values", {0: "batch"})]     )

    @property
    def    a__				(					self     :     str     )							->							Mapping[str, Mapping[int, str]]:

     '''simple docstring'''




     if self.task == "image-classification":
      return OrderedDict([("logits", {0: "batch"})]     )
     else:
      return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})]     )




    @property
    def    a__				(					self     :     List[Any]     )							->							float:

     '''simple docstring'''




     return 1E-4

