


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 ConvNextConfig, ConvNextForImageClassification, ConvNextImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
snake_case__  				=			logging.get_logger(__name__)
def 			__SCREAMING_SNAKE_CASE							(					lowercase__	:str					)							->	Any:
						__magic_name__					:				int				=   ConvNextConfig()

						if "tiny" in checkpoint_url:
												__magic_name__					:				List[str]				=   [3, 3, 9, 3]
												__magic_name__					:				int				=   [9_6, 1_9_2, 3_8_4, 7_6_8]
						if "small" in checkpoint_url:
												__magic_name__					:				Tuple				=   [3, 3, 2_7, 3]
												__magic_name__					:				int				=   [9_6, 1_9_2, 3_8_4, 7_6_8]
						if "base" in checkpoint_url:
												__magic_name__					:				Optional[Any]				=   [3, 3, 2_7, 3]
												__magic_name__					:				str				=   [1_2_8, 2_5_6, 5_1_2, 1_0_2_4]
						if "large" in checkpoint_url:
												__magic_name__					:				List[Any]				=   [3, 3, 2_7, 3]
												__magic_name__					:				Optional[Any]				=   [1_9_2, 3_8_4, 7_6_8, 1_5_3_6]
						if "xlarge" in checkpoint_url:
												__magic_name__					:				int				=   [3, 3, 2_7, 3]
												__magic_name__					:				Union[str, Any]				=   [2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8]

						if "1k" in checkpoint_url:
												__magic_name__					:				int				=   1_0_0_0
												__magic_name__					:				int				=   'imagenet-1k-id2label.json'
												__magic_name__					:				str				=   (1, 1_0_0_0)
						else:
												__magic_name__					:				Tuple				=   2_1_8_4_1
												__magic_name__					:				Optional[int]				=   'imagenet-22k-id2label.json'
												__magic_name__					:				Tuple				=   (1, 2_1_8_4_1)

						__magic_name__					:				str				=   'huggingface/label-files'
						__magic_name__					:				Dict				=   num_labels
						__magic_name__					:				List[Any]				=   json.load(open(hf_hub_download(lowercase__   ,    lowercase__   ,    repo_type='dataset'					)   ,    'r'					)					)
						__magic_name__					:				Optional[int]				=   {int(lowercase__					): v for k, v in idalabel.items()}
						if "1k" not in checkpoint_url:
												# this dataset contains 21843 labels but the model only has 21841
												# we delete the classes as mentioned in https://github.com/google-research/big_transfer/issues/18
												del idalabel[9_2_0_5]
												del idalabel[1_5_0_2_7]
						__magic_name__					:				str				=   idalabel
						__magic_name__					:				Tuple				=   {v: k for k, v in idalabel.items()}
						__magic_name__					:				str				=   hidden_sizes
						__magic_name__					:				Optional[int]				=   depths

						return config, expected_shape
