



'''simple docstring'''



import argparse
import dataclasses
import json
import logging
import os
import shutil
from typing import List, Optional

import datasets
from accelerate import Accelerator
from datasets import load_dataset
from finetuning import finetune
from tqdm.auto import tqdm

import transformers
from transformers import AutoConfig, set_seed
from transformers.trainer_utils import IntervalStrategy


lowerCAmelCase	:		str				=   logging.getLogger(__name__)

lowerCAmelCase	:		Optional[int]				=   '''pytorch_model.bin'''






@dataclasses.dataclass
class 					UpperCAmelCase__	:
					a      :     Union[str, Any]        =							dataclasses.field(
					    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models."""} )
					a      :     Optional[int]        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co."""}							,       )






@dataclasses.dataclass
class 					UpperCAmelCase__	:
					a      :     Optional[int]        =							dataclasses.field(metadata={"""help""": """A csv or a json file containing the training data."""} )
					a      :     Optional[Any]        =							dataclasses.field(metadata={"""help""": """A csv or a json file containing the data to predict on."""} )
					a      :     str        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """A csv or a json file containing the validation data."""} )
					a      :     List[Any]        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """The name of the task to train on."""}							,       )
					a      :     Optional[int]        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """The list of labels for the task."""} )






@dataclasses.dataclass
class 					UpperCAmelCase__	:
					a      :     List[Any]        =							dataclasses.field(
					    metadata={"""help""": """The output directory where the model predictions and checkpoints will be written."""} )
					a      :     List[Any]        =							dataclasses.field(
					    default="""accuracy"""							,       metadata={"""help""": """The evaluation metric used for the task."""} )
					a      :     Optional[Any]        =							dataclasses.field(
					    default="""no"""							,       metadata={
					        """help""": """The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]"""
					    }							,       )
					a      :     int        =							dataclasses.field(
					    default=1_0							,       metadata={"""help""": """Number of evaluation calls with no improvement after which training will be stopped."""}							,       )
					a      :     List[Any]        =							dataclasses.field(
					    default=0.0							,       metadata={
					        """help""": """How much the specified evaluation metric must improve to satisfy early stopping conditions."""
					    }							,       )
					a      :     Optional[int]        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """Whether to filter the pseudo-labeled data based on the confidence score."""}							,       )
					a      :     int        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """Whether to filter the pseudo-labeled data based on the validation performance."""}							,       )
					a      :     int        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """Whether to fine-tune on labeled data after pseudo training."""}							,       )
					a      :     Any        =							dataclasses.field(
					    default=0.0							,       metadata={"""help""": """Confidence threshold for pseudo-labeled data filtering."""}							,       )
					a      :     Tuple        =							dataclasses.field(
					    default=1_0_0							,       metadata={"""help""": """Number of evaluation calls with no improvement after which training will be stopped."""}							,       )
					a      :     int        =							dataclasses.field(
					    default=lowercase_							,       metadata={"""help""": """Random seed for initialization."""}							,       )



def   __lowerCAmelCase	(							lowerCamelCase   :						Optional[int]				,		lowerCamelCase   :						Any				,		lowerCamelCase   :						Dict				,		lowerCamelCase   :						List[Any]				,		lowerCamelCase   :						Dict				,		lowerCamelCase   :						Tuple					):






	'''simple docstring'''




	__lowerCAmelCase												=  datasets.concatenate_datasets([infer_input, infer_output]				,		axis=1					)

	if args.do_filter_by_confidence:
		__lowerCAmelCase												=  dataset.filter(lambda lowerCamelCase					: example["probability"] > args.confidence_threshold					)

	if args.do_filter_by_val_performance:
		assert eval_result >= 0.0 and eval_result <= 1.0
		__lowerCAmelCase												=  int(eval_result * len(lowerCamelCase_					)					)
		print(lowerCamelCase_					)
		__lowerCAmelCase												=  dataset.sort("probability"				,		reverse=lowerCamelCase_					)
		__lowerCAmelCase												=  dataset.select(range(lowerCamelCase_					)					)

	__lowerCAmelCase												=  dataset.remove_columns(["label", "probability"]					)
	__lowerCAmelCase												=  dataset.rename_column("prediction"				,		"label"					)
	__lowerCAmelCase												=  dataset.map(lambda lowerCamelCase					: {"label": idalabel[example["label"]]}					)
	__lowerCAmelCase												=  dataset.shuffle(seed=args.seed					)

	__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		f'''train_pseudo.{args.data_file_extension}'''					)
	if args.data_file_extension == "csv":
		dataset.to_csv(lowerCamelCase_				,		index=lowerCamelCase_					)
	else:
		dataset.to_json(lowerCamelCase_					)




