

"""simple docstring"""





import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_url
from PIL import Image

from transformers import DeformableDetrConfig, DeformableDetrForObjectDetection, DeformableDetrImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
_SCREAMING_SNAKE_CASE							:	Any										=			logging.get_logger(__name__)



def 							_lowerCAmelCase   (    UpperCAmelCase  :				List[Any]					):


					'''simple docstring'''






					if "backbone.0.body" in orig_key:
										UpperCamelCase__		:     List[Any]								=orig_key.replace('''backbone.0.body'''   ,				'''backbone.conv_encoder.model'''					)
					if "transformer" in orig_key:
										UpperCamelCase__		:     int								=orig_key.replace('''transformer.'''   ,				''''''					)
					if "norm1" in orig_key:
										if "encoder" in orig_key:
															UpperCamelCase__		:     int								=orig_key.replace('''norm1'''   ,				'''self_attn_layer_norm'''					)
										else:
															UpperCamelCase__		:     Optional[int]								=orig_key.replace('''norm1'''   ,				'''encoder_attn_layer_norm'''					)
					if "norm2" in orig_key:
										if "encoder" in orig_key:
															UpperCamelCase__		:     Optional[int]								=orig_key.replace('''norm2'''   ,				'''final_layer_norm'''					)
										else:
															UpperCamelCase__		:     Tuple								=orig_key.replace('''norm2'''   ,				'''self_attn_layer_norm'''					)
					if "norm3" in orig_key:
										UpperCamelCase__		:     Optional[int]								=orig_key.replace('''norm3'''   ,				'''final_layer_norm'''					)
					if "linear1" in orig_key:
										UpperCamelCase__		:     int								=orig_key.replace('''linear1'''   ,				'''fc1'''					)
					if "linear2" in orig_key:
										UpperCamelCase__		:     Optional[Any]								=orig_key.replace('''linear2'''   ,				'''fc2'''					)
					if "query_embed" in orig_key:
										UpperCamelCase__		:     Dict								=orig_key.replace('''query_embed'''   ,				'''query_position_embeddings'''					)
					if "cross_attn" in orig_key:
										UpperCamelCase__		:     List[Any]								=orig_key.replace('''cross_attn'''   ,				'''encoder_attn'''					)

					return orig_key



def 							_lowerCAmelCase   (    UpperCAmelCase  :				int					):


					'''simple docstring'''






					for i in range(6					):
										# read in weights + bias of input projection layer of self-attention
										UpperCamelCase__		:     Tuple								=state_dict.pop(F'''decoder.layers.{i}.self_attn.in_proj_weight'''					)
										UpperCamelCase__		:     Dict								=state_dict.pop(F'''decoder.layers.{i}.self_attn.in_proj_bias'''					)
										# next, add query, keys and values (in that order) to the state dict
										UpperCamelCase__		:     int								=in_proj_weight[:256, :]
										UpperCamelCase__		:     int								=in_proj_bias[:256]
										UpperCamelCase__		:     str								=in_proj_weight[256:512, :]
										UpperCamelCase__		:     Dict								=in_proj_bias[256:512]
										UpperCamelCase__		:     Optional[Any]								=in_proj_weight[-256:, :]
										UpperCamelCase__		:     Optional[int]								=in_proj_bias[-256:]



def 							_lowerCAmelCase   (    ):


					'''simple docstring'''






					UpperCamelCase__		:     Dict								="http://images.cocodataset.org/val2017/000000039769.jpg"
					UpperCamelCase__		:     Tuple								=Image.open(requests.get(A_   ,				stream=A_					).raw					)

					return im



