



import argparse
import re

import numpy as np
import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    SamConfig,
    SamImageProcessor,
    SamModel,
    SamProcessor,
    SamVisionConfig,
)


__SCREAMING_SNAKE_CASE           =  {
    """iou_prediction_head.layers.0""": """iou_prediction_head.proj_in""",
    """iou_prediction_head.layers.1""": """iou_prediction_head.layers.0""",
    """iou_prediction_head.layers.2""": """iou_prediction_head.proj_out""",
    """mask_decoder.output_upscaling.0""": """mask_decoder.upscale_conv1""",
    """mask_decoder.output_upscaling.1""": """mask_decoder.upscale_layer_norm""",
    """mask_decoder.output_upscaling.3""": """mask_decoder.upscale_conv2""",
    """mask_downscaling.0""": """mask_embed.conv1""",
    """mask_downscaling.1""": """mask_embed.layer_norm1""",
    """mask_downscaling.3""": """mask_embed.conv2""",
    """mask_downscaling.4""": """mask_embed.layer_norm2""",
    """mask_downscaling.6""": """mask_embed.conv3""",
    """point_embeddings""": """point_embed""",
    """pe_layer.positional_encoding_gaussian_matrix""": """shared_embedding.positional_embedding""",
    """image_encoder""": """vision_encoder""",
    """neck.0""": """neck.conv1""",
    """neck.1""": """neck.layer_norm1""",
    """neck.2""": """neck.conv2""",
    """neck.3""": """neck.layer_norm2""",
    """patch_embed.proj""": """patch_embed.projection""",
    """.norm""": """.layer_norm""",
    """blocks""": """layers""",
}




def 	UpperCAmelCase   (						_lowerCamelCase    ):
   A							:					List[str]              =						{}
   state_dict.pop("pixel_mean" ,    _lowerCamelCase    )
   state_dict.pop("pixel_std" ,    _lowerCamelCase    )

   A							:					Any              =						r""".*.output_hypernetworks_mlps.(\d+).layers.(\d+).*"""

   for key, value in state_dict.items():
      for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items():
         if key_to_modify in key:
            A							:					str              =						key.replace(_lowerCamelCase ,    _lowerCamelCase    )

      if re.match(_lowerCamelCase ,    _lowerCamelCase    ):
         A							:					List[Any]              =						int(re.match(_lowerCamelCase ,    _lowerCamelCase    ).group(2    )    )
         if layer_nb == 0:
            A							:					int              =						key.replace("layers.0" ,    "proj_in"    )
         elif layer_nb == 1:
            A							:					Union[str, Any]              =						key.replace("layers.1" ,    "layers.0"    )
         elif layer_nb == 2:
            A							:					Optional[int]              =						key.replace("layers.2" ,    "proj_out"    )

      A							:					Dict              =						value

   A							:					Optional[int]              =						model_state_dict[
       """prompt_encoder.shared_embedding.positional_embedding"""
   ]

   return model_state_dict




