





import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from torchvision import transforms

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling







def 			A__							(    SCREAMING_SNAKE_CASE__)   ->    Union[str, Any]:
  __snake_case:						Optional[Any]   =						[2, 2, 6, 2] if """tiny""" in model_name else [2, 2, 18, 2]
  __snake_case:						Any   =						True if """large""" in model_name or """huge""" in model_name else False
  __snake_case:						Union[str, Any]   =						True if """large""" in model_name or """huge""" in model_name else False
  __snake_case:						Any   =						True if """large""" in model_name or """huge""" in model_name else False

  if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
    if "fl3" in model_name:
      __snake_case:						Union[str, Any]   =						[3, 3, 3, 3]
      __snake_case:						Dict   =						[5, 5, 5, 5]
    elif "fl4" in model_name:
      __snake_case:						int   =						[4, 4, 4, 4]
      __snake_case:						Tuple   =						[3, 3, 3, 3]

  if "tiny" in model_name or "small" in model_name or "base" in model_name:
    __snake_case:						Tuple   =						[3, 3, 3, 3]
    if "lrf" in model_name:
      __snake_case:						List[str]   =						[3, 3, 3, 3]
    else:
      __snake_case:						List[Any]   =						[2, 2, 2, 2]

  if "tiny" in model_name:
    __snake_case:						int   =						96
  elif "small" in model_name:
    __snake_case:						List[str]   =						96
  elif "base" in model_name:
    __snake_case:						Dict   =						128
  elif "large" in model_name:
    __snake_case:						Optional[int]   =						192
  elif "xlarge" in model_name:
    __snake_case:						List[Any]   =						256
  elif "huge" in model_name:
    __snake_case:						Optional[int]   =						352

  # set label information
  __snake_case:						Any   =						"""huggingface/label-files"""
  if "large" in model_name or "huge" in model_name:
    __snake_case:						str   =						"""imagenet-22k-id2label.json"""
  else:
    __snake_case:						Optional[Any]   =						"""imagenet-1k-id2label.json"""

  __snake_case:						Tuple   =						json.load(open(hf_hub_download(UpperCamelCase__					,					UpperCamelCase__					,					repo_type="""dataset""")					,					"""r"""))
  __snake_case:						Dict   =						{int(UpperCamelCase__): v for k, v in idalabel.items()}
  __snake_case:						Optional[int]   =						{v: k for k, v in idalabel.items()}

  __snake_case:						List[str]   =						FocalNetConfig(
      embed_dim=UpperCamelCase__					,					depths=UpperCamelCase__					,					focal_levels=UpperCamelCase__					,					focal_windows=UpperCamelCase__					,					use_conv_embed=UpperCamelCase__					,					idalabel=UpperCamelCase__					,					labelaid=UpperCamelCase__					,					use_post_layernorm=UpperCamelCase__					,					use_layerscale=UpperCamelCase__					,					)

  return config







def 			A__							(    SCREAMING_SNAKE_CASE__)   ->    List[str]:
  if "patch_embed.proj" in name:
    __snake_case:						List[Any]   =						name.replace("""patch_embed.proj"""					,					"""embeddings.patch_embeddings.projection""")
  if "patch_embed.norm" in name:
    __snake_case:						Optional[int]   =						name.replace("""patch_embed.norm"""					,					"""embeddings.norm""")
  if "layers" in name:
    __snake_case:						str   =						"""encoder.""" + name
  if "encoder.layers" in name:
    __snake_case:						Any   =						name.replace("""encoder.layers"""					,					"""encoder.stages""")
  if "downsample.proj" in name:
    __snake_case:						Any   =						name.replace("""downsample.proj"""					,					"""downsample.projection""")
  if "blocks" in name:
    __snake_case:						List[str]   =						name.replace("""blocks"""					,					"""layers""")
  if "modulation.f.weight" in name or "modulation.f.bias" in name:
    __snake_case:						Tuple   =						name.replace("""modulation.f"""					,					"""modulation.projection_in""")
  if "modulation.h.weight" in name or "modulation.h.bias" in name:
    __snake_case:						List[str]   =						name.replace("""modulation.h"""					,					"""modulation.projection_context""")
  if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
    __snake_case:						Optional[Any]   =						name.replace("""modulation.proj"""					,					"""modulation.projection_out""")

  if name == "norm.weight":
    __snake_case:						Dict   =						"""layernorm.weight"""
  if name == "norm.bias":
    __snake_case:						int   =						"""layernorm.bias"""

  if "head" in name:
    __snake_case:						Union[str, Any]   =						name.replace("""head"""					,					"""classifier""")
  else:
    __snake_case:						Optional[Any]   =						"""focalnet.""" + name

  return name







