





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

    if "tiny" in model_name or "small" in model_name or "base" in model_name:
        lowerCamelCase__       : Optional[int]   					=						[3, 3, 3, 3]
        if "lrf" in model_name:
            lowerCamelCase__       : str   					=						[3, 3, 3, 3]
        else:
            lowerCamelCase__       : Optional[Any]   					=						[2, 2, 2, 2]

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

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

    lowerCamelCase__       : Dict   					=						json.load(open(hf_hub_download(_UpperCAmelCase		,      _UpperCAmelCase		,      repo_type='dataset'					)		,      'r'					)					)
    lowerCamelCase__       : Any   					=						{int(_UpperCAmelCase					): v for k, v in idalabel.items()}
    lowerCamelCase__       : Dict   					=						{v: k for k, v in idalabel.items()}

    lowerCamelCase__       : Union[str, Any]   					=						FocalNetConfig(
        embed_dim=_UpperCAmelCase		,      depths=_UpperCAmelCase		,      focal_levels=_UpperCAmelCase		,      focal_windows=_UpperCAmelCase		,      use_conv_embed=_UpperCAmelCase		,      idalabel=_UpperCAmelCase		,      labelaid=_UpperCAmelCase		,      use_post_layernorm=_UpperCAmelCase		,      use_layerscale=_UpperCAmelCase		,      )

    return config


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

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

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

    return name


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase=False					)     ->	int:
    # fmt: off
    lowerCamelCase__       : Any   					=						{
        '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__       : int   					=						model_name_to_url[model_name]
    print('Checkpoint URL: '		,      _UpperCAmelCase					)
    lowerCamelCase__       : int   					=						torch.hub.load_state_dict_from_url(_UpperCAmelCase		,      map_location='cpu'					)['model']

    # rename keys
    for key in state_dict.copy().keys():
        lowerCamelCase__       : str   					=						state_dict.pop(_UpperCAmelCase					)
        lowerCamelCase__       : List[str]   					=						val

    lowerCamelCase__       : Union[str, Any]   					=						get_focalnet_config(_UpperCAmelCase					)
    lowerCamelCase__       : List[str]   					=						FocalNetForImageClassification(_UpperCAmelCase					)
    model.eval()

    # load state dict
    model.load_state_dict(_UpperCAmelCase					)

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

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

    lowerCamelCase__       : str   					=						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]					),
        ]					)

    lowerCamelCase__       : str   					=						image_transforms(_UpperCAmelCase					).unsqueeze(0					)

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

    lowerCamelCase__       : Optional[int]   					=						model(**_UpperCAmelCase					)

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

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

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


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

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












import gc
import threading
import time

import psutil
import torch

class   lowerCAmelCase				:
 def __init__( self    : str      )     ->      Union[str, Any]:
     lowerCamelCase__       : Optional[Any]   					=						psutil.Process()
     lowerCamelCase__       : Union[str, Any]   					=						False
 def 	A_      ( self    : Optional[int]      )     ->      int:
     lowerCamelCase__       : Optional[Any]   					=						-1

     while True:
         lowerCamelCase__       : Dict   					=						max(self.process.memory_info().rss     ,	self.cpu_memory_peak      )

         # can't sleep or will not catch the peak right (this comment is here on purpose)
         if not self.peak_monitoring:
             break
 def 	A_      ( self    : Tuple      )     ->      Dict:
     lowerCamelCase__       : List[Any]   					=						True
     lowerCamelCase__       : List[str]   					=						threading.Thread(target=self.peak_monitor      )
     lowerCamelCase__       : Union[str, Any]   					=						True
     self.thread.start()







 def 	A_      ( self    : str      )     ->      Dict:
     lowerCamelCase__       : int   					=						False
     self.thread.join()
     return self.cpu_memory_peak


_UpperCAmelCase							:			Dict        =							PeakCPUMemory()


def 			SCREAMING_SNAKE_CASE (			)     ->	Union[str, Any]:
    # Time
    lowerCamelCase__       : List[Any]   					=						{'time': time.time()}

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

    # CPU mem
    lowerCamelCase__       : List[str]   					=						psutil.Process().memory_info().rss
    cpu_peak_tracker.start()

    # GPU mem
    for i in range(torch.cuda.device_count()					):
        lowerCamelCase__       : Union[str, Any]   					=						torch.cuda.memory_allocated(_UpperCAmelCase					)
    torch.cuda.reset_peak_memory_stats()

    return measures


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase					)     ->	Tuple:
    # Time
    lowerCamelCase__       : Optional[int]   					=						{'time': time.time() - start_measures['time']}

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

    # CPU mem
    lowerCamelCase__       : Dict   					=						(psutil.Process().memory_info().rss - start_measures['cpu']) / 2**20
    lowerCamelCase__       : int   					=						(cpu_peak_tracker.stop() - start_measures['cpu']) / 2**20

    # GPU mem
    for i in range(torch.cuda.device_count()					):
        lowerCamelCase__       : List[str]   					=						(torch.cuda.memory_allocated(_UpperCAmelCase					) - start_measures[str(_UpperCAmelCase					)]) / 2**20
        lowerCamelCase__       : Optional[Any]   					=						(torch.cuda.max_memory_allocated(_UpperCAmelCase					) - start_measures[str(_UpperCAmelCase					)]) / 2**20

    return measures


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase		,      _UpperCAmelCase					)     ->	Dict:
    print(F"""{description}:"""					)
    print(F"""- Time: {measures["time"]:.2f}s"""					)
    for i in range(torch.cuda.device_count()					):
        print(F"""- GPU {i} allocated: {measures[str(_UpperCAmelCase					)]:.2f}MiB"""					)
        lowerCamelCase__       : List[str]   					=						measures[F"""{i}-peak"""]
        print(F"""- GPU {i} peak: {peak:.2f}MiB"""					)
    print(F"""- CPU RAM allocated: {measures["cpu"]:.2f}MiB"""					)
    print(F"""- CPU RAM peak: {measures["cpu-peak"]:.2f}MiB"""					)