def   __lowerCAmelCase	(							lowerCamelCase   :						str				,		lowerCamelCase   :						Any				,		lowerCamelCase   :						Optional[Any]				,		lowerCamelCase   :						int				,		**lowerCamelCase   :						Dict					):






	'''simple docstring'''




	__lowerCAmelCase												=  Accelerator()
	# Make one log on every process with the configuration for debugging.
	logging.basicConfig(
	    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"				,		datefmt="%m/%d/%Y %H:%M:%S"				,		level=logging.INFO				,		)
	logger.info(accelerator.state					)

	# Setup logging, we only want one process per machine to log things on the
	# screen. accelerator.is_local_main_process is only True for one process per
	# machine.
	logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR					)

	if accelerator.is_local_main_process:
		datasets.utils.logging.set_verbosity_warning()
		transformers.utils.logging.set_verbosity_info()
	else:
		datasets.utils.logging.set_verbosity_error()
		transformers.utils.logging.set_verbosity_error()

	__lowerCAmelCase												=  STModelArguments(model_name_or_path=lowerCamelCase_					)
	__lowerCAmelCase												=  STDataArguments(train_file=lowerCamelCase_				,		infer_file=lowerCamelCase_					)
	__lowerCAmelCase												=  STTrainingArguments(output_dir=lowerCamelCase_					)
	__lowerCAmelCase												=  argparse.Namespace()

	for arg_class in (model_args, data_args, training_args):
		for key, value in vars(lowerCamelCase_					).items():
			setattr(lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_					)

	for key, value in kwargs.items():
		if hasattr(lowerCamelCase_				,		lowerCamelCase_					):
			setattr(lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_					)

    # Sanity checks
	__lowerCAmelCase												=  {}
	__lowerCAmelCase												=  None

	# You need to provide the training data and the data to predict on
	assert args.train_file is not None
	assert args.infer_file is not None
	__lowerCAmelCase												=  args.train_file
	__lowerCAmelCase												=  args.infer_file

	if args.evaluation_strategy != IntervalStrategy.NO.value:
		assert args.eval_file is not None
		__lowerCAmelCase												=  args.eval_file

	for key in data_files:
		__lowerCAmelCase												=  data_files[key].split("."					)[-1]
		assert extension in ["csv", "json"], f'''`{key}_file` should be a csv or a json file.'''
		if args.data_file_extension is None:
			__lowerCAmelCase												=  extension
		else:
			assert extension == args.data_file_extension, f'''`{key}_file` should be a {args.data_file_extension} file`.'''

	assert (
	    args.eval_metric in datasets.list_metrics()
	), f'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.'''

	# If passed along, set the training seed now.
	if args.seed is not None:
		set_seed(args.seed					)

	logger.info("Creating the initial data directory for self-training..."					)
	__lowerCAmelCase												=  f'''{args.output_dir}/self-train_iter-{{}}'''.format
	__lowerCAmelCase												=  data_dir_format(0					)

	if accelerator.is_main_process:
		if args.output_dir is not None:
			os.makedirs(args.output_dir				,		exist_ok=lowerCamelCase_					)
			os.makedirs(lowerCamelCase_				,		exist_ok=lowerCamelCase_					)
	accelerator.wait_for_everyone()

	__lowerCAmelCase												=  None
	__lowerCAmelCase												=  None
	__lowerCAmelCase												=  0
	__lowerCAmelCase												=  False
	# Show the progress bar
	__lowerCAmelCase												=  tqdm(range(args.max_selftrain_iterations					)				,		disable=not accelerator.is_local_main_process					)

	# Self-train
	for iteration in range(0				,		int(args.max_selftrain_iterations					)					):
		__lowerCAmelCase												=  data_dir_format(lowerCamelCase_					)
		assert os.path.exists(lowerCamelCase_					)

		# Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for
		# iteration > 0
		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"stage-1"					)
		__lowerCAmelCase												=  {
		    """accelerator""": accelerator,
		    """model_name_or_path""": args.model_name_or_path,
		    """cache_dir""": args.cache_dir,
		    """do_train""": True,
		    """train_file""": data_files["""train"""] if iteration == 0 else data_files["""train_pseudo"""],
		    """do_eval""": True if args.eval_file is not None else False,
		    """eval_file""": data_files["""eval"""],
		    """do_predict""": True,
		    """infer_file""": data_files["""infer"""],
		    """task_name""": args.task_name,
		    """label_list""": args.label_list,
		    """output_dir""": current_output_dir,
		    """eval_metric""": args.eval_metric,
		    """evaluation_strategy""": args.evaluation_strategy,
		    """early_stopping_patience""": args.early_stopping_patience,
		    """early_stopping_threshold""": args.early_stopping_threshold,
		    """seed""": args.seed,
		}
		# Add additional training arguments
		for key, value in kwargs.items():
			if key not in arguments_dict and not hasattr(lowerCamelCase_				,		lowerCamelCase_					):
				arguments_dict.update({key: value}					)

		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"best-checkpoint"				,		lowerCamelCase_					)
		if os.path.exists(lowerCamelCase_					):
			logger.info(
			    "Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1."				,		lowerCamelCase_				,		lowerCamelCase_				,		)
		else:
			logger.info("***** Running self-training: iteration: %d, stage: 1 *****"				,		lowerCamelCase_					)
			finetune(**lowerCamelCase_					)
			accelerator.wait_for_everyone()
			assert os.path.exists(lowerCamelCase_					)
			logger.info("Self-training job completed: iteration: %d, stage: 1."				,		lowerCamelCase_					)

		if iteration > 0 and args.finetune_on_labeled_data:
			# Stage 2 (optional): fine-tuning on the original labeled data
			__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"best-checkpoint"					)
			__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"stage-2"					)
			# Update arguments_dict
			__lowerCAmelCase												=  model_path
			__lowerCAmelCase												=  data_files["""train"""]
			__lowerCAmelCase												=  current_output_dir

			__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"best-checkpoint"				,		lowerCamelCase_					)
			if os.path.exists(lowerCamelCase_					):
				logger.info(
				    "Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2."				,		lowerCamelCase_				,		lowerCamelCase_				,		)
			else:
				logger.info("***** Running self-training: iteration: %d, stage: 2 *****"				,		lowerCamelCase_					)
				finetune(**lowerCamelCase_					)
				accelerator.wait_for_everyone()
				assert os.path.exists(lowerCamelCase_					)
				logger.info("Self-training job completed: iteration: %d, stage: 2."				,		lowerCamelCase_					)

		__lowerCAmelCase												=  iteration
		__lowerCAmelCase												=  data_dir_format(iteration + 1					)

		__lowerCAmelCase												=  AutoConfig.from_pretrained(os.path.join(lowerCamelCase_				,		"best-checkpoint"					)					)
		__lowerCAmelCase												=  config.idalabel
		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"eval_results_best-checkpoint.json"					)
		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"test_results_best-checkpoint.json"					)
		assert os.path.exists(lowerCamelCase_					)

		with open(lowerCamelCase_				,		"r"					) as f:
			__lowerCAmelCase												=  float(json.load(lowerCamelCase_					)[args.eval_metric]					)
		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		"infer_output_best-checkpoint.csv"					)
		assert os.path.exists(lowerCamelCase_					)
		# Loading the dataset from local csv or json files.
		__lowerCAmelCase												=  load_dataset(args.data_file_extension				,		data_files={"data": data_files["infer"]}					)["""data"""]
		__lowerCAmelCase												=  load_dataset("csv"				,		data_files={"data": infer_output_file}					)["""data"""]

		if accelerator.is_main_process:
			os.makedirs(lowerCamelCase_				,		exist_ok=lowerCamelCase_					)
			shutil.copy(lowerCamelCase_				,		os.path.join(lowerCamelCase_				,		f'''eval_results_iter-{iteration}.json'''					)					)
			if os.path.exists(lowerCamelCase_					):
				shutil.copy(lowerCamelCase_				,		os.path.join(lowerCamelCase_				,		f'''test_results_iter-{iteration}.json'''					)					)
			create_pseudo_labeled_data(lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_				,		lowerCamelCase_					)
		accelerator.wait_for_everyone()

		__lowerCAmelCase												=  os.path.join(lowerCamelCase_				,		f'''train_pseudo.{args.data_file_extension}'''					)

		if args.evaluation_strategy != IntervalStrategy.NO.value:
			__lowerCAmelCase												=  eval_result

			if best_iteration is None:
				__lowerCAmelCase												=  new_iteration
				__lowerCAmelCase												=  new_eval_result
			else:
				if new_eval_result - best_eval_result > args.early_stopping_threshold:
					__lowerCAmelCase												=  new_iteration
					__lowerCAmelCase												=  new_eval_result
					__lowerCAmelCase												=  0
				else:
					if new_eval_result == best_eval_result:
						__lowerCAmelCase												=  new_iteration
						__lowerCAmelCase												=  new_eval_result
					early_stopping_patience_counter += 1

				if early_stopping_patience_counter >= args.early_stopping_patience:
					__lowerCAmelCase												=  True

		progress_bar.update(1					)

		if should_training_stop:
			break

	if best_iteration is not None:
		# Save the best iteration
		logger.info("Best iteration: %d"				,		lowerCamelCase_					)
		logger.info("Best evaluation result: %s = %f"				,		args.eval_metric				,		lowerCamelCase_					)
		accelerator.wait_for_everyone()
		if accelerator.is_main_process:
			shutil.copy(
			    os.path.join(lowerCamelCase_				,		f'''eval_results_iter-{iteration}.json'''					)				,		os.path.join(lowerCamelCase_				,		"eval_results_best-iteration.json"					)				,		)
	else:
		# Assume that the last iteration is the best
		logger.info("Best iteration: %d"				,		args.max_selftrain_iterations - 1					)
		logger.info("Best evaluation result: %s = %f"				,		args.eval_metric				,		lowerCamelCase_					)
		accelerator.wait_for_everyone()
		if accelerator.is_main_process:
			shutil.copy(
			    os.path.join(lowerCamelCase_				,		f'''eval_results_iter-{args.max_selftrain_iterations - 1}.json'''					)				,		os.path.join(lowerCamelCase_				,		"eval_results_best-iteration.json"					)				,		)