def 	UpperCAmelCase   (						_lowerCamelCase ,    _lowerCamelCase ,    _lowerCamelCase ,    _lowerCamelCase="ybelkada/segment-anything"    ):
   A							:					int              =						hf_hub_download(_lowerCamelCase ,    f"""checkpoints/{model_name}.pth"""    )

   if "sam_vit_b" in model_name:
      A							:					Optional[int]              =						SamConfig()
   elif "sam_vit_l" in model_name:
      A							:					Union[str, Any]              =						SamVisionConfig(
          hidden_size=1024 ,    num_hidden_layers=24 ,    num_attention_heads=16 ,    global_attn_indexes=[5, 11, 17, 23] ,    )

      A							:					Optional[Any]              =						SamConfig(
          vision_config=_lowerCamelCase ,    )
   elif "sam_vit_h" in model_name:
      A							:					Optional[int]              =						SamVisionConfig(
          hidden_size=1280 ,    num_hidden_layers=32 ,    num_attention_heads=16 ,    global_attn_indexes=[7, 15, 23, 31] ,    )

      A							:					Optional[int]              =						SamConfig(
          vision_config=_lowerCamelCase ,    )

   A							:					str              =						torch.load(_lowerCamelCase ,    map_location="cpu"    )
   A							:					Dict              =						replace_keys(_lowerCamelCase    )

   A							:					Any              =						SamImageProcessor()

   A							:					Union[str, Any]              =						SamProcessor(image_processor=_lowerCamelCase    )
   A							:					int              =						SamModel(_lowerCamelCase    )

   hf_model.load_state_dict(_lowerCamelCase    )
   A							:					List[Any]              =						hf_model.to("cuda"    )

   A							:					Tuple              =						"""https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"""
   A							:					str              =						Image.open(requests.get(_lowerCamelCase ,    stream=_lowerCamelCase    ).raw    ).convert("RGB"    )

   A							:					str              =						[[[400, 650]]]
   A							:					Any              =						[[1]]

   A							:					Optional[int]              =						processor(images=np.array(_lowerCamelCase    ) ,    return_tensors="pt"    ).to("cuda"    )

   with torch.no_grad():
      A							:					Optional[Any]              =						hf_model(**_lowerCamelCase    )
   A							:					Any              =						output.iou_scores.squeeze()

   if model_name == "sam_vit_h_4b8939":
      assert scores[-1].item() == 0.5_79_89_02_51_15_96_68

      A							:					Tuple              =						processor(
          images=np.array(_lowerCamelCase    ) ,    input_points=_lowerCamelCase ,    input_labels=_lowerCamelCase ,    return_tensors="pt"    ).to("cuda"    )

      with torch.no_grad():
         A							:					int              =						hf_model(**_lowerCamelCase    )
      A							:					Any              =						output.iou_scores.squeeze()

      assert scores[-1].item() == 0.97_12_60_30_92_19_36_04

      A							:					Union[str, Any]              =						((75, 275, 1725, 850),)

      A							:					Optional[int]              =						processor(images=np.array(_lowerCamelCase    ) ,    input_boxes=_lowerCamelCase ,    return_tensors="pt"    ).to("cuda"    )

      with torch.no_grad():
         A							:					Optional[int]              =						hf_model(**_lowerCamelCase    )
      A							:					Optional[int]              =						output.iou_scores.squeeze()

      assert scores[-1].item() == 0.86_86_01_56_05_92_65_14

      # Test with 2 points and 1 image.
      A							:					int              =						[[[400, 650], [800, 650]]]
      A							:					str              =						[[1, 1]]

      A							:					Dict              =						processor(
          images=np.array(_lowerCamelCase    ) ,    input_points=_lowerCamelCase ,    input_labels=_lowerCamelCase ,    return_tensors="pt"    ).to("cuda"    )

      with torch.no_grad():
         A							:					Optional[int]              =						hf_model(**_lowerCamelCase    )
      A							:					int              =						output.iou_scores.squeeze()

      assert scores[-1].item() == 0.99_36_04_77_92_43_46_92


if __name__ == "__main__":
       __SCREAMING_SNAKE_CASE           =  argparse.ArgumentParser()
       __SCREAMING_SNAKE_CASE           =  ["""sam_vit_b_01ec64""", """sam_vit_h_4b8939""", """sam_vit_l_0b3195"""]
       parser.add_argument(
           """--model_name""",
           default="""sam_vit_h_4b8939""",
           choices=choices,
           type=str,
           help="""Path to hf config.json of model to convert""",
       )
       parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
       parser.add_argument(
           """--push_to_hub""",
           action="""store_true""",
           help="""Whether to push the model and processor to the hub after converting""",
       )
       parser.add_argument(
           """--model_hub_id""",
           default="""ybelkada/segment-anything""",
           choices=choices,
           type=str,
           help="""Path to hf config.json of model to convert""",
       )

       __SCREAMING_SNAKE_CASE           =  parser.parse_args()

       convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)




from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor

