

'''simple docstring'''






import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD


torch.set_grad_enabled(False)



def 			__a		(       lowerCAmelCase__ :    List[Any]     ,  lowerCAmelCase__ :    Any=False      ):
     a__   :      Optional[Any]     =						[]
     for i in range(config.num_hidden_layers      ):
          # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
          rename_keys.append((F'module.blocks.{i}.norm1.weight', F'vit.encoder.layer.{i}.layernorm_before.weight')      )
          rename_keys.append((F'module.blocks.{i}.norm1.bias', F'vit.encoder.layer.{i}.layernorm_before.bias')      )
          rename_keys.append(
              (F'module.blocks.{i}.attn.proj.weight', F'vit.encoder.layer.{i}.attention.output.dense.weight')      )
          rename_keys.append((F'module.blocks.{i}.attn.proj.bias', F'vit.encoder.layer.{i}.attention.output.dense.bias')      )
          rename_keys.append((F'module.blocks.{i}.norm2.weight', F'vit.encoder.layer.{i}.layernorm_after.weight')      )
          rename_keys.append((F'module.blocks.{i}.norm2.bias', F'vit.encoder.layer.{i}.layernorm_after.bias')      )
          rename_keys.append((F'module.blocks.{i}.mlp.fc1.weight', F'vit.encoder.layer.{i}.intermediate.dense.weight')      )
          rename_keys.append((F'module.blocks.{i}.mlp.fc1.bias', F'vit.encoder.layer.{i}.intermediate.dense.bias')      )
          rename_keys.append((F'module.blocks.{i}.mlp.fc2.weight', F'vit.encoder.layer.{i}.output.dense.weight')      )
          rename_keys.append((F'module.blocks.{i}.mlp.fc2.bias', F'vit.encoder.layer.{i}.output.dense.bias')      )

     # projection layer + position embeddings
     rename_keys.extend(
         [
             ('''module.cls_token''', '''vit.embeddings.cls_token'''),
             ('''module.patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''),
             ('''module.patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''),
             ('''module.pos_embed''', '''vit.embeddings.position_embeddings'''),
         ]      )

     if base_model:
          # layernorm + pooler
          rename_keys.extend(
              [
                  ('''module.norm.weight''', '''layernorm.weight'''),
                  ('''module.norm.bias''', '''layernorm.bias'''),
              ]      )

          # if just the base model, we should remove "vit" from all keys that start with "vit"
          a__   :      Any     =						[(pair[0], pair[1][4:]) if pair[1].startswith('''vit'''      ) else pair for pair in rename_keys]
     else:
          # layernorm + classification head
          rename_keys.extend(
              [
                  ('''norm.weight''', '''vit.layernorm.weight'''),
                  ('''norm.bias''', '''vit.layernorm.bias'''),
                  ('''head.weight''', '''classifier.weight'''),
                  ('''head.bias''', '''classifier.bias'''),
              ]      )

     return rename_keys



def 			__a		(       lowerCAmelCase__ :    Any     ,  lowerCAmelCase__ :    Tuple     ,  lowerCAmelCase__ :    Optional[Any]=False      ):




     for i in range(config.num_hidden_layers      ):
          if base_model:
               a__   :      Any     =						''''''
          else:
               a__   :      Tuple     =						'''vit.'''
          # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
          a__   :      str     =						state_dict.pop(F'module.blocks.{i}.attn.qkv.weight'      )
          a__   :      List[Any]     =						state_dict.pop(F'module.blocks.{i}.attn.qkv.bias'      )
          # next, add query, keys and values (in that order) to the state dict
          a__   :      Dict     =						in_proj_weight[
              : config.hidden_size, :
          ]
          a__   :      int     =						in_proj_bias[: config.hidden_size]
          a__   :      Dict     =						in_proj_weight[
              config.hidden_size : config.hidden_size * 2, :
          ]
          a__   :      Optional[int]     =						in_proj_bias[
              config.hidden_size : config.hidden_size * 2
          ]
          a__   :      Any     =						in_proj_weight[
              -config.hidden_size :, :
          ]
          a__   :      Any     =						in_proj_bias[-config.hidden_size :]



