





"""simple docstring"""


import argparse
import json
from pathlib import Path

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

from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


logging.set_verbosity_info()
lowerCamelCase__						= logging.get_logger(__name__)




def        lowercase__     (       lowercase_		,lowercase_=False      )       ->		Optional[Any]:




      """simple docstring"""

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

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

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

            # if just the base model, we should remove "vit" from all keys that start with "vit"
            _UpperCamelCase   :						List[str]    =							[(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        lowercase__     (       lowercase_		,lowercase_		,lowercase_=False      )       ->		List[str]:




      """simple docstring"""

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




def        lowercase__     (       lowercase_      )       ->		int:




      """simple docstring"""

      _UpperCamelCase   :						List[str]    =							["head.weight", "head.bias"]
      for k in ignore_keys:
            state_dict.pop(lowercase_		,lowercase_      )




def        lowercase__     (       lowercase_		,lowercase_		,lowercase_      )       ->		int:




      """simple docstring"""

      _UpperCamelCase   :						Union[str, Any]    =							dct.pop(lowercase_      )
      _UpperCamelCase   :						str    =							val




def        lowercase__     (       )       ->		Union[str, Any]:




      """simple docstring"""

      _UpperCamelCase   :						Optional[int]    =							"http://images.cocodataset.org/val2017/000000039769.jpg"
      _UpperCamelCase   :						Optional[int]    =							Image.open(requests.get(lowercase_		,stream=lowercase_      ).raw      )
      return im




@torch.no_grad()
def        lowercase__     (       lowercase_		,lowercase_		,lowercase_=True      )       ->		Optional[int]:




      """simple docstring"""

      _UpperCamelCase   :						str    =							ViTConfig()
      # patch_size
      if model_name[-1] == "8":
            _UpperCamelCase   :						Tuple    =							8
      # set labels if required
      if not base_model:
            _UpperCamelCase   :						int    =							1_000
            _UpperCamelCase   :						Union[str, Any]    =							"huggingface/label-files"
            _UpperCamelCase   :						str    =							"imagenet-1k-id2label.json"
            _UpperCamelCase   :						Optional[int]    =							json.load(open(hf_hub_download(lowercase_		,lowercase_		,repo_type="dataset"      )		,"r"      )      )
            _UpperCamelCase   :						Union[str, Any]    =							{int(lowercase_      ): v for k, v in idalabel.items()}
            _UpperCamelCase   :						Tuple    =							idalabel
            _UpperCamelCase   :						Any    =							{v: k for k, v in idalabel.items()}
      # size of the architecture
      if model_name in ["dino_vits8", "dino_vits16"]:
            _UpperCamelCase   :						Any    =							384
            _UpperCamelCase   :						int    =							1_536
            _UpperCamelCase   :						List[Any]    =							12
            _UpperCamelCase   :						int    =							6

      # load original model from torch hub
      _UpperCamelCase   :						str    =							torch.hub.load("facebookresearch/dino:main"		,lowercase_      )
      original_model.eval()

      # load state_dict of original model, remove and rename some keys
      _UpperCamelCase   :						str    =							original_model.state_dict()
      if base_model:
            remove_classification_head_(lowercase_      )
      _UpperCamelCase   :						Tuple    =							create_rename_keys(lowercase_		,base_model=lowercase_      )
      for src, dest in rename_keys:
            rename_key(lowercase_		,lowercase_		,lowercase_      )
      read_in_q_k_v(lowercase_		,lowercase_		,lowercase_      )

      # load HuggingFace model
      if base_model:
            _UpperCamelCase   :						Dict    =							ViTModel(lowercase_		,add_pooling_layer=lowercase_      ).eval()
      else:
            _UpperCamelCase   :						Optional[int]    =							ViTForImageClassification(lowercase_      ).eval()
      model.load_state_dict(lowercase_      )

      # Check outputs on an image, prepared by ViTImageProcessor
      _UpperCamelCase   :						Any    =							ViTImageProcessor()
      _UpperCamelCase   :						Dict    =							image_processor(images=prepare_img()		,return_tensors="pt"      )
      _UpperCamelCase   :						Optional[int]    =							encoding["pixel_values"]
      _UpperCamelCase   :						Optional[int]    =							model(lowercase_      )

      if base_model:
            _UpperCamelCase   :						Optional[Any]    =							original_model(lowercase_      )
            assert torch.allclose(lowercase_		,outputs.last_hidden_state[:, 0, :]		,atol=1e-1      )
      else:
            _UpperCamelCase   :						Dict    =							original_model(lowercase_      )
            assert logits.shape == outputs.logits.shape
            assert torch.allclose(lowercase_		,outputs.logits		,atol=1e-3      )

      Path(lowercase_      ).mkdir(exist_ok=lowercase_      )
      print(F'''Saving model {model_name} to {pytorch_dump_folder_path}'''      )
      model.save_pretrained(lowercase_      )
      print(F'''Saving image processor to {pytorch_dump_folder_path}'''      )
      image_processor.save_pretrained(lowercase_      )


if __name__ == "__main__":
  lowerCamelCase__						= argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      "--model_name",
      default="dino_vitb16",
      type=str,
      help="Name of the model trained with DINO 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(
      "--base_model",
      action="store_true",
      help="Whether to only convert the base model (no projection head weights).",
  )

  parser.set_defaults(base_model=True)
  lowerCamelCase__						= parser.parse_args()
  convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)







