



'''simple docstring'''


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  UpperCAmelCase_       (		lowercase__       ):
     a_       		=[2, 2, 6, 2] if "tiny" in model_name else [2, 2, 1_8, 2]
     a_       		=True if "large" in model_name or "huge" in model_name else False
     a_       		=True if "large" in model_name or "huge" in model_name else False
     a_       		=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:
               a_       		=[3, 3, 3, 3]
               a_       		=[5, 5, 5, 5]
          elif "fl4" in model_name:
               a_       		=[4, 4, 4, 4]
               a_       		=[3, 3, 3, 3]

     if "tiny" in model_name or "small" in model_name or "base" in model_name:
          a_       		=[3, 3, 3, 3]
          if "lrf" in model_name:
               a_       		=[3, 3, 3, 3]
          else:
               a_       		=[2, 2, 2, 2]

     if "tiny" in model_name:
          a_       		=9_6
     elif "small" in model_name:
          a_       		=9_6
     elif "base" in model_name:
          a_       		=1_2_8
     elif "large" in model_name:
          a_       		=1_9_2
     elif "xlarge" in model_name:
          a_       		=2_5_6
     elif "huge" in model_name:
          a_       		=3_5_2

     # set label information
     a_       		="huggingface/label-files"
     if "large" in model_name or "huge" in model_name:
          a_       		="imagenet-22k-id2label.json"
     else:
          a_       		="imagenet-1k-id2label.json"

     a_       		=json.load(open(hf_hub_download(_lowerCAmelCase       ,	_lowerCAmelCase       ,	repo_type="dataset"       )       ,	"r"       )       )
     a_       		={int(_lowerCAmelCase       ): v for k, v in idalabel.items()}
     a_       		={v: k for k, v in idalabel.items()}

     a_       		=FocalNetConfig(
         embed_dim=_lowerCAmelCase       ,	depths=_lowerCAmelCase       ,	focal_levels=_lowerCAmelCase       ,	focal_windows=_lowerCAmelCase       ,	use_conv_embed=_lowerCAmelCase       ,	idalabel=_lowerCAmelCase       ,	labelaid=_lowerCAmelCase       ,	use_post_layernorm=_lowerCAmelCase       ,	use_layerscale=_lowerCAmelCase       ,	)

     return config






