



import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

			if is_vision_available():
						from transformers import MaskFormerImageProcessor

if is_vision_available():
			from PIL import Image

class        UpperCAmelCase_  :

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A=2				,							_A=True				,							_A=False				,							_A=10				,							_A=3				,							_A=32 * 4				,							_A=32 * 6				,							_A=4				,							_A=32				,							):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= batch_size
													__SCREAMING_SNAKE_CASE									= is_training
													__SCREAMING_SNAKE_CASE									= use_auxiliary_loss
													__SCREAMING_SNAKE_CASE									= num_queries
													__SCREAMING_SNAKE_CASE									= num_channels
													__SCREAMING_SNAKE_CASE									= min_size
													__SCREAMING_SNAKE_CASE									= max_size
													__SCREAMING_SNAKE_CASE									= num_labels
													__SCREAMING_SNAKE_CASE									= mask_feature_size






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]		).to(
													    _A		)

													__SCREAMING_SNAKE_CASE									= torch.ones([self.batch_size, self.min_size, self.max_size]				,							device=_A		)

													__SCREAMING_SNAKE_CASE									= (
													    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]				,							device=_A		) > 0.5
													).float()
													__SCREAMING_SNAKE_CASE									= (torch.rand((self.batch_size, self.num_labels)				,							device=_A		) > 0.5).long()

													__SCREAMING_SNAKE_CASE									= self.get_config()
													return config, pixel_values, pixel_mask, mask_labels, class_labels






						def 	_A     (   self		):







													'''simple docstring'''

													return MaskFormerConfig.from_backbone_and_decoder_configs(
													    backbone_config=SwinConfig(
													        depths=[1, 1, 1, 1]				,							)				,							decoder_config=DetrConfig(
													        decoder_ffn_dim=128				,							num_queries=self.num_queries				,							decoder_attention_heads=2				,							d_model=self.mask_feature_size				,							)				,							mask_feature_size=self.mask_feature_size				,							fpn_feature_size=self.mask_feature_size				,							num_channels=self.num_channels				,							num_labels=self.num_labels				,							)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.prepare_config_and_inputs()
													__SCREAMING_SNAKE_CASE									= {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
													return config, inputs_dict






						def 	_A     (   self				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= output.encoder_hidden_states
													__SCREAMING_SNAKE_CASE									= output.pixel_decoder_hidden_states
													__SCREAMING_SNAKE_CASE									= output.transformer_decoder_hidden_states

													self.parent.assertTrue(len(_A		)				,							len(config.backbone_config.depths		)		)
													self.parent.assertTrue(len(_A		)				,							len(config.backbone_config.depths		)		)
													self.parent.assertTrue(len(_A		)				,							config.decoder_config.decoder_layers		)






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







													'''simple docstring'''

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= MaskFormerModel(config=_A		)
																				model.to(_A		)
																				model.eval()

																				__SCREAMING_SNAKE_CASE									= model(pixel_values=_A				,							pixel_mask=_A		)
																				__SCREAMING_SNAKE_CASE									= model(_A				,							output_hidden_states=_A		)
													# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
													# encoder and pixel decoder
													self.parent.assertEqual(
													    output.transformer_decoder_last_hidden_state.shape				,							(self.batch_size, self.num_queries, self.mask_feature_size)				,							)
													# let's ensure the other two hidden state exists
													self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None		)
													self.parent.assertTrue(output.encoder_last_hidden_state is not None		)

													if output_hidden_states:
																				self.check_output_hidden_state(_A				,							_A		)






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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerForInstanceSegmentation(config=_A		)
													model.to(_A		)
													model.eval()

													def comm_check_on_output(_A		):
																				# let's still check that all the required stuff is there
																				self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None		)
																				self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None		)
																				self.parent.assertTrue(result.encoder_last_hidden_state is not None		)
																				# okay, now we need to check the logits shape
																				# due to the encoder compression, masks have a //4 spatial size
																				self.parent.assertEqual(
																				    result.masks_queries_logits.shape				,							(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)				,							)
																				# + 1 for null class
																				self.parent.assertEqual(
																				    result.class_queries_logits.shape				,							(self.batch_size, self.num_queries, self.num_labels + 1)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(pixel_values=_A				,							pixel_mask=_A		)
																				__SCREAMING_SNAKE_CASE									= model(_A		)

																				comm_check_on_output(_A		)

																				__SCREAMING_SNAKE_CASE									= model(
																				    pixel_values=_A				,							pixel_mask=_A				,							mask_labels=_A				,							class_labels=_A		)

													comm_check_on_output(_A		)

													self.parent.assertTrue(result.loss is not None		)
													self.parent.assertEqual(result.loss.shape				,							torch.Size([1]		)		)







@require_torch
class        UpperCAmelCase_  (       lowerCamelCase__   ,   lowerCamelCase__   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			Union[str, Any]      =  (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
						UpperCamelCase__      :			Optional[Any]      =  (
						    {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
						    if is_torch_available()
						    else {}
						)

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






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerModelTester(self		)
													__SCREAMING_SNAKE_CASE									= ConfigTester(self				,							config_class=_A				,							has_text_modality=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													self.config_tester.run_common_tests()






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(_A				,							**_A				,							output_hidden_states=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

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






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







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer does not have a get_input_embeddings method'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer is not a generative model'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer does not use token embeddings'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@require_torch_multi_gpu
						@unittest.skip(
						    reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						def 	_A     (   self		):







													'''simple docstring'''

													__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.forward		)
																				# 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		)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													for model_name in ["facebook/maskformer-swin-small-coco"]:
																				__SCREAMING_SNAKE_CASE									= MaskFormerModel.from_pretrained(_A		)
																				self.assertIsNotNone(_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (self.model_tester.min_size,) * 2
													__SCREAMING_SNAKE_CASE									= {
													    "pixel_values": torch.randn((2, 3, *size)				,							device=_A		),
													    "mask_labels": torch.randn((2, 10, *size)				,							device=_A		),
													    "class_labels": torch.zeros(2				,							10				,							device=_A		).long(),
													}

													__SCREAMING_SNAKE_CASE									= MaskFormerForInstanceSegmentation(MaskFormerConfig()		).to(_A		)
													__SCREAMING_SNAKE_CASE									= model(**_A		)
													self.assertTrue(outputs.loss is not None		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(_A				,							**_A				,							output_hidden_states=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__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		).to(_A		)
																				__SCREAMING_SNAKE_CASE									= model(**_A				,							output_attentions=_A		)
																				self.assertTrue(outputs.attentions is not None		)






						def 	_A     (   self		):







													'''simple docstring'''

													if not self.model_tester.is_training:
																				return
													# only MaskFormerForInstanceSegmentation has the loss
													__SCREAMING_SNAKE_CASE									= self.all_model_classes[1]
													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()

													__SCREAMING_SNAKE_CASE									= model_class(_A		)
													model.to(_A		)
													model.train()

													__SCREAMING_SNAKE_CASE									= model(_A				,							mask_labels=_A				,							class_labels=_A		).loss
													loss.backward()






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.all_model_classes[1]
													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													__SCREAMING_SNAKE_CASE									= True
													__SCREAMING_SNAKE_CASE									= True

													__SCREAMING_SNAKE_CASE									= model_class(_A		)
													model.to(_A		)
													model.train()

													__SCREAMING_SNAKE_CASE									= model(_A				,							mask_labels=_A				,							class_labels=_A		)

													__SCREAMING_SNAKE_CASE									= outputs.encoder_hidden_states[0]
													encoder_hidden_states.retain_grad()

													__SCREAMING_SNAKE_CASE									= outputs.pixel_decoder_hidden_states[0]
													pixel_decoder_hidden_states.retain_grad()
													# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
													__SCREAMING_SNAKE_CASE									= outputs.transformer_decoder_hidden_states[0]
													transformer_decoder_hidden_states.retain_grad()

													__SCREAMING_SNAKE_CASE									= outputs.attentions[0]
													attentions.retain_grad()

													outputs.loss.backward(retain_graph=_A		)

													self.assertIsNotNone(encoder_hidden_states.grad		)
													self.assertIsNotNone(pixel_decoder_hidden_states.grad		)
													self.assertIsNotNone(transformer_decoder_hidden_states.grad		)
													self.assertIsNotNone(attentions.grad		)







lowerCAmelCase__		:					List[Any]     						=1E-4






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

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

						'''simple docstring'''




						@cached_property
						def 	_A     (   self		):







													'''simple docstring'''

													return (
													    MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    if is_vision_available()
													    else None
													)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs["pixel_values"].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.encoder_last_hidden_state[0, 0, :3, :3]				,							_A				,							atol=_A		)		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]				,							_A				,							atol=_A		)		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.transformer_decoder_last_hidden_state[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs["pixel_values"].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)
													# masks_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.masks_queries_logits
													self.assertEqual(
													    masks_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)				,							)
													__SCREAMING_SNAKE_CASE									= [
													    [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3],
													    [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5],
													    [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2],
													]
													__SCREAMING_SNAKE_CASE									= torch.tensor(_A		).to(_A		)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,							_A				,							atol=_A		)		)
													# class_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [
													        [1.6512e00, -5.2572e00, -3.3519e00],
													        [3.6169e-02, -5.9025e00, -2.9313e00],
													        [1.0766e-04, -7.7630e00, -5.1263e00],
													    ]		).to(_A		)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs["pixel_values"].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)
													# masks_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.masks_queries_logits
													self.assertEqual(
													    masks_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)				,							)
													__SCREAMING_SNAKE_CASE									= [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]]
													__SCREAMING_SNAKE_CASE									= torch.tensor(_A		).to(_A		)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,							_A				,							atol=_A		)		)
													# class_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]		).to(_A		)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor

													__SCREAMING_SNAKE_CASE									= image_processor(
													    [np.zeros((3, 800, 1_333)		), np.zeros((3, 800, 1_333)		)]				,							segmentation_maps=[np.zeros((384, 384)		).astype(np.floataa		), np.zeros((384, 384)		).astype(np.floataa		)]				,							return_tensors='pt'				,							)

													__SCREAMING_SNAKE_CASE									= inputs["pixel_values"].to(_A		)
													__SCREAMING_SNAKE_CASE									= [el.to(_A		) for el in inputs["mask_labels"]]
													__SCREAMING_SNAKE_CASE									= [el.to(_A		) for el in inputs["class_labels"]]

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)

													self.assertTrue(outputs.loss is not None		)











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)






