






'''simple docstring'''







from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


UpperCamelCase     :   str	      =		logging.get_logger(__name__)




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		_UpperCamelCase						=       ['audio_values', 'audio_mask']






		def __init__(   self ,_lowerCAmelCase=20_48 ,_lowerCAmelCase=1 ,_lowerCAmelCase=[16, 16] ,_lowerCAmelCase=1_28 ,_lowerCAmelCase=4_41_00 ,_lowerCAmelCase=86 ,_lowerCAmelCase=20_48 ,_lowerCAmelCase=0.0 ,**_lowerCAmelCase ,):
								super().__init__(
								    feature_size=_lowerCAmelCase ,sampling_rate=_lowerCAmelCase ,padding_value=_lowerCAmelCase ,**_lowerCAmelCase ,)

								lowerCamelCase__								=	spectrogram_length
								lowerCamelCase__								=	num_channels
								lowerCamelCase__								=	patch_size
								lowerCamelCase__								=	feature_size // self.patch_size[1]
								lowerCamelCase__								=	n_fft
								lowerCamelCase__								=	sampling_rate // hop_length_to_sampling_rate
								lowerCamelCase__								=	sampling_rate
								lowerCamelCase__								=	padding_value
								lowerCamelCase__								=	mel_filter_bank(
								    num_frequency_bins=1 + n_fft // 2 ,num_mel_filters=_lowerCAmelCase ,min_frequency=0.0 ,max_frequency=2_2050.0 ,sampling_rate=_lowerCAmelCase ,norm="""slaney""" ,mel_scale="""slaney""" ,).T






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	spectrogram(
								    _lowerCAmelCase ,window_function(self.n_fft ,"""hann"""							) ,frame_length=self.n_fft ,hop_length=self.hop_length ,power=2.0 ,mel_filters=self.mel_filters.T ,log_mel="""dB""" ,db_range=80.0 ,)
								lowerCamelCase__								=	log_spec[:, :-1]
								lowerCamelCase__								=	log_spec - 20.0
								lowerCamelCase__								=	np.clip(log_spec / 40.0 ,-2.0 ,0.0							) + 1.0
								return log_spec






		def __call__(   self ,_lowerCAmelCase ,_lowerCAmelCase = None ,_lowerCAmelCase = True ,_lowerCAmelCase = None ,_lowerCAmelCase = False ,_lowerCAmelCase = False ,**_lowerCAmelCase ,):

								if sampling_rate is not None:
														if sampling_rate != self.sampling_rate:
																				raise ValueError(
																				    """This feature extractor is set to support sampling rate"""
																				    F''' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'''
																				    F''' with {self.sampling_rate} and not {sampling_rate}.'''							)
								else:
														logger.warning(
														    """It is strongly recommended to pass the `sampling_rate` argument to this function. """
														    """Failing to do so can result in silent errors that might be hard to debug."""							)

								lowerCamelCase__								=	isinstance(_lowerCAmelCase ,np.ndarray							) and len(raw_speech.shape							) > 1
								if is_batched_numpy and len(raw_speech.shape							) > 2:
														raise ValueError(F'''Only mono-channel audio is supported for input to {self}'''							)
								lowerCamelCase__								=	is_batched_numpy or (
								    isinstance(_lowerCAmelCase ,(list, tuple)							) and (isinstance(raw_speech[0] ,(np.ndarray, tuple, list)							))
								)
								if is_batched:
														lowerCamelCase__								=	[np.asarray([speech] ,dtype=np.floataa							).T for speech in raw_speech]
								elif not is_batched and not isinstance(_lowerCAmelCase ,np.ndarray							):
														lowerCamelCase__								=	np.asarray(_lowerCAmelCase ,dtype=np.floataa							)
								elif isinstance(_lowerCAmelCase ,np.ndarray							) and raw_speech.dtype is np.dtype(np.floataa							):
														lowerCamelCase__								=	raw_speech.astype(np.floataa							)
								# always return batch
								if not is_batched:
														lowerCamelCase__								=	[np.asarray([raw_speech]							).T]

								# Convert audio signals to log mel spectrograms, truncate by time axis
								lowerCamelCase__								=	[
								    self._np_extract_fbank_features(waveform.squeeze()							).T[: self.spectrogram_length] for waveform in raw_speech
								]
								if isinstance(audio_features[0] ,_lowerCAmelCase							):
														lowerCamelCase__								=	[np.asarray(_lowerCAmelCase ,dtype=np.floataa							) for feature in audio_features]

								# Create audio attention mask
								lowerCamelCase__								=	max(
								    [ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len for feature in audio_features]							)  # The maximum number of audio patches in a batch
								if return_attention_mask:
														lowerCamelCase__								=	[
														    (ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [1]
														    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]							) * self.freq_len) * [0]
														    for feature in audio_features
														]
														lowerCamelCase__								=	np.array(_lowerCAmelCase							).astype(np.floataa							)

								# convert into correct format for padding
								lowerCamelCase__								=	max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
								lowerCamelCase__								=	np.ones([len(_lowerCAmelCase							), 1, max_time_len, self.feature_size]							).astype(np.floataa							)
								lowerCamelCase__								=	padded_audio_features * self.padding_value
								for i in range(len(_lowerCAmelCase							)							):
														lowerCamelCase__								=	audio_features[i]
														lowerCamelCase__								=	feature

								# return as BatchFeature
								if return_attention_mask:
														lowerCamelCase__								=	{"""audio_values""": padded_audio_features, """audio_mask""": audio_mask}
								else:
														lowerCamelCase__								=	{"""audio_values""": padded_audio_features}

								lowerCamelCase__								=	BatchFeature(data=_lowerCAmelCase ,tensor_type=_lowerCAmelCase							)
								return encoded_inputs