'''simple docstring'''



import argparse
import json

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

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling



def   __lowerCAmelCase					(							lowerCamelCase   :						Any					):






	'''simple docstring'''




	__lowerCAmelCase												=  [2, 2, 6, 2] if "tiny" in model_name else [2, 2, 18, 2]
	__lowerCAmelCase												=  True if "large" in model_name or "huge" in model_name else False
	__lowerCAmelCase												=  True if "large" in model_name or "huge" in model_name else False
	__lowerCAmelCase												=  True if "large" in model_name or "huge" in model_name else False

	if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
		if "fl3" in model_name:
			__lowerCAmelCase												=  [3, 3, 3, 3]
			__lowerCAmelCase												=  [5, 5, 5, 5]
		elif "fl4" in model_name:
			__lowerCAmelCase												=  [4, 4, 4, 4]
			__lowerCAmelCase												=  [3, 3, 3, 3]

	if "tiny" in model_name or "small" in model_name or "base" in model_name:
		__lowerCAmelCase												=  [3, 3, 3, 3]
		if "lrf" in model_name:
			__lowerCAmelCase												=  [3, 3, 3, 3]
		else:
			__lowerCAmelCase												=  [2, 2, 2, 2]

	if "tiny" in model_name:
		__lowerCAmelCase												=  96
	elif "small" in model_name:
		__lowerCAmelCase												=  96
	elif "base" in model_name:
		__lowerCAmelCase												=  1_28
	elif "large" in model_name:
		__lowerCAmelCase												=  1_92
	elif "xlarge" in model_name:
		__lowerCAmelCase												=  2_56
	elif "huge" in model_name:
		__lowerCAmelCase												=  3_52

	# set label information
	__lowerCAmelCase												=  "huggingface/label-files"
	if "large" in model_name or "huge" in model_name:
		__lowerCAmelCase												=  "imagenet-22k-id2label.json"
	else:
		__lowerCAmelCase												=  "imagenet-1k-id2label.json"

	__lowerCAmelCase												=  json.load(open(hf_hub_download(lowerCamelCase				,		lowerCamelCase				,		repo_type="dataset"					)				,		"r"					)					)
	__lowerCAmelCase												=  {int(lowerCamelCase					): v for k, v in idalabel.items()}
	__lowerCAmelCase												=  {v: k for k, v in idalabel.items()}

	__lowerCAmelCase												=  FocalNetConfig(
	    embed_dim=lowerCamelCase				,		depths=lowerCamelCase				,		focal_levels=lowerCamelCase				,		focal_windows=lowerCamelCase				,		use_conv_embed=lowerCamelCase				,		idalabel=lowerCamelCase				,		labelaid=lowerCamelCase				,		use_post_layernorm=lowerCamelCase				,		use_layerscale=lowerCamelCase				,		)

	return config



