


'''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()
__snake_case							   =  logging.get_logger(__name__)


def     a   (    __a				,      __a=False     )						->			int:



					'''simple docstring'''






					UpperCamelCase__     :List[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"
										UpperCamelCase__     :Tuple							      =				[(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   (    __a				,      __a				,      __a=False     )						->			List[Any]:



					'''simple docstring'''






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


def     a   (    __a     )						->			Optional[Any]:



					'''simple docstring'''






					UpperCamelCase__     :List[str]							      =				['''head.weight''', '''head.bias''']
					for k in ignore_keys:
										state_dict.pop(__a				,      __a     )


def     a   (    __a				,      __a				,      __a     )						->			Tuple:



					'''simple docstring'''






					UpperCamelCase__     :str							      =				dct.pop(__a     )
					UpperCamelCase__     :Union[str, Any]							      =				val


def     a   (    )						->			Optional[Any]:



					'''simple docstring'''






					UpperCamelCase__     :Any							      =				'''http://images.cocodataset.org/val2017/000000039769.jpg'''
					UpperCamelCase__     :Optional[Any]							      =				Image.open(requests.get(__a				,      stream=__a     ).raw     )
					return im


@torch.no_grad()
def     a   (    __a				,      __a				,      __a=True     )						->			Dict:



					'''simple docstring'''






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

					# load original model from torch hub
					UpperCamelCase__     :Tuple							      =				torch.hub.load('''facebookresearch/dino:main'''				,      __a     )
					original_model.eval()

					# load state_dict of original model, remove and rename some keys
					UpperCamelCase__     :Any							      =				original_model.state_dict()
					if base_model:
										remove_classification_head_(__a     )
					UpperCamelCase__     :Dict							      =				create_rename_keys(__a				,      base_model=__a     )
					for src, dest in rename_keys:
										rename_key(__a				,      __a				,      __a     )
					read_in_q_k_v(__a				,      __a				,      __a     )

					# load HuggingFace model
					if base_model:
										UpperCamelCase__     :Optional[int]							      =				ViTModel(__a				,      add_pooling_layer=__a     ).eval()
					else:
										UpperCamelCase__     :Union[str, Any]							      =				ViTForImageClassification(__a     ).eval()
					model.load_state_dict(__a     )

					# Check outputs on an image, prepared by ViTImageProcessor
					UpperCamelCase__     :Union[str, Any]							      =				ViTImageProcessor()
					UpperCamelCase__     :Optional[Any]							      =				image_processor(images=prepare_img()				,      return_tensors='''pt'''     )
					UpperCamelCase__     :Dict							      =				encoding['''pixel_values''']
					UpperCamelCase__     :int							      =				model(__a     )

					if base_model:
										UpperCamelCase__     :str							      =				original_model(__a     )
										assert torch.allclose(__a				,      outputs.last_hidden_state[:, 0, :]				,      atol=1e-1     )
					else:
										UpperCamelCase__     :Dict							      =				original_model(__a     )
										assert logits.shape == outputs.logits.shape
										assert torch.allclose(__a				,      outputs.logits				,      atol=1e-3     )

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


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



