



# This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()

class 		UpperCAmelCase__			(			__UpperCamelCase						,__UpperCamelCase						,__UpperCamelCase						,unittest.TestCase   ):




			'''simple docstring'''






			UpperCamelCase									=       StableDiffusionControlNetImgaImgPipeline
			UpperCamelCase									=       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
			UpperCamelCase									=       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			UpperCamelCase									=       IMAGE_TO_IMAGE_IMAGE_PARAMS.union({"""control_image"""}   )
			UpperCamelCase									=       IMAGE_TO_IMAGE_IMAGE_PARAMS




			def  snake_case__     (   self							: int ):
					'''simple docstring'''





					torch.manual_seed(0 )
					__UpperCAmelCase    :     List[str]													=	UNetaDConditionModel(
					    block_out_channels=(32, 64)       ,       layers_per_block=2       ,       sample_size=32       ,       in_channels=4       ,       out_channels=4       ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,       up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')       ,       cross_attention_dim=32       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     Optional[Any]													=	ControlNetModel(
					    block_out_channels=(32, 64)       ,       layers_per_block=2       ,       in_channels=4       ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,       cross_attention_dim=32       ,       conditioning_embedding_out_channels=(16, 32)       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     Union[str, Any]													=	DDIMScheduler(
					    beta_start=0.0_0_0_8_5       ,       beta_end=0.0_1_2       ,       beta_schedule='''scaled_linear'''       ,       clip_sample=a_       ,       set_alpha_to_one=a_       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     Union[str, Any]													=	AutoencoderKL(
					    block_out_channels=[32, 64]       ,       in_channels=3       ,       out_channels=3       ,       down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,       up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,       latent_channels=4       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     Optional[int]													=	CLIPTextConfig(
					    bos_token_id=0       ,       eos_token_id=2       ,       hidden_size=32       ,       intermediate_size=37       ,       layer_norm_eps=1e-05       ,       num_attention_heads=4       ,       num_hidden_layers=5       ,       pad_token_id=1       ,       vocab_size=10_00       ,       )
					__UpperCAmelCase    :     Tuple													=	CLIPTextModel(a_ )
					__UpperCAmelCase    :     List[str]													=	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

					__UpperCAmelCase    :     Dict													=	{
					    '''unet''': unet,
					    '''controlnet''': controlnet,
					    '''scheduler''': scheduler,
					    '''vae''': vae,
					    '''text_encoder''': text_encoder,
					    '''tokenizer''': tokenizer,
					    '''safety_checker''': None,
					    '''feature_extractor''': None,
					}
					return components




			def  snake_case__     (   self							: Tuple       ,       a_							: Dict       ,       a_							: List[str]=0 ):
					'''simple docstring'''





					if str(a_ ).startswith('''mps''' ):
							__UpperCAmelCase    :     Any													=	torch.manual_seed(a_ )
					else:
							__UpperCAmelCase    :     List[Any]													=	torch.Generator(device=a_ ).manual_seed(a_ )

					__UpperCAmelCase    :     Tuple													=	2
					__UpperCAmelCase    :     Optional[Any]													=	randn_tensor(
					    (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,       generator=a_       ,       device=torch.device(a_ )       ,       )
					__UpperCAmelCase    :     Any													=	floats_tensor(control_image.shape       ,       rng=random.Random(a_ ) ).to(a_ )
					__UpperCAmelCase    :     Union[str, Any]													=	image.cpu().permute(0       ,       2       ,       3       ,       1 )[0]
					__UpperCAmelCase    :     str													=	Image.fromarray(np.uinta(a_ ) ).convert('''RGB''' ).resize((64, 64) )
					__UpperCAmelCase    :     Dict													=	{
					    '''prompt''': '''A painting of a squirrel eating a burger''',
					    '''generator''': generator,
					    '''num_inference_steps''': 2,
					    '''guidance_scale''': 6.0,
					    '''output_type''': '''numpy''',
					    '''image''': image,
					    '''control_image''': control_image,
					}

					return inputs




			def  snake_case__     (   self							: int ):
					'''simple docstring'''





					return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3 )




			@unittest.skipIf(
			    torch_device != '''cuda''' or not is_xformers_available()       ,       reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,       )
			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3 )




			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					self._test_inference_batch_single_identical(expected_max_diff=2e-3 )

class 		UpperCAmelCase__			(			__UpperCamelCase						,__UpperCamelCase						,unittest.TestCase   ):




			'''simple docstring'''






			UpperCamelCase									=       StableDiffusionControlNetImgaImgPipeline
			UpperCamelCase									=       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
			UpperCamelCase									=       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			UpperCamelCase									=       frozenset([]   )  # TO_DO: add image_params once refactored VaeImageProcessor.preprocess




			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					torch.manual_seed(0 )
					__UpperCAmelCase    :     Optional[Any]													=	UNetaDConditionModel(
					    block_out_channels=(32, 64)       ,       layers_per_block=2       ,       sample_size=32       ,       in_channels=4       ,       out_channels=4       ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,       up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')       ,       cross_attention_dim=32       ,       )
					torch.manual_seed(0 )

					def init_weights(a_							: Union[str, Any] ):
							if isinstance(a_       ,       torch.nn.Convad ):
									torch.nn.init.normal(m.weight )
									m.bias.data.fill_(1.0 )

					__UpperCAmelCase    :     Any													=	ControlNetModel(
					    block_out_channels=(32, 64)       ,       layers_per_block=2       ,       in_channels=4       ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,       cross_attention_dim=32       ,       conditioning_embedding_out_channels=(16, 32)       ,       )
					controlneta.controlnet_down_blocks.apply(a_ )

					torch.manual_seed(0 )
					__UpperCAmelCase    :     Union[str, Any]													=	ControlNetModel(
					    block_out_channels=(32, 64)       ,       layers_per_block=2       ,       in_channels=4       ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,       cross_attention_dim=32       ,       conditioning_embedding_out_channels=(16, 32)       ,       )
					controlneta.controlnet_down_blocks.apply(a_ )

					torch.manual_seed(0 )
					__UpperCAmelCase    :     Optional[int]													=	DDIMScheduler(
					    beta_start=0.0_0_0_8_5       ,       beta_end=0.0_1_2       ,       beta_schedule='''scaled_linear'''       ,       clip_sample=a_       ,       set_alpha_to_one=a_       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     Optional[int]													=	AutoencoderKL(
					    block_out_channels=[32, 64]       ,       in_channels=3       ,       out_channels=3       ,       down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,       up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,       latent_channels=4       ,       )
					torch.manual_seed(0 )
					__UpperCAmelCase    :     int													=	CLIPTextConfig(
					    bos_token_id=0       ,       eos_token_id=2       ,       hidden_size=32       ,       intermediate_size=37       ,       layer_norm_eps=1e-05       ,       num_attention_heads=4       ,       num_hidden_layers=5       ,       pad_token_id=1       ,       vocab_size=10_00       ,       )
					__UpperCAmelCase    :     List[str]													=	CLIPTextModel(a_ )
					__UpperCAmelCase    :     List[str]													=	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

					__UpperCAmelCase    :     int													=	MultiControlNetModel([controlneta, controlneta] )

					__UpperCAmelCase    :     List[str]													=	{
					    '''unet''': unet,
					    '''controlnet''': controlnet,
					    '''scheduler''': scheduler,
					    '''vae''': vae,
					    '''text_encoder''': text_encoder,
					    '''tokenizer''': tokenizer,
					    '''safety_checker''': None,
					    '''feature_extractor''': None,
					}
					return components




			def  snake_case__     (   self							: Tuple       ,       a_							: str       ,       a_							: str=0 ):
					'''simple docstring'''





					if str(a_ ).startswith('''mps''' ):
							__UpperCAmelCase    :     Union[str, Any]													=	torch.manual_seed(a_ )
					else:
							__UpperCAmelCase    :     Union[str, Any]													=	torch.Generator(device=a_ ).manual_seed(a_ )

					__UpperCAmelCase    :     Optional[int]													=	2

					__UpperCAmelCase    :     str													=	[
					    randn_tensor(
					        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,       generator=a_       ,       device=torch.device(a_ )       ,       ),
					    randn_tensor(
					        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)       ,       generator=a_       ,       device=torch.device(a_ )       ,       ),
					]

					__UpperCAmelCase    :     Dict													=	floats_tensor(control_image[0].shape       ,       rng=random.Random(a_ ) ).to(a_ )
					__UpperCAmelCase    :     Tuple													=	image.cpu().permute(0       ,       2       ,       3       ,       1 )[0]
					__UpperCAmelCase    :     str													=	Image.fromarray(np.uinta(a_ ) ).convert('''RGB''' ).resize((64, 64) )
					__UpperCAmelCase    :     Union[str, Any]													=	{
					    '''prompt''': '''A painting of a squirrel eating a burger''',
					    '''generator''': generator,
					    '''num_inference_steps''': 2,
					    '''guidance_scale''': 6.0,
					    '''output_type''': '''numpy''',
					    '''image''': image,
					    '''control_image''': control_image,
					}

					return inputs




			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	self.get_dummy_components()
					__UpperCAmelCase    :     Any													=	self.pipeline_class(**a_ )
					pipe.to(a_ )

					__UpperCAmelCase    :     Optional[int]													=	1_0.0
					__UpperCAmelCase    :     List[str]													=	4

					__UpperCAmelCase    :     List[str]													=	self.get_dummy_inputs(a_ )
					__UpperCAmelCase    :     str													=	steps
					__UpperCAmelCase    :     Tuple													=	scale
					__UpperCAmelCase    :     List[str]													=	pipe(**a_ )[0]

					__UpperCAmelCase    :     Any													=	self.get_dummy_inputs(a_ )
					__UpperCAmelCase    :     Tuple													=	steps
					__UpperCAmelCase    :     int													=	scale
					__UpperCAmelCase    :     int													=	pipe(**a_       ,       control_guidance_start=0.1       ,       control_guidance_end=0.2 )[0]

					__UpperCAmelCase    :     List[Any]													=	self.get_dummy_inputs(a_ )
					__UpperCAmelCase    :     Dict													=	steps
					__UpperCAmelCase    :     Union[str, Any]													=	scale
					__UpperCAmelCase    :     Union[str, Any]													=	pipe(**a_       ,       control_guidance_start=[0.1, 0.3]       ,       control_guidance_end=[0.2, 0.7] )[0]

					__UpperCAmelCase    :     Tuple													=	self.get_dummy_inputs(a_ )
					__UpperCAmelCase    :     Optional[Any]													=	steps
					__UpperCAmelCase    :     Dict													=	scale
					__UpperCAmelCase    :     List[str]													=	pipe(**a_       ,       control_guidance_start=0.4       ,       control_guidance_end=[0.5, 0.8] )[0]

					# make sure that all outputs are different
					assert np.sum(np.abs(output_a - output_a ) ) > 1e-3
					assert np.sum(np.abs(output_a - output_a ) ) > 1e-3
					assert np.sum(np.abs(output_a - output_a ) ) > 1e-3




			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3 )




			@unittest.skipIf(
			    torch_device != '''cuda''' or not is_xformers_available()       ,       reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,       )
			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3 )




			def  snake_case__     (   self							: Tuple ):
					'''simple docstring'''





					self._test_inference_batch_single_identical(expected_max_diff=2e-3 )




			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Union[str, Any]													=	self.get_dummy_components()
					__UpperCAmelCase    :     str													=	self.pipeline_class(**a_ )
					pipe.to(a_ )
					pipe.set_progress_bar_config(disable=a_ )
					with tempfile.TemporaryDirectory() as tmpdir:
							try:
									# save_pretrained is not implemented for Multi-ControlNet
									pipe.save_pretrained(a_ )
							except NotImplementedError:
									pass

@slow
@require_torch_gpu
class 		UpperCAmelCase__			(			unittest.TestCase   ):




			'''simple docstring'''






			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()




			def  snake_case__     (   self							: Tuple ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	ControlNetModel.from_pretrained('''lllyasviel/sd-controlnet-canny''' )

					__UpperCAmelCase    :     List[Any]													=	StableDiffusionControlNetImgaImgPipeline.from_pretrained(
					    '''runwayml/stable-diffusion-v1-5'''       ,       safety_checker=a_       ,       controlnet=a_ )
					pipe.enable_model_cpu_offload()
					pipe.set_progress_bar_config(disable=a_ )

					__UpperCAmelCase    :     Tuple													=	torch.Generator(device='''cpu''' ).manual_seed(0 )
					__UpperCAmelCase    :     Dict													=	'''evil space-punk bird'''
					__UpperCAmelCase    :     List[str]													=	load_image(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png''' ).resize((5_12, 5_12) )
					__UpperCAmelCase    :     Optional[Any]													=	load_image(
					    '''https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png''' ).resize((5_12, 5_12) )

					__UpperCAmelCase    :     Optional[int]													=	pipe(
					    a_       ,       a_       ,       control_image=a_       ,       generator=a_       ,       output_type='''np'''       ,       num_inference_steps=50       ,       strength=0.6       ,       )

					__UpperCAmelCase    :     Tuple													=	output.images[0]

					assert image.shape == (5_12, 5_12, 3)

					__UpperCAmelCase    :     List[Any]													=	load_numpy(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy''' )

					assert np.abs(expected_image - image ).max() < 9e-2










import argparse
import json
from pathlib import Path

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

from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig
from transformers.utils import logging


logging.set_verbosity_info()
__A   			=logging.get_logger(__name__)

def        a						(      _UpperCAmelCase							:					int						):







		'''simple docstring'''





		__UpperCAmelCase    :     Optional[Any]													=	SwinConfig(
		    embed_dim=1_92 ,   depths=(2, 2, 18, 2) ,   num_heads=(6, 12, 24, 48) ,   window_size=12 ,   out_features=['''stage2''', '''stage3''', '''stage4'''] ,   )

		__UpperCAmelCase    :     Optional[int]													=	DetaConfig(
		    backbone_config=_UpperCAmelCase ,   num_queries=9_00 ,   encoder_ffn_dim=20_48 ,   decoder_ffn_dim=20_48 ,   num_feature_levels=5 ,   assign_first_stage=_UpperCAmelCase ,   with_box_refine=_UpperCAmelCase ,   two_stage=_UpperCAmelCase ,   )

		# set labels
		__UpperCAmelCase    :     Optional[int]													=	'''huggingface/label-files'''
		if "o365" in model_name:
				__UpperCAmelCase    :     Tuple													=	3_66
				__UpperCAmelCase    :     List[str]													=	'''object365-id2label.json'''
		else:
				__UpperCAmelCase    :     Any													=	91
				__UpperCAmelCase    :     int													=	'''coco-detection-id2label.json'''

		__UpperCAmelCase    :     Optional[int]													=	num_labels
		__UpperCAmelCase    :     List[str]													=	json.load(open(cached_download(hf_hub_url(_UpperCAmelCase ,   _UpperCAmelCase ,   repo_type='''dataset'''						)						) ,   '''r'''						)						)
		__UpperCAmelCase    :     str													=	{int(_UpperCAmelCase						): v for k, v in idalabel.items()}
		__UpperCAmelCase    :     Optional[int]													=	idalabel
		__UpperCAmelCase    :     Tuple													=	{v: k for k, v in idalabel.items()}

		return config

def        a						(      _UpperCAmelCase							:					Dict						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[str]													=	[]

		# stem
		# fmt: off
		rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''')						)
		rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''')						)
		rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''')						)
		rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''')						)
		# stages
		for i in range(len(config.backbone_config.depths						)						):
				for j in range(config.backbone_config.depths[i]						):
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias')						)

				if i < 3:
						rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight')						)
						rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias')						)

		rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''')						)
		rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''')						)
		rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''')						)
		rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''')						)
		rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''')						)
		rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''')						)

		# transformer encoder
		for i in range(config.encoder_layers						):
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight')						)
				rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias')						)

		# transformer decoder
		for i in range(config.decoder_layers						):
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight')						)
				rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias')						)

		# fmt: on

		return rename_keys