class 	lowerCamelCase_    :






						'''simple docstring'''







						def __init__(	self      :			List[Any]	,	__lowerCamelCase      :			Optional[int]	,	__lowerCamelCase      :			List[Any]=3	,	__lowerCamelCase      :			Union[str, Any]=32	,	__lowerCamelCase      :			List[Any]=3	,	__lowerCamelCase      :			Optional[int]=10	,	__lowerCamelCase      :			Optional[Any]=[10, 20, 30, 40]	,	__lowerCamelCase      :			Tuple=[1, 1, 2, 1]	,	__lowerCamelCase      :			Optional[Any]=True	,	__lowerCamelCase      :			Dict=True	,	__lowerCamelCase      :			List[str]="relu"	,	__lowerCamelCase      :			Tuple=3	,	__lowerCamelCase      :			Union[str, Any]=None	,	)      ->				str:
									A							:					Optional[Any]              =						parent
									A							:					Optional[int]              =						batch_size
									A							:					List[str]              =						image_size
									A							:					List[str]              =						num_channels
									A							:					Tuple              =						embeddings_size
									A							:					Optional[int]              =						hidden_sizes
									A							:					Dict              =						depths
									A							:					Optional[int]              =						is_training
									A							:					List[str]              =						use_labels
									A							:					List[Any]              =						hidden_act
									A							:					Optional[int]              =						num_labels
									A							:					int              =						scope
									A							:					List[Any]              =						len(__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				List[Any]:
									A							:					str              =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

									A							:					Optional[Any]              =						None
									if self.use_labels:
												A							:					Any              =						ids_tensor([self.batch_size]	,	self.num_labels )

									A							:					List[Any]              =						self.get_config()
									return config, pixel_values, labels







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				int:
									return RegNetConfig(
									    num_channels=self.num_channels	,	embeddings_size=self.embeddings_size	,	hidden_sizes=self.hidden_sizes	,	depths=self.depths	,	hidden_act=self.hidden_act	,	num_labels=self.num_labels	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			Union[str, Any] )      ->				Tuple:
									A							:					List[str]              =						TFRegNetModel(config=__lowerCamelCase )
									A							:					str              =						model(__lowerCamelCase	,	training=__lowerCamelCase )
									# expected last hidden states: B, C, H // 32, W // 32
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,	(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any]	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			Any	,	__lowerCamelCase      :			Tuple )      ->				List[str]:
									A							:					List[Any]              =						self.num_labels
									A							:					int              =						TFRegNetForImageClassification(__lowerCamelCase )
									A							:					str              =						model(__lowerCamelCase	,	labels=__lowerCamelCase	,	training=__lowerCamelCase )
									self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_labels) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Dict:
									A							:					Any              =						self.prepare_config_and_inputs()
									A       , A       , A							:					str              =						config_and_inputs
									A							:					Any              =						{"pixel_values": pixel_values}
									return config, inputs_dict







