




import tensorflow as tf

from ...tf_utils import shape_list







class     __lowerCAmelCase (       tf.keras.layers.Layer ):

			"""simple docstring"""







			def __init__(      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__=1			,						lowerCamelCase__=False			,						**lowerCamelCase__							)    ->      Union[str, Any]:



							'''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       lowercase_    (      self			,						lowerCamelCase__							)    ->      Union[str, Any]:



							'''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       lowercase_    (      lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__=None							)    ->      int:



							'''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       lowercase_    (      lowerCamelCase__			,						lowerCamelCase__							)    ->      int:



							'''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       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__=True			,						lowerCamelCase__=False							)    ->      Any:



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



"""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        UpperCamelCase		:
          @staticmethod
          def 			_UpperCAmelCase   (			*__UpperCamelCase     ,**__UpperCamelCase			)	->      Union[str, Any]:


               '''simple docstring'''


               pass







@is_pipeline_test
@require_vision
@require_torch
class        UpperCamelCase		(			unittest.TestCase  ):
   lowercase							=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				int													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	[
            {
                'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                'candidate_labels': ['cat', 'remote', 'couch'],
            }
        ]
        return object_detector, examples
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      str:


        '''simple docstring'''


        lowercase_	:				Tuple													=	object_detector(examples[0]     ,threshold=0.0			)

        lowercase_	:				Any													=	len(__UpperCamelCase			)
        self.assertGreater(__UpperCamelCase     ,0			)
        self.assertEqual(
            __UpperCamelCase     ,[
                {
                    'score': ANY(__UpperCamelCase			),
                    'label': ANY(__UpperCamelCase			),
                    'box': {'xmin': ANY(__UpperCamelCase			), 'ymin': ANY(__UpperCamelCase			), 'xmax': ANY(__UpperCamelCase			), 'ymax': ANY(__UpperCamelCase			)},
                }
                for i in range(__UpperCamelCase			)
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        pass
   @require_torch
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	object_detector(
            './tests/fixtures/tests_samples/COCO/000000039769.png'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                }
            ]     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                    {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                    {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                ]
            ]     ,)
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[str]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	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(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        pass
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	0.2
        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				str													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
            ]     ,)







   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	2
        lowercase_	:				Optional[Any]													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[Any]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,top_k=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
            ]     ,)

