




'''simple docstring'''



import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    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 							lowerCAmelCase_      (   *__lowerCamelCase :     List[str]  ,				**__lowerCamelCase :     Tuple   ):

       """simple docstring"""
       pass







@is_pipeline_test
@require_vision
@require_torch
class 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   lowerCamelCase_      =						MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



   def 							lowerCAmelCase_      (   self :     Tuple  ,				__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				pipeline(
          "zero-shot-object-detection"  ,				model="hf-internal-testing/tiny-random-owlvit-object-detection"   )

      _SCREAMING_SNAKE_CASE	=				[
          {
              "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
              "candidate_labels": ["cat", "remote", "couch"],
          }
      ]
      return object_detector, examples



   def 							lowerCAmelCase_      (   self :     int  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				object_detector(examples[0]  ,				threshold=0.0   )

      _SCREAMING_SNAKE_CASE	=				len(lowerCAmelCase__   )
      self.assertGreater(lowerCAmelCase__  ,				0   )
      self.assertEqual(
          lowerCAmelCase__  ,				[
              {
                  "score": ANY(lowerCAmelCase__   ),
                  "label": ANY(lowerCAmelCase__   ),
                  "box": {"xmin": ANY(lowerCAmelCase__   ), "ymin": ANY(lowerCAmelCase__   ), "xmax": ANY(lowerCAmelCase__   ), "ymax": ANY(lowerCAmelCase__   )},
              }
              for i in range(lowerCAmelCase__   )
          ]  ,				)



   @require_tf
   @unittest.skip("Zero Shot Object Detection not implemented in TF"   )
   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      pass



   @require_torch
   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				pipeline(
          "zero-shot-object-detection"  ,				model="hf-internal-testing/tiny-random-owlvit-object-detection"   )

      _SCREAMING_SNAKE_CASE	=				object_detector(
          "./tests/fixtures/tests_samples/COCO/000000039769.png"  ,				candidate_labels=["cat", "remote", "couch"]  ,				threshold=0.6_4  ,				)

      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
              {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
              {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
              {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
              {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
              {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
              {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
              {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
              {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
          ]  ,				)

      _SCREAMING_SNAKE_CASE	=				object_detector(
          [
              {
                  "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                  "candidate_labels": ["cat", "remote", "couch"],
              }
          ]  ,				threshold=0.6_4  ,				)

      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              [
                  {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                  {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                  {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 2_0_4, "ymin": 1_6_7, "xmax": 2_3_2, "ymax": 1_9_0}},
                  {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                  {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                  {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 5_7_1, "ymin": 8_3, "xmax": 5_9_8, "ymax": 1_0_3}},
                  {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
                  {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 6_7, "ymin": 2_7_4, "xmax": 9_3, "ymax": 2_9_7}},
                  {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 4_9_4, "ymin": 1_0_5, "xmax": 5_2_1, "ymax": 1_2_7}},
              ]
          ]  ,				)



   @require_torch
   @slow
   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				pipeline("zero-shot-object-detection"   )

      _SCREAMING_SNAKE_CASE	=				object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"  ,				candidate_labels=["cat", "remote", "couch"]  ,				)
      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
              {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
              {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
          ]  ,				)

      _SCREAMING_SNAKE_CASE	=				object_detector(
          [
              {
                  "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                  "candidate_labels": ["cat", "remote", "couch"],
              },
              {
                  "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                  "candidate_labels": ["cat", "remote", "couch"],
              },
          ]  ,				)
      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              [
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
              ],
              [
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 3_3_5, "ymin": 7_4, "xmax": 3_7_1, "ymax": 1_8_7}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 6_4_2, "ymax": 4_7_6}},
              ],
          ]  ,				)



   @require_tf
   @unittest.skip("Zero Shot Object Detection not implemented in TF"   )
   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      pass



   @require_torch
   @slow
   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				0.2
      _SCREAMING_SNAKE_CASE	=				pipeline("zero-shot-object-detection"   )

      _SCREAMING_SNAKE_CASE	=				object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"  ,				candidate_labels=["cat", "remote", "couch"]  ,				threshold=lowerCAmelCase__  ,				)
      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 5_5, "xmax": 3_1_5, "ymax": 4_7_2}},
          ]  ,				)



   @require_torch
   @slow
   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				2
      _SCREAMING_SNAKE_CASE	=				pipeline("zero-shot-object-detection"   )

      _SCREAMING_SNAKE_CASE	=				object_detector(
          "http://images.cocodataset.org/val2017/000000039769.jpg"  ,				candidate_labels=["cat", "remote", "couch"]  ,				top_k=lowerCAmelCase__  ,				)
      self.assertEqual(
          nested_simplify(lowerCAmelCase__  ,				decimals=4   )  ,				[
              {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 3_2_4, "ymin": 2_0, "xmax": 6_4_0, "ymax": 3_7_3}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 4_0, "ymin": 7_2, "xmax": 1_7_7, "ymax": 1_1_5}},
          ]  ,				)







