



import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import (
			    TFEfficientFormerForImageClassification,
			    TFEfficientFormerForImageClassificationWithTeacher,
			    TFEfficientFormerModel,
			)
			from transformers.models.efficientformer.modeling_tf_efficientformer import (
			    TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
			)


if is_vision_available():
			from PIL import Image

			from transformers import EfficientFormerImageProcessor

class        UpperCAmelCase_  :

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A = 13				,							_A = 64				,							_A = 2				,							_A = 3				,							_A = 3				,							_A = True				,							_A = True				,							_A = 128				,							_A=[16, 32, 64, 128]				,							_A = 7				,							_A = 4				,							_A = 37				,							_A = "gelu"				,							_A = 0.1				,							_A = 0.1				,							_A = 10				,							_A = 0.0_2				,							_A = 2				,							_A = 1				,							_A = 128				,							_A = [2, 2, 2, 2]				,							_A = 2				,							_A = 2				,							):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= batch_size
													__SCREAMING_SNAKE_CASE									= image_size
													__SCREAMING_SNAKE_CASE									= patch_size
													__SCREAMING_SNAKE_CASE									= num_channels
													__SCREAMING_SNAKE_CASE									= is_training
													__SCREAMING_SNAKE_CASE									= use_labels
													__SCREAMING_SNAKE_CASE									= hidden_size
													__SCREAMING_SNAKE_CASE									= num_hidden_layers
													__SCREAMING_SNAKE_CASE									= num_attention_heads
													__SCREAMING_SNAKE_CASE									= intermediate_size
													__SCREAMING_SNAKE_CASE									= hidden_act
													__SCREAMING_SNAKE_CASE									= hidden_dropout_prob
													__SCREAMING_SNAKE_CASE									= attention_probs_dropout_prob
													__SCREAMING_SNAKE_CASE									= type_sequence_label_size
													__SCREAMING_SNAKE_CASE									= initializer_range
													__SCREAMING_SNAKE_CASE									= encoder_stride
													__SCREAMING_SNAKE_CASE									= num_attention_outputs
													__SCREAMING_SNAKE_CASE									= embed_dim
													__SCREAMING_SNAKE_CASE									= embed_dim + 1
													__SCREAMING_SNAKE_CASE									= resolution
													__SCREAMING_SNAKE_CASE									= depths
													__SCREAMING_SNAKE_CASE									= hidden_sizes
													__SCREAMING_SNAKE_CASE									= dim
													__SCREAMING_SNAKE_CASE									= mlp_expansion_ratio






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

													__SCREAMING_SNAKE_CASE									= None
													if self.use_labels:
																				__SCREAMING_SNAKE_CASE									= ids_tensor([self.batch_size]				,							self.type_sequence_label_size		)

													__SCREAMING_SNAKE_CASE									= self.get_config()

													return config, pixel_values, labels






						def 	_A     (   self		):







													'''simple docstring'''

													return EfficientFormerConfig(
													    image_size=self.image_size				,							patch_size=self.patch_size				,							num_channels=self.num_channels				,							hidden_size=self.hidden_size				,							num_hidden_layers=self.num_hidden_layers				,							num_attention_heads=self.num_attention_heads				,							intermediate_size=self.intermediate_size				,							hidden_act=self.hidden_act				,							hidden_dropout_prob=self.hidden_dropout_prob				,							attention_probs_dropout_prob=self.attention_probs_dropout_prob				,							is_decoder=_A				,							initializer_range=self.initializer_range				,							encoder_stride=self.encoder_stride				,							resolution=self.resolution				,							depths=self.depths				,							hidden_sizes=self.hidden_sizes				,							dim=self.dim				,							mlp_expansion_ratio=self.mlp_expansion_ratio				,							)






						def 	_A     (   self				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= TFEfficientFormerModel(config=_A		)
													__SCREAMING_SNAKE_CASE									= model(_A				,							training=_A		)
													self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)		)






						def 	_A     (   self				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.type_sequence_label_size
													__SCREAMING_SNAKE_CASE									= TFEfficientFormerForImageClassification(_A		)
													__SCREAMING_SNAKE_CASE									= model(_A				,							labels=_A				,							training=_A		)
													self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)		)

													# test greyscale images
													__SCREAMING_SNAKE_CASE									= 1
													__SCREAMING_SNAKE_CASE									= TFEfficientFormerForImageClassification(_A		)

													__SCREAMING_SNAKE_CASE									= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
													__SCREAMING_SNAKE_CASE									= model(_A				,							labels=_A		)
													self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.prepare_config_and_inputs()
													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= config_and_inputs
													__SCREAMING_SNAKE_CASE									= {'pixel_values': pixel_values}
													return config, inputs_dict