def 			__a		(       lowerCAmelCase__ :    Dict      ):
     a__   :      List[str]     =						['''head.weight''', '''head.bias''']
     for k in ignore_keys:
          state_dict.pop(_UpperCamelCase     ,  _UpperCamelCase      )



def 			__a		(       lowerCAmelCase__ :    Dict      ):
     a__   :      List[str]     =						[
         '''module.fc.fc1.weight''',
         '''module.fc.fc1.bias''',
         '''module.fc.bn1.weight''',
         '''module.fc.bn1.bias''',
         '''module.fc.bn1.running_mean''',
         '''module.fc.bn1.running_var''',
         '''module.fc.bn1.num_batches_tracked''',
         '''module.fc.fc2.weight''',
         '''module.fc.fc2.bias''',
         '''module.fc.bn2.weight''',
         '''module.fc.bn2.bias''',
         '''module.fc.bn2.running_mean''',
         '''module.fc.bn2.running_var''',
         '''module.fc.bn2.num_batches_tracked''',
         '''module.fc.fc3.weight''',
         '''module.fc.fc3.bias''',
     ]
     for k in ignore_keys:
          state_dict.pop(_UpperCamelCase     ,  _UpperCamelCase      )



def 			__a		(       lowerCAmelCase__ :    str     ,  lowerCAmelCase__ :    Optional[Any]     ,  lowerCAmelCase__ :    int      ):
     a__   :      Optional[int]     =						dct.pop(_UpperCamelCase      )
     a__   :      Tuple     =						val



def 			__a		(       lowerCAmelCase__ :    Optional[int]     ,  lowerCAmelCase__ :    str      ):
     a__   :      List[str]     =						ViTMSNConfig()
     a__   :      Optional[Any]     =						1000

     a__   :      List[Any]     =						'''datasets/huggingface/label-files'''
     a__   :      Union[str, Any]     =						'''imagenet-1k-id2label.json'''
     a__   :      List[str]     =						json.load(open(hf_hub_download(_UpperCamelCase     ,  _UpperCamelCase      )     ,  '''r'''      )      )
     a__   :      str     =						{int(_UpperCamelCase      ): v for k, v in idalabel.items()}
     a__   :      Dict     =						idalabel
     a__   :      Tuple     =						{v: k for k, v in idalabel.items()}

     if "s16" in checkpoint_url:
          a__   :      Optional[Any]     =						384
          a__   :      Dict     =						1536
          a__   :      List[str]     =						6
     elif "l16" in checkpoint_url:
          a__   :      Tuple     =						1024
          a__   :      int     =						4096
          a__   :      Any     =						24
          a__   :      str     =						16
          a__   :      Dict     =						0.1
     elif "b4" in checkpoint_url:
          a__   :      Union[str, Any]     =						4
     elif "l7" in checkpoint_url:
          a__   :      Optional[int]     =						7
          a__   :      List[str]     =						1024
          a__   :      List[str]     =						4096
          a__   :      List[str]     =						24
          a__   :      List[str]     =						16
          a__   :      Tuple     =						0.1

     a__   :      Dict     =						ViTMSNModel(_UpperCamelCase      )

     a__   :      Dict     =						torch.hub.load_state_dict_from_url(_UpperCamelCase     ,  map_location='''cpu'''      )['''target_encoder''']

     a__   :      str     =						ViTImageProcessor(size=config.image_size      )

     remove_projection_head(_UpperCamelCase      )
     a__   :      Tuple     =						create_rename_keys(_UpperCamelCase     ,  base_model=_UpperCamelCase      )

     for src, dest in rename_keys:
          rename_key(_UpperCamelCase     ,  _UpperCamelCase     ,  _UpperCamelCase      )
     read_in_q_k_v(_UpperCamelCase     ,  _UpperCamelCase     ,  base_model=_UpperCamelCase      )

     model.load_state_dict(_UpperCamelCase      )
     model.eval()

     a__   :      Optional[int]     =						'''http://images.cocodataset.org/val2017/000000039769.jpg'''

     a__   :      int     =						Image.open(requests.get(_UpperCamelCase     ,  stream=_UpperCamelCase      ).raw      )
     a__   :      Union[str, Any]     =						ViTImageProcessor(
         size=config.image_size     ,  image_mean=_UpperCamelCase     ,  image_std=_UpperCamelCase      )
     a__   :      str     =						image_processor(images=_UpperCamelCase     ,  return_tensors='''pt'''      )

     # forward pass
     torch.manual_seed(2      )
     a__   :      Any     =						model(**_UpperCamelCase      )
     a__   :      Dict     =						outputs.last_hidden_state

     # The following Colab Notebook was used to generate these outputs:
     # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb
     if "s16" in checkpoint_url:
          a__   :      Dict     =						torch.tensor([[-1.0915, -1.4876, -1.1809]]      )
     elif "b16" in checkpoint_url:
          a__   :      int     =						torch.tensor([[14.2889, -18.9045, 11.7281]]      )
     elif "l16" in checkpoint_url:
          a__   :      Any     =						torch.tensor([[41.5028, -22.8681, 45.6475]]      )
     elif "b4" in checkpoint_url:
          a__   :      str     =						torch.tensor([[-4.3868, 5.2932, -0.4137]]      )
     else:
          a__   :      Tuple     =						torch.tensor([[-0.1792, -0.6465, 2.4263]]      )

     # verify logits
     assert torch.allclose(last_hidden_state[:, 0, :3]     ,  _UpperCamelCase     ,  atol=1E-4      )

     print(F'Saving model to {pytorch_dump_folder_path}'      )
     model.save_pretrained(_UpperCamelCase      )

     print(F'Saving image processor to {pytorch_dump_folder_path}'      )
     image_processor.save_pretrained(_UpperCamelCase      )