'''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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


__snake_case							   =  logging.get_logger(__name__)


if is_vision_available():
		import PIL
class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       ['pixel_values']




		def __init__(	self					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = PILImageResampling.BICUBIC					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = True					,     UpperCamelCase_ = 1 / 255					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = True					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							super().__init__(**UpperCamelCase_							)
							UpperCamelCase__     :Tuple							      =				size if size is not None else {'''shortest_edge''': 224}
							UpperCamelCase__     :Optional[int]							      =				get_size_dict(UpperCamelCase_					,     default_to_square=UpperCamelCase_							)
							UpperCamelCase__     :str							      =				crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
							UpperCamelCase__     :Union[str, Any]							      =				get_size_dict(UpperCamelCase_					,     default_to_square=UpperCamelCase_					,     param_name='''crop_size'''							)

							UpperCamelCase__     :Any							      =				do_resize
							UpperCamelCase__     :Union[str, Any]							      =				size
							UpperCamelCase__     :Any							      =				resample
							UpperCamelCase__     :Optional[Any]							      =				do_center_crop
							UpperCamelCase__     :List[str]							      =				crop_size
							UpperCamelCase__     :Optional[int]							      =				do_rescale
							UpperCamelCase__     :Optional[Any]							      =				rescale_factor
							UpperCamelCase__     :Any							      =				do_normalize
							UpperCamelCase__     :int							      =				image_mean if image_mean is not None else OPENAI_CLIP_MEAN
							UpperCamelCase__     :List[str]							      =				image_std if image_std is not None else OPENAI_CLIP_STD
							UpperCamelCase__     :Union[str, Any]							      =				do_convert_rgb




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = PILImageResampling.BICUBIC					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :Union[str, Any]							      =				get_size_dict(UpperCamelCase_					,     default_to_square=UpperCamelCase_							)
							if "shortest_edge" not in size:
												raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''							)
							UpperCamelCase__     :str							      =				get_resize_output_image_size(UpperCamelCase_					,     size=size['''shortest_edge''']					,     default_to_square=UpperCamelCase_							)
							return resize(UpperCamelCase_					,     size=UpperCamelCase_					,     resample=UpperCamelCase_					,     data_format=UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :int							      =				get_size_dict(UpperCamelCase_							)
							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(UpperCamelCase_					,     size=(size['''height'''], size['''width'''])					,     data_format=UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return rescale(UpperCamelCase_					,     scale=UpperCamelCase_					,     data_format=UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return normalize(UpperCamelCase_					,     mean=UpperCamelCase_					,     std=UpperCamelCase_					,     data_format=UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = ChannelDimension.FIRST					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				do_resize if do_resize is not None else self.do_resize
							UpperCamelCase__     :Optional[Any]							      =				size if size is not None else self.size
							UpperCamelCase__     :Optional[int]							      =				get_size_dict(UpperCamelCase_					,     param_name='''size'''					,     default_to_square=UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				resample if resample is not None else self.resample
							UpperCamelCase__     :int							      =				do_center_crop if do_center_crop is not None else self.do_center_crop
							UpperCamelCase__     :Any							      =				crop_size if crop_size is not None else self.crop_size
							UpperCamelCase__     :Any							      =				get_size_dict(UpperCamelCase_					,     param_name='''crop_size'''					,     default_to_square=UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				do_rescale if do_rescale is not None else self.do_rescale
							UpperCamelCase__     :List[Any]							      =				rescale_factor if rescale_factor is not None else self.rescale_factor
							UpperCamelCase__     :Optional[int]							      =				do_normalize if do_normalize is not None else self.do_normalize
							UpperCamelCase__     :Tuple							      =				image_mean if image_mean is not None else self.image_mean
							UpperCamelCase__     :str							      =				image_std if image_std is not None else self.image_std
							UpperCamelCase__     :Optional[Any]							      =				do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

							UpperCamelCase__     :str							      =				make_list_of_images(UpperCamelCase_							)

							if not valid_images(UpperCamelCase_							):
												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.'''							)

							if do_normalize and (image_mean is None or image_std is None):
												raise ValueError('''Image mean and std must be specified if do_normalize is True.'''							)

							# PIL RGBA images are converted to RGB
							if do_convert_rgb:
												UpperCamelCase__     :Any							      =				[convert_to_rgb(UpperCamelCase_							) for image in images]

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

							if do_resize:
												UpperCamelCase__     :Optional[Any]							      =				[self.resize(image=UpperCamelCase_					,     size=UpperCamelCase_					,     resample=UpperCamelCase_							) for image in images]

							if do_center_crop:
												UpperCamelCase__     :Dict							      =				[self.center_crop(image=UpperCamelCase_					,     size=UpperCamelCase_							) for image in images]

							if do_rescale:
												UpperCamelCase__     :Optional[Any]							      =				[self.rescale(image=UpperCamelCase_					,     scale=UpperCamelCase_							) for image in images]

							if do_normalize:
												UpperCamelCase__     :Tuple							      =				[self.normalize(image=UpperCamelCase_					,     mean=UpperCamelCase_					,     std=UpperCamelCase_							) for image in images]

							UpperCamelCase__     :List[str]							      =				[to_channel_dimension_format(UpperCamelCase_					,     UpperCamelCase_							) for image in images]

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