
import unittest
from unittest import skip

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow
from transformers.trainer_utils import set_seed


if is_torch_available():
	import torch

	from transformers import JukeboxModel, JukeboxPrior, JukeboxTokenizer

@require_torch
class        UpperCamelCase__(    unittest.TestCase	):

	"""simple docstring"""

	_A  =		(JukeboxModel,) if is_torch_available() else ()
	_A  =		"openai/jukebox-1b-lyrics"
	_A  =		{
	    "artist": "Zac Brown Band",
	    "genres": "Country",
	    "lyrics": "I met a traveller from an antique land,\n    Who said \"Two vast and trunkless legs of stone\n    Stand in the desert. . . . Near them, on the sand,\n    Half sunk a shattered visage lies, whose frown,\n    And wrinkled lip, and sneer of cold command,\n    Tell that its sculptor well those passions read\n    Which yet survive, stamped on these lifeless things,\n    The hand that mocked them, and the heart that fed;\n    And on the pedestal, these words appear:\n    My name is Ozymandias, King of Kings;\n    Look on my Works, ye Mighty, and despair!\n    Nothing beside remains. Round the decay\n    Of that colossal Wreck, boundless and bare\n    The lone and level sands stretch far away\n    ",
	}
	# fmt: off
	_A  =		[
	    1_8_6_4, 1_5_3_6, 1_2_1_3, 1_8_7_0, 1_3_5_7, 1_5_3_6, 5_1_9, 8_8_0, 1_3_2_3, 7_8_9, 1_0_8_2, 5_3_4,
	    1_0_0_0, 1_4_4_5, 1_1_0_5, 1_1_3_0, 9_6_7, 5_1_5, 1_4_3_4, 1_6_2_0, 5_3_4, 1_4_9_5, 2_8_3, 1_4_4_5,
	    3_3_3, 1_3_0_7, 5_3_9, 1_6_3_1, 1_5_2_8, 3_7_5, 1_4_3_4, 6_7_3, 6_2_7, 7_1_0, 7_7_8, 1_8_8_3,
	    1_4_0_5, 1_2_7_6, 1_4_5_5, 1_2_2_8
	]

	_A  =		[
	    1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
	    6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3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0_136, 0.0_261, 0.0_386, 0.0_497, 0.0_580, 0.0_599, 0.0_583, 0.0_614,
	    0.0_740, 0.0_889, 0.1_023, 0.1_162, 0.1_211, 0.1_212, 0.1_251, 0.1_336,
	    0.1_502, 0.1_686, 0.1_883, 0.2_148, 0.2_363, 0.2_458, 0.2_507, 0.2_531
	]
	_A  =		[
	    0.0_256, -0.0_544, 0.1_600, -0.0_032, 0.1_066, 0.0_825, -0.0_013, 0.3_440,
	    0.0_210, 0.0_412, -0.1_777, -0.0_892, -0.0_164, 0.0_285, -0.0_613, -0.0_617,
	    -0.0_137, -0.0_201, -0.0_175, 0.0_215, -0.0_627, 0.0_520, -0.0_730, 0.0_970,
	    -0.0_100, 0.0_442, -0.0_586, 0.0_207, -0.0_015, -0.0_082
	]
	_A  =		[
	    0.0_415, 0.0_877, 0.0_022, -0.0_055, 0.0_751, 0.0_334, 0.0_324, -0.0_068,
	    0.0_011, 0.0_017, -0.0_676, 0.0_655, -0.0_143, 0.0_399, 0.0_303, 0.0_743,
	    -0.0_168, -0.0_394, -0.1_113, 0.0_124, 0.0_442, 0.0_267, -0.0_003, -0.1_536,
	    -0.0_116, -0.1_837, -0.0_180, -0.1_026, -0.0_777, -0.0_456
	]
	_A  =		[
	    7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_6, 3_8, 3_1, 3_3,
	    4_5, 7_6, 4_1, 3_2, 7_6, 4_5, 4_6, 4_1, 4_0, 3_1, 7_8, 7_6
	]







	def        _a					(      self				:			List[Any] ):
			"""simple docstring"""





			A				=JukeboxTokenizer.from_pretrained(self.model_id )
			A				=tokenizer(**self.metas )["input_ids"]
			return tokens







	@slow
	def        _a					(      self				:			Tuple ):
			"""simple docstring"""





			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()
			A				=self.prepare_inputs()

			set_seed(0 )
			A				=[torch.zeros(1							,			0							,			dtype=torch.long ).cpu() for _ in range(3 )]
			A				=model._sample(snake_case__							,			snake_case__							,			[0]							,			sample_length=40 * model.priors[0].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[0][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2] )

			set_seed(0 )
			A				=model._sample(snake_case__							,			snake_case__							,			[1]							,			sample_length=40 * model.priors[1].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[1][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2] )

			set_seed(0 )
			A				=model._sample(snake_case__							,			snake_case__							,			[2]							,			sample_length=40 * model.priors[2].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[2][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_0, self.EXPECTED_OUTPUT_0_PT_2] )







	@slow
	def        _a					(      self				:			Union[str, Any] ):
			"""simple docstring"""





			A				=False
			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()

			A				=self.prepare_inputs()
			set_seed(0 )
			A				=[torch.zeros(1							,			0							,			dtype=torch.long ) for _ in range(3 )]

			A				=model.priors[0]
			A				=0
			A				=top_prior.get_music_tokens_conds(snake_case__							,			start=snake_case__							,			end=start + top_prior.n_ctx )
			A				=top_prior.get_metadata(labels[0].clone()							,			snake_case__							,			1_05_83_04							,			0 )

			self.assertIsNone(snake_case__ )
			self.assertListEqual(metadata.numpy()[0][:10].tolist()							,			self.EXPECTED_Y_COND )

			A			,    A			,    A				=top_prior.get_cond(snake_case__							,			snake_case__ )
			torch.testing.assert_allclose(
			    audio_conditioning[0][0][:30].detach()							,			torch.tensor(self.EXPECTED_AUDIO_COND )							,			atol=1E-4							,			rtol=1E-4 )
			torch.testing.assert_allclose(
			    metadata_conditioning[0][0][:30].detach()							,			torch.tensor(self.EXPECTED_META_COND )							,			atol=1E-4							,			rtol=1E-4 )
			torch.testing.assert_allclose(
			    lyric_tokens[0, :30].detach()							,			torch.tensor(self.EXPECTED_LYRIC_COND )							,			atol=1E-4							,			rtol=1E-4 )







	@slow
	def        _a					(      self				:			List[str] ):
			"""simple docstring"""





			A				=False

			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()
			set_seed(0 )
			A				=torch.rand((1, 51_20, 1) )
			A				=list(self.prepare_inputs() )

			A				=[model.vqvae.encode(snake_case__							,			start_level=2							,			bs_chunks=waveform.shape[0] )[0], None, None]
			A				=model._sample(
			    snake_case__							,			snake_case__							,			sample_levels=[0]							,			save_results=snake_case__							,			sample_length=40 * model.priors[0].raw_to_tokens )
			torch.testing.assert_allclose(zs[0][0][:40]							,			torch.tensor(self.EXPECTED_PRIMED_0 ) )

			A				=torch.cat((zs[0], torch.zeros(1							,			20_48 - zs[0].shape[-1] ))							,			dim=-1 ).long()
			A				=[upper_a, model.vqvae.encode(snake_case__							,			start_level=1							,			bs_chunks=waveform.shape[0] )[0], None]
			A				=model._sample(
			    snake_case__							,			snake_case__							,			sample_levels=[1]							,			save_results=snake_case__							,			sample_length=40 * model.priors[1].raw_to_tokens )
			torch.testing.assert_allclose(zs[1][0][:40]							,			torch.tensor(self.EXPECTED_PRIMED_1 ) )

			A				=torch.cat((zs[1], torch.zeros(1							,			20_48 - zs[1].shape[-1] ))							,			dim=-1 ).long()
			A				=[upper_a, upper_a, model.vqvae.encode(snake_case__							,			start_level=0							,			bs_chunks=waveform.shape[0] )[0]]
			A				=model._sample(
			    snake_case__							,			snake_case__							,			sample_levels=[2]							,			save_results=snake_case__							,			sample_length=40 * model.priors[2].raw_to_tokens )
			torch.testing.assert_allclose(zs[2][0][:40].cpu()							,			torch.tensor(self.EXPECTED_PRIMED_2 ) )







	@slow
	def        _a					(      self				:			Any ):
			"""simple docstring"""





			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()
			set_seed(0 )
			A				=torch.rand((1, 51_20, 1) )
			with torch.no_grad():
					A				=model.vqvae.encode(snake_case__							,			start_level=2							,			bs_chunks=x.shape[0] )
			torch.testing.assert_allclose(zs[0][0]							,			torch.tensor(self.EXPECTED_VQVAE_ENCODE ) )

			with torch.no_grad():
					A				=model.vqvae.decode(snake_case__							,			start_level=2							,			bs_chunks=x.shape[0] )
			torch.testing.assert_allclose(x[0, :40, 0]							,			torch.tensor(self.EXPECTED_VQVAE_DECODE )							,			atol=1E-4							,			rtol=1E-4 )


