
"""simple docstring"""




from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


UpperCAmelCase__   :					str												=				logging.get_logger(__name__)



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		int       =     ['''input_features''', '''attention_mask''']



				def __init__(self     , SCREAMING_SNAKE_CASE__=80     , SCREAMING_SNAKE_CASE__=1_60_00     , SCREAMING_SNAKE_CASE__=80     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=True     , **SCREAMING_SNAKE_CASE__     , )      ->	Union[str, Any]:



											"""simple docstring"""




											super().__init__(feature_size=SCREAMING_SNAKE_CASE__     , sampling_rate=SCREAMING_SNAKE_CASE__     , padding_value=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Dict     =     num_mel_bins
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     do_ceptral_normalize
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     normalize_means
											SCREAMING_SNAKE_CASE__		: int     =     normalize_vars
											SCREAMING_SNAKE_CASE__		: Any     =     True



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , )      ->	np.ndarray:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     torch.from_numpy(SCREAMING_SNAKE_CASE__					).unsqueeze(0					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     ta_kaldi.fbank(SCREAMING_SNAKE_CASE__     , num_mel_bins=self.num_mel_bins     , sample_frequency=self.sampling_rate					)
											return features.numpy()



				@staticmethod
				def 			__magic_name__					(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = True     , SCREAMING_SNAKE_CASE__ = True     , SCREAMING_SNAKE_CASE__ = 0.0     , )      ->	np.ndarray:



											"""simple docstring"""




											if normalize_means:
																		SCREAMING_SNAKE_CASE__		: Dict     =     x[:input_length].mean(axis=0					)
																		SCREAMING_SNAKE_CASE__		: List[Any]     =     np.subtract(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)
											if normalize_vars:
																		SCREAMING_SNAKE_CASE__		: List[str]     =     x[:input_length].std(axis=0					)
																		SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     np.divide(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)

											if input_length < x.shape[0]:
																		SCREAMING_SNAKE_CASE__		: Tuple     =     padding_value

											# make sure array is in float32
											SCREAMING_SNAKE_CASE__		: List[Any]     =     x.astype(np.floataa					)

											return x



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None					)      ->	List[np.ndarray]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     attention_mask.sum(-1					) if attention_mask is not None else [x.shape[0] for x in input_features]
											return [
											    self.utterance_cmvn(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , self.normalize_means     , self.normalize_vars     , self.padding_value					)
											    for x, n in zip(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)
											]



				def __call__(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = False     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = False     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , **SCREAMING_SNAKE_CASE__     , )      ->	BatchFeature:



											"""simple docstring"""




											if sampling_rate is not None:
																		if sampling_rate != self.sampling_rate:
																									raise ValueError(
																									    F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of'''
																									    F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'''
																									    F''' {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."""					)

											SCREAMING_SNAKE_CASE__		: Any     =     isinstance(SCREAMING_SNAKE_CASE__     , 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}'''					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     is_batched_numpy or (
											    isinstance(SCREAMING_SNAKE_CASE__     , (list, tuple)					) and (isinstance(raw_speech[0]     , (np.ndarray, tuple, list)					))
											)

											if is_batched:
																		SCREAMING_SNAKE_CASE__		: str     =     [np.asarray(SCREAMING_SNAKE_CASE__     , dtype=np.floataa					) for speech in raw_speech]
											elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE__     , np.ndarray					):
																		SCREAMING_SNAKE_CASE__		: Dict     =     np.asarray(SCREAMING_SNAKE_CASE__     , dtype=np.floataa					)
											elif isinstance(SCREAMING_SNAKE_CASE__     , np.ndarray					) and raw_speech.dtype is np.dtype(np.floataa					):
																		SCREAMING_SNAKE_CASE__		: List[Any]     =     raw_speech.astype(np.floataa					)

											# always return batch
											if not is_batched:
																		SCREAMING_SNAKE_CASE__		: List[str]     =     [raw_speech]

											# extract fbank features
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     [self._extract_fbank_features(SCREAMING_SNAKE_CASE__					) for waveform in raw_speech]

											# convert into correct format for padding
											SCREAMING_SNAKE_CASE__		: str     =     BatchFeature({"""input_features""": features}					)

											SCREAMING_SNAKE_CASE__		: int     =     self.pad(
											    SCREAMING_SNAKE_CASE__     , padding=SCREAMING_SNAKE_CASE__     , max_length=SCREAMING_SNAKE_CASE__     , truncation=SCREAMING_SNAKE_CASE__     , pad_to_multiple_of=SCREAMING_SNAKE_CASE__     , return_attention_mask=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__     , )

											# make sure list is in array format
											SCREAMING_SNAKE_CASE__		: Any     =     padded_inputs.get("""input_features"""					)
											if isinstance(input_features[0]     , SCREAMING_SNAKE_CASE__					):
																		SCREAMING_SNAKE_CASE__		: Optional[Any]     =     [np.asarray(SCREAMING_SNAKE_CASE__     , dtype=np.floataa					) for feature in input_features]

											SCREAMING_SNAKE_CASE__		: List[Any]     =     padded_inputs.get("""attention_mask"""					)
											if attention_mask is not None:
																		SCREAMING_SNAKE_CASE__		: Tuple     =     [np.asarray(SCREAMING_SNAKE_CASE__     , dtype=np.intaa					) for array in attention_mask]

											# Utterance-level cepstral mean and variance normalization
											if self.do_ceptral_normalize:
																		SCREAMING_SNAKE_CASE__		: str     =     (
																		    np.array(SCREAMING_SNAKE_CASE__     , dtype=np.intaa					)
																		    if self._get_padding_strategies(SCREAMING_SNAKE_CASE__     , max_length=SCREAMING_SNAKE_CASE__					) is not PaddingStrategy.DO_NOT_PAD
																		    else None
																		)
																		SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.normalize(
																		    padded_inputs["""input_features"""]     , attention_mask=SCREAMING_SNAKE_CASE__					)

											if return_tensors is not None:
																		SCREAMING_SNAKE_CASE__		: int     =     padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE__					)

											return padded_inputs






