mispeech/ced-small
02.4k
1# coding=utf-82# Copyright 2023 Xiaomi Corporation and The HuggingFace Inc. team. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15""" CED model configuration"""16 17import csv18from transformers import PretrainedConfig19from transformers.utils import logging20from transformers.utils.hub import cached_file21 22logger = logging.get_logger(__name__)23 24CED_PRETRAINED_CONFIG_ARCHIVE_MAP = {25 "mispeech/ced-tiny": "https://huggingface.co/mispeech/ced-tiny/resolve/main/config.json",26}27 28 29class CedConfig(PretrainedConfig):30 model_type = "ced"31 32 r"""33 Configuration class for the CED model.34 35 Args:36 name (str, optional, *optional*):37 Name of the pre-defined configuration. Can be "ced-tiny", "ced-mini", "ced-small" or "ced-base".38 attn_drop_rate (float, *optional*, defaults to 0.0):39 Dropout probability for attention weights. Default to 0.0.40 depth (int, *optional*, defaults to 12): Number of transformer layers. Default to 12.41 drop_path_rate (float, *optional*, defaults to 0.0): Drop path is taken from timm. Default to 0.0.42 drop_rate (float, *optional*, defaults to 0.0):43 Dropout probability for input embeddings. Default to 0.0.44 embed_dim (int, *optional*, defaults to 768):45 Dimensionality of the audio patch embeddings. Default to 768.46 eval_avg (str, *optional*, defaults to `"mean"`):47 Type of pooling to use for evaluation. Can be "mean", "token", "dm" or "logit". Default to "mean".48 mlp_ratio (float, *optional*, defaults to 4.0):49 Ratio of hidden size in the feedforward layer to the embedding size. Default to 4.0.50 num_heads (int, *optional*, defaults to 12): Number of attention heads. Default to 12.51 outputdim (int, *optional*, defaults to 527): Dimensionality of the output. Default to 527.52 patch_size (int, *optional*, defaults to 16): Size of the patches. Default to 16.53 patch_stride (int, *optional*, defaults to 16): Stride of the patches. Default to 16.54 pooling (str, *optional*, defaults to `"mean"`):55 Type of pooling to use for the output. Can be "mean", "token", "dm" or "logit". Default to "mean".56 qkv_bias (bool, *optional*, defaults to `True`):57 Whether to include bias terms in the query, key and value projections. Default to True.58 target_length (int, *optional*, defaults to 1012): Frames of an audio chunk. Default to 1012.59 """60 61 def __init__(62 self,63 name=None,64 attn_drop_rate=0.0,65 depth=12,66 drop_path_rate=0.0,67 drop_rate=0.0,68 embed_dim=768,69 eval_avg="mean",70 mlp_ratio=4.0,71 num_heads=12,72 outputdim=527,73 patch_size=16,74 patch_stride=16,75 pooling="mean",76 qkv_bias=True,77 target_length=1012,78 **kwargs,79 ):80 r"""81 TODO: Add docstring82 """83 84 super().__init__(**kwargs)85 86 if name == "ced-tiny":87 embed_dim = 19288 num_heads = 389 elif name == "ced-mini":90 embed_dim = 25691 num_heads = 492 elif name == "ced-small":93 embed_dim = 38494 num_heads = 695 elif name == "ced-base":96 embed_dim = 76897 num_heads = 1298 else:99 logger.info("No model name specified for CedConfig, use default settings.")100 101 assert pooling in ("mean", "token", "dm", "logit")102 self.name = name103 self.attn_drop_rate = attn_drop_rate104 self.center = kwargs.get("center", True)105 self.depth = depth106 self.drop_path_rate = drop_path_rate107 self.drop_rate = drop_rate108 self.embed_dim = embed_dim109 self.eval_avg = eval_avg110 self.f_max = kwargs.get("f_max", 8000)111 self.f_min = kwargs.get("f_min", 0)112 self.hop_size = kwargs.get("hop_size", 160)113 self.mlp_ratio = mlp_ratio114 self.n_fft = kwargs.get("n_fft", 512)115 self.n_mels = kwargs.get("n_mels", 64)116 self.n_mels = kwargs.get("n_mels", 64)117 self.num_heads = num_heads118 self.outputdim = outputdim119 self.pad_last = kwargs.get("pad_last", True)120 self.patch_size = patch_size121 self.patch_stride = patch_stride122 self.pooling = pooling123 self.qkv_bias = qkv_bias124 self.target_length = target_length125 self.win_size = kwargs.get("win_size", 512)126 self.loss = "BCE"127 128 if self.outputdim == 527:129 with open(cached_file("topel/ConvNeXt-Tiny-AT", "class_labels_indices.csv"), "r") as f:130 reader = csv.reader(f) 131 next(reader) # skip header 132 self.id2label = {} 133 for row in reader: 134 idx = int(row[0]) 135 label = row[2] 136 if label not in self.id2label.values(): 137 self.id2label[idx] = label138 self.label2id = {v: k for k, v in self.id2label.items()}139 else:140 self.id2label = None141 self.label2id = None142 