def  UpperCAmelCase_       (		lowercase__       ):
     if "patch_embed.proj" in name:
          a_       		=name.replace("patch_embed.proj"       ,	"embeddings.patch_embeddings.projection"       )
     if "patch_embed.norm" in name:
          a_       		=name.replace("patch_embed.norm"       ,	"embeddings.norm"       )
     if "layers" in name:
          a_       		="encoder." + name
     if "encoder.layers" in name:
          a_       		=name.replace("encoder.layers"       ,	"encoder.stages"       )
     if "downsample.proj" in name:
          a_       		=name.replace("downsample.proj"       ,	"downsample.projection"       )
     if "blocks" in name:
          a_       		=name.replace("blocks"       ,	"layers"       )
     if "modulation.f.weight" in name or "modulation.f.bias" in name:
          a_       		=name.replace("modulation.f"       ,	"modulation.projection_in"       )
     if "modulation.h.weight" in name or "modulation.h.bias" in name:
          a_       		=name.replace("modulation.h"       ,	"modulation.projection_context"       )
     if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
          a_       		=name.replace("modulation.proj"       ,	"modulation.projection_out"       )

     if name == "norm.weight":
          a_       		="layernorm.weight"
     if name == "norm.bias":
          a_       		="layernorm.bias"

     if "head" in name:
          a_       		=name.replace("head"       ,	"classifier"       )
     else:
          a_       		="focalnet." + name

     return name






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__=False       ):
     # fmt: off
     a_       		={
         "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

     a_       		=model_name_to_url[model_name]
     print("Checkpoint URL: "       ,	_lowerCAmelCase       )
     a_       		=torch.hub.load_state_dict_from_url(_lowerCAmelCase       ,	map_location="cpu"       )["model"]

     # rename keys
     for key in state_dict.copy().keys():
          a_       		=state_dict.pop(_lowerCAmelCase       )
          a_       		=val

     a_       		=get_focalnet_config(_lowerCAmelCase       )
     a_       		=FocalNetForImageClassification(_lowerCAmelCase       )
     model.eval()

     # load state dict
     model.load_state_dict(_lowerCAmelCase       )

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

     a_       		=BitImageProcessor(
         do_resize=_lowerCAmelCase       ,	size={"shortest_edge": 2_5_6}       ,	resample=PILImageResampling.BILINEAR       ,	do_center_crop=_lowerCAmelCase       ,	crop_size=2_2_4       ,	do_normalize=_lowerCAmelCase       ,	image_mean=_lowerCAmelCase       ,	image_std=_lowerCAmelCase       ,	)
     a_       		=Image.open(requests.get(_lowerCAmelCase       ,	stream=_lowerCAmelCase       ).raw       )
     a_       		=processor(images=_lowerCAmelCase       ,	return_tensors="pt"       )

     a_       		=transforms.Compose(
         [
             transforms.Resize(2_5_6       ),
             transforms.CenterCrop(2_2_4       ),
             transforms.ToTensor(),
             transforms.Normalize(mean=[0.485, 0.456, 0.406]       ,	std=[0.229, 0.224, 0.225]       ),
         ]       )

     a_       		=image_transforms(_lowerCAmelCase       ).unsqueeze(0       )

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

     a_       		=model(**_lowerCAmelCase       )

     a_       		=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":
          a_       		=torch.tensor([0.2166, -0.4368, 0.2191]       )
     elif model_name == "focalnet-tiny-lrf":
          a_       		=torch.tensor([1.1669, 0.0125, -0.1695]       )
     elif model_name == "focalnet-small":
          a_       		=torch.tensor([0.4917, -0.0430, 0.1341]       )
     elif model_name == "focalnet-small-lrf":
          a_       		=torch.tensor([-0.2588, -0.5342, -0.2331]       )
     elif model_name == "focalnet-base":
          a_       		=torch.tensor([-0.1655, -0.4090, -0.1730]       )
     elif model_name == "focalnet-base-lrf":
          a_       		=torch.tensor([0.5306, -0.0483, -0.3928]       )
     assert torch.allclose(outputs.logits[0, :3]       ,	_lowerCAmelCase       ,	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(_lowerCAmelCase       )
          processor.save_pretrained(_lowerCAmelCase       )

     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__":
  lowercase							       =			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.''',
  )

  lowercase							       =			parser.parse_args()
  convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)







'''simple docstring'''


import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 	UpperCAmelCase	:


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      return self.get_dummy_input()




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      if self.block_type == "down":
           return (4, 3_2, 1_6, 1_6)
      elif self.block_type == "mid":
           return (4, 3_2, 3_2, 3_2)
      elif self.block_type == "up":
           return (4, 3_2, 6_4, 6_4)

      raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""")




 def       lowercase_  (   self							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  ) ->      Dict:



      """simple docstring"""







      a_       		=4
      a_       		=3_2
      a_       		=(3_2, 3_2)

      a_       		=torch.manual_seed(0)
      a_       		=torch.device(lowerCAmelCase_)
      a_       		=(batch_size, num_channels) + sizes
      a_       		=randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)
      a_       		={"hidden_states": hidden_states}

      if include_temb:
           a_       		=1_2_8
           a_       		=randn_tensor((batch_size, temb_channels)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      if include_res_hidden_states_tuple:
           a_       		=torch.manual_seed(1)
           a_       		=(randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_),)

      if include_encoder_hidden_states:
           a_       		=floats_tensor((batch_size, 3_2, 3_2)).to(lowerCAmelCase_)

      if include_skip_sample:
           a_       		=randn_tensor(((batch_size, 3) + sizes)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      return dummy_input




 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      a_       		={
          "in_channels": 3_2,
          "out_channels": 3_2,
          "temb_channels": 1_2_8,
      }
      if self.block_type == "up":
           a_       		=3_2

      if self.block_type == "mid":
           init_dict.pop("out_channels")

      a_       		=self.dummy_input
      return init_dict, inputs_dict




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      unet_block.to(lowerCAmelCase_)
      unet_block.eval()

      with torch.no_grad():
           a_       		=unet_block(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      self.assertEqual(output.shape							,  self.output_shape)

      a_       		=output[0, -1, -3:, -3:]
      a_       		=torch.tensor(lowerCAmelCase_).to(lowerCAmelCase_)
      assert torch_all_close(output_slice.flatten()							,  lowerCAmelCase_							,  atol=5e-3)




 @unittest.skipIf(torch_device == "mps"							,  "Training is not supported in mps")
 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      model.to(lowerCAmelCase_)
      model.train()
      a_       		=model(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      a_       		=torch.device(lowerCAmelCase_)
      a_       		=randn_tensor(output.shape							,  device=lowerCAmelCase_)
      a_       		=torch.nn.functional.mse_loss(lowerCAmelCase_							,  lowerCAmelCase_)
      loss.backward()


