



'''simple docstring'''



import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
lowerCAmelCase	:		List[Any]				=   sys.version_info >= (3, 1_0)



def   __lowerCAmelCase					(							lowerCamelCase   :						Optional[int]=None				,		lowerCamelCase   :						Union[str, Any]=None					):






	'''simple docstring'''




	return field(default_factory=lambda: default				,		metadata=lowerCamelCase					)






@dataclass
class 					UpperCAmelCase__	:
					a      :     int
					a      :     float
					a      :     str
					a      :     bool






@dataclass
class 					UpperCAmelCase__	:
					a      :     int        =							4_2
					a      :     str        =							field(default="""toto"""							,       metadata={"""help""": """help message"""} )






@dataclass
class 					UpperCAmelCase__	:
					a      :     bool        =							False
					a      :     bool        =							True
					a      :     Optional[bool]        =							None






class 					UpperCAmelCase__	(		UpperCamelCase__ ):
					a      :     Optional[int]        =							"""titi"""
					a      :     int        =							"""toto"""






class 					UpperCAmelCase__	(		UpperCamelCase__ ):
					a      :     Union[str, Any]        =							"""titi"""
					a      :     Any        =							"""toto"""
					a      :     Union[str, Any]        =							4_2






@dataclass
class 					UpperCAmelCase__	:
					a      :     BasicEnum        =							"toto"



					def        UpperCAmelCase_ (    self     )     ->			str:
						__lowerCAmelCase												=  BasicEnum(self.foo     )






@dataclass
class 					UpperCAmelCase__	:
					a      :     MixedTypeEnum        =							"toto"



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						__lowerCAmelCase												=  MixedTypeEnum(self.foo     )






@dataclass
class 					UpperCAmelCase__	:
					a      :     Optional[int]        =							None
					a      :     Optional[float]        =							field(default=UpperCamelCase__							,       metadata={"""help""": """help message"""} )
					a      :     Optional[str]        =							None
					a      :     Optional[List[str]]        =							list_field(default=[] )
					a      :     Optional[List[int]]        =							list_field(default=[] )






@dataclass
class 					UpperCAmelCase__	:
					a      :     List[int]        =							list_field(default=[] )
					a      :     List[int]        =							list_field(default=[1, 2, 3] )
					a      :     List[str]        =							list_field(default=["""Hallo""", """Bonjour""", """Hello"""] )
					a      :     List[float]        =							list_field(default=[0.1, 0.2, 0.3] )






@dataclass
class 					UpperCAmelCase__	:
					a      :     List[int]        =							field()
					a      :     str        =							field()
					a      :     BasicEnum        =							field()



					def        UpperCAmelCase_ (    self     )     ->			Dict:
						__lowerCAmelCase												=  BasicEnum(self.required_enum     )






@dataclass
class 					UpperCAmelCase__	:
					a      :     int
					a      :     "BasicEnum"        =							field()
					a      :     "Optional[bool]"        =							None
					a      :     "str"        =							field(default="""toto"""							,       metadata={"""help""": """help message"""} )
					a      :     "List[str]"        =							list_field(default=["""Hallo""", """Bonjour""", """Hello"""] )


if is_python_no_less_than_3_10:






	@dataclass
	class 					UpperCAmelCase__	:
						a      :     bool        =							False
						a      :     bool        =							True
						a      :     bool | None        =							None






	@dataclass
	class 					UpperCAmelCase__	:
						a      :     int | None        =							None
						a      :     float | None        =							field(default=UpperCamelCase__							,       metadata={"""help""": """help message"""} )
						a      :     str | None        =							None
						a      :     list[str] | None        =							list_field(default=[] )
						a      :     list[int] | None        =							list_field(default=[] )