@require_torch
class        UpperCamelCase__(    unittest.TestCase	):

	"""simple docstring"""

	_A  =		(JukeboxModel,) if is_torch_available() else ()
	_A  =		"openai/jukebox-5b-lyrics"
	_A  =		{
	    "artist": "Zac Brown Band",
	    "genres": "Country",
	    "lyrics": "I met a traveller from an antique land,\n    Who said \"Two vast and trunkless legs of stone\n    Stand in the desert. . . . Near them, on the sand,\n    Half sunk a shattered visage lies, whose frown,\n    And wrinkled lip, and sneer of cold command,\n    Tell that its sculptor well those passions read\n    Which yet survive, stamped on these lifeless things,\n    The hand that mocked them, and the heart that fed;\n    And on the pedestal, these words appear:\n    My name is Ozymandias, King of Kings;\n    Look on my Works, ye Mighty, and despair!\n    Nothing beside remains. Round the decay\n    Of that colossal Wreck, boundless and bare\n    The lone and level sands stretch far away\n    ",
	}

	# fmt: off
	_A  =		[
	    1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
	    6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
	    6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
	    6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
	    1_4_8_9, 1_4_8_9, 1_4_8_9, 1_4_8_9,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9, 3_4, 1_2_6_8, 1_8_5, 8_9, 3_4, 8_4_2, 1_8_5, 8_9
	]







	def        _a					(      self				:			str							,			snake_case__				:			Tuple ):
			"""simple docstring"""





			A				=JukeboxTokenizer.from_pretrained(snake_case__ )
			A				=tokenizer(**self.metas )["input_ids"]
			return tokens







	@slow
	def        _a					(      self				:			int ):
			"""simple docstring"""





			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()
			A				=self.prepare_inputs(self.model_id )

			set_seed(0 )
			A				=[torch.zeros(1							,			0							,			dtype=torch.long ).cpu() for _ in range(3 )]
			A				=model._sample(snake_case__							,			snake_case__							,			[0]							,			sample_length=60 * model.priors[0].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[0][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2] )

			set_seed(0 )
			A				=model._sample(snake_case__							,			snake_case__							,			[1]							,			sample_length=60 * model.priors[1].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[1][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2] )

			set_seed(0 )
			A				=model._sample(snake_case__							,			snake_case__							,			[2]							,			sample_length=60 * model.priors[2].raw_to_tokens							,			save_results=snake_case__ )
			self.assertIn(zs[2][0].detach().cpu().tolist()							,			[self.EXPECTED_OUTPUT_0, self.EXPECTED_OUTPUT_0_PT_2] )







	@slow
	@skip("Not enough GPU memory on CI runners" )
	def        _a					(      self				:			Any ):
			"""simple docstring"""





			A				=JukeboxModel.from_pretrained(self.model_id							,			min_duration=0 ).eval()
			A				=[i.cuda() for i in self.prepare_inputs(self.model_id )]

			set_seed(0 )
			model.priors[0].cuda()
			A				=[torch.zeros(1							,			0							,			dtype=torch.long ).cuda() for _ in range(3 )]
			A				=model._sample(snake_case__							,			snake_case__							,			[0]							,			sample_length=60 * model.priors[0].raw_to_tokens							,			save_results=snake_case__ )
			torch.testing.assert_allclose(zs[0][0].cpu()							,			torch.tensor(self.EXPECTED_GPU_OUTPUTS_2 ) )
			model.priors[0].cpu()

			set_seed(0 )
			model.priors[1].cuda()
			A				=model._sample(snake_case__							,			snake_case__							,			[1]							,			sample_length=60 * model.priors[1].raw_to_tokens							,			save_results=snake_case__ )
			torch.testing.assert_allclose(zs[1][0].cpu()							,			torch.tensor(self.EXPECTED_GPU_OUTPUTS_1 ) )
			model.priors[1].cpu()

			set_seed(0 )
			model.priors[2].cuda()
			A				=model._sample(snake_case__							,			snake_case__							,			[2]							,			sample_length=60 * model.priors[2].raw_to_tokens							,			save_results=snake_case__ )
			torch.testing.assert_allclose(zs[2][0].cpu()							,			torch.tensor(self.EXPECTED_GPU_OUTPUTS_0 ) )







	@slow
	def        _a					(      self				:			Any ):
			"""simple docstring"""





			A				="ArthurZ/jukebox_prior_0"
			A				=JukeboxPrior.from_pretrained(snake_case__							,			min_duration=0 ).eval().half().to("cuda" )

			A				=self.prepare_inputs(snake_case__ )[0].cuda()
			A				=model.get_metadata(snake_case__							,			0							,			76_80							,			0 )
			set_seed(0 )
			A				=model.sample(1							,			metadata=snake_case__							,			sample_tokens=60 )
			self.assertIn(outputs[0].cpu().tolist()							,			[self.EXPECTED_GPU_OUTPUTS_2, self.EXPECTED_GPU_OUTPUTS_2_PT_2] )






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

