




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






        @staticmethod
        def     _lowercase     (			*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )							->      Optional[Any]:
             pass

@is_pipeline_test
@require_vision
@require_timm
@require_torch
class        _a    (						unittest.TestCase							):
     SCREAMING_SNAKE_CASE_							:   Any       =						MODEL_FOR_OBJECT_DETECTION_MAPPING






     def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      str:
          _snake_case							=				ObjectDetectionPipeline(model=__lowerCAmelCase ,image_processor=__lowerCAmelCase   )
          return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]






     def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      Dict:
          _snake_case							=				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

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

          _snake_case							=				[
              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"],
          ]
          _snake_case							=				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     _lowercase     (			self   )							->      List[Any]:
          pass






     @require_torch
     def     _lowercase     (			self   )							->      Union[str, Any]:
          _snake_case							=				"hf-internal-testing/tiny-detr-mobilenetsv3"

          _snake_case							=				AutoModelForObjectDetection.from_pretrained(__lowerCAmelCase   )
          _snake_case							=				AutoFeatureExtractor.from_pretrained(__lowerCAmelCase   )
          _snake_case							=				ObjectDetectionPipeline(model=__lowerCAmelCase ,feature_extractor=__lowerCAmelCase   )

          _snake_case							=				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}},
              ] ,)

          _snake_case							=				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     _lowercase     (			self   )							->      Tuple:
          _snake_case							=				"facebook/detr-resnet-50"

          _snake_case							=				AutoModelForObjectDetection.from_pretrained(__lowerCAmelCase   )
          _snake_case							=				AutoFeatureExtractor.from_pretrained(__lowerCAmelCase   )
          _snake_case							=				ObjectDetectionPipeline(model=__lowerCAmelCase ,feature_extractor=__lowerCAmelCase   )

          _snake_case							=				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}},
              ] ,)

          _snake_case							=				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     _lowercase     (			self   )							->      Optional[int]:
          _snake_case							=				"facebook/detr-resnet-50"

          _snake_case							=				pipeline("object-detection" ,model=__lowerCAmelCase   )

          _snake_case							=				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}},
              ] ,)

          _snake_case							=				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     _lowercase     (			self   )							->      Dict:
          _snake_case							=				0.9_9_8_5
          _snake_case							=				"facebook/detr-resnet-50"

          _snake_case							=				pipeline("object-detection" ,model=__lowerCAmelCase   )

          _snake_case							=				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     _lowercase     (			self   )							->      Tuple:
          _snake_case							=				"Narsil/layoutlmv3-finetuned-funsd"
          _snake_case							=				0.9_9_9_3

          _snake_case							=				pipeline("object-detection" ,model=__lowerCAmelCase ,threshold=__lowerCAmelCase   )

          _snake_case							=				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'''





import functools
import operator

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


UpperCamelCase_						:      str       =						logging.get_logger(__name__)

UpperCamelCase_						:      Optional[Any]       =						{
    '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''',
    # See all SEW models at https://huggingface.co/models?filter=sew
}

class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   Dict       =						"""sew"""






     def __init__(			self ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=768 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=3_072 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0_2 ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE="group" ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,_SCREAMING_SNAKE_CASE=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=128 ,_SCREAMING_SNAKE_CASE=16 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=0.0_5 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE="mean" ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=256 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE=1 ,_SCREAMING_SNAKE_CASE=2 ,**_SCREAMING_SNAKE_CASE ,)							->      str:
          super().__init__(**_SCREAMING_SNAKE_CASE ,pad_token_id=_SCREAMING_SNAKE_CASE ,bos_token_id=_SCREAMING_SNAKE_CASE ,eos_token_id=_SCREAMING_SNAKE_CASE   )
          _snake_case							=				hidden_size
          _snake_case							=				feat_extract_norm
          _snake_case							=				feat_extract_activation
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				conv_bias
          _snake_case							=				num_conv_pos_embeddings
          _snake_case							=				num_conv_pos_embedding_groups
          _snake_case							=				len(self.conv_dim   )
          _snake_case							=				num_hidden_layers
          _snake_case							=				intermediate_size
          _snake_case							=				squeeze_factor
          _snake_case							=				hidden_act
          _snake_case							=				num_attention_heads
          _snake_case							=				hidden_dropout
          _snake_case							=				attention_dropout
          _snake_case							=				activation_dropout
          _snake_case							=				feat_proj_dropout
          _snake_case							=				final_dropout
          _snake_case							=				layerdrop
          _snake_case							=				layer_norm_eps
          _snake_case							=				initializer_range
          _snake_case							=				vocab_size

          if (
              (len(self.conv_stride   ) != self.num_feat_extract_layers)
              or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
              or (len(self.conv_dim   ) != self.num_feat_extract_layers)
          ):
               raise ValueError(
                   "Configuration for convolutional layers is incorrect."
                   "It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"
                   f"""but is `len(config.conv_dim) = {len(self.conv_dim   )}`, `len(config.conv_stride)"""
                   f"""= {len(self.conv_stride   )}`, `len(config.conv_kernel) = {len(self.conv_kernel   )}`."""   )

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          _snake_case							=				apply_spec_augment
          _snake_case							=				mask_time_prob
          _snake_case							=				mask_time_length
          _snake_case							=				mask_time_min_masks
          _snake_case							=				mask_feature_prob
          _snake_case							=				mask_feature_length
          _snake_case							=				mask_feature_min_masks

          # ctc loss
          _snake_case							=				ctc_loss_reduction
          _snake_case							=				ctc_zero_infinity

          # sequence classification
          _snake_case							=				use_weighted_layer_sum
          _snake_case							=				classifier_proj_size






     @property
     def     _lowercase     (			self   )							->      Optional[Any]:
          return functools.reduce(operator.mul ,self.conv_stride ,1   )