"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import GLPNImageProcessor



class   __SCREAMING_SNAKE_CASE     (    unittest.TestCase ):







  '''simple docstring'''


  def __init__(		self :							Any       , __a :							Any       , __a :							Optional[Any]=7       , __a :							Tuple=3       , __a :							int=18       , __a :							Optional[Any]=30       , __a :							Optional[Any]=400       , __a :							List[str]=True       , __a :							Any=32       , __a :							Tuple=True       , )		->					List[str]:
        _UpperCamelCase   :						List[str]    =							parent
        _UpperCamelCase   :						int    =							batch_size
        _UpperCamelCase   :						List[str]    =							num_channels
        _UpperCamelCase   :						Union[str, Any]    =							image_size
        _UpperCamelCase   :						Optional[Any]    =							min_resolution
        _UpperCamelCase   :						Union[str, Any]    =							max_resolution
        _UpperCamelCase   :						Optional[Any]    =							do_resize
        _UpperCamelCase   :						str    =							size_divisor
        _UpperCamelCase   :						Optional[Any]    =							do_rescale


  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Union[str, Any]:
        return {
            "do_resize": self.do_resize,
            "size_divisor": self.size_divisor,
            "do_rescale": self.do_rescale,
        }





@require_torch
@require_vision
class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase		,	unittest.TestCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:Any						  =       GLPNImageProcessor if is_vision_available() else None

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Any:
        _UpperCamelCase   :						Optional[Any]    =							GLPNImageProcessingTester(self						)

  @property
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					Dict:
        return self.image_processor_tester.prepare_image_processor_dict()

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Union[str, Any]:
        _UpperCamelCase   :						int    =							self.image_processing_class(**self.image_processor_dict						)
        self.assertTrue(hasattr(__a       , "do_resize"						)						)
        self.assertTrue(hasattr(__a       , "size_divisor"						)						)
        self.assertTrue(hasattr(__a       , "resample"						)						)
        self.assertTrue(hasattr(__a       , "do_rescale"						)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[Any]:
        pass

  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Optional[Any]:
        # Initialize image_processing
        _UpperCamelCase   :						Optional[Any]    =							self.image_processing_class(**self.image_processor_dict						)
        # create random PIL images
        _UpperCamelCase   :						List[str]    =							prepare_image_inputs(self.image_processor_tester       , equal_resolution=__a						)
        for image in image_inputs:
              self.assertIsInstance(__a       , Image.Image						)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        _UpperCamelCase   :						Optional[int]    =							image_processing(image_inputs[0]       , return_tensors="pt"						).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Dict:
        # Initialize image_processing
        _UpperCamelCase   :						Optional[int]    =							self.image_processing_class(**self.image_processor_dict						)
        # create random numpy tensors
        _UpperCamelCase   :						List[str]    =							prepare_image_inputs(self.image_processor_tester       , equal_resolution=__a       , numpify=__a						)
        for image in image_inputs:
              self.assertIsInstance(__a       , np.ndarray						)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        _UpperCamelCase   :						Dict    =							image_processing(image_inputs[0]       , return_tensors="pt"						).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)


  def  __SCREAMING_SNAKE_CASE					(		self :							Any						)		->					Union[str, Any]:
        # Initialize image_processing
        _UpperCamelCase   :						Optional[int]    =							self.image_processing_class(**self.image_processor_dict						)
        # create random PyTorch tensors
        _UpperCamelCase   :						Dict    =							prepare_image_inputs(self.image_processor_tester       , equal_resolution=__a       , torchify=__a						)
        for image in image_inputs:
              self.assertIsInstance(__a       , torch.Tensor						)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        _UpperCamelCase   :						Optional[int]    =							image_processing(image_inputs[0]       , return_tensors="pt"						).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)
