






import argparse
import collections

import jax
import jax.numpy as jnp
import torch
import torch.nn as nn
from clip.model import CLIP
from flax.training import checkpoints
from huggingface_hub import Repository

from transformers import (
    CLIPTokenizer,
    OwlViTConfig,
    OwlViTForObjectDetection,
    OwlViTImageProcessor,
    OwlViTModel,
    OwlViTProcessor,
)


lowerCamelCase			:							int       ={
    '''vit_b32''': {
        '''embed_dim''': 512,
        '''image_resolution''': 768,
        '''context_length''': 16,
        '''vocab_size''': 49408,
        '''vision_layers''': 12,
        '''vision_width''': 768,
        '''vision_patch_size''': 32,
        '''transformer_width''': 512,
        '''transformer_heads''': 8,
        '''transformer_layers''': 12,
    },
    '''vit_b16''': {
        '''embed_dim''': 512,
        '''image_resolution''': 768,
        '''context_length''': 16,
        '''vocab_size''': 49408,
        '''vision_layers''': 12,
        '''vision_width''': 768,
        '''vision_patch_size''': 16,
        '''transformer_width''': 512,
        '''transformer_heads''': 8,
        '''transformer_layers''': 12,
    },
    '''vit_l14''': {
        '''embed_dim''': 768,
        '''image_resolution''': 840,
        '''context_length''': 16,
        '''vocab_size''': 49408,
        '''vision_layers''': 24,
        '''vision_width''': 1024,
        '''vision_patch_size''': 14,
        '''transformer_width''': 768,
        '''transformer_heads''': 12,
        '''transformer_layers''': 12,
    },
}







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase=""      ,					__lowerCAmelCase="/"    )				->			List[str]:
     UpperCamelCase__    :     int        =						[]

     for k, v in params.items():
          UpperCamelCase__    :     Optional[int]        =						parent_key + sep + k if parent_key else k

          if isinstance(UpperCamelCase__      ,					collections.MutableMapping    ):
               items.extend(flatten_nested_dict(UpperCamelCase__      ,					UpperCamelCase__      ,					sep=UpperCamelCase__    ).items()    )
          else:
               items.append((new_key, v)    )
     return dict(UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase    )				->			Optional[int]:
     return jax.tree_util.tree_map(lambda __lowerCAmelCase    : x.astype(jnp.floataa    ) if x.dtype == jnp.bfloataa else x      ,					UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Optional[Any]:
     UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__    :     str        =						pt_attn_layer.in_proj_weight.chunk(3      ,					dim=0    )
     UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__    :     int        =						pt_attn_layer.in_proj_bias.chunk(3      ,					dim=0    )

     UpperCamelCase__    :     Optional[int]        =						pt_attn_layer.out_proj.weight
     UpperCamelCase__    :     str        =						pt_attn_layer.out_proj.bias

     UpperCamelCase__    :     Tuple        =						q_proj
     UpperCamelCase__    :     int        =						q_proj_bias

     UpperCamelCase__    :     Optional[Any]        =						k_proj
     UpperCamelCase__    :     List[str]        =						k_proj_bias

     UpperCamelCase__    :     Optional[int]        =						v_proj
     UpperCamelCase__    :     List[str]        =						v_proj_bias

     UpperCamelCase__    :     str        =						out_proj_weights
     UpperCamelCase__    :     Union[str, Any]        =						out_proj_bias







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			List[Any]:
     copy_linear(hf_mlp.fca      ,					pt_mlp.c_fc    )
     copy_linear(hf_mlp.fca      ,					pt_mlp.c_proj    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			List[Any]:
     UpperCamelCase__    :     Any        =						pt_linear.weight
     UpperCamelCase__    :     List[Any]        =						pt_linear.bias







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			List[str]:
     copy_linear(hf_layer.layer_norma      ,					pt_layer.ln_a    )
     copy_linear(hf_layer.layer_norma      ,					pt_layer.ln_a    )

     # copy MLP
     copy_mlp(hf_layer.mlp      ,					pt_layer.mlp    )

     # copy attn
     copy_attn_layer(hf_layer.self_attn      ,					pt_layer.attn    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			List[str]:






     for hf_layer, pt_layer in zip(UpperCamelCase__      ,					UpperCamelCase__    ):
          copy_layer(UpperCamelCase__      ,					UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			str:
     UpperCamelCase__    :     Any        =						pt_model.token_embedding.weight
     UpperCamelCase__    :     Tuple        =						pt_model.positional_embedding

     # copy layer norm
     copy_linear(hf_encoder.final_layer_norm      ,					pt_model.ln_final    )

     # copy hidden layers
     copy_layers(hf_encoder.encoder.layers      ,					pt_model.transformer.resblocks    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Dict:
     UpperCamelCase__    :     Optional[int]        =						pt_model.text_projection.data.T

     # copy text encoder
     copy_encoder(hf_model.text_model      ,					UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			List[str]:
     UpperCamelCase__    :     Union[str, Any]        =						pt_model.visual.proj.data.T

     # copy layer norms
     copy_linear(hf_model.vision_model.pre_layernorm      ,					pt_model.visual.ln_pre    )
     copy_linear(hf_model.vision_model.post_layernorm      ,					pt_model.visual.ln_post    )

     # copy embeds
     UpperCamelCase__    :     Any        =						pt_model.visual.conva.weight.data
     UpperCamelCase__    :     Optional[int]        =						pt_model.visual.class_embedding
     UpperCamelCase__    :     List[Any]        =						pt_model.visual.positional_embedding.data

     # copy encoder
     copy_layers(hf_model.vision_model.encoder.layers      ,					pt_model.visual.transformer.resblocks    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Tuple:
     UpperCamelCase__    :     List[str]        =						flatten_nested_dict(flax_params["backbone"]["merged_class_token"]    )

     UpperCamelCase__    :     int        =						torch.from_numpy(flax_class_token_params["scale"]    )
     UpperCamelCase__    :     List[str]        =						torch.from_numpy(flax_class_token_params["bias"]    )
     UpperCamelCase__    :     Union[str, Any]        =						nn.Parameter(UpperCamelCase__    )
     UpperCamelCase__    :     Tuple        =						nn.Parameter(UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Dict:
     UpperCamelCase__    :     Tuple        =						hf_model.state_dict()
     UpperCamelCase__    :     Optional[Any]        =						{}

     # Rename class prediction head flax params to pytorch HF
     UpperCamelCase__    :     Tuple        =						flatten_nested_dict(flax_params["class_head"]    )

     for flax_key, v in flax_class_params.items():
          UpperCamelCase__    :     Optional[Any]        =						flax_key.replace("/"      ,					"."    )
          UpperCamelCase__    :     Any        =						torch_key.replace(".kernel"      ,					".weight"    )
          UpperCamelCase__    :     Dict        =						torch_key.replace("Dense_0"      ,					"dense0"    )
          UpperCamelCase__    :     Optional[int]        =						"class_head." + torch_key

          if "weight" in torch_key and v.ndim == 2:
               UpperCamelCase__    :     str        =						v.T

          UpperCamelCase__    :     List[Any]        =						nn.Parameter(torch.from_numpy(UpperCamelCase__    )    )

     # Rename box prediction box flax params to pytorch HF
     UpperCamelCase__    :     Dict        =						flatten_nested_dict(flax_params["obj_box_head"]    )

     for flax_key, v in flax_box_params.items():
          UpperCamelCase__    :     int        =						flax_key.replace("/"      ,					"."    )
          UpperCamelCase__    :     Any        =						torch_key.replace(".kernel"      ,					".weight"    )
          UpperCamelCase__    :     Tuple        =						torch_key.replace("_"      ,					""    ).lower()
          UpperCamelCase__    :     Optional[int]        =						"box_head." + torch_key

          if "weight" in torch_key and v.ndim == 2:
               UpperCamelCase__    :     str        =						v.T

          UpperCamelCase__    :     str        =						nn.Parameter(torch.from_numpy(UpperCamelCase__    )    )

     # Copy flax params to PyTorch params
     for name, param in new_params.items():
          if name in pt_params.keys():
               pt_params[name].copy_(UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Optional[int]:






     for k, v in flax_attn_params.items():
          if k.startswith("transformer"    ):
               UpperCamelCase__    :     List[Any]        =						k.replace("transformer.resblocks"      ,					"text_model.encoder.layers"    )
          else:
               UpperCamelCase__    :     Any        =						k.replace("visual.transformer.resblocks"      ,					"vision_model.encoder.layers"    )

          UpperCamelCase__    :     Optional[Any]        =						torch_key.replace("attn"      ,					"self_attn"    )
          UpperCamelCase__    :     Any        =						torch_key.replace("key"      ,					"k_proj"    )
          UpperCamelCase__    :     str        =						torch_key.replace("value"      ,					"v_proj"    )
          UpperCamelCase__    :     List[Any]        =						torch_key.replace("query"      ,					"q_proj"    )
          UpperCamelCase__    :     str        =						torch_key.replace("out"      ,					"out_proj"    )

          if "bias" in torch_key and v.ndim == 2:
               UpperCamelCase__    :     Tuple        =						v.shape[0] * v.shape[1]
               UpperCamelCase__    :     Dict        =						v.reshape(UpperCamelCase__    )

          if "weight" in torch_key and "out" in torch_key:
               UpperCamelCase__    :     Dict        =						(v.shape[0] * v.shape[1], v.shape[2])
               UpperCamelCase__    :     str        =						v.reshape(UpperCamelCase__    ).T

          if "weight" in torch_key and "out" not in torch_key:
               UpperCamelCase__    :     int        =						(v.shape[0], v.shape[1] * v.shape[2])
               UpperCamelCase__    :     Union[str, Any]        =						v.reshape(UpperCamelCase__    ).T

          # Copy flax CLIP attn params to HF PyTorch params
          UpperCamelCase__    :     Dict        =						torch.from_numpy(UpperCamelCase__    )
          hf_backbone.state_dict()[torch_key].copy_(UpperCamelCase__    )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase    )				->			List[str]:
     UpperCamelCase__    :     Optional[Any]        =						{}
     UpperCamelCase__    :     int        =						[]

     for k, v in params.items():
          if "attn." in k:
               UpperCamelCase__    :     Union[str, Any]        =						k[: k.rindex("attn."    ) + 5]
               if base in processed_attn_layers:
                    continue

               processed_attn_layers.append(UpperCamelCase__    )
               UpperCamelCase__    :     Optional[int]        =						params[base + "out.weight"].shape[-1]
               UpperCamelCase__    :     Dict        =						params[base + "out.weight"].reshape(UpperCamelCase__      ,					UpperCamelCase__    ).T
               UpperCamelCase__    :     Any        =						params[base + "out.bias"]
          else:
               UpperCamelCase__    :     str        =						v
     return new_params







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase    )				->			Union[str, Any]:
     UpperCamelCase__    :     Dict        =						CLIP(**UpperCamelCase__    )
     torch_model.eval()
     UpperCamelCase__    :     Optional[Any]        =						torch_model.state_dict()

     UpperCamelCase__    :     List[Any]        =						flatten_nested_dict(flax_params["backbone"]["clip"]    )
     UpperCamelCase__    :     Any        =						{}

     for flax_key, v in flax_clip_params.items():
          UpperCamelCase__    :     int        =						flax_key.replace("/"      ,					"."    )
          UpperCamelCase__    :     Optional[int]        =						torch_key.replace("text.token_embedding.embedding"      ,					"token_embedding.kernel"    )

          if (
              torch_key.startswith("text.transformer"    )
              or torch_key.startswith("text.text_projection"    )
              or torch_key.startswith("text.ln_final"    )
              or torch_key.startswith("text.positional_embedding"    )
          ):
               UpperCamelCase__    :     Any        =						torch_key[5:]

          UpperCamelCase__    :     List[Any]        =						torch_key.replace("text_projection.kernel"      ,					"text_projection"    )
          UpperCamelCase__    :     int        =						torch_key.replace("visual.proj.kernel"      ,					"visual.proj"    )
          UpperCamelCase__    :     Optional[int]        =						torch_key.replace(".scale"      ,					".weight"    )
          UpperCamelCase__    :     Dict        =						torch_key.replace(".kernel"      ,					".weight"    )

          if "conv" in torch_key or "downsample.0.weight" in torch_key:
               UpperCamelCase__    :     List[str]        =						v.transpose(3      ,					2      ,					0      ,					1    )

          elif "weight" in torch_key and v.ndim == 2 and "embedding" not in torch_key:
               # Fully connected layers are transposed, embeddings are not
               UpperCamelCase__    :     Tuple        =						v.T

          UpperCamelCase__    :     Any        =						v

     UpperCamelCase__    :     Union[str, Any]        =						_convert_attn_layers(UpperCamelCase__    )
     new_torch_params.update(UpperCamelCase__    )
     UpperCamelCase__    :     Dict        =						{}

     # Copy flax CLIP backbone params to PyTorch params
     for name, param in new_torch_params.items():
          if name in torch_clip_params.keys():
               UpperCamelCase__    :     Tuple        =						torch.from_numpy(new_torch_params[name]    )
               torch_clip_params[name].copy_(UpperCamelCase__    )
          else:
               UpperCamelCase__    :     str        =						param

     return torch_clip_params, torch_model, attn_params







@torch.no_grad()
def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase      ,					__lowerCAmelCase      ,					__lowerCAmelCase      ,					__lowerCAmelCase      ,					__lowerCAmelCase=None    )				->			Dict:
     UpperCamelCase__    :     Tuple        =						Repository(UpperCamelCase__      ,					clone_from=f'google/{pytorch_dump_folder_path}'    )
     repo.git_pull()

     if config_path is not None:
          UpperCamelCase__    :     Optional[int]        =						OwlViTConfig.from_pretrained(UpperCamelCase__    )
     else:
          UpperCamelCase__    :     List[str]        =						OwlViTConfig()

     UpperCamelCase__    :     List[Any]        =						OwlViTModel(UpperCamelCase__    ).eval()
     UpperCamelCase__    :     Union[str, Any]        =						OwlViTForObjectDetection(UpperCamelCase__    ).eval()

     copy_text_model_and_projection(UpperCamelCase__      ,					UpperCamelCase__    )
     copy_vision_model_and_projection(UpperCamelCase__      ,					UpperCamelCase__    )
     UpperCamelCase__    :     Dict        =						pt_backbone.logit_scale
     copy_flax_attn_params(UpperCamelCase__      ,					UpperCamelCase__    )

     UpperCamelCase__    :     Tuple        =						hf_backbone
     copy_class_merge_token(UpperCamelCase__      ,					UpperCamelCase__    )
     copy_class_box_heads(UpperCamelCase__      ,					UpperCamelCase__    )

     # Save HF model
     hf_model.save_pretrained(repo.local_dir    )

     # Initialize image processor
     UpperCamelCase__    :     Optional[Any]        =						OwlViTImageProcessor(
         size=config.vision_config.image_size      ,					crop_size=config.vision_config.image_size    )
     # Initialize tokenizer
     UpperCamelCase__    :     Union[str, Any]        =						CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"      ,					pad_token="!"      ,					model_max_length=16    )

     # Initialize processor
     UpperCamelCase__    :     Union[str, Any]        =						OwlViTProcessor(image_processor=UpperCamelCase__      ,					tokenizer=UpperCamelCase__    )
     image_processor.save_pretrained(repo.local_dir    )
     processor.save_pretrained(repo.local_dir    )

     repo.git_add()
     repo.git_commit("Upload model and processor"    )
     repo.git_push()


if __name__ == "__main__":
 lowerCamelCase			:							Dict       =argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '''--owlvit_version''',
     default=None,
     type=str,
     required=True,
     help='''OWL-ViT model name [clip_b16, clip_b32, clip_l14].''',
 )
 parser.add_argument(
     '''--owlvit_checkpoint''', default=None, type=str, required=True, help='''Path to flax model checkpoint.'''
 )
 parser.add_argument('''--hf_config''', default=None, type=str, required=True, help='''Path to HF model config.''')
 parser.add_argument(
     '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model.'''
 )
 lowerCamelCase			:							List[str]       =parser.parse_args()

 # Initialize PyToch clip model
 lowerCamelCase			:							Any       =args.owlvit_version
 if model_name == "clip_b16":
  lowerCamelCase			:							Any       =CONFIGS['''vit_b16''']
 elif model_name == "clip_b32":
  lowerCamelCase			:							str       =CONFIGS['''vit_b32''']
 elif model_name == "clip_l14":
  lowerCamelCase			:							List[str]       =CONFIGS['''vit_l14''']

 # Load from checkpoint and convert params to float-32
 lowerCamelCase			:							Optional[int]       =checkpoints.restore_checkpoint(args.owlvit_checkpoint, target=None)['''optimizer''']['''target''']
 lowerCamelCase			:							Tuple       =jax.tree_util.tree_map(lambda x: x.astype(jnp.floataa) if x.dtype == jnp.bfloataa else x, variables)
 del variables

 # Convert CLIP backbone
 lowerCamelCase       ,    lowerCamelCase       ,    lowerCamelCase			:							Union[str, Any]       =convert_clip_backbone(flax_params, torch_config)

 convert_owlvit_checkpoint(clip_pt, flax_params, attn_params, args.pytorch_dump_folder_path, args.hf_config)












import numpy as np  # noqa: E402

from ...configuration_utils import ConfigMixin, register_to_config
from ...schedulers.scheduling_utils import SchedulerMixin


try:
 import librosa  # noqa: E402

 lowerCamelCase			:							List[str]       =True
 lowerCamelCase			:							Dict       =''''''
except Exception as e:
 lowerCamelCase			:							str       =False
 lowerCamelCase			:							List[Any]       =(
     F"""Cannot import librosa because {e}. Make sure to correctly install librosa to be able to install it."""
 )


from PIL import Image  # noqa: E402





class     __a						(					A__ ,     A__ ):
 _lowerCAmelCase : Optional[Any] 				=				'''mel_config.json'''







 @register_to_config
 def __init__(	self      :     Any							,     SCREAMING_SNAKE_CASE      :     int = 2_56							,     SCREAMING_SNAKE_CASE      :     int = 2_56							,     SCREAMING_SNAKE_CASE      :     int = 2_20_50							,     SCREAMING_SNAKE_CASE      :     int = 20_48							,     SCREAMING_SNAKE_CASE      :     int = 5_12							,     SCREAMING_SNAKE_CASE      :     int = 80							,     SCREAMING_SNAKE_CASE      :     int = 32							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     str        =						hop_length
      UpperCamelCase__    :     Optional[Any]        =						sample_rate
      UpperCamelCase__    :     Dict        =						n_fft
      UpperCamelCase__    :     str        =						top_db
      UpperCamelCase__    :     Optional[Any]        =						n_iter
      self.set_resolution(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)
      UpperCamelCase__    :     str        =						None

      if not _librosa_can_be_imported:
           raise ValueError(_import_error		)







 def       __lowercase      (	self      :     Tuple							,     SCREAMING_SNAKE_CASE      :     int							,     SCREAMING_SNAKE_CASE      :     int		):


      '''simple docstring'''






      UpperCamelCase__    :     Dict        =						x_res
      UpperCamelCase__    :     Dict        =						y_res
      UpperCamelCase__    :     Dict        =						self.y_res
      UpperCamelCase__    :     Any        =						self.x_res * self.hop_length - 1







 def       __lowercase      (	self      :     str							,     SCREAMING_SNAKE_CASE      :     str = None							,     SCREAMING_SNAKE_CASE      :     np.ndarray = None		):


      '''simple docstring'''






      if audio_file is not None:
           UpperCamelCase__       ,  UpperCamelCase__    :     int        =						librosa.load(SCREAMING_SNAKE_CASE							,     mono=SCREAMING_SNAKE_CASE							,     sr=self.sr		)
      else:
           UpperCamelCase__    :     List[Any]        =						raw_audio

      # Pad with silence if necessary.
      if len(self.audio		) < self.x_res * self.hop_length:
           UpperCamelCase__    :     List[Any]        =						np.concatenate([self.audio, np.zeros((self.x_res * self.hop_length - len(self.audio		),)		)]		)







 def       __lowercase      (	self      :     List[Any]		):


      '''simple docstring'''






      return len(self.audio		) // self.slice_size







 def       __lowercase      (	self      :     int							,     SCREAMING_SNAKE_CASE      :     int = 0		):


      '''simple docstring'''






      return self.audio[self.slice_size * slice : self.slice_size * (slice + 1)]







 def       __lowercase      (	self      :     Tuple		):


      '''simple docstring'''






      return self.sr







 def       __lowercase      (	self      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     int		):


      '''simple docstring'''






      UpperCamelCase__    :     Optional[int]        =						librosa.feature.melspectrogram(
          y=self.get_audio_slice(SCREAMING_SNAKE_CASE		)							,     sr=self.sr							,     n_fft=self.n_fft							,     hop_length=self.hop_length							,     n_mels=self.n_mels		)
      UpperCamelCase__    :     Tuple        =						librosa.power_to_db(SCREAMING_SNAKE_CASE							,     ref=np.max							,     top_db=self.top_db		)
      UpperCamelCase__    :     Optional[Any]        =						(((log_S + self.top_db) * 2_55 / self.top_db).clip(0							,     2_55		) + 0.5).astype(np.uinta		)
      UpperCamelCase__    :     Optional[Any]        =						Image.fromarray(SCREAMING_SNAKE_CASE		)
      return image







 def       __lowercase      (	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Image.Image		):


      '''simple docstring'''






      UpperCamelCase__    :     Optional[Any]        =						np.frombuffer(image.tobytes()							,     dtype="uint8"		).reshape((image.height, image.width)		)
      UpperCamelCase__    :     str        =						bytedata.astype("float"		) * self.top_db / 2_55 - self.top_db
      UpperCamelCase__    :     Any        =						librosa.db_to_power(SCREAMING_SNAKE_CASE		)
      UpperCamelCase__    :     Tuple        =						librosa.feature.inverse.mel_to_audio(
          SCREAMING_SNAKE_CASE							,     sr=self.sr							,     n_fft=self.n_fft							,     hop_length=self.hop_length							,     n_iter=self.n_iter		)
      return audio