@require_tf
class        UpperCAmelCase_  (       UpperCamelCase_   ,   UpperCamelCase_   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			List[str]      =  (
						    (
						        TFEfficientFormerModel,
						        TFEfficientFormerForImageClassificationWithTeacher,
						        TFEfficientFormerForImageClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						UpperCamelCase__      :			Optional[Any]      =  (
						    {
						        '''feature-extraction''': TFEfficientFormerModel,
						        '''image-classification''': (
						            TFEfficientFormerForImageClassification,
						            TFEfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_tf_available()
						    else {}
						)

						UpperCamelCase__      :			List[str]      =  False

						UpperCamelCase__      :			List[str]      =  False
						UpperCamelCase__      :			Union[str, Any]      =  False
						UpperCamelCase__      :			Optional[int]      =  False
						UpperCamelCase__      :			int      =  False






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= TFEfficientFormerModelTester(self		)
													__SCREAMING_SNAKE_CASE									= ConfigTester(
													    self				,							config_class=_A				,							has_text_modality=_A				,							hidden_size=37		)






						def 	_A     (   self		):







													'''simple docstring'''

													self.config_tester.run_common_tests()






						@unittest.skip(reason='EfficientFormer does not use inputs_embeds'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='EfficientFormer does not support input and output embeddings'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__SCREAMING_SNAKE_CASE									= model_class(_A		)
																				__SCREAMING_SNAKE_CASE									= inspect.signature(model.call		)
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				__SCREAMING_SNAKE_CASE									= [*signature.parameters.keys()]

																				__SCREAMING_SNAKE_CASE									= ['pixel_values']
																				self.assertListEqual(arg_names[:1]				,							_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													def check_hidden_states_output(_A				,							_A				,							_A		):
																				__SCREAMING_SNAKE_CASE									= model_class(_A		)

																				__SCREAMING_SNAKE_CASE									= model(**self._prepare_for_class(_A				,							_A		)				,							training=_A		)
																				__SCREAMING_SNAKE_CASE									= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																				__SCREAMING_SNAKE_CASE									= getattr(
																				    self.model_tester				,							'expected_num_hidden_layers'				,							self.model_tester.num_hidden_layers + 1		)
																				self.assertEqual(len(_A		)				,							_A		)

																				if hasattr(self.model_tester				,							'encoder_seq_length'		):
																											__SCREAMING_SNAKE_CASE									= self.model_tester.encoder_seq_length
																											if hasattr(self.model_tester				,							'chunk_length'		) and self.model_tester.chunk_length > 1:
																																		__SCREAMING_SNAKE_CASE									= seq_length * self.model_tester.chunk_length
																				else:
																											__SCREAMING_SNAKE_CASE									= self.model_tester.seq_length

																				self.assertListEqual(
																				    list(hidden_states[-1].shape[-2:]		)				,							[seq_length, self.model_tester.hidden_size]				,							)

																				if config.is_encoder_decoder:
																											__SCREAMING_SNAKE_CASE									= outputs.decoder_hidden_states

																											self.asseretIsInstance(_A				,							(list, tuple)		)
																											self.assertEqual(len(_A		)				,							_A		)
																											__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'seq_length'				,							_A		)
																											__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'decoder_seq_length'				,							_A		)

																											self.assertListEqual(
																											    list(hidden_states[-1].shape[-2:]		)				,							[decoder_seq_length, self.model_tester.hidden_size]				,							)

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__SCREAMING_SNAKE_CASE									= True
																				check_hidden_states_output(_A				,							_A				,							_A		)

																				# check that output_hidden_states also work using config
																				del inputs_dict["output_hidden_states"]
																				__SCREAMING_SNAKE_CASE									= True

																				check_hidden_states_output(_A				,							_A				,							_A		)






						def 	_A     (   self				,							_A				,							_A				,							_A=False		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= super()._prepare_for_class(_A				,							_A				,							return_labels=_A		)

													if return_labels:
																				if model_class.__name__ == "TFEfficientFormerForImageClassificationWithTeacher":
																											del inputs_dict["labels"]

													return inputs_dict






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_A		)






						@unittest.skip(reason='EfficientFormer does not implement masked image modeling yet'		)
						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_image_modeling(*_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*_A		)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__SCREAMING_SNAKE_CASE									= TFEfficientFormerModel.from_pretrained(_A		)
																				self.assertIsNotNone(_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()
													__SCREAMING_SNAKE_CASE									= True

													__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'seq_length'				,							_A		)
													__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'encoder_seq_length'				,							_A		)
													__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'key_length'				,							_A		)
													__SCREAMING_SNAKE_CASE									= getattr(self.model_tester				,							'chunk_length'				,							_A		)

													if chunk_length is not None and hasattr(self.model_tester				,							'num_hashes'		):
																				__SCREAMING_SNAKE_CASE									= encoder_seq_length * self.model_tester.num_hashes

													for model_class in self.all_model_classes:
																				__SCREAMING_SNAKE_CASE									= True
																				__SCREAMING_SNAKE_CASE									= False
																				__SCREAMING_SNAKE_CASE									= True
																				__SCREAMING_SNAKE_CASE									= model_class(_A		)

																				__SCREAMING_SNAKE_CASE									= model(**self._prepare_for_class(_A				,							_A		)				,							training=_A		)
																				__SCREAMING_SNAKE_CASE									= outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
																				self.assertEqual(len(_A		)				,							self.model_tester.num_attention_outputs		)

																				# check that output_attentions also work using config
																				del inputs_dict["output_attentions"]
																				__SCREAMING_SNAKE_CASE									= True
																				__SCREAMING_SNAKE_CASE									= model_class(_A		)
																				__SCREAMING_SNAKE_CASE									= model(**self._prepare_for_class(_A				,							_A		)				,							training=_A		)

																				__SCREAMING_SNAKE_CASE									= outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
																				self.assertEqual(len(_A		)				,							self.model_tester.num_attention_outputs		)

																				if chunk_length is not None:
																											self.assertListEqual(
																											    list(attentions[0].shape[-4:]		)				,							[self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]				,							)
																				else:
																											self.assertListEqual(
																											    list(attentions[0].shape[-3:]		)				,							[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]				,							)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				# Prepare our model
																				__SCREAMING_SNAKE_CASE									= model_class(_A		)
																				# These are maximally general inputs for the model, with multiple None dimensions
																				# Hopefully this will catch any conditionals that fail for flexible shapes
																				__SCREAMING_SNAKE_CASE									= {
																				    key: tf.keras.Input(shape=val.shape[1:]				,							dtype=val.dtype				,							name=_A		)
																				    for key, val in model.input_signature.items()
																				    if key in model.dummy_inputs
																				}
																				__SCREAMING_SNAKE_CASE									= model(_A		)
																				self.assertTrue(outputs_dict is not None		)







def      __lowercase       (			)    ->			List[Any]:
							__SCREAMING_SNAKE_CASE									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
							return image

@require_tf
@require_vision
class        UpperCAmelCase_  (       unittest.TestCase	):

						'''simple docstring'''




						@cached_property
						def 	_A     (   self		):







													'''simple docstring'''

													return (
													    EfficientFormerImageProcessor.from_pretrained('snap-research/efficientformer-l1-300'		)
													    if is_vision_available()
													    else None
													)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= TFEfficientFormerForImageClassification.from_pretrained('snap-research/efficientformer-l1-300'		)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(images=_A				,							return_tensors='tf'		)
													# forward pass
													__SCREAMING_SNAKE_CASE									= model(**_A				,							training=_A		)
													# verify the logits
													__SCREAMING_SNAKE_CASE									= tf.TensorShape((1, 1_000)		)
													self.assertEqual(outputs.logits.shape				,							_A		)
													__SCREAMING_SNAKE_CASE									= tf.constant([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]		)
													self.assertTrue(np.allclose(outputs.logits[0, :3]				,							_A				,							atol=1e-4		)		)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
													    'snap-research/efficientformer-l1-300'		)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(images=_A				,							return_tensors='tf'		)
													# forward pass
													__SCREAMING_SNAKE_CASE									= model(**_A				,							training=_A		)
													# verify the logits
													__SCREAMING_SNAKE_CASE									= tf.TensorShape((1, 1_000)		)
													self.assertEqual(outputs.logits.shape				,							_A		)
													__SCREAMING_SNAKE_CASE									= tf.constant([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]		)
													self.assertTrue(np.allclose(outputs.logits[0, :3]				,							_A				,							atol=1e-4		)		)











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      __lowercase       (			a__       )    ->			Optional[Any]:
							__SCREAMING_SNAKE_CASE									= [2, 2, 6, 2] if 'tiny' in model_name else [2, 2, 18, 2]
							__SCREAMING_SNAKE_CASE									= True if 'large' in model_name or 'huge' in model_name else False
							__SCREAMING_SNAKE_CASE									= True if 'large' in model_name or 'huge' in model_name else False
							__SCREAMING_SNAKE_CASE									= 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:
																					__SCREAMING_SNAKE_CASE									= [3, 3, 3, 3]
																					__SCREAMING_SNAKE_CASE									= [5, 5, 5, 5]
														elif "fl4" in model_name:
																					__SCREAMING_SNAKE_CASE									= [4, 4, 4, 4]
																					__SCREAMING_SNAKE_CASE									= [3, 3, 3, 3]

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

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

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

							__SCREAMING_SNAKE_CASE									= json.load(open(hf_hub_download(a__							,       a__							,       repo_type='dataset'       )							,       'r'       )       )
							__SCREAMING_SNAKE_CASE									= {int(a__       ): v for k, v in idalabel.items()}
							__SCREAMING_SNAKE_CASE									= {v: k for k, v in idalabel.items()}

							__SCREAMING_SNAKE_CASE									= 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      __lowercase       (			a__       )    ->			Any:
							if "patch_embed.proj" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('patch_embed.proj'							,       'embeddings.patch_embeddings.projection'       )
							if "patch_embed.norm" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('patch_embed.norm'							,       'embeddings.norm'       )
							if "layers" in name:
														__SCREAMING_SNAKE_CASE									= 'encoder.' + name
							if "encoder.layers" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('encoder.layers'							,       'encoder.stages'       )
							if "downsample.proj" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('downsample.proj'							,       'downsample.projection'       )
							if "blocks" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('blocks'							,       'layers'       )
							if "modulation.f.weight" in name or "modulation.f.bias" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('modulation.f'							,       'modulation.projection_in'       )
							if "modulation.h.weight" in name or "modulation.h.bias" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('modulation.h'							,       'modulation.projection_context'       )
							if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
														__SCREAMING_SNAKE_CASE									= name.replace('modulation.proj'							,       'modulation.projection_out'       )

							if name == "norm.weight":
														__SCREAMING_SNAKE_CASE									= 'layernorm.weight'
							if name == "norm.bias":
														__SCREAMING_SNAKE_CASE									= 'layernorm.bias'

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

							return name






def      __lowercase       (			a__							,       a__							,       a__=False       )    ->			Dict:
							# fmt: off
							__SCREAMING_SNAKE_CASE									= {
							    '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

							__SCREAMING_SNAKE_CASE									= model_name_to_url[model_name]
							print('Checkpoint URL: '							,       a__       )
							__SCREAMING_SNAKE_CASE									= torch.hub.load_state_dict_from_url(a__							,       map_location='cpu'       )['model']

							# rename keys
							for key in state_dict.copy().keys():
														__SCREAMING_SNAKE_CASE									= state_dict.pop(a__       )
														__SCREAMING_SNAKE_CASE									= val

							__SCREAMING_SNAKE_CASE									= get_focalnet_config(a__       )
							__SCREAMING_SNAKE_CASE									= FocalNetForImageClassification(a__       )
							model.eval()

							# load state dict
							model.load_state_dict(a__       )

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

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

							__SCREAMING_SNAKE_CASE									= 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]       ),
							    ]       )

							__SCREAMING_SNAKE_CASE									= image_transforms(a__       ).unsqueeze(0       )

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

							__SCREAMING_SNAKE_CASE									= model(**a__       )

							__SCREAMING_SNAKE_CASE									= 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":
														__SCREAMING_SNAKE_CASE									= torch.tensor([0.2166, -0.4368, 0.2191]       )
							elif model_name == "focalnet-tiny-lrf":
														__SCREAMING_SNAKE_CASE									= torch.tensor([1.1669, 0.0125, -0.1695]       )
							elif model_name == "focalnet-small":
														__SCREAMING_SNAKE_CASE									= torch.tensor([0.4917, -0.0430, 0.1341]       )
							elif model_name == "focalnet-small-lrf":
														__SCREAMING_SNAKE_CASE									= torch.tensor([-0.2588, -0.5342, -0.2331]       )
							elif model_name == "focalnet-base":
														__SCREAMING_SNAKE_CASE									= torch.tensor([-0.1655, -0.4090, -0.1730]       )
							elif model_name == "focalnet-base-lrf":
														__SCREAMING_SNAKE_CASE									= torch.tensor([0.5306, -0.0483, -0.3928]       )
							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__		:					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.''',
			)

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