def 			A__							(    SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__=False)   ->    List[Any]:
  __snake_case:						Dict   =						{
      """focalnet-tiny""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth""",
      """focalnet-tiny-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth""",
      """focalnet-small""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth""",
      """focalnet-small-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth""",
      """focalnet-base""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth""",
      """focalnet-base-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth""",
      """focalnet-large-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth""",
      """focalnet-large-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth""",
      """focalnet-xlarge-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth""",
      """focalnet-xlarge-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth""",
  }
  # fmt: on

  __snake_case:						Optional[Any]   =						model_name_to_url[model_name]
  print("""Checkpoint URL: """					,					UpperCamelCase__)
  __snake_case:						List[Any]   =						torch.hub.load_state_dict_from_url(UpperCamelCase__					,					map_location="""cpu""")["""model"""]

  # rename keys
  for key in state_dict.copy().keys():
    __snake_case:						List[str]   =						state_dict.pop(UpperCamelCase__)
    __snake_case:						Any   =						val

  __snake_case:						Union[str, Any]   =						get_focalnet_config(UpperCamelCase__)
  __snake_case:						List[Any]   =						FocalNetForImageClassification(UpperCamelCase__)
  model.eval()

  # load state dict
  model.load_state_dict(UpperCamelCase__)

  # verify conversion
  __snake_case:						Tuple   =						"""http://images.cocodataset.org/val2017/000000039769.jpg"""

  __snake_case:						Optional[Any]   =						BitImageProcessor(
      do_resize=UpperCamelCase__					,					size={"""shortest_edge""": 256}					,					resample=PILImageResampling.BILINEAR					,					do_center_crop=UpperCamelCase__					,					crop_size=224					,					do_normalize=UpperCamelCase__					,					image_mean=UpperCamelCase__					,					image_std=UpperCamelCase__					,					)
  __snake_case:						Optional[Any]   =						Image.open(requests.get(UpperCamelCase__					,					stream=UpperCamelCase__).raw)
  __snake_case:						Union[str, Any]   =						processor(images=UpperCamelCase__					,					return_tensors="""pt""")

  __snake_case:						Tuple   =						transforms.Compose(
      [
          transforms.Resize(256),
          transforms.CenterCrop(224),
          transforms.ToTensor(),
          transforms.Normalize(mean=[0.485, 0.456, 0.406]					,					std=[0.229, 0.224, 0.225]),
      ])

  __snake_case:						Dict   =						image_transforms(UpperCamelCase__).unsqueeze(0)

  # verify pixel_values
  assert torch.allclose(inputs.pixel_values					,					UpperCamelCase__					,					atol=1e-4)

  __snake_case:						Any   =						model(**UpperCamelCase__)

  __snake_case:						Dict   =						outputs.logits.argmax(-1).item()
  print("""Predicted class:"""					,					model.config.idalabel[predicted_class_idx])

  print("""First values of logits:"""					,					outputs.logits[0, :3])

  if model_name == "focalnet-tiny":
    __snake_case:						str   =						torch.tensor([0.2_166, -0.4_368, 0.2_191])
  elif model_name == "focalnet-tiny-lrf":
    __snake_case:						int   =						torch.tensor([1.1_669, 0.0_125, -0.1_695])
  elif model_name == "focalnet-small":
    __snake_case:						Optional[Any]   =						torch.tensor([0.4_917, -0.0_430, 0.1_341])
  elif model_name == "focalnet-small-lrf":
    __snake_case:						Optional[Any]   =						torch.tensor([-0.2_588, -0.5_342, -0.2_331])
  elif model_name == "focalnet-base":
    __snake_case:						Dict   =						torch.tensor([-0.1_655, -0.4_090, -0.1_730])
  elif model_name == "focalnet-base-lrf":
    __snake_case:						int   =						torch.tensor([0.5_306, -0.0_483, -0.3_928])
  assert torch.allclose(outputs.logits[0, :3]					,					UpperCamelCase__					,					atol=1e-4)
  print("""Looks ok!""")

  if pytorch_dump_folder_path is not None:
    print(F'''Saving model and processor of {model_name} to {pytorch_dump_folder_path}''')
    model.save_pretrained(UpperCamelCase__)
    processor.save_pretrained(UpperCamelCase__)

  if push_to_hub:
    print(F'''Pushing model and processor of {model_name} to the hub...''')
    model.push_to_hub(F'''{model_name}''')
    processor.push_to_hub(F'''{model_name}''')


if __name__ == "__main__":
      __UpperCAmelCase       :   List[Any]				       =							argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          "--model_name",
          default="focalnet-tiny",
          type=str,
          help="Name of the FocalNet model you\'d like to convert.",
      )
      parser.add_argument(
          "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
      )
      parser.add_argument(
          "--push_to_hub",
          action="store_true",
          help="Whether to push the model and processor to the hub.",
      )

      __UpperCAmelCase       :   Dict				       =							parser.parse_args()
      convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)














"""simple docstring"""




import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list






