





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







def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase=False ) ->				Any:



			"""simple docstring"""


			__snake_case					:      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"
						__snake_case					:      Dict							      =     [(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    ,	_lowerCamelCase    ,	_lowerCamelCase=False ) ->				Tuple:



			"""simple docstring"""


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







def 					_a						(       _lowerCamelCase ) ->				Dict:



			"""simple docstring"""


			__snake_case					:      Union[str, Any]							      =     ["""head.weight""", """head.bias"""]
			for k in ignore_keys:
						state_dict.pop(_lowerCamelCase    ,	_lowerCamelCase )







def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				str:



			"""simple docstring"""


			__snake_case					:      Dict							      =     dct.pop(_lowerCamelCase )
			__snake_case					:      List[Any]							      =     val







def 					_a						(       ) ->				Tuple:



			"""simple docstring"""


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







@torch.no_grad()
def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase=True ) ->				str:



			"""simple docstring"""


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

			# load original model from torch hub
			__snake_case					:      List[str]							      =     torch.hub.load("""facebookresearch/dino:main"""    ,	_lowerCamelCase )
			original_model.eval()

			# load state_dict of original model, remove and rename some keys
			__snake_case					:      str							      =     original_model.state_dict()
			if base_model:
						remove_classification_head_(_lowerCamelCase )
			__snake_case					:      str							      =     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    ,	_lowerCamelCase )

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

			# Check outputs on an image, prepared by ViTImageProcessor
			__snake_case					:      List[str]							      =     ViTImageProcessor()
			__snake_case					:      Tuple							      =     image_processor(images=prepare_img()    ,	return_tensors="""pt""" )
			__snake_case					:      Any							      =     encoding["""pixel_values"""]
			__snake_case					:      Optional[int]							      =     model(_lowerCamelCase )

			if base_model:
						__snake_case					:      List[Any]							      =     original_model(_lowerCamelCase )
						assert torch.allclose(_lowerCamelCase    ,	outputs.last_hidden_state[:, 0, :]    ,	atol=1E-1 )
			else:
						__snake_case					:      Dict							      =     original_model(_lowerCamelCase )
						assert logits.shape == outputs.logits.shape
						assert torch.allclose(_lowerCamelCase    ,	outputs.logits    ,	atol=1E-3 )

			Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase )
			print(F'''Saving model {model_name} 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__":
	__UpperCamelCase					    =	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)
	__UpperCamelCase					    =	parser.parse_args()
	convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)









'''simple docstring'''






from sklearn.metrics import matthews_corrcoef

import datasets


__UpperCamelCase					    =	"\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction.  The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n"

__UpperCamelCase					    =	"\nArgs:\n    predictions (list of int): Predicted labels, as returned by a model.\n    references (list of int): Ground truth labels.\n    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n    matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n    Example 1, a basic example with only predictions and references as inputs:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.54\n\n    Example 2, the same example as above, but also including sample weights:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.1\n\n    Example 3, the same example as above, but with sample weights that cause a negative correlation:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])\n        >>> print(round(results['matthews_correlation'], 2))\n        -0.25\n"

__UpperCamelCase					    =	"\\n@article{scikit-learn,\n  title={Scikit-learn: Machine Learning in {P}ython},\n  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n  journal={Journal of Machine Learning Research},\n  volume={12},\n  pages={2825--2830},\n  year={2011}\n}\n"




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						,					_KWARGS_DESCRIPTION		)
class  _A (					datasets.Metric		):

		def 	lowercase__     (   self					:   Tuple			)					->    Dict:


					"""simple docstring"""

					return datasets.MetricInfo(
					    description=_DESCRIPTION  ,      citation=_CITATION  ,      inputs_description=_KWARGS_DESCRIPTION  ,      features=datasets.Features(
					        {
					            """predictions""": datasets.Value("""int32"""			),
					            """references""": datasets.Value("""int32"""			),
					        }			)  ,      reference_urls=[
					        """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html"""
					    ]  ,      )

		def 	lowercase__     (   self					:   List[Any]  ,      __magic_name__					:   Tuple  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   Union[str, Any]=None			)					->    Optional[int]:


					"""simple docstring"""

					return {
					    "matthews_correlation": float(matthews_corrcoef(__magic_name__  ,      __magic_name__  ,      sample_weight=__magic_name__			)			),
					}


