



"""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       __magic_name__    (   _lowerCamelCase				: Dict					):
							__a		:		Dict									=       [2, 2, 6, 2] if """tiny""" in model_name else [2, 2, 1_8, 2]
							__a		:		str									=       True if """large""" in model_name or """huge""" in model_name else False
							__a		:		List[str]									=       True if """large""" in model_name or """huge""" in model_name else False
							__a		:		List[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:
																					__a		:		Optional[Any]									=       [3, 3, 3, 3]
																					__a		:		Optional[int]									=       [5, 5, 5, 5]
														elif "fl4" in model_name:
																					__a		:		Union[str, Any]									=       [4, 4, 4, 4]
																					__a		:		Optional[Any]									=       [3, 3, 3, 3]

							if "tiny" in model_name or "small" in model_name or "base" in model_name:
														__a		:		Union[str, Any]									=       [3, 3, 3, 3]
														if "lrf" in model_name:
																					__a		:		int									=       [3, 3, 3, 3]
														else:
																					__a		:		Union[str, Any]									=       [2, 2, 2, 2]

							if "tiny" in model_name:
														__a		:		List[str]									=       9_6
							elif "small" in model_name:
														__a		:		Any									=       9_6
							elif "base" in model_name:
														__a		:		str									=       1_2_8
							elif "large" in model_name:
														__a		:		Any									=       1_9_2
							elif "xlarge" in model_name:
														__a		:		int									=       2_5_6
							elif "huge" in model_name:
														__a		:		Tuple									=       3_5_2

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

							__a		:		Any									=       json.load(open(hf_hub_download(_lowerCamelCase  ,       _lowerCamelCase  ,       repo_type="""dataset"""					)  ,       """r"""					)					)
							__a		:		Optional[int]									=       {int(_lowerCamelCase					): v for k, v in idalabel.items()}
							__a		:		str									=       {v: k for k, v in idalabel.items()}

							__a		:		List[Any]									=       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       __magic_name__    (   _lowerCamelCase				: Optional[Any]					):
							if "patch_embed.proj" in name:
														__a		:		Union[str, Any]									=       name.replace("""patch_embed.proj"""  ,       """embeddings.patch_embeddings.projection"""					)
							if "patch_embed.norm" in name:
														__a		:		Tuple									=       name.replace("""patch_embed.norm"""  ,       """embeddings.norm"""					)
							if "layers" in name:
														__a		:		List[str]									=       """encoder.""" + name
							if "encoder.layers" in name:
														__a		:		str									=       name.replace("""encoder.layers"""  ,       """encoder.stages"""					)
							if "downsample.proj" in name:
														__a		:		int									=       name.replace("""downsample.proj"""  ,       """downsample.projection"""					)
							if "blocks" in name:
														__a		:		List[str]									=       name.replace("""blocks"""  ,       """layers"""					)
							if "modulation.f.weight" in name or "modulation.f.bias" in name:
														__a		:		List[Any]									=       name.replace("""modulation.f"""  ,       """modulation.projection_in"""					)
							if "modulation.h.weight" in name or "modulation.h.bias" in name:
														__a		:		List[Any]									=       name.replace("""modulation.h"""  ,       """modulation.projection_context"""					)
							if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
														__a		:		Optional[Any]									=       name.replace("""modulation.proj"""  ,       """modulation.projection_out"""					)

							if name == "norm.weight":
														__a		:		Tuple									=       """layernorm.weight"""
							if name == "norm.bias":
														__a		:		Dict									=       """layernorm.bias"""

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

							return name





def       __magic_name__    (   _lowerCamelCase				: Optional[int]  ,       _lowerCamelCase				: Dict  ,       _lowerCamelCase				: Any=False					):
							# fmt: off
							__a		:		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

							__a		:		str									=       model_name_to_url[model_name]
							print("""Checkpoint URL: """  ,       _lowerCamelCase					)
							__a		:		int									=       torch.hub.load_state_dict_from_url(_lowerCamelCase  ,       map_location="""cpu"""					)["""model"""]

							# rename keys
							for key in state_dict.copy().keys():
														__a		:		List[Any]									=       state_dict.pop(_lowerCamelCase					)
														__a		:		List[Any]									=       val

							__a		:		str									=       get_focalnet_config(_lowerCamelCase					)
							__a		:		Dict									=       FocalNetForImageClassification(_lowerCamelCase					)
							model.eval()

							# load state dict
							model.load_state_dict(_lowerCamelCase					)

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

							__a		:		Dict									=       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		:		str									=       Image.open(requests.get(_lowerCamelCase  ,       stream=_lowerCamelCase					).raw					)
							__a		:		List[Any]									=       processor(images=_lowerCamelCase  ,       return_tensors="""pt"""					)

							__a		:		int									=       transforms.Compose(
							    [
							        transforms.Resize(2_5_6					),
							        transforms.CenterCrop(2_2_4					),
							        transforms.ToTensor(),
							        transforms.Normalize(mean=[0.4_85, 0.4_56, 0.4_06]  ,       std=[0.2_29, 0.2_24, 0.2_25]					),
							    ]					)

							__a		:		List[str]									=       image_transforms(_lowerCamelCase					).unsqueeze(0					)

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

							__a		:		List[str]									=       model(**_lowerCamelCase					)

							__a		:		Optional[Any]									=       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		:		Dict									=       torch.tensor([0.21_66, -0.43_68, 0.21_91]					)
							elif model_name == "focalnet-tiny-lrf":
														__a		:		Any									=       torch.tensor([1.16_69, 0.01_25, -0.16_95]					)
							elif model_name == "focalnet-small":
														__a		:		str									=       torch.tensor([0.49_17, -0.04_30, 0.13_41]					)
							elif model_name == "focalnet-small-lrf":
														__a		:		Dict									=       torch.tensor([-0.25_88, -0.53_42, -0.23_31]					)
							elif model_name == "focalnet-base":
														__a		:		Union[str, Any]									=       torch.tensor([-0.16_55, -0.40_90, -0.17_30]					)
							elif model_name == "focalnet-base-lrf":
														__a		:		List[str]									=       torch.tensor([0.53_06, -0.04_83, -0.39_28]					)
							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 importlib

import torch
import yaml
from omegaconf import OmegaConf
from taming.models.vqgan import VQModel





def       __magic_name__    (   _lowerCamelCase				: int  ,       _lowerCamelCase				: Optional[Any]=False					):
							__a		:		Dict									=       OmegaConf.load(_lowerCamelCase					)
							if display:
														print(yaml.dump(OmegaConf.to_container(_lowerCamelCase					)					)					)
							return config





def       __magic_name__    (   _lowerCamelCase				: str  ,       _lowerCamelCase				: Union[str, Any]=None  ,       _lowerCamelCase				: int=None					):
							if conf_path is None:
														__a		:		str									=       """./model_checkpoints/vqgan_only.yaml"""
							__a		:		List[Any]									=       load_config(_lowerCamelCase  ,       display=_lowerCamelCase					)
							__a		:		Dict									=       VQModel(**config.model.params					)
							if ckpt_path is None:
														__a		:		List[Any]									=       """./model_checkpoints/vqgan_only.pt"""
							__a		:		Tuple									=       torch.load(_lowerCamelCase  ,       map_location=_lowerCamelCase					)
							if ".ckpt" in ckpt_path:
														__a		:		List[str]									=       sd["""state_dict"""]
							model.load_state_dict(_lowerCamelCase  ,       strict=_lowerCamelCase					)
							model.to(_lowerCamelCase					)
							del sd
							return model





def       __magic_name__    (   _lowerCamelCase				: Optional[int]  ,       _lowerCamelCase				: List[str]					):
							__a							,     __a							,     __a		:		Tuple									=       model.encode(_lowerCamelCase					)
							print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}'''					)
							__a		:		Union[str, Any]									=       model.decode(_lowerCamelCase					)
							return xrec





def       __magic_name__    (   _lowerCamelCase				: Optional[int]  ,       _lowerCamelCase				: Union[str, Any]=False					):
							__a							,     __a		:		Optional[Any]									=       string.rsplit("""."""  ,       1					)
							if reload:
														__a		:		Optional[Any]									=       importlib.import_module(_lowerCamelCase					)
														importlib.reload(_lowerCamelCase					)
							return getattr(importlib.import_module(_lowerCamelCase  ,       package=_lowerCamelCase					)  ,       cls					)





def       __magic_name__    (   _lowerCamelCase				: Any					):
							if "target" not in config:
														raise KeyError("""Expected key `target` to instantiate."""					)
							return get_obj_from_str(config["""target"""]					)(**config.get("""params"""  ,       {}					)					)





def       __magic_name__    (   _lowerCamelCase				: Any  ,       _lowerCamelCase				: Dict  ,       _lowerCamelCase				: int=True  ,       _lowerCamelCase				: int=True					):
							__a		:		Union[str, Any]									=       instantiate_from_config(_lowerCamelCase					)
							if sd is not None:
														model.load_state_dict(_lowerCamelCase					)
							if gpu:
														model.cuda()
							if eval_mode:
														model.eval()
							return {"model": model}







def       __magic_name__    (   _lowerCamelCase				: Any  ,       _lowerCamelCase				: Union[str, Any]  ,       _lowerCamelCase				: str  ,       _lowerCamelCase				: int					):
							# load the specified checkpoint
							if ckpt:
														__a		:		List[str]									=       torch.load(_lowerCamelCase  ,       map_location="""cpu"""					)
														__a		:		Any									=       pl_sd["""global_step"""]
														print(F'''loaded model from global step {global_step}.'''					)
							else:
														__a		:		List[Any]									=       {"""state_dict""": None}
														__a		:		Any									=       None
							__a		:		Union[str, Any]									=       load_model_from_config(config.model  ,       pl_sd["""state_dict"""]  ,       gpu=_lowerCamelCase  ,       eval_mode=_lowerCamelCase					)["""model"""]
							return model, global_step