"""simple docstring"""




import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

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


UpperCAmelCase__   :					Optional[Any]												=				logging.get_logger(__name__)

UpperCAmelCase__   :					str												=				{
    'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json',
    # See all DETR models at https://huggingface.co/models?filter=detr
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     '''detr'''
				__UpperCamelCase   :		List[Any]       =     ['''past_key_values''']
				__UpperCamelCase   :		List[Any]       =     {
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''encoder_attention_heads''',
				}



				def __init__(self     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__="relu"     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1.0     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__="sine"     , SCREAMING_SNAKE_CASE__="resnet50"     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , **SCREAMING_SNAKE_CASE__     , )      ->	Optional[Any]:



											"""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."""					)
																									SCREAMING_SNAKE_CASE__		: Any     =     CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]					)
																		elif isinstance(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					):
																									SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     backbone_config.get("""model_type"""					)
																									SCREAMING_SNAKE_CASE__		: int     =     CONFIG_MAPPING[backbone_model_type]
																									SCREAMING_SNAKE_CASE__		: str     =     config_class.from_dict(SCREAMING_SNAKE_CASE__					)
																		# set timm attributes to None
																		SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: List[str]     =     None, None, None

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_timm_backbone
											SCREAMING_SNAKE_CASE__		: Tuple     =     backbone_config
											SCREAMING_SNAKE_CASE__		: List[Any]     =     num_channels
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_queries
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     d_model
											SCREAMING_SNAKE_CASE__		: str     =     encoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: str     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     encoder_attention_heads
											SCREAMING_SNAKE_CASE__		: Any     =     decoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     decoder_layers
											SCREAMING_SNAKE_CASE__		: Dict     =     decoder_attention_heads
											SCREAMING_SNAKE_CASE__		: List[str]     =     dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     activation_function
											SCREAMING_SNAKE_CASE__		: Any     =     init_std
											SCREAMING_SNAKE_CASE__		: Dict     =     init_xavier_std
											SCREAMING_SNAKE_CASE__		: Any     =     encoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     decoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Dict     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: List[str]     =     auxiliary_loss
											SCREAMING_SNAKE_CASE__		: List[Any]     =     position_embedding_type
											SCREAMING_SNAKE_CASE__		: List[str]     =     backbone
											SCREAMING_SNAKE_CASE__		: Dict     =     use_pretrained_backbone
											SCREAMING_SNAKE_CASE__		: Any     =     dilation
											# Hungarian matcher
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     class_cost
											SCREAMING_SNAKE_CASE__		: Tuple     =     bbox_cost
											SCREAMING_SNAKE_CASE__		: List[Any]     =     giou_cost
											# Loss coefficients
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     dice_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     bbox_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Any     =     giou_loss_coefficient
											SCREAMING_SNAKE_CASE__		: List[str]     =     eos_coefficient
											super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.encoder_attention_heads



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.d_model



				@classmethod
				def 			__magic_name__					(cls     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Tuple:



											"""simple docstring"""




											return cls(backbone_config=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Dict[str, any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     copy.deepcopy(self.__dict__					)
											if output["backbone_config"] is not None:
																		SCREAMING_SNAKE_CASE__		: Any     =     self.backbone_config.to_dict()
											SCREAMING_SNAKE_CASE__		: Dict     =     self.__class__.model_type
											return output






class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		Any       =     version.parse('''1.11'''						)



				@property
				def 			__magic_name__					(self					)      ->	Mapping[str, Mapping[int, str]]:



											"""simple docstring"""




											return OrderedDict(
											    [
											        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
											        ("""pixel_mask""", {0: """batch"""}),
											    ]					)



				@property
				def 			__magic_name__					(self					)      ->	float:



											"""simple docstring"""




											return 1E-5



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return 12




