






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       snake_case_		(     lowerCAmelCase_       :						int      ):
							__lowercase      :							Any				=     [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 18, 2]
							__lowercase      :							int				=     True if '''large''' in model_name or '''huge''' in model_name else False
							__lowercase      :							Dict				=     True if '''large''' in model_name or '''huge''' in model_name else False
							__lowercase      :							List[str]				=     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:
																					__lowercase      :							int				=     [3, 3, 3, 3]
																					__lowercase      :							str				=     [5, 5, 5, 5]
														elif "fl4" in model_name:
																					__lowercase      :							List[str]				=     [4, 4, 4, 4]
																					__lowercase      :							Union[str, Any]				=     [3, 3, 3, 3]

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

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

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

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

							__lowercase      :							Any				=     FocalNetConfig(
							    embed_dim=a_       ,       depths=a_       ,       focal_levels=a_       ,       focal_windows=a_       ,       use_conv_embed=a_       ,       idalabel=a_       ,       labelaid=a_       ,       use_post_layernorm=a_       ,       use_layerscale=a_       ,       )

							return config
def       snake_case_		(     lowerCAmelCase_       :						List[Any]      ):
							if "patch_embed.proj" in name:
														__lowercase      :							Dict				=     name.replace("""patch_embed.proj"""       ,       """embeddings.patch_embeddings.projection"""      )
							if "patch_embed.norm" in name:
														__lowercase      :							Dict				=     name.replace("""patch_embed.norm"""       ,       """embeddings.norm"""      )
							if "layers" in name:
														__lowercase      :							Any				=     '''encoder.''' + name
							if "encoder.layers" in name:
														__lowercase      :							Any				=     name.replace("""encoder.layers"""       ,       """encoder.stages"""      )
							if "downsample.proj" in name:
														__lowercase      :							Optional[int]				=     name.replace("""downsample.proj"""       ,       """downsample.projection"""      )
							if "blocks" in name:
														__lowercase      :							int				=     name.replace("""blocks"""       ,       """layers"""      )
							if "modulation.f.weight" in name or "modulation.f.bias" in name:
														__lowercase      :							int				=     name.replace("""modulation.f"""       ,       """modulation.projection_in"""      )
							if "modulation.h.weight" in name or "modulation.h.bias" in name:
														__lowercase      :							Union[str, Any]				=     name.replace("""modulation.h"""       ,       """modulation.projection_context"""      )
							if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
														__lowercase      :							Tuple				=     name.replace("""modulation.proj"""       ,       """modulation.projection_out"""      )

							if name == "norm.weight":
														__lowercase      :							Tuple				=     '''layernorm.weight'''
							if name == "norm.bias":
														__lowercase      :							List[Any]				=     '''layernorm.bias'''

							if "head" in name:
														__lowercase      :							List[Any]				=     name.replace("""head"""       ,       """classifier"""      )
							else:
														__lowercase      :							Tuple				=     '''focalnet.''' + name

							return name