@require_tf
class 	lowerCamelCase_    ( _A						,_A						,unittest.TestCase						):






						'''simple docstring'''
						a__   =  (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
						a__   =  (
						    {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification}
						    if is_tf_available()
						    else {}
						)

						a__   =  False
						a__   =  False
						a__   =  False
						a__   =  False
						a__   =  False







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				List[str]:
									A							:					Optional[Any]              =						TFRegNetModelTester(self )
									A							:					int              =						ConfigTester(self	,	config_class=__lowerCamelCase	,	has_text_modality=__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Tuple:
									return







						@unittest.skip(reason="RegNet does not use inputs_embeds" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				Optional[Any]:
									pass







						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices("GPU" ) ) == 0	,	reason="TF does not support backprop for grouped convolutions on CPU."	,	)
						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Any:
									super().test_keras_fit()







						@unittest.skip(reason="RegNet does not support input and output embeddings" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Union[str, Any]:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Tuple:
									A       , A							:					List[str]              =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A							:					Union[str, Any]              =						model_class(__lowerCamelCase )
												A							:					int              =						inspect.signature(model.call )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												A							:					Union[str, Any]              =						[*signature.parameters.keys()]

												A							:					Optional[int]              =						["pixel_values"]
												self.assertListEqual(arg_names[:1]	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple )      ->				Tuple:
									A							:					Tuple              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				List[str]:
									def check_hidden_states_output(__lowerCamelCase      :			Union[str, Any]	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			str ):
												A							:					int              =						model_class(__lowerCamelCase )
												A							:					int              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase )	,	training=__lowerCamelCase )

												A							:					Any              =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												A							:					Dict              =						self.model_tester.num_stages
												self.assertEqual(len(__lowerCamelCase )	,	expected_num_stages + 1 )

												# RegNet's feature maps are of shape (batch_size, num_channels, height, width)
												self.assertListEqual(
												    list(hidden_states[0].shape[-2:] )	,	[self.model_tester.image_size // 2, self.model_tester.image_size // 2]	,	)

									A       , A							:					int              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					str              =						["basic", "bottleneck"]
									for model_class in self.all_model_classes:
												for layer_type in layers_type:
															A							:					List[str]              =						layer_type
															A							:					List[Any]              =						True
															check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															A							:					Union[str, Any]              =						True

															check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Dict:
									A       , A							:					Tuple              =						self.model_tester.prepare_config_and_inputs_for_common()

									def check_equivalence(__lowerCamelCase      :			int	,	__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			Optional[Any]={} ):
												A							:					Optional[int]              =						model(__lowerCamelCase	,	return_dict=__lowerCamelCase	,	**__lowerCamelCase )
												A							:					int              =						model(__lowerCamelCase	,	return_dict=__lowerCamelCase	,	**__lowerCamelCase ).to_tuple()

												def recursive_check(__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			Any ):
															if isinstance(__lowerCamelCase	,	(List, Tuple) ):
																		for tuple_iterable_value, dict_iterable_value in zip(__lowerCamelCase	,	__lowerCamelCase ):
																					recursive_check(__lowerCamelCase	,	__lowerCamelCase )
															elif tuple_object is None:
																		return
															else:
																		self.assertTrue(
																		    all(tf.equal(__lowerCamelCase	,	__lowerCamelCase ) )	,	msg=(
																		        "Tuple and dict output are not equal. Difference:"
																		        F""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}"""
																		    )	,	)

															recursive_check(__lowerCamelCase	,	__lowerCamelCase )

									for model_class in self.all_model_classes:
												A							:					Tuple              =						model_class(__lowerCamelCase )

												A							:					Optional[int]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase )
												A							:					Tuple              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase )
												check_equivalence(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )

												A							:					Optional[int]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												A							:					List[Any]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												check_equivalence(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )

												A							:					Optional[Any]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase )
												A							:					str              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase )
												check_equivalence(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase	,	{"output_hidden_states": True} )

												A							:					Optional[Any]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												A							:					Tuple              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												check_equivalence(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase	,	{"output_hidden_states": True} )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Optional[Any]:
									A							:					List[str]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Union[str, Any]:
									for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A							:					Union[str, Any]              =						TFRegNetModel.from_pretrained(__lowerCamelCase )
												self.assertIsNotNone(__lowerCamelCase )







def 	UpperCAmelCase   (						):
			A							:					Dict              =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
			return image






@require_tf
@require_vision
class 	lowerCamelCase_    ( unittest.TestCase						):






						'''simple docstring'''







						@cached_property
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Optional[Any]:
									return (
									    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )
									    if is_vision_available()
									    else None
									)







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Optional[Any]:
									A							:					List[Any]              =						TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

									A							:					Optional[int]              =						self.default_image_processor
									A							:					List[Any]              =						prepare_img()
									A							:					str              =						image_processor(images=__lowerCamelCase	,	return_tensors="tf" )

									# forward pass
									A							:					List[Any]              =						model(**__lowerCamelCase	,	training=__lowerCamelCase )

									# verify the logits
									A							:					Dict              =						tf.TensorShape((1, 10_00) )
									self.assertEqual(outputs.logits.shape	,	__lowerCamelCase )

									A							:					Optional[Any]              =						tf.constant([-0.4180, -1.5051, -3.4836] )

									tf.debugging.assert_near(outputs.logits[0, :3]	,	__lowerCamelCase	,	atol=1e-4 )