import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


__UpperCamelCase   :			Any          = logging.get_logger(__name__)

__UpperCamelCase   :			Tuple          = {
    "SenseTime/deformable-detr": "https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json",
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}

class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    '''deformable_detr'''
							UpperCamelCase__   	=    {
							    '''hidden_size''': '''d_model''',
							    '''num_attention_heads''': '''encoder_attention_heads''',
							}
							def __init__(		self		:List[str]						, __magic_name__		:Tuple=True						, __magic_name__		:str=None						, __magic_name__		:Tuple=3						, __magic_name__		:List[str]=300						, __magic_name__		:Dict=1024						, __magic_name__		:Tuple=6						, __magic_name__		:Tuple=1024						, __magic_name__		:int=8						, __magic_name__		:List[Any]=6						, __magic_name__		:Tuple=1024						, __magic_name__		:List[Any]=8						, __magic_name__		:Union[str, Any]=0.0						, __magic_name__		:Union[str, Any]=True						, __magic_name__		:str="relu"						, __magic_name__		:Optional[Any]=256						, __magic_name__		:int=0.1						, __magic_name__		:List[str]=0.0						, __magic_name__		:str=0.0						, __magic_name__		:str=0.02						, __magic_name__		:int=1.0						, __magic_name__		:List[str]=True						, __magic_name__		:Union[str, Any]=False						, __magic_name__		:List[Any]="sine"						, __magic_name__		:Union[str, Any]="resnet50"						, __magic_name__		:int=True						, __magic_name__		:Any=False						, __magic_name__		:List[str]=4						, __magic_name__		:Tuple=4						, __magic_name__		:Union[str, Any]=4						, __magic_name__		:Optional[int]=False						, __magic_name__		:Tuple=300						, __magic_name__		:Tuple=False						, __magic_name__		:List[Any]=1						, __magic_name__		:List[str]=5						, __magic_name__		:List[Any]=2						, __magic_name__		:Optional[int]=1						, __magic_name__		:Union[str, Any]=1						, __magic_name__		:List[Any]=5						, __magic_name__		:Optional[Any]=2						, __magic_name__		:Optional[int]=0.1						, __magic_name__		:List[str]=0.25						, __magic_name__		:str=False						, **__magic_name__		:List[str]						, ):







								'''simple docstring'''







								if backbone_config is not None and use_timm_backbone:
									raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""							)

								if not use_timm_backbone:
									if backbone_config is None:
										logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""							)
										a									=				CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]							)
									elif isinstance(__magic_name__						, __magic_name__							):
										a									=				backbone_config.get("""model_type"""							)
										a									=				CONFIG_MAPPING[backbone_model_type]
										a									=				config_class.from_dict(__magic_name__							)
								a									=				use_timm_backbone
								a									=				backbone_config
								a									=				num_channels
								a									=				num_queries
								a									=				max_position_embeddings
								a									=				d_model
								a									=				encoder_ffn_dim
								a									=				encoder_layers
								a									=				encoder_attention_heads
								a									=				decoder_ffn_dim
								a									=				decoder_layers
								a									=				decoder_attention_heads
								a									=				dropout
								a									=				attention_dropout
								a									=				activation_dropout
								a									=				activation_function
								a									=				init_std
								a									=				init_xavier_std
								a									=				encoder_layerdrop
								a									=				auxiliary_loss
								a									=				position_embedding_type
								a									=				backbone
								a									=				use_pretrained_backbone
								a									=				dilation
								# deformable attributes
								a									=				num_feature_levels
								a									=				encoder_n_points
								a									=				decoder_n_points
								a									=				two_stage
								a									=				two_stage_num_proposals
								a									=				with_box_refine
								if two_stage is True and with_box_refine is False:
									raise ValueError("""If two_stage is True, with_box_refine must be True."""							)
								# Hungarian matcher
								a									=				class_cost
								a									=				bbox_cost
								a									=				giou_cost
								# Loss coefficients
								a									=				mask_loss_coefficient
								a									=				dice_loss_coefficient
								a									=				bbox_loss_coefficient
								a									=				giou_loss_coefficient
								a									=				eos_coefficient
								a									=				focal_alpha
								a									=				disable_custom_kernels
								super().__init__(is_encoder_decoder=__magic_name__						, **__magic_name__							)
							@property
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								return self.encoder_attention_heads
							@property
							def  lowerCamelCase__				(		self		:int							):







								'''simple docstring'''







								return self.d_model





							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								a									=				copy.deepcopy(self.__dict__							)
								if self.backbone_config is not None:
									a									=				self.backbone_config.to_dict()
								a									=				self.__class__.model_type
								return output

