"""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       (					UpperCAmelCase_					:       List[Any]						)							->    str:

	'''simple docstring'''





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

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

	if "tiny" in model_name:
		__snake_case    :							Optional[int]   	=				96
	elif "small" in model_name:
		__snake_case    :							Dict   	=				96
	elif "base" in model_name:
		__snake_case    :							Tuple   	=				1_28
	elif "large" in model_name:
		__snake_case    :							Dict   	=				1_92
	elif "xlarge" in model_name:
		__snake_case    :							str   	=				2_56
	elif "huge" in model_name:
		__snake_case    :							List[Any]   	=				3_52

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

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

	__snake_case    :							Dict   	=				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 						__UpperCAmelCase       (					UpperCAmelCase_					:       List[Any]						)							->    Dict:

	'''simple docstring'''





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

	if name == "norm.weight":
		__snake_case    :							Optional[int]   	=				'layernorm.weight'
	if name == "norm.bias":
		__snake_case    :							Optional[Any]   	=				'layernorm.bias'

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

	return name
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       List[str]    ,							UpperCAmelCase_					:       str    ,							UpperCAmelCase_					:       Optional[Any]=False						)							->    str:

	'''simple docstring'''





	__snake_case    :							int   	=				{
	    '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    :							Union[str, Any]   	=				model_name_to_url[model_name]
	print('Checkpoint URL: '    ,							UpperCAmelCase_						)
	__snake_case    :							Union[str, 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    :							Optional[Any]   	=				val

	__snake_case    :							List[str]   	=				get_focalnet_config(UpperCAmelCase_						)
	__snake_case    :							int   	=				FocalNetForImageClassification(UpperCAmelCase_						)
	model.eval()

	# load state dict
	model.load_state_dict(UpperCAmelCase_						)

	# verify conversion
	__snake_case    :							Any   	=				'http://images.cocodataset.org/val2017/000000039769.jpg'

	__snake_case    :							Optional[Any]   	=				BitImageProcessor(
	    do_resize=UpperCAmelCase_    ,							size={'shortest_edge': 2_56}    ,							resample=PILImageResampling.BILINEAR    ,							do_center_crop=UpperCAmelCase_    ,							crop_size=2_24    ,							do_normalize=UpperCAmelCase_    ,							image_mean=UpperCAmelCase_    ,							image_std=UpperCAmelCase_    ,							)
	__snake_case    :							int   	=				Image.open(requests.get(UpperCAmelCase_    ,							stream=UpperCAmelCase_						).raw						)
	__snake_case    :							Dict   	=				processor(images=UpperCAmelCase_    ,							return_tensors='pt'						)

	__snake_case    :							Optional[Any]   	=				transforms.Compose(
	    [
	        transforms.Resize(2_56						),
	        transforms.CenterCrop(2_24						),
	        transforms.ToTensor(),
	        transforms.Normalize(mean=[0.485, 0.456, 0.406]    ,							std=[0.229, 0.224, 0.225]						),
	    ]						)

	__snake_case    :							Union[str, Any]   	=				image_transforms(UpperCAmelCase_						).unsqueeze(0						)

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

	__snake_case    :							Optional[Any]   	=				model(**UpperCAmelCase_						)

	__snake_case    :							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":
		__snake_case    :							Union[str, Any]   	=				torch.tensor([0.2_166, -0.4_368, 0.2_191]						)
	elif model_name == "focalnet-tiny-lrf":
		__snake_case    :							Any   	=				torch.tensor([1.1_669, 0.0_125, -0.1_695]						)
	elif model_name == "focalnet-small":
		__snake_case    :							str   	=				torch.tensor([0.4_917, -0.0_430, 0.1_341]						)
	elif model_name == "focalnet-small-lrf":
		__snake_case    :							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    :							Union[str, Any]   	=				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__":
	_a  :  Optional[int]=				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.",
	)

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






"""simple docstring"""







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


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

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

class 	UpperCamelCase    (					lowercase		):
				UpperCAmelCase       :   Dict					=	"""layoutlmv3"""



				def __init__(self       :   List[str]			,						_A       :   Optional[int]=5_02_65			,						_A       :   List[str]=7_68			,						_A       :   List[Any]=12			,						_A       :   List[str]=12			,						_A       :   Optional[int]=30_72			,						_A       :   str="gelu"			,						_A       :   int=0.1			,						_A       :   Tuple=0.1			,						_A       :   List[Any]=5_12			,						_A       :   List[str]=2			,						_A       :   List[Any]=0.02			,						_A       :   Tuple=1E-5			,						_A       :   Dict=1			,						_A       :   str=0			,						_A       :   str=2			,						_A       :   List[str]=10_24			,						_A       :   Optional[Any]=1_28			,						_A       :   Union[str, Any]=1_28			,						_A       :   Union[str, Any]=True			,						_A       :   Union[str, Any]=32			,						_A       :   Any=1_28			,						_A       :   Optional[Any]=64			,						_A       :   List[Any]=2_56			,						_A       :   str=True			,						_A       :   List[Any]=True			,						_A       :   Tuple=True			,						_A       :   Tuple=2_24			,						_A       :   Tuple=3			,						_A       :   Optional[Any]=16			,						_A       :   Tuple=None			,						**_A       :   Optional[int]			,						)					->			Optional[int]:
					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			,						)
					__snake_case    :							List[Any]   	=				max_ad_position_embeddings
					__snake_case    :							List[str]   	=				coordinate_size
					__snake_case    :							int   	=				shape_size
					__snake_case    :							List[str]   	=				has_relative_attention_bias
					__snake_case    :							Union[str, Any]   	=				rel_pos_bins
					__snake_case    :							Tuple   	=				max_rel_pos
					__snake_case    :							Optional[Any]   	=				has_spatial_attention_bias
					__snake_case    :							Union[str, Any]   	=				rel_ad_pos_bins
					__snake_case    :							Any   	=				max_rel_ad_pos
					__snake_case    :							Tuple   	=				text_embed
					__snake_case    :							str   	=				visual_embed
					__snake_case    :							List[str]   	=				input_size
					__snake_case    :							List[Any]   	=				num_channels
					__snake_case    :							Union[str, Any]   	=				patch_size
					__snake_case    :							str   	=				classifier_dropout







class 	UpperCamelCase    (					lowercase		):
				UpperCAmelCase       :   Optional[int]					=	version.parse("""1.12"""		)



				@property
				def       _lowercase  (self       :   Optional[int])					->			Mapping[str, Mapping[int, str]]:
					# The order of inputs is different for question answering and sequence classification
					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       _lowercase  (self       :   Tuple)					->			float:
					return 1E-5



				@property
				def       _lowercase  (self       :   Optional[int])					->			int:
					return 12






				def       _lowercase  (self       :   str			,						_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]:
					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
					__snake_case    :							Union[str, Any]   	=				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
					__snake_case    :							Any   	=				processor.tokenizer.num_special_tokens_to_add(_A)
					__snake_case    :							Optional[int]   	=				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
					__snake_case    :							Optional[int]   	=				[[' '.join([processor.tokenizer.unk_token]) * seq_length]] * batch_size

					# Generate dummy bounding boxes
					__snake_case    :							Dict   	=				[[[48, 84, 73, 1_28]]] * 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)
					__snake_case    :							Optional[Any]   	=				self._generate_dummy_images(_A			,						_A			,						_A			,						_A)

					__snake_case    :							Dict   	=				dict(
					    processor(
					        _A			,						text=_A			,						boxes=_A			,						return_tensors=_A			,						))

					return inputs