@torch.no_grad()
def 							_lowerCAmelCase   (    UpperCAmelCase  :				Optional[int]   ,				UpperCAmelCase  :				str   ,				UpperCAmelCase  :				Optional[Any]   ,				UpperCAmelCase  :				Union[str, Any]   ,				UpperCAmelCase  :				List[Any]   ,				UpperCAmelCase  :				str   ,				UpperCAmelCase  :				Optional[int]   ,				):


					'''simple docstring'''






					UpperCamelCase__		:     Optional[int]								=DeformableDetrConfig()
					# set config attributes
					if single_scale:
										UpperCamelCase__		:     Union[str, Any]								=1
					UpperCamelCase__		:     Dict								=dilation
					UpperCamelCase__		:     Dict								=with_box_refine
					UpperCamelCase__		:     Dict								=two_stage
					# set labels
					UpperCamelCase__		:     int								=91
					UpperCamelCase__		:     str								="huggingface/label-files"
					UpperCamelCase__		:     Union[str, Any]								="coco-detection-id2label.json"
					UpperCamelCase__		:     Tuple								=json.load(open(cached_download(hf_hub_url(A_   ,				A_   ,				repo_type='''dataset'''					)					)   ,				'''r'''					)					)
					UpperCamelCase__		:     Dict								={int(A_					): v for k, v in idalabel.items()}
					UpperCamelCase__		:     int								=idalabel
					UpperCamelCase__		:     List[Any]								={v: k for k, v in idalabel.items()}

					# load image processor
					UpperCamelCase__		:     Union[str, Any]								=DeformableDetrImageProcessor(format='''coco_detection'''					)

					# prepare image
					UpperCamelCase__		:     Tuple								=prepare_img()
					UpperCamelCase__		:     Dict								=image_processor(images=A_   ,				return_tensors='''pt'''					)
					UpperCamelCase__		:     Any								=encoding["pixel_values"]

					logger.info('''Converting model...'''					)

					# load original state dict
					UpperCamelCase__		:     Any								=torch.load(A_   ,				map_location='''cpu'''					)["model"]
					# rename keys
					for key in state_dict.copy().keys():
										UpperCamelCase__		:     Union[str, Any]								=state_dict.pop(A_					)
										UpperCamelCase__		:     int								=val
					# query, key and value matrices need special treatment
					read_in_q_k_v(A_					)
					# important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them
					UpperCamelCase__		:     List[Any]								="model."
					for key in state_dict.copy().keys():
										if not key.startswith('''class_embed'''					) and not key.startswith('''bbox_embed'''					):
															UpperCamelCase__		:     str								=state_dict.pop(A_					)
															UpperCamelCase__		:     List[Any]								=val
										# finally, create HuggingFace model and load state dict
					UpperCamelCase__		:     Optional[int]								=DeformableDetrForObjectDetection(A_					)
					model.load_state_dict(A_					)
					model.eval()

					UpperCamelCase__		:     Optional[int]								="cuda" if torch.cuda.is_available() else "cpu"
					model.to(A_					)
					# verify our conversion
					UpperCamelCase__		:     Union[str, Any]								=model(pixel_values.to(A_					)					)

					UpperCamelCase__		:     Optional[Any]								=torch.tensor(
					    [[-9.6645, -4.3449, -5.8705], [-9.7035, -3.8504, -5.0724], [-10.5_634, -5.3379, -7.5116]]					)
					UpperCamelCase__		:     Dict								=torch.tensor([[0.8693, 0.2289, 0.2492], [0.3150, 0.5489, 0.5845], [0.5563, 0.7580, 0.8518]]					)

					if single_scale:
										UpperCamelCase__		:     Tuple								=torch.tensor(
										    [[-9.9051, -4.2541, -6.4852], [-9.6947, -4.0854, -6.8033], [-10.0_665, -5.8470, -7.7003]]					)
										UpperCamelCase__		:     List[Any]								=torch.tensor([[0.7292, 0.4991, 0.5532], [0.7959, 0.2426, 0.4236], [0.7582, 0.3518, 0.4451]]					)

					if single_scale and dilation:
										UpperCamelCase__		:     Optional[int]								=torch.tensor(
										    [[-8.9652, -4.1074, -5.6635], [-9.0596, -4.9447, -6.6075], [-10.1_178, -4.5275, -6.2671]]					)
										UpperCamelCase__		:     Optional[int]								=torch.tensor([[0.7665, 0.4130, 0.4769], [0.8364, 0.1841, 0.3391], [0.6261, 0.3895, 0.7978]]					)

					if with_box_refine:
										UpperCamelCase__		:     Optional[int]								=torch.tensor(
										    [[-8.8895, -5.4187, -6.8153], [-8.4706, -6.1668, -7.6184], [-9.0042, -5.5359, -6.9141]]					)
										UpperCamelCase__		:     Optional[int]								=torch.tensor([[0.7828, 0.2208, 0.4323], [0.0892, 0.5996, 0.1319], [0.5524, 0.6389, 0.8914]]					)

					if with_box_refine and two_stage:
										UpperCamelCase__		:     str								=torch.tensor(
										    [[-6.7108, -4.3213, -6.3777], [-8.9014, -6.1799, -6.7240], [-6.9315, -4.4735, -6.2298]]					)
										UpperCamelCase__		:     Optional[int]								=torch.tensor([[0.2583, 0.5499, 0.4683], [0.7652, 0.9068, 0.4882], [0.5490, 0.2763, 0.0564]]					)

					print('''Logits:'''   ,				outputs.logits[0, :3, :3]					)

					assert torch.allclose(outputs.logits[0, :3, :3]   ,				expected_logits.to(A_					)   ,				atol=1E-4					)
					assert torch.allclose(outputs.pred_boxes[0, :3, :3]   ,				expected_boxes.to(A_					)   ,				atol=1E-4					)

					print('''Everything ok!'''					)

					# Save model and image processor
					logger.info(F'''Saving PyTorch model and image processor to {pytorch_dump_folder_path}...'''					)
					Path(A_					).mkdir(exist_ok=A_					)
					model.save_pretrained(A_					)
					image_processor.save_pretrained(A_					)

					# Push to hub
					if push_to_hub:
										UpperCamelCase__		:     Tuple								="deformable-detr"
										model_name += "-single-scale" if single_scale else ""
										model_name += "-dc5" if dilation else ""
										model_name += "-with-box-refine" if with_box_refine else ""
										model_name += "-two-stage" if two_stage else ""
										print('''Pushing model to hub...'''					)
										model.push_to_hub(repo_path_or_name=A_   ,				organization='''nielsr'''   ,				commit_message='''Add model'''					)