def    UpperCamelCase_ (	a_		)     ->Any:
		if "backbone.0.body" in orig_key:
				A				=orig_key.replace("backbone.0.body"  ,				"backbone.conv_encoder.model"		)
		if "transformer" in orig_key:
				A				=orig_key.replace("transformer."  ,				""		)
		if "norm1" in orig_key:
				if "encoder" in orig_key:
						A				=orig_key.replace("norm1"  ,				"self_attn_layer_norm"		)
				else:
						A				=orig_key.replace("norm1"  ,				"encoder_attn_layer_norm"		)
		if "norm2" in orig_key:
				if "encoder" in orig_key:
						A				=orig_key.replace("norm2"  ,				"final_layer_norm"		)
				else:
						A				=orig_key.replace("norm2"  ,				"self_attn_layer_norm"		)
		if "norm3" in orig_key:
				A				=orig_key.replace("norm3"  ,				"final_layer_norm"		)
		if "linear1" in orig_key:
				A				=orig_key.replace("linear1"  ,				"fc1"		)
		if "linear2" in orig_key:
				A				=orig_key.replace("linear2"  ,				"fc2"		)
		if "query_embed" in orig_key:
				A				=orig_key.replace("query_embed"  ,				"query_position_embeddings"		)
		if "cross_attn" in orig_key:
				A				=orig_key.replace("cross_attn"  ,				"encoder_attn"		)

		return orig_key

