



"""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     UpperCamelCase_   (   lowerCAmelCase__				:    Union[str, Any]   ,       lowerCAmelCase__				:    Union[str, Any]=False						)						->       Any:

  """simple docstring"""




  lowerCAmelCase_       :    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"
    lowerCAmelCase_       :    Optional[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     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[int]   ,       lowerCAmelCase__				:    Optional[int]   ,       lowerCAmelCase__				:    Any=False						)						->       Optional[int]:

  """simple docstring"""




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

def     UpperCamelCase_   (   lowerCAmelCase__				:    Union[str, Any]						)						->       Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_       :    List[Any]								=			['head.weight', 'head.bias']
  for k in ignore_keys:
    state_dict.pop(lowerCAmelCase__   ,       lowerCAmelCase__						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    List[str]						)						->       Any:

  """simple docstring"""




  lowerCAmelCase_       :    Tuple								=			[
      '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(lowerCAmelCase__   ,       lowerCAmelCase__						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    Union[str, Any]   ,       lowerCAmelCase__				:    Any   ,       lowerCAmelCase__				:    Dict						)						->       List[str]:

  """simple docstring"""




  lowerCAmelCase_       :    Union[str, Any]								=			dct.pop(lowerCAmelCase__						)
  lowerCAmelCase_       :    Optional[Any]								=			val

def     UpperCamelCase_   (   lowerCAmelCase__				:    List[str]   ,       lowerCAmelCase__				:    Optional[int]						)						->       Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_       :    Optional[int]								=			ViTMSNConfig()
  lowerCAmelCase_       :    Tuple								=			1000

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

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

  lowerCAmelCase_       :    Union[str, Any]								=			ViTMSNModel(lowerCAmelCase__						)

  lowerCAmelCase_       :    Union[str, Any]								=			torch.hub.load_state_dict_from_url(lowerCAmelCase__   ,       map_location='cpu'						)['target_encoder']

  lowerCAmelCase_       :    Optional[int]								=			ViTImageProcessor(size=config.image_size						)

  remove_projection_head(lowerCAmelCase__						)
  lowerCAmelCase_       :    List[Any]								=			create_rename_keys(lowerCAmelCase__   ,       base_model=lowerCAmelCase__						)

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

  model.load_state_dict(lowerCAmelCase__						)
  model.eval()

  lowerCAmelCase_       :    List[Any]								=			'http://images.cocodataset.org/val2017/000000039769.jpg'

  lowerCAmelCase_       :    str								=			Image.open(requests.get(lowerCAmelCase__   ,       stream=lowerCAmelCase__						).raw						)
  lowerCAmelCase_       :    Dict								=			ViTImageProcessor(
      size=config.image_size   ,       image_mean=lowerCAmelCase__   ,       image_std=lowerCAmelCase__						)
  lowerCAmelCase_       :    Tuple								=			image_processor(images=lowerCAmelCase__   ,       return_tensors='pt'						)

  # forward pass
  torch.manual_seed(2						)
  lowerCAmelCase_       :    List[str]								=			model(**lowerCAmelCase__						)
  lowerCAmelCase_       :    Union[str, Any]								=			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:
    lowerCAmelCase_       :    Optional[Any]								=			torch.tensor([[-1.0915, -1.4876, -1.1809]]						)
  elif "b16" in checkpoint_url:
    lowerCAmelCase_       :    Union[str, Any]								=			torch.tensor([[14.2889, -18.9045, 11.7281]]						)
  elif "l16" in checkpoint_url:
    lowerCAmelCase_       :    Any								=			torch.tensor([[41.5028, -22.8681, 45.6475]]						)
  elif "b4" in checkpoint_url:
    lowerCAmelCase_       :    Union[str, Any]								=			torch.tensor([[-4.3868, 5.2932, -0.4137]]						)
  else:
    lowerCAmelCase_       :    Tuple								=			torch.tensor([[-0.1792, -0.6465, 2.4263]]						)

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

  print(f"Saving model to {pytorch_dump_folder_path}"						)
  model.save_pretrained(lowerCAmelCase__						)

  print(f"Saving image processor to {pytorch_dump_folder_path}"						)
  image_processor.save_pretrained(lowerCAmelCase__						)


if __name__ == "__main__":
     lowercase__		:    List[Any]				   =     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."""
     )

     lowercase__		:    List[str]				   =     parser.parse_args()
     convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)










