





'''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()
lowercase__   :				Optional[int]      =   logging.get_logger(__name__)






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






def 	_lowerCAmelCase      (  __snake_case  :			Union[str, Any]  )					->					List[Any]:
     __A						:			Dict								=	['head.weight', 'head.bias']
     for k in ignore_keys:
          state_dict.pop(__snake_case			,							__snake_case  )






def 	_lowerCAmelCase      (  __snake_case  :			Union[str, Any]			,							__snake_case  :			Tuple			,							__snake_case  :			int  )					->					List[str]:
     __A						:			int								=	dct.pop(__snake_case  )
     __A						:			int								=	val






def 	_lowerCAmelCase      (  )					->					Tuple:
     __A						:			Dict								=	'http://images.cocodataset.org/val2017/000000039769.jpg'
     __A						:			Union[str, Any]								=	Image.open(requests.get(__snake_case			,							stream=__snake_case  ).raw  )
     return im






@torch.no_grad()
def 	_lowerCAmelCase      (  __snake_case  :			Dict			,							__snake_case  :			List[str]			,							__snake_case  :			List[Any]=True  )					->					List[Any]:
     __A						:			Dict								=	ViTConfig()
     # patch_size
     if model_name[-1] == "8":
          __A						:			List[Any]								=	8
     # set labels if required
     if not base_model:
          __A						:			List[Any]								=	10_00
          __A						:			Any								=	'huggingface/label-files'
          __A						:			List[str]								=	'imagenet-1k-id2label.json'
          __A						:			List[Any]								=	json.load(open(hf_hub_download(__snake_case			,							__snake_case			,							repo_type='dataset'  )			,							'r'  )  )
          __A						:			List[str]								=	{int(__snake_case  ): v for k, v in idalabel.items()}
          __A						:			Optional[int]								=	idalabel
          __A						:			int								=	{v: k for k, v in idalabel.items()}
     # size of the architecture
     if model_name in ["dino_vits8", "dino_vits16"]:
          __A						:			Dict								=	3_84
          __A						:			List[str]								=	15_36
          __A						:			int								=	12
          __A						:			Optional[Any]								=	6

     # load original model from torch hub
     __A						:			Dict								=	torch.hub.load('facebookresearch/dino:main'			,							__snake_case  )
     original_model.eval()

     # load state_dict of original model, remove and rename some keys
     __A						:			List[Any]								=	original_model.state_dict()
     if base_model:
          remove_classification_head_(__snake_case  )
     __A						:			Optional[Any]								=	create_rename_keys(__snake_case			,							base_model=__snake_case  )
     for src, dest in rename_keys:
          rename_key(__snake_case			,							__snake_case			,							__snake_case  )
     read_in_q_k_v(__snake_case			,							__snake_case			,							__snake_case  )

     # load HuggingFace model
     if base_model:
          __A						:			Any								=	ViTModel(__snake_case			,							add_pooling_layer=__snake_case  ).eval()
     else:
          __A						:			int								=	ViTForImageClassification(__snake_case  ).eval()
     model.load_state_dict(__snake_case  )

     # Check outputs on an image, prepared by ViTImageProcessor
     __A						:			Optional[Any]								=	ViTImageProcessor()
     __A						:			Optional[Any]								=	image_processor(images=prepare_img()			,							return_tensors='pt'  )
     __A						:			List[Any]								=	encoding['pixel_values']
     __A						:			Dict								=	model(__snake_case  )

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

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


if __name__ == "__main__":
  lowercase__   :				Tuple      =   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)
  lowercase__   :				Dict      =   parser.parse_args()
  convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)






'''simple docstring'''




import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''):
  lowercase__   :				Dict      =   {
      '''linear''': PIL.Image.Resampling.BILINEAR,
      '''bilinear''': PIL.Image.Resampling.BILINEAR,
      '''bicubic''': PIL.Image.Resampling.BICUBIC,
      '''lanczos''': PIL.Image.Resampling.LANCZOS,
      '''nearest''': PIL.Image.Resampling.NEAREST,
  }
else:
  lowercase__   :				Any      =   {
      '''linear''': PIL.Image.LINEAR,
      '''bilinear''': PIL.Image.BILINEAR,
      '''bicubic''': PIL.Image.BICUBIC,
      '''lanczos''': PIL.Image.LANCZOS,
      '''nearest''': PIL.Image.NEAREST,
  }






def 	_lowerCAmelCase      (  __snake_case  :			Any      )					->					Optional[Any]:
     __A						:			Dict								=	(images / 2 + 0.5).clamp(0			,							1      )
     __A						:			str								=	images.cpu().permute(0			,							2			,							3			,							1      ).float().numpy()
     __A						:			Dict								=	numpy_to_pil(__snake_case      )
     return images







def 	_lowerCAmelCase      (  __snake_case  :			List[Any]      )					->					Optional[Any]:
     if images.ndim == 3:
          __A						:			List[Any]								=	images[None, ...]
     __A						:			List[str]								=	(images * 2_55).round().astype('uint8'      )
     if images.shape[-1] == 1:
          # special case for grayscale (single channel) images
          __A						:			str								=	[Image.fromarray(image.squeeze()			,							mode='L'      ) for image in images]
     else:
          __A						:			str								=	[Image.fromarray(__snake_case      ) for image in images]

     return pil_images