'''simple docstring'''



import torch

from transformers import AutoModel







class 		lowercase_   (			torch.nn.Module       ):


   """simple docstring"""

   def __init__(   self :     List[Any]  ,				__lowerCamelCase :     Union[str, Any]="sayef/fsner-bert-base-uncased"   ):

      """simple docstring"""
      super(__lowerCamelCase  ,				self   ).__init__()

      _SCREAMING_SNAKE_CASE	=				AutoModel.from_pretrained(__lowerCamelCase  ,				return_dict=__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				torch.nn.CosineSimilarity(3  ,				1e-08   )
      _SCREAMING_SNAKE_CASE	=				torch.nn.Softmax(dim=1   )



   def 							lowerCAmelCase_      (   self :     Dict  ,				**__lowerCamelCase :     Any   ):

      """simple docstring"""
      return self.bert(**__lowerCamelCase   ).last_hidden_state



   def 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     List[str]   ):

      """simple docstring"""
      return token_embeddings.sum(2  ,				keepdim=__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Tuple=1   ):

      """simple docstring"""
      return self.softmax(T * self.cos(__lowerCamelCase  ,				__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     int  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				W_supports["sizes"].tolist()
      _SCREAMING_SNAKE_CASE	=				W_supports["start_token_id"].item()
      _SCREAMING_SNAKE_CASE	=				W_supports["end_token_id"].item()

      del W_supports["sizes"]
      del W_supports["start_token_id"]
      del W_supports["end_token_id"]

      _SCREAMING_SNAKE_CASE	=				self.BERT(**__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				self.BERT(**__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				None
      _SCREAMING_SNAKE_CASE	=				None

      _SCREAMING_SNAKE_CASE	=				W_supports["input_ids"] == start_token_id
      _SCREAMING_SNAKE_CASE	=				W_supports["input_ids"] == end_token_id

      for i, size in enumerate(__lowerCamelCase   ):
         if i == 0:
            _SCREAMING_SNAKE_CASE	=				0
         else:
            _SCREAMING_SNAKE_CASE	=				support_sizes[i - 1]

         _SCREAMING_SNAKE_CASE	=				S[s : s + size][start_token_masks[s : s + size]]
         _SCREAMING_SNAKE_CASE	=				S[s : s + size][end_token_masks[s : s + size]]

         _SCREAMING_SNAKE_CASE	=				torch.matmul(q[i]  ,				s_start.T   ).sum(1   ).softmax(0   )
         _SCREAMING_SNAKE_CASE	=				torch.matmul(q[i]  ,				s_end.T   ).sum(1   ).softmax(0   )

         if p_starts is not None:
            _SCREAMING_SNAKE_CASE	=				torch.vstack((p_starts, p_start)   )
            _SCREAMING_SNAKE_CASE	=				torch.vstack((p_ends, p_end)   )
         else:
            _SCREAMING_SNAKE_CASE	=				p_start
            _SCREAMING_SNAKE_CASE	=				p_end

      return p_starts, p_ends

