






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 							A     (       a_					)     ->						int:
					__UpperCamelCase      :					Union[str, Any]       		=[2, 2, 6, 2] if 'tiny' in model_name else [2, 2, 18, 2]
					__UpperCamelCase      :					str       		=True if 'large' in model_name or 'huge' in model_name else False
					__UpperCamelCase      :					List[Any]       		=True if 'large' in model_name or 'huge' in model_name else False
					__UpperCamelCase      :					List[str]       		=True if 'large' in model_name or 'huge' in model_name else False

					if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
										if "fl3" in model_name:
															__UpperCamelCase      :					Tuple       		=[3, 3, 3, 3]
															__UpperCamelCase      :					int       		=[5, 5, 5, 5]
										elif "fl4" in model_name:
															__UpperCamelCase      :					Dict       		=[4, 4, 4, 4]
															__UpperCamelCase      :					int       		=[3, 3, 3, 3]

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

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

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

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

					__UpperCamelCase      :					List[Any]       		=FocalNetConfig(
					    embed_dim=a_							,depths=a_							,focal_levels=a_							,focal_windows=a_							,use_conv_embed=a_							,idalabel=a_							,labelaid=a_							,use_post_layernorm=a_							,use_layerscale=a_							,)

					return config






def 							A     (       a_					)     ->						Dict:







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

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

					if "head" in name:
										__UpperCamelCase      :					Optional[int]       		=name.replace('head'							,'classifier'					)
					else:
										__UpperCamelCase      :					int       		='focalnet.' + name

					return name






def 							A     (       a_							,a_							,a_=False					)     ->						Any:
					__UpperCamelCase      :					List[str]       		={
					    '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

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

					# rename keys
					for key in state_dict.copy().keys():
										__UpperCamelCase      :					int       		=state_dict.pop(a_					)
										__UpperCamelCase      :					Optional[int]       		=val

					__UpperCamelCase      :					List[str]       		=get_focalnet_config(a_					)
					__UpperCamelCase      :					Tuple       		=FocalNetForImageClassification(a_					)
					model.eval()

					# load state dict
					model.load_state_dict(a_					)

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

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

					__UpperCamelCase      :					Any       		=transforms.Compose(
					    [
					        transforms.Resize(256					),
					        transforms.CenterCrop(224					),
					        transforms.ToTensor(),
					        transforms.Normalize(mean=[0.485, 0.456, 0.406]							,std=[0.229, 0.224, 0.225]					),
					    ]					)

					__UpperCamelCase      :					int       		=image_transforms(a_					).unsqueeze(0					)

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

					__UpperCamelCase      :					Tuple       		=model(**a_					)

					__UpperCamelCase      :					List[Any]       		=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":
										__UpperCamelCase      :					Union[str, Any]       		=torch.tensor([0.2_166, -0.4_368, 0.2_191]					)
					elif model_name == "focalnet-tiny-lrf":
										__UpperCamelCase      :					Tuple       		=torch.tensor([1.1_669, 0.0_125, -0.1_695]					)
					elif model_name == "focalnet-small":
										__UpperCamelCase      :					List[Any]       		=torch.tensor([0.4_917, -0.0_430, 0.1_341]					)
					elif model_name == "focalnet-small-lrf":
										__UpperCamelCase      :					Dict       		=torch.tensor([-0.2_588, -0.5_342, -0.2_331]					)
					elif model_name == "focalnet-base":
										__UpperCamelCase      :					str       		=torch.tensor([-0.1_655, -0.4_090, -0.1_730]					)
					elif model_name == "focalnet-base-lrf":
										__UpperCamelCase      :					str       		=torch.tensor([0.5_306, -0.0_483, -0.3_928]					)
					assert torch.allclose(outputs.logits[0, :3]							,a_							,atol=1e-4					)
					print('Looks ok!'					)

					if pytorch_dump_folder_path is not None:
										print(F'Saving model and processor of {model_name} to {pytorch_dump_folder_path}'					)
										model.save_pretrained(a_					)
										processor.save_pretrained(a_					)

					if push_to_hub:
										print(F'Pushing model and processor of {model_name} to the hub...'					)
										model.push_to_hub(F'{model_name}'					)
										processor.push_to_hub(F'{model_name}'					)


if __name__ == "__main__":
							A_       :str       =						argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '''--model_name''',
							    default='''focalnet-tiny''',
							    type=str,
							    help='''Name of the FocalNet model you\'d like to convert.''',
							)
							parser.add_argument(
							    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
							)
							parser.add_argument(
							    '''--push_to_hub''',
							    action='''store_true''',
							    help='''Whether to push the model and processor to the hub.''',
							)

							A_       :Union[str, Any]       =						parser.parse_args()
							convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)





