



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 					snake_case__						:






      """simple docstring"""


      @staticmethod
      def    lowercase_   (       *_snake_case  :  Optional[Any], **_snake_case  :  int					)   ->Optional[int]:
             pass




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






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






     def    lowercase_   (       self  :  Optional[int], _snake_case  :  Dict, _snake_case  :  Dict, _snake_case  :  Any					)   ->Dict:
            snake_case__							:	List[Any]   	=	pipeline(
                'zero-shot-object-detection', model='hf-internal-testing/tiny-random-owlvit-object-detection'					)

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






     def    lowercase_   (       self  :  Union[str, Any], _snake_case  :  Optional[int], _snake_case  :  Optional[int]					)   ->Any:
            snake_case__							:	str   	=	object_detector(examples[0], threshold=0.0					)

            snake_case__							:	Any   	=	len(_snake_case					)
            self.assertGreater(_snake_case, 0					)
            self.assertEqual(
                _snake_case, [
                    {
                        'score': ANY(_snake_case					),
                        'label': ANY(_snake_case					),
                        'box': {'xmin': ANY(_snake_case					), 'ymin': ANY(_snake_case					), 'xmax': ANY(_snake_case					), 'ymax': ANY(_snake_case					)},
                    }
                    for i in range(_snake_case					)
                ], )






     @require_tf
     @unittest.skip('Zero Shot Object Detection not implemented in TF'					)
     def    lowercase_   (       self  :  int					)   ->Optional[int]:
            pass






     @require_torch
     def    lowercase_   (       self  :  Any					)   ->Tuple:
            snake_case__							:	Dict   	=	pipeline(
                'zero-shot-object-detection', model='hf-internal-testing/tiny-random-owlvit-object-detection'					)

            snake_case__							:	Tuple   	=	object_detector(
                './tests/fixtures/tests_samples/COCO/000000039769.png', candidate_labels=['cat', 'remote', 'couch'], threshold=0.6_4, )

            self.assertEqual(
                nested_simplify(_snake_case, 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}},
                ], )

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

            self.assertEqual(
                nested_simplify(_snake_case, 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    lowercase_   (       self  :  List[Any]					)   ->Dict:
            snake_case__							:	str   	=	pipeline('zero-shot-object-detection'					)

            snake_case__							:	str   	=	object_detector(
                'http://images.cocodataset.org/val2017/000000039769.jpg', candidate_labels=['cat', 'remote', 'couch'], )
            self.assertEqual(
                nested_simplify(_snake_case, 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}},
                ], )

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






     @require_torch
     @slow
     def    lowercase_   (       self  :  Optional[int]					)   ->Tuple:
            snake_case__							:	Tuple   	=	0.2
            snake_case__							:	Union[str, Any]   	=	pipeline('zero-shot-object-detection'					)

            snake_case__							:	Optional[Any]   	=	object_detector(
                'http://images.cocodataset.org/val2017/000000039769.jpg', candidate_labels=['cat', 'remote', 'couch'], threshold=_snake_case, )
            self.assertEqual(
                nested_simplify(_snake_case, 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    lowercase_   (       self  :  List[Any]					)   ->str:
            snake_case__							:	List[Any]   	=	2
            snake_case__							:	Optional[int]   	=	pipeline('zero-shot-object-detection'					)

            snake_case__							:	List[str]   	=	object_detector(
                'http://images.cocodataset.org/val2017/000000039769.jpg', candidate_labels=['cat', 'remote', 'couch'], top_k=_snake_case, )
            self.assertEqual(
                nested_simplify(_snake_case, 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}},
                ], )











import argparse

import torch

from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert
from transformers.utils import logging


logging.set_verbosity_info()
def        lowercase_							(A : str		,					A : List[Any]		,					A : Any      ):
       # Initialise PyTorch model
       snake_case__							:	List[Any]   	=	LxmertConfig.from_json_file(A      )
       print(F'''Building PyTorch model from configuration: {config}'''      )
       snake_case__							:	List[str]   	=	LxmertForPreTraining(A      )

       # Load weights from tf checkpoint
       load_tf_weights_in_lxmert(A		,					A		,					A      )

       # Save pytorch-model
       print(F'''Save PyTorch model to {pytorch_dump_path}'''      )
       torch.save(model.state_dict()		,					A      )


if __name__ == "__main__":
 a_   :Union[str, Any]        =      argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
 )
 parser.add_argument(
     "--config_file",
     default=None,
     type=str,
     required=True,
     help="The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.",
 )
 parser.add_argument(
     "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
 )
 a_   :Optional[int]        =      parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)