if __name__ == "__main__":
 __SCREAMING_SNAKE_CASE     =						argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--checkpoint_url',
     default='https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar',
     type=str,
     help='URL of the checkpoint you\'d like to convert.',
 )
 parser.add_argument(
     '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.'
 )

 __SCREAMING_SNAKE_CASE     =						parser.parse_args()
 convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)




"""simple docstring"""


from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return math.sqrt(sum(pow(a - b       ,					2 ) for a, b in zip(_UpperCamelCase       ,					_UpperCamelCase ) ) )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			if dataset.ndim != value_array.ndim:
						__lowerCAmelCase							       =     (
						    "Wrong input data's dimensions... "
						    f"dataset : {dataset.ndim}, value_array : {value_array.ndim}"
						)
						raise ValueError(_UpperCamelCase )

			try:
						if dataset.shape[1] != value_array.shape[1]:
									__lowerCAmelCase							       =     (
									    "Wrong input data's shape... "
									    f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"
									)
									raise ValueError(_UpperCamelCase )
			except IndexError:
						if dataset.ndim != value_array.ndim:
									raise TypeError("Wrong shape" )

			if dataset.dtype != value_array.dtype:
						__lowerCAmelCase							       =     (
						    "Input data have different datatype... "
						    f"dataset : {dataset.dtype}, value_array : {value_array.dtype}"
						)
						raise TypeError(_UpperCamelCase )

			__lowerCAmelCase							       =     []

			for value in value_array:
						__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					dataset[0] )
						__lowerCAmelCase							       =     dataset[0].tolist()

						for dataset_value in dataset[1:]:
									__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					_UpperCamelCase )

									if dist > temp_dist:
												__lowerCAmelCase							       =     temp_dist
												__lowerCAmelCase							       =     dataset_value.tolist()

						answer.append([vector, dist] )

			return answer


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return np.dot(_UpperCamelCase       ,					_UpperCamelCase ) / (norm(_UpperCamelCase ) * norm(_UpperCamelCase ))


if __name__ == "__main__":
	import doctest

	doctest.testmod()