if __name__ == "__main__":
				_SCREAMING_SNAKE_CASE							:	int										=			argparse.ArgumentParser()

				parser.add_argument(
				    """--checkpoint_path""",
				    type=str,
				    default="""/home/niels/checkpoints/deformable_detr/r50_deformable_detr-checkpoint.pth""",
				    help="""Path to Pytorch checkpoint (.pth file) you'd like to convert.""",
				)
				parser.add_argument("""--single_scale""", action="""store_true""", help="""Whether to set config.num_features_levels = 1.""")
				parser.add_argument("""--dilation""", action="""store_true""", help="""Whether to set config.dilation=True.""")
				parser.add_argument("""--with_box_refine""", action="""store_true""", help="""Whether to set config.with_box_refine=True.""")
				parser.add_argument("""--two_stage""", action="""store_true""", help="""Whether to set config.two_stage=True.""")
				parser.add_argument(
				    """--pytorch_dump_folder_path""",
				    default=None,
				    type=str,
				    required=True,
				    help="""Path to the folder to output PyTorch model.""",
				)
				parser.add_argument(
				    """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
				)
				_SCREAMING_SNAKE_CASE							:	int										=			parser.parse_args()
				convert_deformable_detr_checkpoint(
				    args.checkpoint_path,
				    args.single_scale,
				    args.dilation,
				    args.with_box_refine,
				    args.two_stage,
				    args.pytorch_dump_folder_path,
				    args.push_to_hub,
				)






"""simple docstring"""







import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


__lowercase          = logging.getLogger(__name__)






@dataclass
class 		_A    :







    """simple docstring"""




    UpperCAmelCase			:				str      =			field(
        metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}    )
    UpperCAmelCase			:				Optional[str]      =			field(
        default=_a       ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}    )
    UpperCAmelCase			:				Optional[str]      =			field(
        default=_a       ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}    )
    UpperCAmelCase			:				bool      =			field(default=_a       ,metadata={"""help""": """Set this flag to use fast tokenization."""}    )
    # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
    # or just modify its tokenizer_config.json.
    UpperCAmelCase			:				Optional[str]      =			field(
        default=_a       ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}       ,)






def 	lowercase     (							)-> Dict:
  '''simple docstring'''







  a		:						Tuple 				=     HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)			)

  if len(sys.argv			) == 2 and sys.argv[1].endswith(".json"			):
    # If we pass only one argument to the script and it's the path to a json file,
    # let's parse it to get our arguments.
    a				,						a				,						a		:						Any 				=     parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]			)			)
  else:
    a				,						a				,						a		:						List[str] 				=     parser.parse_args_into_dataclasses()

  if (
      os.path.exists(training_args.output_dir			)
      and os.listdir(training_args.output_dir			)
      and training_args.do_train
      and not training_args.overwrite_output_dir
  ):
    raise ValueError(
        F'''Output directory ({training_args.output_dir}) already exists and is not empty. Use'''
        " --overwrite_output_dir to overcome."			)

  # Setup logging
  logging.basicConfig(
      format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"    ,  datefmt="%m/%d/%Y %H:%M:%S"    ,  level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN    ,  )
  logger.warning(
      "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"    ,  training_args.local_rank    ,  training_args.device    ,  training_args.n_gpu    ,  bool(training_args.local_rank != -1			)    ,  training_args.fpaa    ,  )
  # Set the verbosity to info of the Transformers logger (on main process only):
  if is_main_process(training_args.local_rank			):
    transformers.utils.logging.set_verbosity_info()
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()
  logger.info("Training/evaluation parameters %s"    ,  A_			)

  # Prepare Question-Answering task
  # Load pretrained model and tokenizer
  #
  # Distributed training:
  # The .from_pretrained methods guarantee that only one local process can concurrently
  # download model & vocab.

  a		:						str 				=     AutoConfig.from_pretrained(
      model_args.config_name if model_args.config_name else model_args.model_name_or_path    ,  cache_dir=model_args.cache_dir    ,  )
  a		:						str 				=     AutoTokenizer.from_pretrained(
      model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path    ,  cache_dir=model_args.cache_dir    ,  use_fast=A_    ,  )
  a		:						List[Any] 				=     AutoModelForQuestionAnswering.from_pretrained(
      model_args.model_name_or_path    ,  from_tf=bool(".ckpt" in model_args.model_name_or_path			)    ,  config=A_    ,  cache_dir=model_args.cache_dir    ,  )

  # Get datasets
  a		:						int 				=     hasattr(model.config    ,  "lang2id"			)
  a		:						Any 				=     (
      SquadDataset(
          A_    ,  tokenizer=A_    ,  is_language_sensitive=A_    ,  cache_dir=model_args.cache_dir			)
      if training_args.do_train
      else None
  )
  a		:						Tuple 				=     (
      SquadDataset(
          A_    ,  tokenizer=A_    ,  mode="dev"    ,  is_language_sensitive=A_    ,  cache_dir=model_args.cache_dir    ,  )
      if training_args.do_eval
      else None
  )

  # Data collator
  a		:						str 				=     DataCollatorWithPadding(A_    ,  pad_to_multiple_of=8			) if training_args.fpaa else None

  # Initialize our Trainer
  a		:						Tuple 				=     Trainer(
      model=A_    ,  args=A_    ,  train_dataset=A_    ,  eval_dataset=A_    ,  data_collator=A_    ,  )

  # Training
  if training_args.do_train:
    trainer.train(
        model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path			) else None			)
    trainer.save_model()
    # For convenience, we also re-save the tokenizer to the same directory,
    # so that you can share your model easily on huggingface.co/models =)
    if trainer.is_world_master():
      tokenizer.save_pretrained(training_args.output_dir			)


def 	lowercase     (							A_			)-> int:
  '''simple docstring'''







  main()


if __name__ == "__main__":
 main()