def    UpperCamelCase_ (	a_		)     ->Dict:
		# transformer decoder self-attention layers
		for i in range(6		):
				# read in weights + bias of input projection layer of self-attention
				A				=state_dict.pop(f'''decoder.layers.{i}.self_attn.in_proj_weight'''		)
				A				=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
				A				=in_proj_weight[:256, :]
				A				=in_proj_bias[:256]
				A				=in_proj_weight[256:512, :]
				A				=in_proj_bias[256:512]
				A				=in_proj_weight[-256:, :]
				A				=in_proj_bias[-256:]

def    UpperCamelCase_ (	)     ->Any:
		A				="http://images.cocodataset.org/val2017/000000039769.jpg"
		A				=Image.open(requests.get(a_  ,				stream=a_		).raw		)

		return im

@torch.no_grad()
def    UpperCamelCase_ (	a_  ,				a_  ,				a_  ,				a_  ,				a_  ,				a_  ,				a_  ,				)     ->Optional[Any]:
		A				=DeformableDetrConfig()
		# set config attributes
		if single_scale:
				A				=1
		A				=dilation
		A				=with_box_refine
		A				=two_stage
		# set labels
		A				=91
		A				="huggingface/label-files"
		A				="coco-detection-id2label.json"
		A				=json.load(open(cached_download(hf_hub_url(a_  ,				a_  ,				repo_type="dataset"		)		)  ,				"r"		)		)
		A				={int(a_		): v for k, v in idalabel.items()}
		A				=idalabel
		A				={v: k for k, v in idalabel.items()}

		# load image processor
		A				=DeformableDetrImageProcessor(format="coco_detection"		)

		# prepare image
		A				=prepare_img()
		A				=image_processor(images=a_  ,				return_tensors="pt"		)
		A				=encoding["pixel_values"]

		logger.info("Converting model..."		)

		# load original state dict
		A				=torch.load(a_  ,				map_location="cpu"		)["model"]
		# rename keys
		for key in state_dict.copy().keys():
				A				=state_dict.pop(a_		)
				A				=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
		A				="model."
		for key in state_dict.copy().keys():
				if not key.startswith("class_embed"		) and not key.startswith("bbox_embed"		):
						A				=state_dict.pop(a_		)
						A				=val
    # finally, create HuggingFace model and load state dict
		A				=DeformableDetrForObjectDetection(a_		)
		model.load_state_dict(a_		)
		model.eval()

		A				="cuda" if torch.cuda.is_available() else "cpu"
		model.to(a_		)
		# verify our conversion
		A				=model(pixel_values.to(a_		)		)

		A				=torch.tensor(
		    [[-9.6645, -4.3449, -5.8705], [-9.7035, -3.8504, -5.0724], [-10.5634, -5.3379, -7.5116]]		)
		A				=torch.tensor([[0.8693, 0.2289, 0.2492], [0.3150, 0.5489, 0.5845], [0.5563, 0.7580, 0.8518]]		)

		if single_scale:
				A				=torch.tensor(
				    [[-9.9051, -4.2541, -6.4852], [-9.6947, -4.0854, -6.8033], [-10.0665, -5.8470, -7.7003]]		)
				A				=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:
				A				=torch.tensor(
				    [[-8.9652, -4.1074, -5.6635], [-9.0596, -4.9447, -6.6075], [-10.1178, -4.5275, -6.2671]]		)
				A				=torch.tensor([[0.7665, 0.4130, 0.4769], [0.8364, 0.1841, 0.3391], [0.6261, 0.3895, 0.7978]]		)

		if with_box_refine:
				A				=torch.tensor(
				    [[-8.8895, -5.4187, -6.8153], [-8.4706, -6.1668, -7.6184], [-9.0042, -5.5359, -6.9141]]		)
				A				=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:
				A				=torch.tensor(
				    [[-6.7108, -4.3213, -6.3777], [-8.9014, -6.1799, -6.7240], [-6.9315, -4.4735, -6.2298]]		)
				A				=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:
				A				="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__":
	__a							  =						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."""
	)
	__a							  =						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,
	)