@require_torchaudio
@require_sentencepiece
class        UpperCamelCase			(	unittest.TestCase      ):


 def _lowercase	(     self		:						int							)					->       List[str]:
   _a  :       Any             =	"""laion/clap-htsat-unfused"""
   _a  :       Union[str, Any]             =	tempfile.mkdtemp()


 def _lowercase	(     self		:						List[Any]							,						**UpperCAmelCase__		:						Any							)					->       Dict:
   return RobertaTokenizer.from_pretrained(self.checkpoint							,						**UpperCAmelCase__							)


 def _lowercase	(     self		:						List[Any]							,						**UpperCAmelCase__		:						List[str]							)					->       int:
   return ClapFeatureExtractor.from_pretrained(self.checkpoint							,						**UpperCAmelCase__							)


 def _lowercase	(     self		:						List[Any]							)					->       Tuple:
   shutil.rmtree(self.tmpdirname							)


 def _lowercase	(     self		:						List[str]							)					->       Optional[int]:
   _a  :       List[str]             =	self.get_tokenizer()
   _a  :       Any             =	self.get_feature_extractor()

   _a  :       Optional[Any]             =	ClapProcessor(tokenizer=UpperCAmelCase__							,						feature_extractor=UpperCAmelCase__							)

   processor.save_pretrained(self.tmpdirname							)
   _a  :       List[str]             =	ClapProcessor.from_pretrained(self.tmpdirname							)

   self.assertEqual(processor.tokenizer.get_vocab()							,						tokenizer.get_vocab()							)
   self.assertIsInstance(processor.tokenizer							,						UpperCAmelCase__							)

   self.assertEqual(processor.feature_extractor.to_json_string()							,						feature_extractor.to_json_string()							)
   self.assertIsInstance(processor.feature_extractor							,						UpperCAmelCase__							)


 def _lowercase	(     self		:						Dict							)					->       Optional[int]:
   _a  :       Tuple             =	ClapProcessor(tokenizer=self.get_tokenizer()							,						feature_extractor=self.get_feature_extractor()							)
   processor.save_pretrained(self.tmpdirname							)

   _a  :       Dict             =	self.get_tokenizer(bos_token="""(BOS)"""							,						eos_token="""(EOS)"""							)
   _a  :       Union[str, Any]             =	self.get_feature_extractor(do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   _a  :       Union[str, Any]             =	ClapProcessor.from_pretrained(
       self.tmpdirname							,						bos_token="""(BOS)"""							,						eos_token="""(EOS)"""							,						do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   self.assertEqual(processor.tokenizer.get_vocab()							,						tokenizer_add_kwargs.get_vocab()							)
   self.assertIsInstance(processor.tokenizer							,						UpperCAmelCase__							)

   self.assertEqual(processor.feature_extractor.to_json_string()							,						feature_extractor_add_kwargs.to_json_string()							)
   self.assertIsInstance(processor.feature_extractor							,						UpperCAmelCase__							)


 def _lowercase	(     self		:						List[str]							)					->       Optional[Any]:
   _a  :       Optional[int]             =	self.get_feature_extractor()
   _a  :       Tuple             =	self.get_tokenizer()

   _a  :       List[Any]             =	ClapProcessor(tokenizer=UpperCAmelCase__							,						feature_extractor=UpperCAmelCase__							)

   _a  :       Any             =	floats_list((3, 1000)							)

   _a  :       List[Any]             =	feature_extractor(UpperCAmelCase__							,						return_tensors="""np"""							)
   _a  :       List[str]             =	processor(audios=UpperCAmelCase__							,						return_tensors="""np"""							)

   for key in input_feat_extract.keys():
     self.assertAlmostEqual(input_feat_extract[key].sum()							,						input_processor[key].sum()							,						delta=1E-2							)


 def _lowercase	(     self		:						Tuple							)					->       Optional[int]:
   _a  :       List[str]             =	self.get_feature_extractor()
   _a  :       Any             =	self.get_tokenizer()

   _a  :       Any             =	ClapProcessor(tokenizer=UpperCAmelCase__							,						feature_extractor=UpperCAmelCase__							)

   _a  :       Optional[int]             =	"""This is a test string"""

   _a  :       Tuple             =	processor(text=UpperCAmelCase__							)

   _a  :       int             =	tokenizer(UpperCAmelCase__							)

   for key in encoded_tok.keys():
     self.assertListEqual(encoded_tok[key]							,						encoded_processor[key]							)


 def _lowercase	(     self		:						List[Any]							)					->       Any:
   _a  :       str             =	self.get_feature_extractor()
   _a  :       List[str]             =	self.get_tokenizer()

   _a  :       List[Any]             =	ClapProcessor(tokenizer=UpperCAmelCase__							,						feature_extractor=UpperCAmelCase__							)

   _a  :       Any             =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

   _a  :       Dict             =	processor.batch_decode(UpperCAmelCase__							)
   _a  :       Any             =	tokenizer.batch_decode(UpperCAmelCase__							)

   self.assertListEqual(UpperCAmelCase__							,						UpperCAmelCase__							)







 def _lowercase	(     self		:						Dict							)					->       List[str]:
   _a  :       str             =	self.get_feature_extractor()
   _a  :       Optional[Any]             =	self.get_tokenizer()

   _a  :       Union[str, Any]             =	ClapProcessor(tokenizer=UpperCAmelCase__							,						feature_extractor=UpperCAmelCase__							)

   self.assertListEqual(
       processor.model_input_names[2:]							,						feature_extractor.model_input_names							,						msg="""`processor` and `feature_extractor` model input names do not match"""							,						)