def 			__SCREAMING_SNAKE_CASE							(					lowercase__	:Any					)							->	Dict:
						if "downsample_layers.0.0" in name:
												__magic_name__					:				Optional[int]				=   name.replace('downsample_layers.0.0'   ,    'embeddings.patch_embeddings'					)
						if "downsample_layers.0.1" in name:
												__magic_name__					:				List[str]				=   name.replace('downsample_layers.0.1'   ,    'embeddings.norm'					)  # we rename to layernorm later on
						if "downsample_layers.1.0" in name:
												__magic_name__					:				str				=   name.replace('downsample_layers.1.0'   ,    'stages.1.downsampling_layer.0'					)
						if "downsample_layers.1.1" in name:
												__magic_name__					:				List[Any]				=   name.replace('downsample_layers.1.1'   ,    'stages.1.downsampling_layer.1'					)
						if "downsample_layers.2.0" in name:
												__magic_name__					:				Optional[int]				=   name.replace('downsample_layers.2.0'   ,    'stages.2.downsampling_layer.0'					)
						if "downsample_layers.2.1" in name:
												__magic_name__					:				Dict				=   name.replace('downsample_layers.2.1'   ,    'stages.2.downsampling_layer.1'					)
						if "downsample_layers.3.0" in name:
												__magic_name__					:				Any				=   name.replace('downsample_layers.3.0'   ,    'stages.3.downsampling_layer.0'					)
						if "downsample_layers.3.1" in name:
												__magic_name__					:				Dict				=   name.replace('downsample_layers.3.1'   ,    'stages.3.downsampling_layer.1'					)
						if "stages" in name and "downsampling_layer" not in name:
												# stages.0.0. for instance should be renamed to stages.0.layers.0.
												__magic_name__					:				Optional[Any]				=   name[: len('stages.0'					)] + '.layers' + name[len('stages.0'					) :]
						if "stages" in name:
												__magic_name__					:				Any				=   name.replace('stages'   ,    'encoder.stages'					)
						if "norm" in name:
												__magic_name__					:				List[str]				=   name.replace('norm'   ,    'layernorm'					)
						if "gamma" in name:
												__magic_name__					:				Optional[Any]				=   name.replace('gamma'   ,    'layer_scale_parameter'					)
						if "head" in name:
												__magic_name__					:				List[Any]				=   name.replace('head'   ,    'classifier'					)

						return name
def 			__SCREAMING_SNAKE_CASE							(					)							->	int:
						__magic_name__					:				str				=   'http://images.cocodataset.org/val2017/000000039769.jpg'
						__magic_name__					:				List[Any]				=   Image.open(requests.get(lowercase__   ,    stream=lowercase__					).raw					)
						return im