def   __lowerCAmelCase					(							lowerCamelCase   :						Union[str, Any]					):






	'''simple docstring'''




	if "patch_embed.proj" in name:
		__lowerCAmelCase												=  name.replace("patch_embed.proj"				,		"embeddings.patch_embeddings.projection"					)
	if "patch_embed.norm" in name:
		__lowerCAmelCase												=  name.replace("patch_embed.norm"				,		"embeddings.norm"					)
	if "layers" in name:
		__lowerCAmelCase												=  "encoder." + name
	if "encoder.layers" in name:
		__lowerCAmelCase												=  name.replace("encoder.layers"				,		"encoder.stages"					)
	if "downsample.proj" in name:
		__lowerCAmelCase												=  name.replace("downsample.proj"				,		"downsample.projection"					)
	if "blocks" in name:
		__lowerCAmelCase												=  name.replace("blocks"				,		"layers"					)
	if "modulation.f.weight" in name or "modulation.f.bias" in name:
		__lowerCAmelCase												=  name.replace("modulation.f"				,		"modulation.projection_in"					)
	if "modulation.h.weight" in name or "modulation.h.bias" in name:
		__lowerCAmelCase												=  name.replace("modulation.h"				,		"modulation.projection_context"					)
	if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
		__lowerCAmelCase												=  name.replace("modulation.proj"				,		"modulation.projection_out"					)

	if name == "norm.weight":
		__lowerCAmelCase												=  "layernorm.weight"
	if name == "norm.bias":
		__lowerCAmelCase												=  "layernorm.bias"

	if "head" in name:
		__lowerCAmelCase												=  name.replace("head"				,		"classifier"					)
	else:
		__lowerCAmelCase												=  "focalnet." + name

	return name



