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        snake_case_					(  snake_case					)  ->    Union[str, Any]:
       lowercase__:       str        =       [2, 2, 6, 2] if "tiny" in model_name else [2, 2, 18, 2]
       lowercase__:       Optional[Any]        =       True if "large" in model_name or "huge" in model_name else False
       lowercase__:       int        =       True if "large" in model_name or "huge" in model_name else False
       lowercase__:       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:
                     lowercase__:       Any        =       [3, 3, 3, 3]
                     lowercase__:       Any        =       [5, 5, 5, 5]
              elif "fl4" in model_name:
                     lowercase__:       Tuple        =       [4, 4, 4, 4]
                     lowercase__:       Tuple        =       [3, 3, 3, 3]

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

       if "tiny" in model_name:
              lowercase__:       Tuple        =       96
       elif "small" in model_name:
              lowercase__:       Union[str, Any]        =       96
       elif "base" in model_name:
              lowercase__:       Tuple        =       1_28
       elif "large" in model_name:
              lowercase__:       Union[str, Any]        =       1_92
       elif "xlarge" in model_name:
              lowercase__:       Optional[Any]        =       2_56
       elif "huge" in model_name:
              lowercase__:       List[str]        =       3_52

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

       lowercase__:       Dict        =       json.load(open(hf_hub_download(__snake_case      ,							__snake_case      ,							repo_type='dataset'					)      ,							'r'					)					)
       lowercase__:       List[Any]        =       {int(__snake_case					): v for k, v in idalabel.items()}
       lowercase__:       Optional[int]        =       {v: k for k, v in idalabel.items()}

       lowercase__:       Optional[int]        =       FocalNetConfig(
           embed_dim=__snake_case      ,							depths=__snake_case      ,							focal_levels=__snake_case      ,							focal_windows=__snake_case      ,							use_conv_embed=__snake_case      ,							idalabel=__snake_case      ,							labelaid=__snake_case      ,							use_post_layernorm=__snake_case      ,							use_layerscale=__snake_case      ,							)

       return config




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

       if name == "norm.weight":
              lowercase__:       int        =       "layernorm.weight"
       if name == "norm.bias":
              lowercase__:       List[Any]        =       "layernorm.bias"

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

       return name




def        snake_case_					(  snake_case      ,							snake_case      ,							snake_case=False					)  ->    List[Any]:
       # fmt: off
       lowercase__:       int        =       {
           "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

       lowercase__:       Optional[int]        =       model_name_to_url[model_name]
       print('Checkpoint URL: '      ,							__snake_case					)
       lowercase__:       List[Any]        =       torch.hub.load_state_dict_from_url(__snake_case      ,							map_location='cpu'					)["model"]

       # rename keys
       for key in state_dict.copy().keys():
              lowercase__:       str        =       state_dict.pop(__snake_case					)
              lowercase__:       Tuple        =       val

       lowercase__:       Union[str, Any]        =       get_focalnet_config(__snake_case					)
       lowercase__:       Any        =       FocalNetForImageClassification(__snake_case					)
       model.eval()

       # load state dict
       model.load_state_dict(__snake_case					)

       # verify conversion
       lowercase__:       Union[str, Any]        =       "http://images.cocodataset.org/val2017/000000039769.jpg"

       lowercase__:       str        =       BitImageProcessor(
           do_resize=__snake_case      ,							size={'shortest_edge': 2_56}      ,							resample=PILImageResampling.BILINEAR      ,							do_center_crop=__snake_case      ,							crop_size=2_24      ,							do_normalize=__snake_case      ,							image_mean=__snake_case      ,							image_std=__snake_case      ,							)
       lowercase__:       Dict        =       Image.open(requests.get(__snake_case      ,							stream=__snake_case					).raw					)
       lowercase__:       Optional[Any]        =       processor(images=__snake_case      ,							return_tensors='pt'					)

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

       lowercase__:       Optional[Any]        =       image_transforms(__snake_case					).unsqueeze(0					)

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

       lowercase__:       int        =       model(**__snake_case					)

       lowercase__:       Optional[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":
              lowercase__:       str        =       torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]					)
       elif model_name == "focalnet-tiny-lrf":
              lowercase__:       int        =       torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5]					)
       elif model_name == "focalnet-small":
              lowercase__:       List[Any]        =       torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1]					)
       elif model_name == "focalnet-small-lrf":
              lowercase__:       str        =       torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1]					)
       elif model_name == "focalnet-base":
              lowercase__:       List[str]        =       torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0]					)
       elif model_name == "focalnet-base-lrf":
              lowercase__:       Tuple        =       torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8]					)
       assert torch.allclose(outputs.logits[0, :3]      ,							__snake_case      ,							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(__snake_case					)
              processor.save_pretrained(__snake_case					)

       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			=							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			=							parser.parse_args()
 convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)






import warnings
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__lowerCAmelCase			=							logging.get_logger(__name__)