'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
class   __lowerCAmelCase						(lowercase_  ,     unittest.TestCase     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Dict  					=							ShapEImgaImgPipeline
      lowerCAmelCase__					:     List[str]  					=							["""image"""]
      lowerCAmelCase__					:     Any  					=							["""image"""]
      lowerCAmelCase__					:     Any  					=							[
          """num_images_per_prompt""",
          """num_inference_steps""",
          """generator""",
          """latents""",
          """guidance_scale""",
          """frame_size""",
          """output_type""",
          """return_dict""",
      ]
      lowerCAmelCase__					:     Tuple  					=							False



      @property
      def       UpperCamelCase__		(self    :    List[Any] ):

             '''simple docstring'''







             return 32



      @property
      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







             return 32



      @property
      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             return self.time_input_dim * 4



      @property
      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             return 8



      @property
      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             torch.manual_seed(0 )
             lowercase__      			= CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size   ,						image_size=64   ,						projection_dim=self.text_embedder_hidden_size   ,						intermediate_size=37   ,						num_attention_heads=4   ,						num_channels=3   ,						num_hidden_layers=5   ,						patch_size=1   ,						)

             lowercase__      			= CLIPVisionModel(UpperCamelCase )
             return model



      @property
      def       UpperCamelCase__		(self    :    List[Any] ):

             '''simple docstring'''







             lowercase__      			= CLIPImageProcessor(
                 crop_size=224   ,						do_center_crop=UpperCamelCase   ,						do_normalize=UpperCamelCase   ,						do_resize=UpperCamelCase   ,						image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]   ,						image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]   ,						resample=3   ,						size=224   ,						)

             return image_processor



      @property
      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







             torch.manual_seed(0 )

             lowercase__      			= {
                 '''num_attention_heads''': 2,
                 '''attention_head_dim''': 16,
                 '''embedding_dim''': self.time_input_dim,
                 '''num_embeddings''': 32,
                 '''embedding_proj_dim''': self.text_embedder_hidden_size,
                 '''time_embed_dim''': self.time_embed_dim,
                 '''num_layers''': 1,
                 '''clip_embed_dim''': self.time_input_dim * 2,
                 '''additional_embeddings''': 0,
                 '''time_embed_act_fn''': '''gelu''',
                 '''norm_in_type''': '''layer''',
                 '''embedding_proj_norm_type''': '''layer''',
                 '''encoder_hid_proj_type''': None,
                 '''added_emb_type''': None,
             }

             lowercase__      			= PriorTransformer(**UpperCamelCase )
             return model



      @property
      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             torch.manual_seed(0 )

             lowercase__      			= {
                 '''param_shapes''': (
                     (self.renderer_dim, 93),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                 ),
                 '''d_latent''': self.time_input_dim,
                 '''d_hidden''': self.renderer_dim,
                 '''n_output''': 12,
                 '''background''': (
                     0.1,
                     0.1,
                     0.1,
                 ),
             }
             lowercase__      			= ShapERenderer(**UpperCamelCase )
             return model



      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= self.dummy_prior
             lowercase__      			= self.dummy_image_encoder
             lowercase__      			= self.dummy_image_processor
             lowercase__      			= self.dummy_renderer

             lowercase__      			= HeunDiscreteScheduler(
                 beta_schedule='''exp'''   ,						num_train_timesteps=1024   ,						prediction_type='''sample'''   ,						use_karras_sigmas=UpperCamelCase   ,						clip_sample=UpperCamelCase   ,						clip_sample_range=1.0   ,						)
             lowercase__      			= {
                 '''prior''': prior,
                 '''image_encoder''': image_encoder,
                 '''image_processor''': image_processor,
                 '''renderer''': renderer,
                 '''scheduler''': scheduler,
             }

             return components



      def       UpperCamelCase__		(self    :    Optional[int]   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    str=0 ):

             '''simple docstring'''







             lowercase__      			= floats_tensor((1, 3, 64, 64)   ,						rng=random.Random(UpperCamelCase ) ).to(UpperCamelCase )

             if str(UpperCamelCase ).startswith('''mps''' ):
                    lowercase__      			= torch.manual_seed(UpperCamelCase )
             else:
                    lowercase__      			= torch.Generator(device=UpperCamelCase ).manual_seed(UpperCamelCase )
             lowercase__      			= {
                 '''image''': input_image,
                 '''generator''': generator,
                 '''num_inference_steps''': 1,
                 '''frame_size''': 32,
                 '''output_type''': '''np''',
             }
             return inputs



      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







             lowercase__      			= '''cpu'''

             lowercase__      			= self.get_dummy_components()

             lowercase__      			= self.pipeline_class(**UpperCamelCase )
             lowercase__      			= pipe.to(UpperCamelCase )

             pipe.set_progress_bar_config(disable=UpperCamelCase )

             lowercase__      			= pipe(**self.get_dummy_inputs(UpperCamelCase ) )
             lowercase__      			= output.images[0]
             lowercase__      			= image[0, -3:, -3:, -1]

             assert image.shape == (20, 32, 32, 3)

             lowercase__      			= np.array(
                 [
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                     0.00_03_92_16,
                 ] )

             assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2



      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







             self._test_inference_batch_consistent(batch_sizes=[1, 2] )



      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             lowercase__      			= torch_device == '''cpu'''
             lowercase__      			= True
             self._test_inference_batch_single_identical(
                 batch_size=2   ,						test_max_difference=UpperCamelCase   ,						relax_max_difference=UpperCamelCase   ,						)






      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             lowercase__      			= self.get_dummy_components()
             lowercase__      			= self.pipeline_class(**UpperCamelCase )
             lowercase__      			= pipe.to(UpperCamelCase )
             pipe.set_progress_bar_config(disable=UpperCamelCase )

             lowercase__      			= 1
             lowercase__      			= 2

             lowercase__      			= self.get_dummy_inputs(UpperCamelCase )

             for key in inputs.keys():
                    if key in self.batch_params:
                           lowercase__      			= batch_size * [inputs[key]]

             lowercase__      			= pipe(**UpperCamelCase   ,						num_images_per_prompt=UpperCamelCase )[0]

             assert images.shape[0] == batch_size * num_images_per_prompt






@slow
@require_torch_gpu
class   __lowerCAmelCase						(unittest.TestCase     ):





      '''simple docstring'''






      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()






      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= load_image(
                 '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' )
             lowercase__      			= load_numpy(
                 '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
                 '''/shap_e/test_shap_e_img2img_out.npy''' )
             lowercase__      			= ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' )
             lowercase__      			= pipe.to(UpperCamelCase )
             pipe.set_progress_bar_config(disable=UpperCamelCase )

             lowercase__      			= torch.Generator(device=UpperCamelCase ).manual_seed(0 )

             lowercase__      			= pipe(
                 UpperCamelCase   ,						generator=UpperCamelCase   ,						guidance_scale=3.0   ,						num_inference_steps=64   ,						frame_size=64   ,						output_type='''np'''   ,						).images[0]

             assert images.shape == (20, 64, 64, 3)

             assert_mean_pixel_difference(UpperCamelCase   ,						UpperCamelCase )