def   __lowerCAmelCase					(							lowerCamelCase   :						int				,		lowerCamelCase   :						Any				,		lowerCamelCase   :						Union[str, Any]=False					):






	'''simple docstring'''




	__lowerCAmelCase												=  {
	    "focalnet-tiny": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth",
	    "focalnet-tiny-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth",
	    "focalnet-small": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth",
	    "focalnet-small-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth",
	    "focalnet-base": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth",
	    "focalnet-base-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth",
	    "focalnet-large-lrf-fl3": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth",
	    "focalnet-large-lrf-fl4": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth",
	    "focalnet-xlarge-lrf-fl3": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth",
	    "focalnet-xlarge-lrf-fl4": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth",
	}
	# fmt: on

	__lowerCAmelCase												=  model_name_to_url[model_name]
	print("Checkpoint URL: "				,		lowerCamelCase					)
	__lowerCAmelCase												=  torch.hub.load_state_dict_from_url(lowerCamelCase				,		map_location="cpu"					)["model"]

	# rename keys
	for key in state_dict.copy().keys():
		__lowerCAmelCase												=  state_dict.pop(lowerCamelCase					)
		__lowerCAmelCase												=  val

	__lowerCAmelCase												=  get_focalnet_config(lowerCamelCase					)
	__lowerCAmelCase												=  FocalNetForImageClassification(lowerCamelCase					)
	model.eval()

	# load state dict
	model.load_state_dict(lowerCamelCase					)

	# verify conversion
	__lowerCAmelCase												=  "http://images.cocodataset.org/val2017/000000039769.jpg"

	__lowerCAmelCase												=  BitImageProcessor(
	    do_resize=lowerCamelCase				,		size={"shortest_edge": 2_56}				,		resample=PILImageResampling.BILINEAR				,		do_center_crop=lowerCamelCase				,		crop_size=2_24				,		do_normalize=lowerCamelCase				,		image_mean=lowerCamelCase				,		image_std=lowerCamelCase				,		)
	__lowerCAmelCase												=  Image.open(requests.get(lowerCamelCase				,		stream=lowerCamelCase					).raw					)
	__lowerCAmelCase												=  processor(images=lowerCamelCase				,		return_tensors="pt"					)

	__lowerCAmelCase												=  transforms.Compose(
	    [
	        transforms.Resize(2_56					),
	        transforms.CenterCrop(2_24					),
	        transforms.ToTensor(),
	        transforms.Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6]				,		std=[0.2_2_9, 0.2_2_4, 0.2_2_5]					),
	    ]					)

	__lowerCAmelCase												=  image_transforms(lowerCamelCase					).unsqueeze(0					)

	# verify pixel_values
	assert torch.allclose(inputs.pixel_values				,		lowerCamelCase				,		atol=1e-4					)

	__lowerCAmelCase												=  model(**lowerCamelCase					)

	__lowerCAmelCase												=  outputs.logits.argmax(-1					).item()
	print("Predicted class:"				,		model.config.idalabel[predicted_class_idx]					)

	print("First values of logits:"				,		outputs.logits[0, :3]					)

	if model_name == "focalnet-tiny":
		__lowerCAmelCase												=  torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]					)
	elif model_name == "focalnet-tiny-lrf":
		__lowerCAmelCase												=  torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5]					)
	elif model_name == "focalnet-small":
		__lowerCAmelCase												=  torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1]					)
	elif model_name == "focalnet-small-lrf":
		__lowerCAmelCase												=  torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1]					)
	elif model_name == "focalnet-base":
		__lowerCAmelCase												=  torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0]					)
	elif model_name == "focalnet-base-lrf":
		__lowerCAmelCase												=  torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8]					)
	assert torch.allclose(outputs.logits[0, :3]				,		lowerCamelCase				,		atol=1e-4					)
	print("Looks ok!"					)

	if pytorch_dump_folder_path is not None:
		print(f'''Saving model and processor of {model_name} to {pytorch_dump_folder_path}'''					)
		model.save_pretrained(lowerCamelCase					)
		processor.save_pretrained(lowerCamelCase					)

	if push_to_hub:
		print(f'''Pushing model and processor of {model_name} to the hub...'''					)
		model.push_to_hub(f'''{model_name}'''					)
		processor.push_to_hub(f'''{model_name}'''					)


if __name__ == "__main__":
	lowerCAmelCase	:		Tuple				=   argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    '''--model_name''',
	    default='''focalnet-tiny''',
	    type=str,
	    help='''Name of the FocalNet model 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(
	    '''--push_to_hub''',
	    action='''store_true''',
	    help='''Whether to push the model and processor to the hub.''',
	)

	lowerCAmelCase	:		Optional[int]				=   parser.parse_args()
	convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)