def        a						(      _UpperCAmelCase							:					Tuple ,   _UpperCAmelCase							:					str ,   _UpperCAmelCase							:					Union[str, Any]						):







		'''simple docstring'''





		__UpperCAmelCase    :     Optional[int]													=	dct.pop(_UpperCAmelCase						)
		__UpperCAmelCase    :     List[Any]													=	val

def        a						(      _UpperCAmelCase							:					Dict ,   _UpperCAmelCase							:					List[Any]						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[str]													=	[int(backbone_config.embed_dim * 2**i						) for i in range(len(backbone_config.depths						)						)]
		for i in range(len(backbone_config.depths						)						):
				__UpperCAmelCase    :     str													=	num_features[i]
				for j in range(backbone_config.depths[i]						):
						# fmt: off
						# read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias)
						__UpperCAmelCase    :     List[str]													=	state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight'						)
						__UpperCAmelCase    :     List[Any]													=	state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias'						)
						# next, add query, keys and values (in that order) to the state dict
						__UpperCAmelCase    :     Dict													=	in_proj_weight[:dim, :]
						__UpperCAmelCase    :     List[str]													=	in_proj_bias[: dim]
						__UpperCAmelCase    :     str													=	in_proj_weight[
						    dim : dim * 2, :
						]
						__UpperCAmelCase    :     Any													=	in_proj_bias[
						    dim : dim * 2
						]
						__UpperCAmelCase    :     Tuple													=	in_proj_weight[
						    -dim :, :
						]
						__UpperCAmelCase    :     int													=	in_proj_bias[-dim :]
						# fmt: on

def        a						(      _UpperCAmelCase							:					Optional[int] ,   _UpperCAmelCase							:					Any						):







		'''simple docstring'''





		__UpperCAmelCase    :     int													=	config.d_model
		for i in range(config.decoder_layers						):
				# read in weights + bias of input projection layer of self-attention
				__UpperCAmelCase    :     List[str]													=	state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight'						)
				__UpperCAmelCase    :     Tuple													=	state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias'						)
				# next, add query, keys and values (in that order) to the state dict
				__UpperCAmelCase    :     Union[str, Any]													=	in_proj_weight[:hidden_size, :]
				__UpperCAmelCase    :     List[Any]													=	in_proj_bias[:hidden_size]
				__UpperCAmelCase    :     int													=	in_proj_weight[
				    hidden_size : hidden_size * 2, :
				]
				__UpperCAmelCase    :     str													=	in_proj_bias[hidden_size : hidden_size * 2]
				__UpperCAmelCase    :     Tuple													=	in_proj_weight[-hidden_size:, :]
				__UpperCAmelCase    :     Optional[Any]													=	in_proj_bias[-hidden_size:]

def        a						(      ):







		'''simple docstring'''





		__UpperCAmelCase    :     Union[str, Any]													=	'''http://images.cocodataset.org/val2017/000000039769.jpg'''
		__UpperCAmelCase    :     int													=	Image.open(requests.get(_UpperCAmelCase ,   stream=_UpperCAmelCase						).raw						)

		return im

@torch.no_grad()
def        a						(      _UpperCAmelCase							:					str ,   _UpperCAmelCase							:					List[Any] ,   _UpperCAmelCase							:					List[str]						):







		'''simple docstring'''





		__UpperCAmelCase    :     Tuple													=	get_deta_config(_UpperCAmelCase						)

		# load original state dict
		if model_name == "deta-swin-large":
				__UpperCAmelCase    :     Dict													=	hf_hub_download(repo_id='''nielsr/deta-checkpoints''' ,   filename='''adet_swin_ft.pth'''						)
		elif model_name == "deta-swin-large-o365":
				__UpperCAmelCase    :     Any													=	hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' ,   filename='''deta_swin_pt_o365.pth'''						)
		else:
				raise ValueError(f'Model name {model_name} not supported'						)

		__UpperCAmelCase    :     str													=	torch.load(_UpperCAmelCase ,   map_location='''cpu'''						)['''model''']

		# original state dict
		for name, param in state_dict.items():
				print(_UpperCAmelCase ,   param.shape						)

		# rename keys
		__UpperCAmelCase    :     int													=	create_rename_keys(_UpperCAmelCase						)
		for src, dest in rename_keys:
				rename_key(_UpperCAmelCase ,   _UpperCAmelCase ,   _UpperCAmelCase						)
		read_in_swin_q_k_v(_UpperCAmelCase ,   config.backbone_config						)
		read_in_decoder_q_k_v(_UpperCAmelCase ,   _UpperCAmelCase						)

		# fix some prefixes
		for key in state_dict.copy().keys():
				if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key:
						__UpperCAmelCase    :     Optional[Any]													=	state_dict.pop(_UpperCAmelCase						)
						__UpperCAmelCase    :     Union[str, Any]													=	val
				if "input_proj" in key:
						__UpperCAmelCase    :     Union[str, Any]													=	state_dict.pop(_UpperCAmelCase						)
						__UpperCAmelCase    :     List[str]													=	val
				if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key:
						__UpperCAmelCase    :     Union[str, Any]													=	state_dict.pop(_UpperCAmelCase						)
						__UpperCAmelCase    :     Union[str, Any]													=	val

    # finally, create HuggingFace model and load state dict
		__UpperCAmelCase    :     Union[str, Any]													=	DetaForObjectDetection(_UpperCAmelCase						)
		model.load_state_dict(_UpperCAmelCase						)
		model.eval()

		__UpperCAmelCase    :     Optional[int]													=	'''cuda''' if torch.cuda.is_available() else '''cpu'''
		model.to(_UpperCAmelCase						)

		# load image processor
		__UpperCAmelCase    :     str													=	DetaImageProcessor(format='''coco_detection'''						)

		# verify our conversion on image
		__UpperCAmelCase    :     str													=	prepare_img()
		__UpperCAmelCase    :     Optional[int]													=	processor(images=_UpperCAmelCase ,   return_tensors='''pt'''						)
		__UpperCAmelCase    :     List[Any]													=	encoding['''pixel_values''']
		__UpperCAmelCase    :     List[str]													=	model(pixel_values.to(_UpperCAmelCase						)						)

		# verify logits
		print('''Logits:''' ,   outputs.logits[0, :3, :3]						)
		print('''Boxes:''' ,   outputs.pred_boxes[0, :3, :3]						)
		if model_name == "deta-swin-large":
				__UpperCAmelCase    :     str													=	torch.tensor(
				    [[-7.63_08, -2.84_85, -5.37_37], [-7.20_37, -4.55_05, -4.80_27], [-7.29_43, -4.26_11, -4.66_17]]						)
				__UpperCAmelCase    :     Union[str, Any]													=	torch.tensor([[0.49_87, 0.49_69, 0.99_99], [0.25_49, 0.54_98, 0.48_05], [0.54_98, 0.27_57, 0.05_69]]						)
		elif model_name == "deta-swin-large-o365":
				__UpperCAmelCase    :     Optional[Any]													=	torch.tensor(
				    [[-8.01_22, -3.57_20, -4.97_17], [-8.15_47, -3.68_86, -4.63_89], [-7.66_10, -3.61_94, -5.01_34]]						)
				__UpperCAmelCase    :     str													=	torch.tensor([[0.25_23, 0.55_49, 0.48_81], [0.77_15, 0.41_49, 0.46_01], [0.55_03, 0.27_53, 0.05_75]]						)
		assert torch.allclose(outputs.logits[0, :3, :3] ,   expected_logits.to(_UpperCAmelCase						) ,   atol=1e-4						)
		assert torch.allclose(outputs.pred_boxes[0, :3, :3] ,   expected_boxes.to(_UpperCAmelCase						) ,   atol=1e-4						)
		print('''Everything ok!'''						)

		if pytorch_dump_folder_path:
				# Save model and processor
				logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...'						)
				Path(_UpperCAmelCase						).mkdir(exist_ok=_UpperCAmelCase						)
				model.save_pretrained(_UpperCAmelCase						)
				processor.save_pretrained(_UpperCAmelCase						)

		# Push to hub
		if push_to_hub:
				print('''Pushing model and processor to hub...'''						)
				model.push_to_hub(f'jozhang97/{model_name}'						)
				processor.push_to_hub(f'jozhang97/{model_name}'						)


if __name__ == "__main__":
				__A   			=argparse.ArgumentParser()

				parser.add_argument(
				    "--model_name",
				    type=str,
				    default="deta-swin-large",
				    choices=["deta-swin-large", "deta-swin-large-o365"],
				    help="Name of the model you'd like to convert.",
				)
				parser.add_argument(
				    "--pytorch_dump_folder_path",
				    default=None,
				    type=str,
				    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_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)





