






'''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    a__				(					*__lowerCamelCase     :     int				,     **__lowerCamelCase     :     Optional[int]     )							->							str:

    '''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    a__				(					self     :     int				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Union[str, Any]     )							->							Dict:

   '''simple docstring'''




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

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

  def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Tuple     )							->							str:

   '''simple docstring'''




   lowerCamelCase__  =     object_detector(examples[0]				,     threshold=0.0     )

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

  @require_tf
  @unittest.skip("Zero Shot Object Detection not implemented in TF"     )
  def    a__				(					self     :     Optional[int]     )							->							Union[str, Any]:

   '''simple docstring'''




   pass

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

   '''simple docstring'''




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

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

   self.assertEqual(
       nested_simplify(_lowercase				,     decimals=4     )				,     [
           {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
           {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
           {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
           {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
           {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
       ]				,     )

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

   self.assertEqual(
       nested_simplify(_lowercase				,     decimals=4     )				,     [
           [
               {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
               {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
               {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
               {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
               {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
           ]
       ]				,     )

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

   '''simple docstring'''




   lowerCamelCase__  =     pipeline("zero-shot-object-detection"     )

   lowerCamelCase__  =     object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"				,     candidate_labels=["cat", "remote", "couch"]				,     )
   self.assertEqual(
       nested_simplify(_lowercase				,     decimals=4     )				,     [
           {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
           {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
           {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
           {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
       ]				,     )

   lowerCamelCase__  =     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(_lowercase				,     decimals=4     )				,     [
           [
               {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
               {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
               {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
           ],
           [
               {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
               {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
               {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
               {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
               {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
           ],
       ]				,     )

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

   '''simple docstring'''




   pass

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

   '''simple docstring'''




   lowerCamelCase__  =     0.2
   lowerCamelCase__  =     pipeline("zero-shot-object-detection"     )

   lowerCamelCase__  =     object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"				,     candidate_labels=["cat", "remote", "couch"]				,     threshold=_lowercase				,     )
   self.assertEqual(
       nested_simplify(_lowercase				,     decimals=4     )				,     [
           {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
           {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
       ]				,     )




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

   '''simple docstring'''




   lowerCamelCase__  =     2
   lowerCamelCase__  =     pipeline("zero-shot-object-detection"     )

   lowerCamelCase__  =     object_detector(
       "http://images.cocodataset.org/val2017/000000039769.jpg"				,     candidate_labels=["cat", "remote", "couch"]				,     top_k=_lowercase				,     )
   self.assertEqual(
       nested_simplify(_lowercase				,     decimals=4     )				,     [
           {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
           {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
       ]				,     )









'''simple docstring'''





import tensorflow as tf

from ...tf_utils import shape_list
class 				lowercase       (							tf.keras.layers.Layer      ):


    '''simple docstring'''

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     int=1				,     __lowerCamelCase     :     Tuple=False				,     **__lowerCamelCase     :     Dict     )							->							str:

     '''simple docstring'''




     super().__init__(**__lowerCamelCase     )

     lowerCamelCase__  =     vocab_size
     lowerCamelCase__  =     d_embed
     lowerCamelCase__  =     d_proj

     lowerCamelCase__  =     cutoffs + [vocab_size]
     lowerCamelCase__  =     [0] + self.cutoffs
     lowerCamelCase__  =     div_val

     lowerCamelCase__  =     self.cutoffs[0]
     lowerCamelCase__  =     len(self.cutoffs     ) - 1
     lowerCamelCase__  =     self.shortlist_size + self.n_clusters
     lowerCamelCase__  =     keep_order

     lowerCamelCase__  =     []
     lowerCamelCase__  =     []

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

     '''simple docstring'''




     if self.n_clusters > 0:
      lowerCamelCase__  =     self.add_weight(
          shape=(self.n_clusters, self.d_embed)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name="cluster_weight"     )
      lowerCamelCase__  =     self.add_weight(
          shape=(self.n_clusters,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name="cluster_bias"     )

     if self.div_val == 1:
      for i in range(len(self.cutoffs     )     ):
       if self.d_proj != self.d_embed:
        lowerCamelCase__  =     self.add_weight(
            shape=(self.d_embed, self.d_proj)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_projs_._{i}'''				,     )
        self.out_projs.append(__lowerCamelCase     )
       else:
        self.out_projs.append(__lowerCamelCase     )
       lowerCamelCase__  =     self.add_weight(
           shape=(self.vocab_size, self.d_embed)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._weight'''				,     )
       lowerCamelCase__  =     self.add_weight(
           shape=(self.vocab_size,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._bias'''				,     )
       self.out_layers.append((weight, bias)     )
     else:
      for i in range(len(self.cutoffs     )     ):
       lowerCamelCase__  ,	lowerCamelCase__  =     self.cutoff_ends[i], self.cutoff_ends[i + 1]
       lowerCamelCase__  =     self.d_embed // (self.div_val**i)

       lowerCamelCase__  =     self.add_weight(
           shape=(d_emb_i, self.d_proj)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_projs_._{i}'''     )
       self.out_projs.append(__lowerCamelCase     )
       lowerCamelCase__  =     self.add_weight(
           shape=(r_idx - l_idx, d_emb_i)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._weight'''				,     )
       lowerCamelCase__  =     self.add_weight(
           shape=(r_idx - l_idx,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._bias'''				,     )
       self.out_layers.append((weight, bias)     )
     super().build(__lowerCamelCase     )

    @staticmethod
    def    a__				(					__lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     List[str]=None     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     x
     if proj is not None:
      lowerCamelCase__  =     tf.einsum("ibd,ed->ibe"				,     __lowerCamelCase				,     __lowerCamelCase     )
     return tf.einsum("ibd,nd->ibn"				,     __lowerCamelCase				,     __lowerCamelCase     ) + b

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

     '''simple docstring'''




     lowerCamelCase__  =     shape_list(__lowerCamelCase     )
     lowerCamelCase__  =     tf.range(lp_size[0]				,     dtype=target.dtype     )
     lowerCamelCase__  =     tf.stack([r, target]				,     1     )
     return tf.gather_nd(__lowerCamelCase				,     __lowerCamelCase     )




    def    a__				(					self     :     Any				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     str=True				,     __lowerCamelCase     :     Tuple=False     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  =     0
     if self.n_clusters == 0:
      lowerCamelCase__  =     self._logit(__lowerCamelCase				,     self.out_layers[0][0]				,     self.out_layers[0][1]				,     self.out_projs[0]     )
      if target is not None:
       lowerCamelCase__  =     tf.nn.sparse_softmax_cross_entropy_with_logits(labels=__lowerCamelCase				,     logits=__lowerCamelCase     )
      lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase				,     axis=-1     )
     else:
      lowerCamelCase__  =     shape_list(__lowerCamelCase     )
      lowerCamelCase__  =     []
      lowerCamelCase__  =     tf.zeros(hidden_sizes[:2]     )
      for i in range(len(self.cutoffs     )     ):
       lowerCamelCase__  ,	lowerCamelCase__  =     self.cutoff_ends[i], self.cutoff_ends[i + 1]
       if target is not None:
        lowerCamelCase__  =     (target >= l_idx) & (target < r_idx)
        lowerCamelCase__  =     tf.where(__lowerCamelCase     )
        lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     ) - l_idx

       if self.div_val == 1:
        lowerCamelCase__  =     self.out_layers[0][0][l_idx:r_idx]
        lowerCamelCase__  =     self.out_layers[0][1][l_idx:r_idx]
       else:
        lowerCamelCase__  =     self.out_layers[i][0]
        lowerCamelCase__  =     self.out_layers[i][1]

       if i == 0:
        lowerCamelCase__  =     tf.concat([cur_W, self.cluster_weight]				,     0     )
        lowerCamelCase__  =     tf.concat([cur_b, self.cluster_bias]				,     0     )

        lowerCamelCase__  =     self._logit(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase				,     self.out_projs[0]     )
        lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase     )
        out.append(head_logprob[..., : self.cutoffs[0]]     )
        if target is not None:
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     self._gather_logprob(__lowerCamelCase				,     __lowerCamelCase     )
       else:
        lowerCamelCase__  =     self._logit(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase				,     self.out_projs[i]     )
        lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase     )
        lowerCamelCase__  =     self.cutoffs[0] + i - 1  # No probability for the head cluster
        lowerCamelCase__  =     head_logprob[..., cluster_prob_idx, None] + tail_logprob
        out.append(__lowerCamelCase     )
        if target is not None:
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     self._gather_logprob(__lowerCamelCase				,     __lowerCamelCase     )
         cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
       if target is not None:
        loss += tf.scatter_nd(__lowerCamelCase				,     -cur_logprob				,     shape_list(__lowerCamelCase     )     )
      lowerCamelCase__  =     tf.concat(__lowerCamelCase				,     axis=-1     )

     if target is not None:
      if return_mean:
       lowerCamelCase__  =     tf.reduce_mean(__lowerCamelCase     )
      # Add the training-time loss value to the layer using `self.add_loss()`.
      self.add_loss(__lowerCamelCase     )

      # Log the loss as a metric (we could log arbitrary metrics,
      # including different metrics for training and inference.
      self.add_metric(__lowerCamelCase				,     name=self.name				,     aggregation="mean" if return_mean else ""     )

     return out