"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowercase__		:    Tuple				   =     logging.get_logger(__name__)







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					["""pixel_values"""]



      def __init__(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Dict[str, int]] = None   ,      SCREAMING_SNAKE_CASE_    :				PILImageResampling = PILImageResampling.BICUBIC   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Union[int, float] = 1 / 2_5_5   ,      SCREAMING_SNAKE_CASE_    :				Dict[str, int] = None   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[float, List[float]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[float, List[float]]] = None   ,      **SCREAMING_SNAKE_CASE_    :				int   ,      ):
        super().__init__(**SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Dict								=			size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
        lowerCAmelCase_       :    List[Any]								=			get_size_dict(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Tuple								=			crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
        lowerCAmelCase_       :    List[str]								=			get_size_dict(SCREAMING_SNAKE_CASE_   ,      default_to_square=SCREAMING_SNAKE_CASE_   ,      param_name='crop_size'	)

        lowerCAmelCase_       :    List[Any]								=			do_resize
        lowerCAmelCase_       :    Any								=			do_rescale
        lowerCAmelCase_       :    int								=			do_normalize
        lowerCAmelCase_       :    List[str]								=			do_center_crop
        lowerCAmelCase_       :    Dict								=			crop_size
        lowerCAmelCase_       :    Optional[Any]								=			size
        lowerCAmelCase_       :    Tuple								=			resample
        lowerCAmelCase_       :    Optional[int]								=			rescale_factor
        lowerCAmelCase_       :    Optional[Any]								=			image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
        lowerCAmelCase_       :    List[str]								=			image_std if image_std is not None else IMAGENET_DEFAULT_STD



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				np.ndarray   ,      SCREAMING_SNAKE_CASE_    :				Dict[str, int]   ,      SCREAMING_SNAKE_CASE_    :				PILImageResampling = PILImageResampling.BILINEAR   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, ChannelDimension]] = None   ,      **SCREAMING_SNAKE_CASE_    :				str   ,      ):
        lowerCAmelCase_       :    str								=			get_size_dict(SCREAMING_SNAKE_CASE_	)

        if "shortest_edge" in size:
          lowerCAmelCase_       :    Any								=			get_resize_output_image_size(SCREAMING_SNAKE_CASE_   ,      size=size['shortest_edge']   ,      default_to_square=SCREAMING_SNAKE_CASE_	)
          # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
        elif "height" in size and "width" in size:
          lowerCAmelCase_       :    Union[str, Any]								=			(size['height'], size['width'])
        else:
          raise ValueError(F"Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}"	)
        return resize(SCREAMING_SNAKE_CASE_   ,      size=SCREAMING_SNAKE_CASE_   ,      resample=SCREAMING_SNAKE_CASE_   ,      data_format=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				np.ndarray   ,      SCREAMING_SNAKE_CASE_    :				Dict[str, int]   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, ChannelDimension]] = None   ,      **SCREAMING_SNAKE_CASE_    :				int   ,      ):
        lowerCAmelCase_       :    Any								=			get_size_dict(SCREAMING_SNAKE_CASE_	)
        if "height" not in size or "width" not in size:
          raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}"	)
        return center_crop(SCREAMING_SNAKE_CASE_   ,      size=(size['height'], size['width'])   ,      data_format=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int   ,      SCREAMING_SNAKE_CASE_    :				np.ndarray   ,      SCREAMING_SNAKE_CASE_    :				float   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, ChannelDimension]] = None   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]	):
        return rescale(SCREAMING_SNAKE_CASE_   ,      scale=SCREAMING_SNAKE_CASE_   ,      data_format=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				np.ndarray   ,      SCREAMING_SNAKE_CASE_    :				Union[float, List[float]]   ,      SCREAMING_SNAKE_CASE_    :				Union[float, List[float]]   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, ChannelDimension]] = None   ,      **SCREAMING_SNAKE_CASE_    :				str   ,      ):
        return normalize(SCREAMING_SNAKE_CASE_   ,      mean=SCREAMING_SNAKE_CASE_   ,      std=SCREAMING_SNAKE_CASE_   ,      data_format=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				ImageInput   ,      SCREAMING_SNAKE_CASE_    :				Optional[bool] = None   ,      SCREAMING_SNAKE_CASE_    :				Dict[str, int] = None   ,      SCREAMING_SNAKE_CASE_    :				PILImageResampling = None   ,      SCREAMING_SNAKE_CASE_    :				bool = None   ,      SCREAMING_SNAKE_CASE_    :				int = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[bool] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[float] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[bool] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[float, List[float]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[float, List[float]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, TensorType]] = None   ,      SCREAMING_SNAKE_CASE_    :				Union[str, ChannelDimension] = ChannelDimension.FIRST   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      ):
        lowerCAmelCase_       :    Optional[int]								=			do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase_       :    Optional[int]								=			do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase_       :    Optional[Any]								=			do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase_       :    Dict								=			do_center_crop if do_center_crop is not None else self.do_center_crop
        lowerCAmelCase_       :    Dict								=			crop_size if crop_size is not None else self.crop_size
        lowerCAmelCase_       :    Optional[Any]								=			get_size_dict(SCREAMING_SNAKE_CASE_   ,      param_name='crop_size'   ,      default_to_square=SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Union[str, Any]								=			resample if resample is not None else self.resample
        lowerCAmelCase_       :    Optional[Any]								=			rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase_       :    str								=			image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase_       :    List[str]								=			image_std if image_std is not None else self.image_std

        lowerCAmelCase_       :    Tuple								=			size if size is not None else self.size
        lowerCAmelCase_       :    str								=			get_size_dict(SCREAMING_SNAKE_CASE_	)

        if not is_batched(SCREAMING_SNAKE_CASE_	):
          lowerCAmelCase_       :    List[Any]								=			[images]

        if not valid_images(SCREAMING_SNAKE_CASE_	):
          raise ValueError(
              'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
              'torch.Tensor, tf.Tensor or jax.ndarray.'	)

        if do_resize and size is None:
          raise ValueError('Size must be specified if do_resize is True.'	)

        if do_center_crop and crop_size is None:
          raise ValueError('Crop size must be specified if do_center_crop is True.'	)

        if do_rescale and rescale_factor is None:
          raise ValueError('Rescale factor must be specified if do_rescale is True.'	)

        # All transformations expect numpy arrays.
        lowerCAmelCase_       :    str								=			[to_numpy_array(SCREAMING_SNAKE_CASE_	) for image in images]

        if do_resize:
          lowerCAmelCase_       :    Any								=			[self.resize(image=SCREAMING_SNAKE_CASE_   ,      size=SCREAMING_SNAKE_CASE_   ,      resample=SCREAMING_SNAKE_CASE_	) for image in images]

        if do_center_crop:
          lowerCAmelCase_       :    Optional[int]								=			[self.center_crop(image=SCREAMING_SNAKE_CASE_   ,      size=SCREAMING_SNAKE_CASE_	) for image in images]

        if do_rescale:
          lowerCAmelCase_       :    Tuple								=			[self.rescale(image=SCREAMING_SNAKE_CASE_   ,      scale=SCREAMING_SNAKE_CASE_	) for image in images]

        if do_normalize:
          lowerCAmelCase_       :    str								=			[self.normalize(image=SCREAMING_SNAKE_CASE_   ,      mean=SCREAMING_SNAKE_CASE_   ,      std=SCREAMING_SNAKE_CASE_	) for image in images]

        lowerCAmelCase_       :    Any								=			[to_channel_dimension_format(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) for image in images]

        lowerCAmelCase_       :    Optional[Any]								=			{'pixel_values': images}
        return BatchFeature(data=SCREAMING_SNAKE_CASE_   ,      tensor_type=SCREAMING_SNAKE_CASE_	)