class   __a      (   __UpperCamelCase				):
 __lowercase			:							Tuple       					=			['input_values', 'attention_mask']
 def __init__(       self    ,			lowerCAmelCase__ = 1    ,			lowerCAmelCase__ = 16_000    ,			lowerCAmelCase__ = 0.0    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = 80    ,			lowerCAmelCase__ = 16    ,			lowerCAmelCase__ = 64    ,			lowerCAmelCase__ = "hann_window"    ,			lowerCAmelCase__ = 1.0    ,			lowerCAmelCase__ = 80    ,			lowerCAmelCase__ = 7_600    ,			lowerCAmelCase__ = 1E-10    ,			lowerCAmelCase__ = 2    ,			lowerCAmelCase__ = True    ,			**lowerCAmelCase__    ,			) ->    List[Any]:




        '''simple docstring'''
        super().__init__(feature_size=lowerCAmelCase__    ,			sampling_rate=lowerCAmelCase__    ,			padding_value=lowerCAmelCase__    ,			**lowerCAmelCase__			)
        lowercase__:       Dict        =       do_normalize
        lowercase__:       Optional[Any]        =       return_attention_mask

        lowercase__:       str        =       num_mel_bins
        lowercase__:       Dict        =       hop_length
        lowercase__:       Dict        =       win_length
        lowercase__:       Optional[int]        =       win_function
        lowercase__:       Any        =       frame_signal_scale
        lowercase__:       Tuple        =       fmin
        lowercase__:       Tuple        =       fmax
        lowercase__:       Dict        =       mel_floor
        lowercase__:       int        =       reduction_factor

        lowercase__:       List[Any]        =       win_length * sampling_rate // 1_000
        lowercase__:       Optional[Any]        =       hop_length * sampling_rate // 1_000
        lowercase__:       Optional[int]        =       optimal_fft_length(self.sample_size			)
        lowercase__:       Optional[Any]        =       (self.n_fft // 2) + 1

        lowercase__:       str        =       window_function(window_length=self.sample_size    ,			name=self.win_function    ,			periodic=lowerCAmelCase__			)

        lowercase__:       Optional[Any]        =       mel_filter_bank(
            num_frequency_bins=self.n_freqs    ,			num_mel_filters=self.num_mel_bins    ,			min_frequency=self.fmin    ,			max_frequency=self.fmax    ,			sampling_rate=self.sampling_rate    ,			norm='slaney'    ,			mel_scale='slaney'    ,			)

        if frame_signal_scale != 1.0:
               warnings.warn(
                   'The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers'    ,			lowerCAmelCase__    ,			)
        if reduction_factor != 2.0:
               warnings.warn(
                   'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'    ,			lowerCAmelCase__    ,			)
 @staticmethod
 # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
 def 		SCREAMING_SNAKE_CASE__       (       lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__ = 0.0			) ->    List[np.ndarray]:




        '''simple docstring'''
        if attention_mask is not None:
               lowercase__:       List[str]        =       np.array(lowerCAmelCase__    ,			np.intaa			)
               lowercase__:       Tuple        =       []

               for vector, length in zip(lowerCAmelCase__    ,			attention_mask.sum(-1			)			):
                      lowercase__:       int        =       (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7			)
                      if length < normed_slice.shape[0]:
                             lowercase__:       Tuple        =       padding_value

                      normed_input_values.append(lowerCAmelCase__			)
        else:
               lowercase__:       Union[str, Any]        =       [(x - x.mean()) / np.sqrt(x.var() + 1E-7			) for x in input_values]

        return normed_input_values
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			) ->    np.ndarray:




        '''simple docstring'''
        lowercase__:       List[str]        =       spectrogram(
            lowerCAmelCase__    ,			window=self.window    ,			frame_length=self.sample_size    ,			hop_length=self.sample_stride    ,			fft_length=self.n_fft    ,			mel_filters=self.mel_filters    ,			mel_floor=self.mel_floor    ,			log_mel='log10'    ,			)
        return log_mel_spec.T
 def __call__(       self    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			**lowerCAmelCase__    ,			) ->    BatchFeature:




        '''simple docstring'''
        if audio is None and audio_target is None:
               raise ValueError('You must provide either `audio` or `audio_target` values.'			)

        if sampling_rate is not None:
               if sampling_rate != self.sampling_rate:
                      raise ValueError(
                          F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                          F' {self.sampling_rate}. Please make sure that the provided audio input was sampled with'
                          F' {self.sampling_rate} and not {sampling_rate}.'			)
        else:
               logger.warning(
                   'It is strongly recommended to pass the ``sampling_rate`` argument to this function. '
                   'Failing to do so can result in silent errors that might be hard to debug.'			)

        if audio is not None:
               lowercase__:       Dict        =       self._process_audio(
                   lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			**lowerCAmelCase__    ,			)
        else:
               lowercase__:       str        =       None

        if audio_target is not None:
               lowercase__:       List[str]        =       self._process_audio(
                   lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			**lowerCAmelCase__    ,			)

               if inputs is None:
                      return inputs_target
               else:
                      lowercase__:       int        =       inputs_target['input_values']
                      lowercase__:       List[str]        =       inputs_target.get('attention_mask'			)
                      if decoder_attention_mask is not None:
                             lowercase__:       Optional[int]        =       decoder_attention_mask

        return inputs
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			**lowerCAmelCase__    ,			) ->    BatchFeature:




        '''simple docstring'''
        lowercase__:       int        =       isinstance(lowerCAmelCase__    ,			np.ndarray			) and len(speech.shape			) > 1
        if is_batched_numpy and len(speech.shape			) > 2:
               raise ValueError(F'Only mono-channel audio is supported for input to {self}'			)
        lowercase__:       Tuple        =       is_batched_numpy or (
            isinstance(lowerCAmelCase__    ,			(list, tuple)			) and (isinstance(speech[0]    ,			(np.ndarray, tuple, list)			))
        )

        if is_batched:
               lowercase__:       Dict        =       [np.asarray(lowerCAmelCase__    ,			dtype=np.floataa			) for speech in speech]
        elif not is_batched and not isinstance(lowerCAmelCase__    ,			np.ndarray			):
               lowercase__:       Optional[Any]        =       np.asarray(lowerCAmelCase__    ,			dtype=np.floataa			)
        elif isinstance(lowerCAmelCase__    ,			np.ndarray			) and speech.dtype is np.dtype(np.floataa			):
               lowercase__:       Optional[Any]        =       speech.astype(np.floataa			)

        # always return batch
        if not is_batched:
               lowercase__:       Optional[int]        =       [speech]

        # needed to make pad() work on spectrogram inputs
        lowercase__:       str        =       self.feature_size

        # convert into correct format for padding
        if is_target:
               lowercase__:       int        =       [self._extract_mel_features(lowerCAmelCase__			) for waveform in speech]
               lowercase__:       Dict        =       BatchFeature({'input_values': features}			)
               lowercase__:       Union[str, Any]        =       self.num_mel_bins
        else:
               lowercase__:       Union[str, Any]        =       BatchFeature({'input_values': speech}			)

        lowercase__:       Dict        =       self.pad(
            lowerCAmelCase__    ,			padding=lowerCAmelCase__    ,			max_length=lowerCAmelCase__    ,			truncation=lowerCAmelCase__    ,			pad_to_multiple_of=lowerCAmelCase__    ,			return_attention_mask=lowerCAmelCase__    ,			**lowerCAmelCase__    ,			)

        lowercase__:       List[str]        =       feature_size_hack

        # convert input values to correct format
        lowercase__:       Union[str, Any]        =       padded_inputs['input_values']
        if not isinstance(input_values[0]    ,			np.ndarray			):
               lowercase__:       List[Any]        =       [np.asarray(lowerCAmelCase__    ,			dtype=np.floataa			) for array in input_values]
        elif (
            not isinstance(lowerCAmelCase__    ,			np.ndarray			)
            and isinstance(input_values[0]    ,			np.ndarray			)
            and input_values[0].dtype is np.dtype(np.floataa			)
        ):
               lowercase__:       Dict        =       [array.astype(np.floataa			) for array in input_values]
        elif isinstance(lowerCAmelCase__    ,			np.ndarray			) and input_values.dtype is np.dtype(np.floataa			):
               lowercase__:       Tuple        =       input_values.astype(np.floataa			)

        # convert attention_mask to correct format
        lowercase__:       Tuple        =       padded_inputs.get('attention_mask'			)
        if attention_mask is not None:
               lowercase__:       str        =       [np.asarray(lowerCAmelCase__    ,			dtype=np.intaa			) for array in attention_mask]

        # zero-mean and unit-variance normalization
        if not is_target and self.do_normalize:
               lowercase__:       Tuple        =       (
                   attention_mask
                   if self._get_padding_strategies(lowerCAmelCase__    ,			max_length=lowerCAmelCase__			) is not PaddingStrategy.DO_NOT_PAD
                   else None
               )
               lowercase__:       str        =       self.zero_mean_unit_var_norm(
                   padded_inputs['input_values']    ,			attention_mask=lowerCAmelCase__    ,			padding_value=self.padding_value			)

        if return_tensors is not None:
               lowercase__:       Union[str, Any]        =       padded_inputs.convert_to_tensors(lowerCAmelCase__			)

        return padded_inputs
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict[str, Any]:




        '''simple docstring'''
        lowercase__:       int        =       super().to_dict()

        # Don't serialize these as they are derived from the other properties.
        lowercase__:       str        =       ['window', 'mel_filters', 'sample_size', 'sample_stride', 'n_fft', 'n_freqs']
        for name in names:
               if name in output:
                      del output[name]

        return output