def       snake_case_		(     lowerCAmelCase_       :						Any       ,       lowerCAmelCase_       :						Optional[int]       ,       lowerCAmelCase_       :						Union[str, Any]=False      ):
							# fmt: off
							__lowercase      :							Optional[Any]				=     {
							    '''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

							__lowercase      :							Optional[int]				=     model_name_to_url[model_name]
							print("""Checkpoint URL: """       ,       a_      )
							__lowercase      :							List[Any]				=     torch.hub.load_state_dict_from_url(a_       ,       map_location="""cpu"""      )['''model''']

							# rename keys
							for key in state_dict.copy().keys():
														__lowercase      :							Union[str, Any]				=     state_dict.pop(a_      )
														__lowercase      :							Union[str, Any]				=     val

							__lowercase      :							Optional[int]				=     get_focalnet_config(a_      )
							__lowercase      :							Union[str, Any]				=     FocalNetForImageClassification(a_      )
							model.eval()

							# load state dict
							model.load_state_dict(a_      )

							# verify conversion
							__lowercase      :							Dict				=     '''http://images.cocodataset.org/val2017/000000039769.jpg'''

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

							__lowercase      :							Dict				=     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]      ),
							    ]      )

							__lowercase      :							List[str]				=     image_transforms(a_      ).unsqueeze(0      )

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

							__lowercase      :							Optional[Any]				=     model(**a_      )

							__lowercase      :							int				=     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":
														__lowercase      :							Optional[Any]				=     torch.tensor([0.2_166, -0.4_368, 0.2_191]      )
							elif model_name == "focalnet-tiny-lrf":
														__lowercase      :							Dict				=     torch.tensor([1.1_669, 0.0_125, -0.1_695]      )
							elif model_name == "focalnet-small":
														__lowercase      :							Any				=     torch.tensor([0.4_917, -0.0_430, 0.1_341]      )
							elif model_name == "focalnet-small-lrf":
														__lowercase      :							List[str]				=     torch.tensor([-0.2_588, -0.5_342, -0.2_331]      )
							elif model_name == "focalnet-base":
														__lowercase      :							Any				=     torch.tensor([-0.1_655, -0.4_090, -0.1_730]      )
							elif model_name == "focalnet-base-lrf":
														__lowercase      :							Dict				=     torch.tensor([0.5_306, -0.0_483, -0.3_928]      )
							assert torch.allclose(outputs.logits[0, :3]       ,       a_       ,       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(a_      )
														processor.save_pretrained(a_      )

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

		lowerCamelCase : List[Any]						   =       parser.parse_args()
		convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


if TYPE_CHECKING:
	from ...processing_utils import ProcessorMixin
	from ...utils import TensorType


lowerCamelCase : Optional[int]						   =       logging.get_logger(__name__)

lowerCamelCase : Tuple						   =       {
    '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''',
}

class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					_A						:			int									=      '''layoutlmv3'''



					def __init__(  self				:	Dict		,     __a				:	List[str]=50265		,     __a				:	str=768		,     __a				:	List[Any]=12		,     __a				:	List[Any]=12		,     __a				:	List[str]=3072		,     __a				:	Optional[Any]="gelu"		,     __a				:	Optional[int]=0.1		,     __a				:	List[Any]=0.1		,     __a				:	Tuple=512		,     __a				:	int=2		,     __a				:	Any=0.02		,     __a				:	Union[str, Any]=1E-5		,     __a				:	List[str]=1		,     __a				:	List[Any]=0		,     __a				:	int=2		,     __a				:	str=1024		,     __a				:	str=128		,     __a				:	List[Any]=128		,     __a				:	Tuple=True		,     __a				:	Optional[int]=32		,     __a				:	Any=128		,     __a				:	List[Any]=64		,     __a				:	Tuple=256		,     __a				:	str=True		,     __a				:	int=True		,     __a				:	Optional[Any]=True		,     __a				:	Any=224		,     __a				:	str=3		,     __a				:	List[str]=16		,     __a				:	Union[str, Any]=None		,     **__a				:	List[Any]		,     )			->  List[str]:


												"""simple docstring"""

												super().__init__(
												    vocab_size=__a		,     hidden_size=__a		,     num_hidden_layers=__a		,     num_attention_heads=__a		,     intermediate_size=__a		,     hidden_act=__a		,     hidden_dropout_prob=__a		,     attention_probs_dropout_prob=__a		,     max_position_embeddings=__a		,     type_vocab_size=__a		,     initializer_range=__a		,     layer_norm_eps=__a		,     pad_token_id=__a		,     bos_token_id=__a		,     eos_token_id=__a		,     **__a		,     )
												__lowercase      :							int				=     max_ad_position_embeddings
												__lowercase      :							Any				=     coordinate_size
												__lowercase      :							Optional[Any]				=     shape_size
												__lowercase      :							str				=     has_relative_attention_bias
												__lowercase      :							int				=     rel_pos_bins
												__lowercase      :							Union[str, Any]				=     max_rel_pos
												__lowercase      :							str				=     has_spatial_attention_bias
												__lowercase      :							str				=     rel_ad_pos_bins
												__lowercase      :							List[Any]				=     max_rel_ad_pos
												__lowercase      :							Tuple				=     text_embed
												__lowercase      :							int				=     visual_embed
												__lowercase      :							Tuple				=     input_size
												__lowercase      :							Dict				=     num_channels
												__lowercase      :							str				=     patch_size
												__lowercase      :							Optional[int]				=     classifier_dropout







class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					_A						:			str									=      version.parse('''1.12'''			)



					@property
					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  Mapping[str, Mapping[int, str]]:


												"""simple docstring"""

												if self.task in ["question-answering", "sequence-classification"]:
																			return OrderedDict(
																			    [
																			        ("""input_ids""", {0: """batch""", 1: """sequence"""}),
																			        ("""attention_mask""", {0: """batch""", 1: """sequence"""}),
																			        ("""bbox""", {0: """batch""", 1: """sequence"""}),
																			        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
																			    ]							)
												else:
																			return OrderedDict(
																			    [
																			        ("""input_ids""", {0: """batch""", 1: """sequence"""}),
																			        ("""bbox""", {0: """batch""", 1: """sequence"""}),
																			        ("""attention_mask""", {0: """batch""", 1: """sequence"""}),
																			        ("""pixel_values""", {0: """batch""", 1: """num_channels"""}),
																			    ]							)



					@property
					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  float:


												"""simple docstring"""

												return 1E-5



					@property
					def       lowerCAmelCase				(  self				:	str							)			->  int:


												"""simple docstring"""

												return 12



					def       lowerCAmelCase				(  self				:	List[Any]		,     __a				:	"ProcessorMixin"		,     __a				:	int = -1		,     __a				:	int = -1		,     __a				:	bool = False		,     __a				:	Optional["TensorType"] = None		,     __a				:	int = 3		,     __a				:	int = 40		,     __a				:	int = 40		,     )			->  Mapping[str, Any]:


												"""simple docstring"""

												setattr(processor.image_processor		,     """apply_ocr"""		,     __a							)

												# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
												__lowercase      :							Tuple				=     compute_effective_axis_dimension(
												    __a		,     fixed_dimension=OnnxConfig.default_fixed_batch		,     num_token_to_add=0							)
												# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
												__lowercase      :							Tuple				=     processor.tokenizer.num_special_tokens_to_add(__a							)
												__lowercase      :							Tuple				=     compute_effective_axis_dimension(
												    __a		,     fixed_dimension=OnnxConfig.default_fixed_sequence		,     num_token_to_add=__a							)
												# Generate dummy inputs according to compute batch and sequence
												__lowercase      :							Union[str, Any]				=     [[""" """.join([processor.tokenizer.unk_token]							) * seq_length]] * batch_size

												# Generate dummy bounding boxes
												__lowercase      :							Tuple				=     [[[48, 84, 73, 128]]] * batch_size

												# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
												# batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
												__lowercase      :							Tuple				=     self._generate_dummy_images(__a		,     __a		,     __a		,     __a							)

												__lowercase      :							int				=     dict(
												    processor(
												        __a		,     text=__a		,     boxes=__a		,     return_tensors=__a		,     )							)

												return inputs