class 					UpperCAmelCase__	(		unittest.TestCase ):



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			Any:
						self.assertEqual(len(a._actions     )			,				len(b._actions     )     )
						for x, y in zip(a._actions			,				b._actions     ):
							__lowerCAmelCase												=  {k: v for k, v in vars(UpperCamelCase     ).items() if k != "container"}
							__lowerCAmelCase												=  {k: v for k, v in vars(UpperCamelCase     ).items() if k != "container"}

							# Choices with mixed type have custom function as "type"
							# So we need to compare results directly for equality
							if xx.get("choices"			,				UpperCamelCase     ) and yy.get("choices"			,				UpperCamelCase     ):
								for expected_choice in yy["choices"] + xx["choices"]:
									self.assertEqual(xx["type"](UpperCamelCase     )			,				yy["type"](UpperCamelCase     )     )
								del xx["type"], yy["type"]

							self.assertEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			str:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument("--bar"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument("--baz"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument("--flag"			,				type=UpperCamelCase			,				default=UpperCamelCase			,				const=UpperCamelCase			,				nargs="?"     )
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  ["--foo", "1", "--baz", "quux", "--bar", "0.5"]
						((__lowerCAmelCase)							,       )												=  parser.parse_args_into_dataclasses(UpperCamelCase			,				look_for_args_file=UpperCamelCase     )
						self.assertFalse(example.flag     )



					def        UpperCAmelCase_ (    self     )     ->			Dict:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo"			,				default=42			,				type=UpperCamelCase     )
						expected.add_argument("--baz"			,				default="toto"			,				type=UpperCamelCase			,				help="help message"     )
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo"			,				type=UpperCamelCase			,				default=UpperCamelCase			,				const=UpperCamelCase			,				nargs="?"     )
						expected.add_argument("--baz"			,				type=UpperCamelCase			,				default=UpperCamelCase			,				const=UpperCamelCase			,				nargs="?"     )
						# A boolean no_* argument always has to come after its "default: True" regular counter-part
						# and its default must be set to False
						expected.add_argument("--no_baz"			,				action="store_false"			,				default=UpperCamelCase			,				dest="baz"     )
						expected.add_argument("--opt"			,				type=UpperCamelCase			,				default=UpperCamelCase     )

						__lowerCAmelCase												=  [WithDefaultBoolExample]
						if is_python_no_less_than_3_10:
							dataclass_types.append(UpperCamelCase     )

						for dataclass_type in dataclass_types:
							__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )
							self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

							__lowerCAmelCase												=  parser.parse_args([]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				baz=UpperCamelCase			,				opt=UpperCamelCase     )     )

							__lowerCAmelCase												=  parser.parse_args(["--foo", "--no_baz"]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				baz=UpperCamelCase			,				opt=UpperCamelCase     )     )

							__lowerCAmelCase												=  parser.parse_args(["--foo", "--baz"]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				baz=UpperCamelCase			,				opt=UpperCamelCase     )     )

							__lowerCAmelCase												=  parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				baz=UpperCamelCase			,				opt=UpperCamelCase     )     )

							__lowerCAmelCase												=  parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				baz=UpperCamelCase			,				opt=UpperCamelCase     )     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument(
						    "--foo"			,				default="toto"			,				choices=["titi", "toto", 42]			,				type=make_choice_type_function(["titi", "toto", 42]     )			,				)
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  parser.parse_args([]     )
						self.assertEqual(args.foo			,				"toto"     )
						__lowerCAmelCase												=  parser.parse_args_into_dataclasses([]     )[0]
						self.assertEqual(enum_ex.foo			,				MixedTypeEnum.toto     )

						__lowerCAmelCase												=  parser.parse_args(["--foo", "titi"]     )
						self.assertEqual(args.foo			,				"titi"     )
						__lowerCAmelCase												=  parser.parse_args_into_dataclasses(["--foo", "titi"]     )[0]
						self.assertEqual(enum_ex.foo			,				MixedTypeEnum.titi     )

						__lowerCAmelCase												=  parser.parse_args(["--foo", "42"]     )
						self.assertEqual(args.foo			,				42     )
						__lowerCAmelCase												=  parser.parse_args_into_dataclasses(["--foo", "42"]     )[0]
						self.assertEqual(enum_ex.foo			,				MixedTypeEnum.fourtytwo     )



					def        UpperCAmelCase_ (    self     )     ->			List[str]:






						@dataclass
						class 					UpperCAmelCase__	:
											a      :     Literal["titi", "toto", 4_2]        =							"toto"


						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument(
						    "--foo"			,				default="toto"			,				choices=("titi", "toto", 42)			,				type=make_choice_type_function(["titi", "toto", 42]     )			,				)
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  parser.parse_args([]     )
						self.assertEqual(args.foo			,				"toto"     )

						__lowerCAmelCase												=  parser.parse_args(["--foo", "titi"]     )
						self.assertEqual(args.foo			,				"titi"     )

						__lowerCAmelCase												=  parser.parse_args(["--foo", "42"]     )
						self.assertEqual(args.foo			,				42     )



					def        UpperCAmelCase_ (    self     )     ->			str:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo_int"			,				nargs="+"			,				default=[]			,				type=UpperCamelCase     )
						expected.add_argument("--bar_int"			,				nargs="+"			,				default=[1, 2, 3]			,				type=UpperCamelCase     )
						expected.add_argument("--foo_str"			,				nargs="+"			,				default=["Hallo", "Bonjour", "Hello"]			,				type=UpperCamelCase     )
						expected.add_argument("--foo_float"			,				nargs="+"			,				default=[0.1, 0.2, 0.3]			,				type=UpperCamelCase     )

						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  parser.parse_args([]     )
						self.assertEqual(
						    UpperCamelCase			,				Namespace(foo_int=[]			,				bar_int=[1, 2, 3]			,				foo_str=["Hallo", "Bonjour", "Hello"]			,				foo_float=[0.1, 0.2, 0.3]     )			,				)

						__lowerCAmelCase												=  parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split()     )
						self.assertEqual(UpperCamelCase			,				Namespace(foo_int=[1]			,				bar_int=[2, 3]			,				foo_str=["a", "b", "c"]			,				foo_float=[0.1, 0.7]     )     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[int]:
						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo"			,				default=UpperCamelCase			,				type=UpperCamelCase     )
						expected.add_argument("--bar"			,				default=UpperCamelCase			,				type=UpperCamelCase			,				help="help message"     )
						expected.add_argument("--baz"			,				default=UpperCamelCase			,				type=UpperCamelCase     )
						expected.add_argument("--ces"			,				nargs="+"			,				default=[]			,				type=UpperCamelCase     )
						expected.add_argument("--des"			,				nargs="+"			,				default=[]			,				type=UpperCamelCase     )

						__lowerCAmelCase												=  [OptionalExample]
						if is_python_no_less_than_3_10:
							dataclass_types.append(UpperCamelCase     )

						for dataclass_type in dataclass_types:
							__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

							self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )

							__lowerCAmelCase												=  parser.parse_args([]     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=UpperCamelCase			,				bar=UpperCamelCase			,				baz=UpperCamelCase			,				ces=[]			,				des=[]     )     )

							__lowerCAmelCase												=  parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split()     )
							self.assertEqual(UpperCamelCase			,				Namespace(foo=12			,				bar=3.14			,				baz="42"			,				ces=["a", "b", "c"]			,				des=[1, 2, 3]     )     )



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--required_list"			,				nargs="+"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument("--required_str"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument(
						    "--required_enum"			,				type=make_choice_type_function(["titi", "toto"]     )			,				choices=["titi", "toto"]			,				required=UpperCamelCase			,				)
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  argparse.ArgumentParser()
						expected.add_argument("--foo"			,				type=UpperCamelCase			,				required=UpperCamelCase     )
						expected.add_argument(
						    "--required_enum"			,				type=make_choice_type_function(["titi", "toto"]     )			,				choices=["titi", "toto"]			,				required=UpperCamelCase			,				)
						expected.add_argument("--opt"			,				type=UpperCamelCase			,				default=UpperCamelCase     )
						expected.add_argument("--baz"			,				default="toto"			,				type=UpperCamelCase			,				help="help message"     )
						expected.add_argument("--foo_str"			,				nargs="+"			,				default=["Hallo", "Bonjour", "Hello"]			,				type=UpperCamelCase     )
						self.argparsersEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  {
						    "foo": 12,
						    "bar": 3.14,
						    "baz": "42",
						    "flag": True,
						}

						__lowerCAmelCase												=  parser.parse_dict(UpperCamelCase     )[0]
						__lowerCAmelCase												=  BasicExample(**UpperCamelCase     )
						self.assertEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  {
						    "foo": 12,
						    "bar": 3.14,
						    "baz": "42",
						    "flag": True,
						    "extra": 42,
						}

						self.assertRaises(UpperCamelCase			,				parser.parse_dict			,				UpperCamelCase			,				allow_extra_keys=UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  {
						    "foo": 12,
						    "bar": 3.14,
						    "baz": "42",
						    "flag": True,
						}
						with tempfile.TemporaryDirectory() as tmp_dir:
							__lowerCAmelCase												=  os.path.join(UpperCamelCase			,				"temp_json"     )
							os.mkdir(UpperCamelCase     )
							with open(temp_local_path + ".json"			,				"w+"     ) as f:
								json.dump(UpperCamelCase			,				UpperCamelCase     )
							__lowerCAmelCase												=  parser.parse_yaml_file(Path(temp_local_path + ".json"     )     )[0]

						__lowerCAmelCase												=  BasicExample(**UpperCamelCase     )
						self.assertEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )

						__lowerCAmelCase												=  {
						    "foo": 12,
						    "bar": 3.14,
						    "baz": "42",
						    "flag": True,
						}
						with tempfile.TemporaryDirectory() as tmp_dir:
							__lowerCAmelCase												=  os.path.join(UpperCamelCase			,				"temp_yaml"     )
							os.mkdir(UpperCamelCase     )
							with open(temp_local_path + ".yaml"			,				"w+"     ) as f:
								yaml.dump(UpperCamelCase			,				UpperCamelCase     )
							__lowerCAmelCase												=  parser.parse_yaml_file(Path(temp_local_path + ".yaml"     )     )[0]
						__lowerCAmelCase												=  BasicExample(**UpperCamelCase     )
						self.assertEqual(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  HfArgumentParser(UpperCamelCase     )
						self.assertIsNotNone(UpperCamelCase     )




'''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   __lowerCAmelCase					(							lowerCamelCase   :						Any					):






	'''simple docstring'''




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

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

	if "tiny" in model_name:
		__lowerCAmelCase												=  96
	elif "small" in model_name:
		__lowerCAmelCase												=  96
	elif "base" in model_name:
		__lowerCAmelCase												=  1_28
	elif "large" in model_name:
		__lowerCAmelCase												=  1_92
	elif "xlarge" in model_name:
		__lowerCAmelCase												=  2_56
	elif "huge" in model_name:
		__lowerCAmelCase												=  3_52

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

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

	__lowerCAmelCase												=  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   __lowerCAmelCase					(							lowerCamelCase   :						Union[str, Any]					):






	'''simple docstring'''




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

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

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

	return name



def   __lowerCAmelCase					(							lowerCamelCase   :						int				,		lowerCamelCase   :						Any				,		lowerCamelCase   :						Union[str, Any]=False					):






	'''simple docstring'''




	__lowerCAmelCase												=  {
	    "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

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

	# rename keys
	for key in state_dict.copy().keys():
		__lowerCAmelCase												=  state_dict.pop(lowerCamelCase					)
		__lowerCAmelCase												=  val

	__lowerCAmelCase												=  get_focalnet_config(lowerCamelCase					)
	__lowerCAmelCase												=  FocalNetForImageClassification(lowerCamelCase					)
	model.eval()

	# load state dict
	model.load_state_dict(lowerCamelCase					)

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

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

	__lowerCAmelCase												=  transforms.Compose(
	    [
	        transforms.Resize(2_56					),
	        transforms.CenterCrop(2_24					),
	        transforms.ToTensor(),
	        transforms.Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6]				,		std=[0.2_2_9, 0.2_2_4, 0.2_2_5]					),
	    ]					)

	__lowerCAmelCase												=  image_transforms(lowerCamelCase					).unsqueeze(0					)

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

	__lowerCAmelCase												=  model(**lowerCamelCase					)

	__lowerCAmelCase												=  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":
		__lowerCAmelCase												=  torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]					)
	elif model_name == "focalnet-tiny-lrf":
		__lowerCAmelCase												=  torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5]					)
	elif model_name == "focalnet-small":
		__lowerCAmelCase												=  torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1]					)
	elif model_name == "focalnet-small-lrf":
		__lowerCAmelCase												=  torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1]					)
	elif model_name == "focalnet-base":
		__lowerCAmelCase												=  torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0]					)
	elif model_name == "focalnet-base-lrf":
		__lowerCAmelCase												=  torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8]					)
	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__":
	lowerCAmelCase	:		Tuple				=   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	:		Optional[int]				=   parser.parse_args()
	convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)