@torch.no_grad()
def 			__SCREAMING_SNAKE_CASE							(					lowercase__	:str   ,    lowercase__	:Dict					)							->	Union[str, Any]:
						__magic_name__		,			__magic_name__					:				Tuple				=   get_convnext_config(lowercase__					)
						# load original state_dict from URL
						__magic_name__					:				List[Any]				=   torch.hub.load_state_dict_from_url(lowercase__					)['model']
						# rename keys
						for key in state_dict.copy().keys():
												__magic_name__					:				int				=   state_dict.pop(lowercase__					)
												__magic_name__					:				List[str]				=   val
						# add prefix to all keys expect classifier head
						for key in state_dict.copy().keys():
												__magic_name__					:				str				=   state_dict.pop(lowercase__					)
												if not key.startswith('classifier'					):
																		__magic_name__					:				List[Any]				=   'convnext.' + key
												__magic_name__					:				str				=   val

						# load HuggingFace model
						__magic_name__					:				Any				=   ConvNextForImageClassification(lowercase__					)
						model.load_state_dict(lowercase__					)
						model.eval()

						# Check outputs on an image, prepared by ConvNextImageProcessor
						__magic_name__					:				str				=   2_2_4 if '224' in checkpoint_url else 3_8_4
						__magic_name__					:				List[Any]				=   ConvNextImageProcessor(size=lowercase__					)
						__magic_name__					:				Optional[int]				=   image_processor(images=prepare_img()   ,    return_tensors='pt'					).pixel_values

						__magic_name__					:				Any				=   model(lowercase__					).logits

						# note: the logits below were obtained without center cropping
						if checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_tiny_1k_224_ema.pth":
												__magic_name__					:				str				=   torch.tensor([-0.1_2_1_0, -0.6_6_0_5, 0.1_9_1_8]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_small_1k_224_ema.pth":
												__magic_name__					:				List[str]				=   torch.tensor([-0.4_4_7_3, -0.1_8_4_7, -0.6_3_6_5]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_1k_224_ema.pth":
												__magic_name__					:				Union[str, Any]				=   torch.tensor([0.4_5_2_5, 0.7_5_3_9, 0.0_3_0_8]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_1k_384.pth":
												__magic_name__					:				Optional[Any]				=   torch.tensor([0.3_5_6_1, 0.6_3_5_0, -0.0_3_8_4]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_1k_224_ema.pth":
												__magic_name__					:				List[Any]				=   torch.tensor([0.4_1_7_4, -0.0_9_8_9, 0.1_4_8_9]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_1k_384.pth":
												__magic_name__					:				List[str]				=   torch.tensor([0.2_5_1_3, -0.1_3_4_9, -0.1_6_1_3]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_224.pth":
												__magic_name__					:				List[Any]				=   torch.tensor([1.2_9_8_0, 0.3_6_3_1, -0.1_1_9_8]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_224.pth":
												__magic_name__					:				Any				=   torch.tensor([1.2_9_6_3, 0.1_2_2_7, 0.1_7_2_3]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_224.pth":
												__magic_name__					:				Tuple				=   torch.tensor([1.7_9_5_6, 0.8_3_9_0, 0.2_8_2_0]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_1k_224.pth":
												__magic_name__					:				Optional[int]				=   torch.tensor([-0.2_8_2_2, -0.0_5_0_2, -0.0_8_7_8]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_1k_384.pth":
												__magic_name__					:				List[Any]				=   torch.tensor([-0.5_6_7_2, -0.0_7_3_0, -0.4_3_4_8]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_1k_224.pth":
												__magic_name__					:				Union[str, Any]				=   torch.tensor([0.2_6_8_1, 0.2_3_6_5, 0.6_2_4_6]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_1k_384.pth":
												__magic_name__					:				Tuple				=   torch.tensor([-0.2_6_4_2, 0.3_9_3_1, 0.5_1_1_6]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_1k_224_ema.pth":
												__magic_name__					:				Dict				=   torch.tensor([-0.6_6_7_7, -0.1_8_7_3, -0.8_3_7_9]					)
						elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_1k_384_ema.pth":
												__magic_name__					:				Dict				=   torch.tensor([-0.7_7_4_9, -0.2_9_6_7, -0.6_4_4_4]					)
						else:
												raise ValueError(f"Unknown URL: {checkpoint_url}"					)

						assert torch.allclose(logits[0, :3]   ,    lowercase__   ,    atol=1e-3					)
						assert logits.shape == expected_shape

						Path(lowercase__					).mkdir(exist_ok=lowercase__					)
						print(f"Saving model to {pytorch_dump_folder_path}"					)
						model.save_pretrained(lowercase__					)
						print(f"Saving image processor to {pytorch_dump_folder_path}"					)
						image_processor.save_pretrained(lowercase__					)

						print('Pushing model to the hub...'					)
						__magic_name__					:				List[Any]				=   'convnext'
						if "tiny" in checkpoint_url:
												model_name += "-tiny"
						elif "small" in checkpoint_url:
												model_name += "-small"
						elif "base" in checkpoint_url:
												model_name += "-base"
						elif "xlarge" in checkpoint_url:
												model_name += "-xlarge"
						elif "large" in checkpoint_url:
												model_name += "-large"
						if "224" in checkpoint_url:
												model_name += "-224"
						elif "384" in checkpoint_url:
												model_name += "-384"
						if "22k" in checkpoint_url and "1k" not in checkpoint_url:
												model_name += "-22k"
						if "22k" in checkpoint_url and "1k" in checkpoint_url:
												model_name += "-22k-1k"

						model.push_to_hub(
						    repo_path_or_name=Path(lowercase__   ,    lowercase__					)   ,    organization='nielsr'   ,    commit_message='Add model'   ,    )


if __name__ == "__main__":
			snake_case__  				=			argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    '--checkpoint_url',
			    default='https://dl.fbaipublicfiles.com/convnext/convnext_tiny_1k_224_ema.pth',
			    type=str,
			    help='URL of the original ConvNeXT checkpoint you\'d like to convert.',
			)
			parser.add_argument(
			    '--pytorch_dump_folder_path',
			    default=None,
			    type=str,
			    required=True,
			    help='Path to the output PyTorch model directory.',
			)

			snake_case__  				=			parser.parse_args()
			convert_convnext_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)






import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
			import sqlitea

			import sqlalchemy


snake_case__  				=			datasets.utils.logging.get_logger(__name__)



@dataclass
class      snake_case__	(   datasets.BuilderConfig	):
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   True
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   None
				__lowerCAmelCase		=   10_000
				__lowerCAmelCase		=   None
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										if self.sql is None:
																raise ValueError('sql must be specified'   )
										if self.con is None:
																raise ValueError('con must be specified'   )
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase  ,   _UpperCAmelCase = None  ,   ):




										"""simple docstring"""




										__magic_name__					:				Optional[int]				=   config_kwargs.copy()
										# We need to stringify the Selectable object to make its hash deterministic

										# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
										__magic_name__					:				Union[str, Any]				=   config_kwargs['sql']
										if not isinstance(_UpperCAmelCase  ,   _UpperCAmelCase   ):
																if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																						import sqlalchemy

																						if isinstance(_UpperCAmelCase  ,   sqlalchemy.sql.Selectable   ):
																												__magic_name__					:				List[Any]				=   sqlalchemy.create_engine(config_kwargs['con'].split('://'   )[0] + '://'   )
																												__magic_name__					:				Any				=   str(sql.compile(dialect=engine.dialect   )   )
																												__magic_name__					:				str				=   sql_str
																						else:
																												raise TypeError(
																												    F"Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(_UpperCAmelCase   )}: {sql}"   )
																else:
																						raise TypeError(
																						    F"Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(_UpperCAmelCase   )}: {sql}"   )
										__magic_name__					:				List[Any]				=   config_kwargs['con']
										if not isinstance(_UpperCAmelCase  ,   _UpperCAmelCase   ):
																__magic_name__					:				Dict				=   id(_UpperCAmelCase   )
																logger.info(
																    F"SQL connection 'con' of type {type(_UpperCAmelCase   )} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."   )

										return super().create_config_id(_UpperCAmelCase  ,   custom_features=_UpperCAmelCase   )





				@property
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				str				=   {
										    'index_col': self.index_col,
										    'columns': self.columns,
										    'params': self.params,
										    'coerce_float': self.coerce_float,
										    'parse_dates': self.parse_dates,
										}
										return pd_read_sql_kwargs






class      snake_case__	(   datasets.ArrowBasedBuilder	):
				__lowerCAmelCase		=   SqlConfig
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										return datasets.DatasetInfo(features=self.config.features   )
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,   gen_kwargs={}   )]
				def 			UpperCamelCase_    (	self  ,   _UpperCAmelCase   ):




										"""simple docstring"""




										if self.config.features is not None:
																__magic_name__					:				List[Any]				=   self.config.features.arrow_schema
																if all(not require_storage_cast(_UpperCAmelCase   ) for feature in self.config.features.values()   ):
																						# cheaper cast
																						__magic_name__					:				Dict				=   pa.Table.from_arrays([pa_table[field.name] for field in schema]  ,   schema=_UpperCAmelCase   )
																else:
																						# more expensive cast; allows str <-> int/float or str to Audio for example
																						__magic_name__					:				List[Any]				=   table_cast(_UpperCAmelCase  ,   _UpperCAmelCase   )
										return pa_table





				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				int				=   self.config.chunksize
										__magic_name__					:				Tuple				=   pd.read_sql(
										    self.config.sql  ,   self.config.con  ,   chunksize=_UpperCAmelCase  ,   **self.config.pd_read_sql_kwargs   )
										__magic_name__					:				Dict				=   [sql_reader] if chunksize is None else sql_reader
										for chunk_idx, df in enumerate(_UpperCAmelCase   ):
																__magic_name__					:				Optional[Any]				=   pa.Table.from_pandas(_UpperCAmelCase   )
																yield chunk_idx, self._cast_table(_UpperCAmelCase   )


