code
stringlengths
17
6.64M
class UpstreamExpert(UpstreamBase): '\n Extract baseline features from wavforms by torchaudio.compliance.kaldi or torchaudio preprocessor\n Support: spectrogram, fbank, mfcc, mel, linear\n ' def __init__(self, model_config, **kwargs): super().__init__(**kwargs) with open(model_config, 'r') as file: self.config = yaml.load(file, Loader=yaml.FullLoader) if ('kaldi' in self.config): (self.extracter, self.output_dim, frame_shift) = get_extracter(self.config) self.downsample_rate = round(((frame_shift * SAMPLE_RATE) / 1000)) else: (self.extracter, self.output_dim, _) = get_preprocessor(self.config, process_input_only=True) self.downsample_rate = round(((self.config.get('hop_ms', 10) * SAMPLE_RATE) / 1000)) def _extractor_forward(self, wavs): feats = [] for wav in wavs: feats.append(self.extracter(wav)) return feats def get_downsample_rates(self, key: str) -> int: return self.downsample_rate def _preprocessor_forward(self, wavs): wav_lengths = [len(wav) for wav in wavs] feats = pad_sequence(wavs, batch_first=True) feats = feats.unsqueeze(1) feats = self.extracter(feats)[0] ratio = (len(feats[0]) / wav_lengths[0]) feat_lengths = [round((l * ratio)) for l in wav_lengths] feats = [f[:l] for (f, l) in zip(feats, feat_lengths)] return feats def forward(self, wavs): if ('kaldi' in self.config): feats = self._extractor_forward(wavs) else: feats = self._preprocessor_forward(wavs) padded_feats = pad_sequence(feats, batch_first=True) return {'last_hidden_state': padded_feats, 'hidden_states': [padded_feats]}
def get_extracter(config): transforms = [ExtractAudioFeature(**config.get('kaldi', {})), Delta(**config.get('delta', {})), CMVN(**config.get('cmvn', {}))] extracter = nn.Sequential(*transforms) output_dim = extracter(torch.randn((EXAMPLE_SEC * SAMPLE_RATE))).size((- 1)) return (extracter, output_dim, extracter[0].frame_shift)
class ExtractAudioFeature(nn.Module): def __init__(self, feat_type='fbank', **kwargs): super(ExtractAudioFeature, self).__init__() self.extract_fn = eval(f'torchaudio.compliance.kaldi.{feat_type}') self.kwargs = kwargs[feat_type] self.frame_shift = self.kwargs.get('frame_shift', 10.0) def forward(self, waveform): x = self.extract_fn(waveform.view(1, (- 1)), sample_frequency=SAMPLE_RATE, **self.kwargs) return x
class Delta(nn.Module): def __init__(self, order=2, **kwargs): super(Delta, self).__init__() self.order = order self.compute_delta = transforms.ComputeDeltas(**kwargs) def forward(self, x): feats = [x] for o in range(self.order): feat = feats[(- 1)].transpose(0, 1).unsqueeze(0) delta = self.compute_delta(feat) feats.append(delta.squeeze(0).transpose(0, 1)) x = torch.cat(feats, dim=(- 1)) return x
class CMVN(nn.Module): def __init__(self, use_cmvn, eps=1e-10): super(CMVN, self).__init__() self.eps = eps self.use_cmvn = use_cmvn def forward(self, x): if self.use_cmvn: x = ((x - x.mean(dim=0, keepdim=True)) / (self.eps + x.std(dim=0, keepdim=True))) return x
def baseline_local(model_config, *args, **kwargs): '\n Baseline feature\n model_config: PATH\n ' assert os.path.isfile(model_config) return _UpstreamExpert(model_config, *args, **kwargs)
def baseline(*args, **kwargs): '\n Baseline feature - Fbank, or Mel-scale spectrogram\n ' return fbank(*args, **kwargs)
def spectrogram(*args, **kwargs): '\n Baseline feature - Linear-scale spectrogram\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'spectrogram.yaml') return baseline_local(*args, **kwargs)
def fbank(*args, **kwargs): '\n Baseline feature - Fbank, or Mel-scale spectrogram\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'fbank.yaml') return baseline_local(*args, **kwargs)
def fbank_no_cmvn(*args, **kwargs): '\n Baseline feature - Fbank, or Mel-scale spectrogram\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'fbank_no_cmvn.yaml') return baseline_local(*args, **kwargs)
def mfcc(*args, **kwargs): '\n Baseline feature - MFCC\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'mfcc.yaml') return baseline_local(*args, **kwargs)
def mel(*args, **kwargs): '\n Baseline feature - Mel\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'mel.yaml') return baseline_local(*args, **kwargs)
def linear(*args, **kwargs): '\n Baseline feature - Linear\n ' kwargs['model_config'] = os.path.join(os.path.dirname(__file__), 'linear.yaml') return baseline_local(*args, **kwargs)
def load_yaml_config(path_to_config): 'Loads yaml configuration settings as an EasyDict object.' path_to_config = Path(path_to_config) assert path_to_config.is_file() with open(path_to_config) as f: yaml_contents = yaml.safe_load(f) return Namespace(**yaml_contents)
class PrecomputedNorm(nn.Module): 'Normalization using Pre-computed Mean/Std.\n Args:\n stats: Precomputed (mean, std).\n axis: Axis setting used to calculate mean/variance.\n ' def __init__(self, stats, axis=[1, 2]): super().__init__() self.axis = axis (self.mean, self.std) = stats def forward(self, X: torch.Tensor) -> torch.Tensor: return ((X - self.mean) / self.std) def __repr__(self): format_string = (self.__class__.__name__ + f'(mean={self.mean}, std={self.std}, axis={self.axis})') return format_string
class NetworkCommonMixIn(): 'Common mixin for network definition.' def load_weight(self, weight_file, device): 'Utility to load a weight file to a device.' state_dict = torch.load(weight_file, map_location=device) if ('state_dict' in state_dict): state_dict = state_dict['state_dict'] weights = {} for k in state_dict: m = re.search('(^fc\\.|\\.fc\\.|^features\\.|\\.features\\.)', k) if (m is None): continue new_k = k[m.start():] new_k = (new_k[1:] if (new_k[0] == '.') else new_k) weights[new_k] = state_dict[k] self.load_state_dict(weights) self.eval() logging.info(f'Using audio embbeding network pretrained weight: {Path(weight_file).name}') return self def set_trainable(self, trainable=False): for p in self.parameters(): if p.requires_grad: p.requires_grad = trainable
class AudioNTT2020Task6(nn.Module, NetworkCommonMixIn): 'DCASE2020 Task6 NTT Solution Audio Embedding Network.' def __init__(self, n_mels, d): super().__init__() self.features = nn.Sequential(nn.Conv2d(1, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2), nn.Conv2d(64, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2), nn.Conv2d(64, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2)) self.fc = nn.Sequential(nn.Linear((64 * (n_mels // (2 ** 3))), d), nn.ReLU(), nn.Dropout(p=0.3), nn.Linear(d, d), nn.ReLU()) self.d = d def forward(self, x): x = self.features(x) x = x.permute(0, 3, 2, 1) (B, T, D, C) = x.shape x = x.reshape((B, T, (C * D))) x = self.fc(x) return x
class AudioNTT2020(AudioNTT2020Task6): 'BYOL-A General Purpose Representation Network.\n This is an extension of the DCASE 2020 Task 6 NTT Solution Audio Embedding Network.\n ' def __init__(self, n_mels=64, d=512): super().__init__(n_mels=n_mels, d=d) def forward(self, x): x = super().forward(x) (x1, _) = torch.max(x, dim=1) x2 = torch.mean(x, dim=1) x = (x1 + x2) assert ((x.shape[1] == self.d) and (x.ndim == 2)) return x
def byol_a_2048(refresh=False, **kwds): ckpt = _urls_to_filepaths('https://github.com/nttcslab/byol-a/raw/master/pretrained_weights/AudioNTT2020-BYOLA-64x96d2048.pth', refresh=refresh) return _UpstreamExpert(ckpt, DEFAULT_CONFIG_PATH, 2048, **kwds)
def byol_a_1024(refresh=False, **kwds): ckpt = _urls_to_filepaths('https://github.com/nttcslab/byol-a/raw/master/pretrained_weights/AudioNTT2020-BYOLA-64x96d1024.pth', refresh=refresh) return _UpstreamExpert(ckpt, DEFAULT_CONFIG_PATH, 1024, **kwds)
def byol_a_512(refresh=False, **kwds): ckpt = _urls_to_filepaths('https://github.com/nttcslab/byol-a/raw/master/pretrained_weights/AudioNTT2020-BYOLA-64x96d512.pth', refresh=refresh) return _UpstreamExpert(ckpt, DEFAULT_CONFIG_PATH, 512, **kwds)
def default(val, def_val): return (def_val if (val is None) else val)
def flatten(t): return t.reshape(t.shape[0], (- 1))
def singleton(cache_key): def inner_fn(fn): @wraps(fn) def wrapper(self, *args, **kwargs): instance = getattr(self, cache_key) if (instance is not None): return instance instance = fn(self, *args, **kwargs) setattr(self, cache_key, instance) return instance return wrapper return inner_fn
def get_module_device(module): return next(module.parameters()).device
def set_requires_grad(model, val): for p in model.parameters(): p.requires_grad = val
def loss_fn(x, y): x = F.normalize(x, dim=(- 1), p=2) y = F.normalize(y, dim=(- 1), p=2) return (2 - (2 * (x * y).sum(dim=(- 1))))
class EMA(): def __init__(self, beta): super().__init__() self.beta = beta def update_average(self, old, new): if (old is None): return new return ((old * self.beta) + ((1 - self.beta) * new))
def update_moving_average(ema_updater, ma_model, current_model): for (current_params, ma_params) in zip(current_model.parameters(), ma_model.parameters()): (old_weight, up_weight) = (ma_params.data, current_params.data) ma_params.data = ema_updater.update_average(old_weight, up_weight)
class MLP(nn.Module): def __init__(self, dim, projection_size, hidden_size=4096, use_bn=True): super().__init__() self.lin1 = nn.Linear(dim, hidden_size) self.lin2 = nn.Linear(hidden_size, projection_size) self.use_bn = use_bn self.bn = nn.BatchNorm1d(hidden_size) self.relu = nn.ReLU(inplace=True) 'self.net = nn.Sequential(\n nn.Linear(dim, hidden_size),\n nn.BatchNorm1d(hidden_size),\n nn.ReLU(inplace=True),\n nn.Linear(hidden_size, projection_size)\n )' def forward(self, x): x = self.lin1(x) if self.use_bn: x = self.bn(x) x = self.relu(x) x = self.lin2(x) return x
class NetWrapper(nn.Module): def __init__(self, net, projection_size, projection_hidden_size, layer=(- 2)): super().__init__() self.net = net self.layer = layer self.projector = None self.projection_size = projection_size self.projection_hidden_size = projection_hidden_size self.hidden = {} self.hook_registered = False def _find_layer(self): if (type(self.layer) == str): modules = dict([*self.net.named_modules()]) return modules.get(self.layer, None) elif (type(self.layer) == int): children = [*self.net.children()] return children[self.layer] return None def _hook(self, _, input, output): device = input[0].device self.hidden[device] = flatten(output) def _register_hook(self): layer = self._find_layer() assert (layer is not None), f'hidden layer ({self.layer}) not found' handle = layer.register_forward_hook(self._hook) self.hook_registered = True @singleton('projector') def _get_projector(self, hidden): (_, dim) = hidden.shape projector = MLP(dim, self.projection_size, self.projection_hidden_size) return projector.to(hidden) def get_representation(self, x): if (self.layer == (- 1)): return self.net(x) if (not self.hook_registered): self._register_hook() self.hidden.clear() _ = self.net(x) hidden = self.hidden[x.device] self.hidden.clear() assert (hidden is not None), f'hidden layer {self.layer} never emitted an output' return hidden def forward(self, x, return_projection=True): representation = self.get_representation(x) if (not return_projection): return representation projector = self._get_projector(representation) projection = projector(representation) return (projection, representation)
class BYOL(nn.Module): 'BYOL training module that is:\n - Decoupled augmentations.\n - Accepts two augmented inputs independently.\n ' def __init__(self, net, image_size, hidden_layer=(- 1), projection_size=256, projection_hidden_size=4096, moving_average_decay=0.99, use_momentum=True, channels=1): super().__init__() self.net = net self.online_encoder = NetWrapper(net, projection_size, projection_hidden_size, layer=hidden_layer) self.use_momentum = use_momentum self.target_encoder = None self.target_ema_updater = EMA(moving_average_decay) self.online_predictor = MLP(projection_size, projection_size, projection_hidden_size) device = get_module_device(net) self.to(device) with torch.no_grad(): self.forward(torch.randn(2, channels, image_size[0], image_size[1]), torch.randn(2, channels, image_size[0], image_size[1])) @singleton('target_encoder') def _get_target_encoder(self): target_encoder = copy.deepcopy(self.online_encoder) set_requires_grad(target_encoder, False) return target_encoder def reset_moving_average(self): del self.target_encoder self.target_encoder = None def update_moving_average(self): assert self.use_momentum, 'you do not need to update the moving average, since you have turned off momentum for the target encoder' assert (self.target_encoder is not None), 'target encoder has not been created yet' update_moving_average(self.target_ema_updater, self.target_encoder, self.online_encoder) def forward(self, image_one, image_two, return_embedding=False, return_projection=True): (online_proj_one, _) = self.online_encoder(image_one) (online_proj_two, _) = self.online_encoder(image_two) online_pred_one = self.online_predictor(online_proj_one) online_pred_two = self.online_predictor(online_proj_two) with torch.no_grad(): target_encoder = (self._get_target_encoder() if self.use_momentum else self.online_encoder) (target_proj_one, _) = target_encoder(image_one) (target_proj_two, _) = target_encoder(image_two) target_proj_one.detach_() target_proj_two.detach_() loss_one = loss_fn(online_pred_one, target_proj_two.detach()) loss_two = loss_fn(online_pred_two, target_proj_one.detach()) loss = (loss_one + loss_two) return loss.mean()
def get_timestamp(): 'ex) Outputs 202104220830' return datetime.datetime.now().strftime('%y%m%d%H%M')
def load_yaml_config(path_to_config): 'Loads yaml configuration settings as an EasyDict object.' path_to_config = Path(path_to_config) assert path_to_config.is_file() with open(path_to_config) as f: yaml_contents = yaml.safe_load(f) cfg = Namespace(**yaml_contents) return cfg
def get_logger(name): logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M', level=logging.DEBUG) logger = logging.getLogger(name) return logger
class MelSpectrogramLibrosa(): 'Mel spectrogram using librosa.' def __init__(self, fs=16000, n_fft=1024, shift=160, n_mels=64, fmin=60, fmax=7800): (self.fs, self.n_fft, self.shift, self.n_mels, self.fmin, self.fmax) = (fs, n_fft, shift, n_mels, fmin, fmax) self.mfb = librosa.filters.mel(sr=fs, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax) def __call__(self, audio): X = librosa.stft(np.array(audio), n_fft=self.n_fft, hop_length=self.shift) return torch.tensor(np.matmul(self.mfb, ((np.abs(X) ** 2) + np.finfo(float).eps)))
class WaveInLMSOutDataset(Dataset): 'Wave in, log-mel spectrogram out, dataset class.\n\n Choosing librosa or torchaudio:\n librosa: Stable but slower.\n torchaudio: Faster but cannot reproduce the exact performance of pretrained weight,\n which might be caused by the difference with librosa. Librosa was used in the pretraining.\n\n Args:\n cfg: Configuration settings.\n audio_files: List of audio file pathnames.\n labels: List of labels corresponding to the audio files.\n transform: Transforms (augmentations), callable.\n use_librosa: True if using librosa for converting audio to log-mel spectrogram (LMS).\n ' def __init__(self, cfg, audio_files, labels, transform, use_librosa=False): assert ((labels is None) or (len(audio_files) == len(labels))), 'The number of audio files and labels has to be the same.' super().__init__() self.cfg = cfg self.files = audio_files self.labels = labels self.transform = transform self.unit_length = int((cfg.unit_sec * cfg.sample_rate)) self.to_melspecgram = (MelSpectrogramLibrosa(fs=cfg.sample_rate, n_fft=cfg.n_fft, shift=cfg.hop_length, n_mels=cfg.n_mels, fmin=cfg.f_min, fmax=cfg.f_max) if use_librosa else torchaudio.transforms.MelSpectrogram(sample_rate=cfg.sample_rate, n_fft=cfg.n_fft, win_length=cfg.win_length, hop_length=cfg.hop_length, n_mels=cfg.n_mels, f_min=cfg.f_min, f_max=cfg.f_max, power=2)) def __len__(self): return len(self.files) def __getitem__(self, idx): try: (wav, sr) = torchaudio.load(self.files[idx]) except RuntimeError: print(self.files[idx]) raise FileNotFoundError(self.files[idx]) assert (sr == self.cfg.sample_rate), f'Convert .wav files to {self.cfg.sample_rate} Hz. {self.files[idx]} has {sr} Hz.' assert (wav.shape[0] == 1), f'Convert .wav files to single channel audio, {self.files[idx]} has {wav.shape[0]} channels.' wav = wav[0] length_adj = (self.unit_length - len(wav)) if (length_adj > 0): half_adj = (length_adj // 2) wav = F.pad(wav, (half_adj, (length_adj - half_adj))) length_adj = (self.unit_length - len(wav)) start = (random.randint(0, length_adj) if (length_adj > 0) else 0) wav = wav[start:(start + self.unit_length)] lms = (self.to_melspecgram(wav) + torch.finfo().eps).log().unsqueeze(0) if self.transform: lms = self.transform(lms) if (self.labels is not None): return (lms, torch.tensor(self.labels[idx])) return lms
class AudioNTT2020Task6(nn.Module, NetworkCommonMixIn): 'DCASE2020 Task6 NTT Solution Audio Embedding Network.' def __init__(self, n_mels, d): super().__init__() self.features = nn.Sequential(nn.Conv2d(1, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2), nn.Conv2d(64, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2), nn.Conv2d(64, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, stride=2)) self.fc = nn.Sequential(nn.Linear((64 * (n_mels // (2 ** 3))), d), nn.ReLU(), nn.Dropout(p=0.3), nn.Linear(d, d), nn.ReLU()) self.d = d def forward(self, x): x = self.features(x) x = x.permute(0, 3, 2, 1) (B, T, D, C) = x.shape x = x.reshape((B, T, (C * D))) x = self.fc(x) return x
class AudioNTT2020(AudioNTT2020Task6): 'BYOL-A General Purpose Representation Network.\n\n This is an extension of the DCASE 2020 Task 6 NTT Solution Audio Embedding Network.\n ' sample_rate = 16000 embedding_size = 2048 scene_embedding_size = embedding_size timestamp_embedding_size = embedding_size def __init__(self, n_mels=64, d=512): super().__init__(n_mels=n_mels, d=d) def forward(self, x): x = super().forward(x) x = (x.mean(1) + x.amax(1)) assert ((x.shape[1] == self.d) and (x.ndim == 2)) return x
def group_dict_by_key(cond, d): return_val = [dict(), dict()] for key in d.keys(): match = bool(cond(key)) ind = int((not match)) return_val[ind][key] = d[key] return (*return_val,)
def group_by_key_prefix_and_remove_prefix(prefix, d): (kwargs_with_prefix, kwargs) = group_dict_by_key((lambda x: x.startswith(prefix)), d) kwargs_without_prefix = dict(map((lambda x: (x[0][len(prefix):], x[1])), tuple(kwargs_with_prefix.items()))) return (kwargs_without_prefix, kwargs)
class LayerNorm(nn.Module): 'Layer normalization, but done in channel dimension #1' def __init__(self, dim, eps=1e-05): super().__init__() self.eps = eps self.g = nn.Parameter(torch.ones(1, dim, 1, 1)) self.b = nn.Parameter(torch.zeros(1, dim, 1, 1)) def forward(self, x): std = torch.var(x, dim=1, unbiased=False, keepdim=True).sqrt() mean = torch.mean(x, dim=1, keepdim=True) return ((((x - mean) / (std + self.eps)) * self.g) + self.b)
class PreNorm(nn.Module): 'Pre-Normalization layer' def __init__(self, dim, fn): super().__init__() self.norm = LayerNorm(dim) self.fn = fn def forward(self, x, **kwargs): x = self.norm(x) return self.fn(x, **kwargs)
class FeedForward(nn.Module): 'Convolutional projection in the transformer.' def __init__(self, dim, mult=4, dropout=0.0): super().__init__() self.net = nn.Sequential(nn.Conv2d(dim, (dim * mult), 1), nn.GELU(), nn.Dropout(dropout), nn.Conv2d((dim * mult), dim, 1), nn.Dropout(dropout)) def forward(self, x): return self.net(x)
class DepthWiseConv2d(nn.Module): 'Depthwise convolutional layer' def __init__(self, dim_in, dim_out, kernel_size, padding, stride, bias=True): super().__init__() self.net = nn.Sequential(nn.Conv2d(dim_in, dim_in, kernel_size=kernel_size, padding=padding, groups=dim_in, stride=stride, bias=bias), nn.BatchNorm2d(dim_in), nn.Conv2d(dim_in, dim_out, kernel_size=1, bias=bias)) def forward(self, x): return self.net(x)
class Attention(nn.Module): 'Custom Attention layer' def __init__(self, dim, proj_kernel, kv_proj_stride, heads=8, dim_head=64, dropout=0.0): super().__init__() inner_dim = (dim_head * heads) padding = (proj_kernel // 2) self.heads = heads self.scale = (dim_head ** (- 0.5)) self.attend = nn.Softmax(dim=(- 1)) self.to_q = DepthWiseConv2d(dim, inner_dim, proj_kernel, padding=padding, stride=1, bias=False) self.to_kv = DepthWiseConv2d(dim, (inner_dim * 2), proj_kernel, padding=padding, stride=kv_proj_stride, bias=False) self.to_out = nn.Sequential(nn.Conv2d(inner_dim, dim, 1), nn.Dropout(dropout)) def forward(self, x): from einops import rearrange, repeat from einops.layers.torch import Rearrange shape = x.shape (b, n, _, y, h) = (*shape, self.heads) (q, k, v) = (self.to_q(x), *self.to_kv(x).chunk(2, dim=1)) (q, k, v) = map((lambda t: rearrange(t, 'b (h d) x y -> (b h) (x y) d', h=h)), (q, k, v)) dots = (torch.einsum('b i d, b j d -> b i j', q, k) * self.scale) attn = self.attend(dots) out = torch.einsum('b i j, b j d -> b i d', attn, v) out = rearrange(out, '(b h) (x y) d -> b (h d) x y', h=h, y=y) return self.to_out(out)
class Transformer(nn.Module): 'Custom Transformer layer.' def __init__(self, dim, proj_kernel, kv_proj_stride, depth, heads, dim_head=64, mlp_mult=4, dropout=0.0): super().__init__() self.layers = nn.ModuleList([]) for _ in range(depth): self.layers.append(nn.ModuleList([PreNorm(dim, Attention(dim, proj_kernel=proj_kernel, kv_proj_stride=kv_proj_stride, heads=heads, dim_head=dim_head, dropout=dropout)), PreNorm(dim, FeedForward(dim, mlp_mult, dropout=dropout))])) def forward(self, x): for (attn, ff) in self.layers: x = (attn(x) + x) x = (ff(x) + x) return x
class CvT(nn.Module): 'Convolutional Transformer module.\n\n Adapted for self-supervised training\n\n Attributes\n ----------\n s{i}_emb_dim: int\n Embedding dimention at stage i\n\n s{i}_emb_kernel: int\n Convolutional kernel size at stage i\n\n s{i}_emb_stride: int\n Convolutional stride at stage i\n\n s{i}_kv_proj_stride: int\n Convolutional stride in the convolutional projection layers at stage i\n\n s{i}_heads: int\n Number of attention heads at stage i\n\n s{i}_depth: int\n Transformer depth at stage i\n\n s{i}_mlp_mult: int\n MLP ratio at stage i\n\n dropout: float\n Dropout ratio\n ' sample_rate = 16000 embedding_size = 2048 scene_embedding_size = embedding_size timestamp_embedding_size = embedding_size def __init__(self, *, s1_emb_dim=64, s1_emb_kernel=7, s1_emb_stride=4, s1_proj_kernel=3, s1_kv_proj_stride=2, s1_heads=1, s1_depth=1, s1_mlp_mult=4, s2_emb_dim=192, s2_emb_kernel=3, s2_emb_stride=2, s2_proj_kernel=3, s2_kv_proj_stride=2, s2_heads=3, s2_depth=2, s2_mlp_mult=4, s3_emb_dim=384, s3_emb_kernel=3, s3_emb_stride=2, s3_proj_kernel=3, s3_kv_proj_stride=2, s3_heads=6, s3_depth=10, s3_mlp_mult=4, dropout=0.0, pool='mean'): super().__init__() kwargs = dict(locals()) dim = 1 layers = [] for prefix in ('s1', 's2', 's3'): (config, kwargs) = group_by_key_prefix_and_remove_prefix(f'{prefix}_', kwargs) layers.append(nn.Sequential(nn.Conv2d(dim, config['emb_dim'], kernel_size=config['emb_kernel'], padding=(config['emb_kernel'] // 2), stride=config['emb_stride']), LayerNorm(config['emb_dim']), Transformer(dim=config['emb_dim'], proj_kernel=config['proj_kernel'], kv_proj_stride=config['kv_proj_stride'], depth=config['depth'], heads=config['heads'], mlp_mult=config['mlp_mult'], dropout=dropout))) dim = config['emb_dim'] self.pool = pool assert (self.pool in ['mean', 'max', 'mean+max']) if (self.pool == 'mean'): self.pool_layers = nn.Sequential(nn.AdaptiveAvgPool2d(1), nn.Flatten()) elif (self.pool == 'max'): self.pool_layers = (nn.Sequential(nn.AdaptiveMaxPool2d(1), nn.Flatten()),) else: self.pool_layers = nn.Sequential(nn.Identity()) self.layers = nn.Sequential(*layers, *self.pool_layers) def forward(self, x): x = self.layers(x) if (self.pool == 'mean+max'): x = x.permute(0, 3, 2, 1) (B, T, D, C) = x.shape x = x.reshape((B, T, (C * D))) x = (x.mean(1) + x.amax(1)) return x
def conv3x3(in_planes: int, out_planes: int, stride: int=1, groups: int=1, dilation: int=1, standardize_weights: bool=False) -> nn.Conv2d: '3x3 convolution with padding' conv = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, groups=groups, bias=False, dilation=dilation) return (weight_norm(conv) if standardize_weights else conv)
def conv1x1(in_planes: int, out_planes: int, stride: int=1, standardize_weights: bool=False) -> nn.Conv2d: '1x1 convolution' conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) return (weight_norm(conv) if standardize_weights else conv)
class BasicBlock(nn.Module): expansion: int = 1 def __init__(self, inplanes: int, planes: int, stride: int=1, downsample: Optional[nn.Module]=None, groups: int=1, base_width: int=64, dilation: int=1, norm_layer: Optional[Callable[(..., nn.Module)]]=None, standardize_weights: bool=False) -> None: super(BasicBlock, self).__init__() if (norm_layer is None): norm_layer = nn.BatchNorm2d if ((groups != 1) or (base_width != 64)): raise ValueError('BasicBlock only supports groups=1 and base_width=64') if (dilation > 1): raise NotImplementedError('Dilation > 1 not supported in BasicBlock') self.conv1 = conv3x3(inplanes, planes, stride, standardize_weights=standardize_weights) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes, standardize_weights=standardize_weights) self.bn2 = norm_layer(planes) self.downsample = downsample self.stride = stride def forward(self, x: Tensor) -> Tensor: identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if (self.downsample is not None): identity = self.downsample(x) out += identity out = self.relu(out) return out
class Bottleneck(nn.Module): expansion: int = 4 def __init__(self, inplanes: int, planes: int, stride: int=1, downsample: Optional[nn.Module]=None, groups: int=1, base_width: int=64, dilation: int=1, norm_layer: Optional[Callable[(..., nn.Module)]]=None, standardize_weights: bool=False) -> None: super(Bottleneck, self).__init__() if (norm_layer is None): norm_layer = nn.BatchNorm2d width = (int((planes * (base_width / 64.0))) * groups) self.conv1 = conv1x1(inplanes, width, standardize_weights=standardize_weights) self.bn1 = norm_layer(width) self.conv2 = conv3x3(width, width, stride, groups, dilation, standardize_weights=standardize_weights) self.bn2 = norm_layer(width) self.conv3 = conv1x1(width, (planes * self.expansion), standardize_weights=standardize_weights) self.bn3 = norm_layer((planes * self.expansion)) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x: Tensor) -> Tensor: identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if (self.downsample is not None): identity = self.downsample(x) out += identity out = self.relu(out) return out
class ResNetish(nn.Module): sample_rate = 16000 embedding_size = 2048 scene_embedding_size = embedding_size timestamp_embedding_size = embedding_size def __init__(self, block: Type[Union[(BasicBlock, Bottleneck)]], layers: List[int], num_classes: int=1000, zero_init_residual: bool=False, groups: int=1, width_per_group: int=64, replace_stride_with_dilation: Optional[List[bool]]=None, norm_layer: Optional[Callable[(..., nn.Module)]]=None, standardize_weights: bool=False) -> None: super(ResNetish, self).__init__() if (norm_layer is None): norm_layer = nn.BatchNorm2d self._norm_layer = norm_layer self.inplanes = 64 self.dilation = 1 if (replace_stride_with_dilation is None): replace_stride_with_dilation = [False, False, False] if (len(replace_stride_with_dilation) != 3): raise ValueError('replace_stride_with_dilation should be None or a 3-element tuple, got {}'.format(replace_stride_with_dilation)) self.groups = groups self.base_width = width_per_group conv1 = nn.Conv2d(1, self.inplanes, kernel_size=7, stride=1, padding=3, bias=False) self.conv1 = (weight_norm(conv1) if standardize_weights else conv1) self.bn1 = norm_layer(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], standardize_weights=standardize_weights) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0], standardize_weights=standardize_weights) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1], standardize_weights=standardize_weights) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2], standardize_weights=standardize_weights) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0) def _make_layer(self, block: Type[Union[(BasicBlock, Bottleneck)]], planes: int, blocks: int, stride: int=1, dilate: bool=False, standardize_weights: bool=False) -> nn.Sequential: norm_layer = self._norm_layer downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if ((stride != 1) or (self.inplanes != (planes * block.expansion))): downsample = nn.Sequential(conv1x1(self.inplanes, (planes * block.expansion), stride, standardize_weights), norm_layer((planes * block.expansion))) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer)) self.inplanes = (planes * block.expansion) for _ in range(1, blocks): layers.append(block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer)) return nn.Sequential(*layers) def _forward_impl(self, x: Tensor) -> Tensor: x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = x.permute(0, 3, 2, 1) (B, T, D, C) = x.shape x = x.reshape((B, T, (C * D))) x = (x.mean(1) + x.amax(1)) return x def forward(self, x: Tensor) -> Tensor: return self._forward_impl(x)
def _resnetish(arch: str, block: Type[Union[(BasicBlock, Bottleneck)]], layers: List[int], pretrained: bool, progress: bool, **kwargs: Any) -> ResNetish: model = ResNetish(block, layers, **kwargs) return model
def resnetish10(pretrained: bool=False, progress: bool=True, **kwargs: Any) -> ResNetish: 'ResNet-10 model from\n `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_.\n Args:\n pretrained (bool): If True, returns a model pre-trained on ImageNet\n progress (bool): If True, displays a progress bar of the download to stderr\n ' return _resnetish('resnetish18', BasicBlock, [1, 1, 1, 1], pretrained, progress, **kwargs)
def resnetish18(pretrained: bool=False, progress: bool=True, **kwargs: Any) -> ResNetish: 'ResNet-18 model from\n `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_.\n Args:\n pretrained (bool): If True, returns a model pre-trained on ImageNet\n progress (bool): If True, displays a progress bar of the download to stderr\n ' return _resnetish('resnetish18', BasicBlock, [2, 2, 2, 2], pretrained, progress, **kwargs)
def resnetish34(pretrained: bool=False, progress: bool=True, **kwargs: Any) -> ResNetish: 'ResNet-34 model from\n `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_.\n Adapted for Audio from\n `"CNN architectures for large-scale audio classification" <https://arxiv.org/abs/1609.09430>`_.\n Args:\n pretrained (bool): If True, returns a model pre-trained on ImageNet\n progress (bool): If True, displays a progress bar of the download to stderr\n ' return _resnetish('resnetish34', BasicBlock, [3, 4, 6, 3], pretrained, progress, **kwargs)
def resnetish50(pretrained: bool=False, progress: bool=True, **kwargs: Any) -> ResNetish: 'ResNet-50 model from\n `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_.\n Adapted for Audio from\n `"CNN architectures for large-scale audio classification" <https://arxiv.org/abs/1609.09430>`_.\n Args:\n pretrained (bool): If True, returns a model pre-trained on ImageNet\n progress (bool): If True, displays a progress bar of the download to stderr\n ' return _resnetish('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress, **kwargs)
class Lambda(nn.Module): '[NOT USED] Custom tensorflow-like Lambda function layer.' def __init__(self, function): super(Lambda, self).__init__() self.function = function def forward(self, x: Tensor) -> Tensor: return self.function(x)
class NetworkCommonMixIn(): 'Common mixin for network definition.' def load_weight(self, weight_file, device): 'Utility to load a weight file to a device.' state_dict = torch.load(weight_file, map_location=device) if ('state_dict' in state_dict): state_dict = state_dict['state_dict'] weights = {} for k in state_dict: m = re.search('(^fc\\.|\\.fc\\.|^features\\.|\\.features\\.)', k) if (m is None): continue new_k = k[m.start():] new_k = (new_k[1:] if (new_k[0] == '.') else new_k) weights[new_k] = state_dict[k] self.load_state_dict(weights) self.eval() logging.info(f'Using audio embbeding network pretrained weight: {Path(weight_file).name}') return self def set_trainable(self, trainable=False): for p in self.parameters(): if p.requires_grad: p.requires_grad = trainable
class UpstreamExpert(nn.Module): def __init__(self, ckpt: str=None, model_name: str=None, window_secs: float=1, hop_secs: float=0.05, model_config: str=None): super().__init__() self.model = serab.load_model(ckpt, model_name) self.frame_duration = (window_secs * 1000) self.hop_size = (hop_secs * 1000) self.model_config = model_config def get_downsample_rates(self, key: str=None) -> int: return int(((self.hop_size / 1000) * SAMPLE_RATE)) def forward(self, wavs: List[Tensor]) -> Dict[(str, Union[(Tensor, List[Tensor])])]: padded_wavs = pad_sequence(wavs, batch_first=True) (embeddings, timestamps) = serab.get_timestamp_embeddings(padded_wavs, self.model, self.frame_duration, self.hop_size) return {'hidden_states': [embeddings]}
def byol_s_default(refresh: bool=False, **kwds): kwds['model_name'] = 'default' kwds['ckpt'] = _urls_to_filepaths('https://github.com/GasserElbanna/serab-byols/raw/main/checkpoints/default2048_BYOLAs64x96-2105311814-e100-bs256-lr0003-rs42.pth', refresh=refresh) return _UpstreamExpert(**kwds)
def byol_s_cvt(refresh: bool=False, **kwds): kwds['model_name'] = 'cvt' kwds['ckpt'] = _urls_to_filepaths('https://github.com/GasserElbanna/serab-byols/raw/main/checkpoints/cvt_s1-d1-e64_s2-d1-e256_s3-d1-e512_BYOLAs64x96-osandbyolaloss6373-e100-bs256-lr0003-rs42.pth', refresh=refresh) return _UpstreamExpert(**kwds)
def byol_s_resnetish34(refresh: bool=False, **kwds): kwds['model_name'] = 'resnetish34' kwds['ckpt'] = _urls_to_filepaths('https://github.com/GasserElbanna/serab-byols/raw/main/checkpoints/resnetish34_BYOLAs64x96-2105271915-e100-bs256-lr0003-rs42.pth', refresh=refresh) return _UpstreamExpert(**kwds)
def get_model(model_name: str='', cfg={}) -> torch.nn.Module: 'Define the model object.\n\n Parameters\n ----------\n model_name: str, the name for pretrained model\n cfg: dict, the cfg parameters\n\n Returns\n -------\n torch.nn.Module object or a tensorflow "trackable" object\n ' if (model_name == 'default'): model = AudioNTT2020(n_mels=cfg.n_mels, d=cfg.feature_d) elif (model_name == 'resnetish34'): model = resnetish34() elif (model_name == 'clstm'): model = CLSTM() elif (model_name == 'cvt'): (s1_depth, s2_depth, s3_depth) = cfg.depths (s1_emb_dim, s2_emb_dim, s3_emb_dim) = cfg.embed_dims (s1_mlp_mult, s2_mlp_mult, s3_mlp_mult) = cfg.mlp_mults model = CvT(s1_emb_dim=s1_emb_dim, s1_depth=s1_depth, s1_mlp_mult=s1_mlp_mult, s2_emb_dim=s2_emb_dim, s2_depth=s2_depth, s2_mlp_mult=s2_mlp_mult, s3_emb_dim=s3_emb_dim, s3_depth=s3_depth, s3_mlp_mult=s3_mlp_mult, pool=cfg.cvt_pool) else: raise ValueError('Model not found.') return model
def load_model(model_file_path: str='', model_name: str='default', cfg_path: str=None) -> torch.nn.Module: 'Load pre-trained DL models.\n\n Parameters\n ----------\n model_name: str, the name for pretrained model\n model_file_path: str, the path for pretrained model\n cfg_path: str, the path for yaml file including parameters value\n\n Returns\n -------\n torch.nn.Module object or a tensorflow "trackable" object\n Model loaded with pre-training weights\n ' cfg_path = (cfg_path or (Path(__file__).parent / 'config.yaml')) cfg = load_yaml_config(cfg_path) model = get_model(model_name, cfg) state_dict = torch.load(model_file_path) model.load_state_dict(state_dict) return model
def get_timestamp_embeddings(audio_list: List, model: torch.nn.Module, frame_duration: float=TIMESTAMP_FRAME_DUR, hop_size: float=TIMESTAMP_HOP_SIZE, cfg_path: str=None) -> Tuple[(Tensor, Tensor)]: '\n This function returns embeddings at regular intervals centered at timestamps. Both\n the embeddings and corresponding timestamps (in milliseconds) are returned.\n Args:\n audio_list: List of torch tensor audios.\n model: Loaded model.\n frame_duration: Frame (segement) duration in milliseconds\n hop_size: Hop size in milliseconds.\n NOTE: Not required by the HEAR API. We add this optional parameter\n to improve the efficiency of scene embedding.\n cfg_path: str, the path for yaml file including parameters value\n Returns:\n - Tensor: embeddings, A float32 Tensor with shape (n_sounds, n_timestamps,\n model.timestamp_embedding_size).\n - Tensor: timestamps, Centered timestamps in milliseconds corresponding\n to each embedding in the output. Shape: (n_sounds, n_timestamps).\n ' cfg_path = (cfg_path or (Path(__file__).parent / 'config.yaml')) cfg = load_yaml_config(cfg_path) to_melspec = MelSpectrogram(sample_rate=cfg.sample_rate, n_fft=cfg.n_fft, win_length=cfg.win_length, hop_length=cfg.hop_length, n_mels=cfg.n_mels, f_min=cfg.f_min, f_max=cfg.f_max).to(audio_list[0].device) model = model.to(audio_list[0].device) (frames, timestamps) = frame_audio(audio_list, frame_size=((frame_duration / 1000) * cfg.sample_rate), hop_size=hop_size, sample_rate=cfg.sample_rate) (audio_batches, num_frames, _) = frames.shape frames = frames.flatten(end_dim=1) melspec_frames = (to_melspec(frames) + torch.finfo(torch.float).eps).log() normalizer = PrecomputedNorm(compute_timestamp_stats(melspec_frames)) melspec_frames = normalizer(melspec_frames).unsqueeze(1) embeddings = model(melspec_frames) embeddings = embeddings.reshape(audio_batches, num_frames, (- 1)) return (embeddings, timestamps)
def get_scene_embeddings(audio_list: List, model: torch.nn.Module, cfg_path: str=None) -> Tensor: '\n This function returns a single embedding for each audio clip. In this baseline\n implementation we simply summarize the temporal embeddings from\n get_timestamp_embeddings() using torch.mean().\n Args:\n audio_list: list of torch tensor audios (audios should be resampled to 16kHz).\n model: Loaded model.\n cfg_path:\n Returns:\n - embeddings, A float32 Tensor with shape\n (n_sounds, model.scene_embedding_size).\n ' cfg_path = (cfg_path or (Path(__file__).parent / 'config.yaml')) device = audio_list[0].device cfg = load_yaml_config(cfg_path) to_melspec = MelSpectrogram(sample_rate=cfg.sample_rate, n_fft=cfg.n_fft, win_length=cfg.win_length, hop_length=cfg.hop_length, n_mels=cfg.n_mels, f_min=cfg.f_min, f_max=cfg.f_max).to(device) stats = compute_scene_stats(audio_list, to_melspec) normalizer = PrecomputedNorm(stats) model = model.to(device) embeddings = generate_byols_embeddings(model, audio_list, to_melspec, normalizer, device) return embeddings
def get_default_cpc_config(): parser = set_default_cpc_config(argparse.ArgumentParser()) return parser.parse_args([])
def set_default_cpc_config(parser): group = parser.add_argument_group('Architecture configuration', description="The arguments defining the model's architecture.") group.add_argument('--hiddenEncoder', type=int, default=256, help='Hidden dimension of the encoder network.') group.add_argument('--hiddenGar', type=int, default=256, help='Hidden dimension of the auto-regressive network') group.add_argument('--nPredicts', type=int, default=12, help='Number of steps to predict.') group.add_argument('--negativeSamplingExt', type=int, default=128, help='Number of negative samples to take.') group.add_argument('--learningRate', type=float, default=0.0002) group.add_argument('--schedulerStep', type=int, default=(- 1), help='Step of the learning rate scheduler: at each step the learning rate is divided by 2. Default: no scheduler.') group.add_argument('--schedulerRamp', type=int, default=None, help='Enable a warm up phase for the learning rate: adds a linear ramp of the given size.') group.add_argument('--beta1', type=float, default=0.9, help='Value of beta1 for the Adam optimizer') group.add_argument('--beta2', type=float, default=0.999, help='Value of beta2 for the Adam optimizer') group.add_argument('--epsilon', type=float, default=1e-08, help='Value of epsilon for the Adam optimizer') group.add_argument('--sizeWindow', type=int, default=20480, help='Number of frames to consider at each batch.') group.add_argument('--nEpoch', type=int, default=200, help='Number of epoch to run') group.add_argument('--samplingType', type=str, default='samespeaker', choices=['samespeaker', 'uniform', 'samesequence', 'sequential'], help='How to sample the negative examples in the CPC loss.') group.add_argument('--nLevelsPhone', type=int, default=1, help='(Supervised mode only). Number of layers in the phone classification network.') group.add_argument('--cpc_mode', type=str, default=None, choices=['reverse', 'none'], help='Some variations on CPC.') group.add_argument('--encoder_type', type=str, choices=['cpc', 'mfcc', 'lfb'], default='cpc', help='Replace the encoder network by mfcc features or learned filter banks') group.add_argument('--normMode', type=str, default='layerNorm', choices=['instanceNorm', 'ID', 'layerNorm', 'batchNorm'], help='Type of normalization to use in the encoder network (default is layerNorm).') group.add_argument('--onEncoder', action='store_true', help="(Supervised mode only) Perform the classification on the encoder's output.") group.add_argument('--random_seed', type=int, default=None, help='Set a specific random seed.') group.add_argument('--speakerEmbedding', type=int, default=0, help='(Depreciated) Feed the prediction network with speaker embeddings along with the usual sequence.') group.add_argument('--arMode', default='LSTM', choices=['GRU', 'LSTM', 'RNN', 'no_ar', 'transformer'], help='Architecture to use for the auto-regressive network (default is lstm).') group.add_argument('--nLevelsGRU', type=int, default=1, help='Number of layers in the autoregressive network.') group.add_argument('--rnnMode', type=str, default='transformer', choices=['transformer', 'RNN', 'LSTM', 'linear', 'ffd', 'conv4', 'conv8', 'conv12'], help='Architecture to use for the prediction network') group.add_argument('--dropout', action='store_true', help='Add a dropout layer at the output of the prediction network.') group.add_argument('--abspos', action='store_true', help='If the prediction network is a transformer, active to use absolute coordinates.') return parser
class UpstreamExpert(UpstreamBase): def __init__(self, ckpt, **kwargs): super().__init__(**kwargs) locArgs = get_default_cpc_config() checkpoint = torch.load(ckpt, map_location='cpu') loadArgs(locArgs, argparse.Namespace(**checkpoint['config'])) encoderNet = getEncoder(locArgs) arNet = getAR(locArgs) self.model = cpcmodel(encoderNet, arNet) self.model.load_state_dict(checkpoint['weights'], strict=False) if (len(self.hooks) == 0): self.add_hook('self.model.gEncoder', (lambda input, output: output.transpose(1, 2))) self.add_hook('self.model.gAR', (lambda input, output: output)) def get_downsample_rates(self, key: str) -> int: return 160 def forward(self, wavs): padded_wav = pad_sequence(wavs, batch_first=True) features = self.model(padded_wav.unsqueeze(1), None)[0]
def cpc_local(ckpt, *args, **kwargs): '\n The model from local ckpt\n ckpt (str): PATH\n ' assert os.path.isfile(ckpt) return _UpstreamExpert(ckpt, *args, **kwargs)
def cpc_url(ckpt, refresh=False, *args, **kwargs): '\n The model from URL\n ckpt (str): URL\n ' return cpc_local(_urls_to_filepaths(ckpt), *args, **kwargs)
def modified_cpc(refresh=False, *args, **kwargs): '\n The model from official repository\n ' kwargs['ckpt'] = 'https://dl.fbaipublicfiles.com/librilight/CPC_checkpoints/60k_epoch4-d0f474de.pt' return cpc_url(*args, refresh=refresh, **kwargs)
def load_and_convert_fairseq_ckpt(fairseq_source: str, output_path: str): (state, cfg) = load_fairseq_ckpt(fairseq_source) output_state = {'task_cfg': cfg['task'], 'model_cfg': cfg['model'], 'model_weight': state['model']} Path(output_path).parent.mkdir(exist_ok=True, parents=True) torch.save(output_state, output_path) load_converted_model(output_path)
def load_converted_model(ckpt: str): ckpt_state = torch.load(ckpt, map_location='cpu') for required_key in ['task_cfg', 'model_cfg', 'model_weight']: if (required_key not in ckpt_state): raise ValueError(f'{ckpt} is not a valid checkpoint since the required key: {required_key} is missing') task_cfg = merge_with_parent(AudioPretrainingConfig, ckpt_state['task_cfg']) model_cfg = merge_with_parent(Data2VecAudioConfig, ckpt_state['model_cfg']) model = Data2VecAudioModel(model_cfg) model.remove_pretraining_modules() del ckpt_state['model_weight']['_ema'] model.load_state_dict(ckpt_state['model_weight']) return (model, task_cfg)
class EMAModuleConfig(): ema_decay: float = field(default=0.9999, metadata={'help': 'decay for exponential moving average model'}) ema_fp32: bool = field(default=False, metadata={'help': 'If true, store EMA model in fp32 even if model is in fp16'})
class EMAModule(): 'Exponential Moving Average of Fairseq Models' def __init__(self, model, config: EMAModuleConfig, device=None, skip_keys=None): '\n @param model model to initialize the EMA with\n @param config EMAConfig object with configuration like\n ema_decay, ema_update_freq, ema_fp32\n @param device If provided, copy EMA to this device (e.g. gpu).\n Otherwise EMA is in the same device as the model.\n ' self.decay = config.ema_decay self.model = copy.deepcopy(model) self.model.requires_grad_(False) self.config = config self.skip_keys = (skip_keys or set()) self.fp32_params = {} if (device is not None): logging.info(f'Copying EMA model to device {device}') self.model = self.model.to(device=device) if self.config.ema_fp32: self.build_fp32_params() self.update_freq_counter = 0 def build_fp32_params(self, state_dict=None): '\n Store a copy of the EMA params in fp32.\n If state dict is passed, the EMA params is copied from\n the provided state dict. Otherwise, it is copied from the\n current EMA model parameters.\n ' if (not self.config.ema_fp32): raise RuntimeError('build_fp32_params should not be called if ema_fp32=False. Use ema_fp32=True if this is really intended.') if (state_dict is None): state_dict = self.model.state_dict() def _to_float(t): return (t.float() if torch.is_floating_point(t) else t) for param_key in state_dict: if (param_key in self.fp32_params): self.fp32_params[param_key].copy_(state_dict[param_key]) else: self.fp32_params[param_key] = _to_float(state_dict[param_key]) def restore(self, state_dict, build_fp32_params=False): 'Load data from a model spec into EMA model' self.model.load_state_dict(state_dict, strict=False) if build_fp32_params: self.build_fp32_params(state_dict) def set_decay(self, decay): self.decay = decay def get_decay(self): return self.decay def _step_internal(self, new_model): 'One update of the EMA model based on new model weights' decay = self.decay ema_state_dict = {} ema_params = (self.fp32_params if self.config.ema_fp32 else self.model.state_dict()) for (key, param) in new_model.named_parameters(): if isinstance(param, dict): continue try: ema_param = ema_params[key] except KeyError: ema_param = (param.float().clone() if (param.ndim == 1) else copy.deepcopy(param)) ema_params[key] = ema_param if (param.shape != ema_param.shape): raise ValueError(('incompatible tensor shapes between model param and ema param' + '{} vs. {}'.format(param.shape, ema_param.shape))) if ('version' in key): continue if ((key in self.skip_keys) or (not param.requires_grad)): ema_params[key].copy_(param.to(dtype=ema_param.dtype).data) ema_param = ema_params[key] else: ema_param.mul_(decay) ema_param.add_(param.data.to(dtype=ema_param.dtype), alpha=(1 - decay)) ema_state_dict[key] = ema_param for (key, param) in new_model.named_buffers(): ema_state_dict[key] = param self.restore(ema_state_dict, build_fp32_params=False) @torch.no_grad() def step(self, new_model): self._step_internal(new_model) def reverse(self, model): '\n Load the model parameters from EMA model.\n Useful for inference or fine-tuning from the EMA model.\n ' d = self.model.state_dict() if ('_ema' in d): del d['_ema'] model.load_state_dict(d, strict=False) return model
@dataclass class Data2VecAudioConfig(Wav2Vec2Config): loss_beta: float = field(default=0, metadata={'help': 'beta for smooth l1 loss. 0 means use l2 loss'}) loss_scale: Optional[float] = field(default=None, metadata={'help': 'scale the reconstruction loss by this constant. if None then scales by 1/sqrt(dim)'}) average_top_k_layers: int = field(default=8, metadata={'help': 'how many layers to average'}) layer_norm_target_layer: bool = False instance_norm_target_layer: bool = False instance_norm_targets: bool = False layer_norm_targets: bool = False batch_norm_target_layer: bool = False group_norm_target_layer: bool = False ema_decay: float = field(default=0.999, metadata={'help': 'initial ema decay rate'}) ema_end_decay: float = field(default=0.9999, metadata={'help': 'final ema decay rate'}) ema_anneal_end_step: int = None ema_transformer_only: bool = field(default=True, metadata={'help': 'whether to momentum update only the transformer'}) ema_layers_only: bool = field(default=True, metadata={'help': 'whether to momentum update only the transformer layers'}) max_update: int = None min_target_var: float = field(default=0.1, metadata={'help': 'stop training if target var falls below this'}) min_pred_var: float = field(default=0.01, metadata={'help': 'stop training if prediction var falls below this'})
def get_annealed_rate(start, end, curr_step, total_steps): r = (end - start) pct_remaining = (1 - (curr_step / total_steps)) return (end - (r * pct_remaining))
class Data2VecAudioModel(torch.nn.Module): def __init__(self, cfg: Data2VecAudioConfig): super().__init__() self.cfg = cfg feature_enc_layers = eval(cfg.conv_feature_layers) self.extractor_embed = feature_enc_layers[(- 1)][0] self.ema = None self.embed = cfg.encoder_embed_dim self.average_top_k_layers = cfg.average_top_k_layers self.loss_beta = cfg.loss_beta self.loss_scale = cfg.loss_scale self.feature_extractor = ConvFeatureExtractionModel(conv_layers=feature_enc_layers, dropout=0.0, mode=cfg.extractor_mode, conv_bias=cfg.conv_bias) self.post_extract_proj = nn.Linear(self.extractor_embed, cfg.encoder_embed_dim) self.mask_prob = cfg.mask_prob self.mask_selection = cfg.mask_selection self.mask_other = cfg.mask_other self.mask_length = cfg.mask_length self.no_mask_overlap = cfg.no_mask_overlap self.mask_min_space = cfg.mask_min_space self.mask_channel_prob = cfg.mask_channel_prob self.mask_channel_before = cfg.mask_channel_before self.mask_channel_selection = cfg.mask_channel_selection self.mask_channel_other = cfg.mask_channel_other self.mask_channel_length = cfg.mask_channel_length self.no_mask_channel_overlap = cfg.no_mask_channel_overlap self.mask_channel_min_space = cfg.mask_channel_min_space self.dropout_input = nn.Dropout(cfg.dropout_input) self.dropout_features = nn.Dropout(cfg.dropout_features) self.feature_grad_mult = cfg.feature_grad_mult self.mask_emb = nn.Parameter(torch.FloatTensor(cfg.encoder_embed_dim).uniform_()) self.encoder = TransformerEncoder(cfg) self.layer_norm = LayerNorm(self.extractor_embed) self.final_proj = nn.Linear(self.embed, self.embed) self.num_updates = 0 def make_ema_teacher(self): ema_config = EMAModuleConfig(ema_decay=self.cfg.ema_decay, ema_fp32=True) skip_keys = set() if self.cfg.ema_layers_only: self.cfg.ema_transformer_only = True for (k, _) in self.encoder.pos_conv.named_parameters(): skip_keys.add(f'pos_conv.{k}') self.ema = EMAModule((self.encoder if self.cfg.ema_transformer_only else self), ema_config, skip_keys=skip_keys) def set_num_updates(self, num_updates): super().set_num_updates(num_updates) if ((self.ema is None) and (self.final_proj is not None)): logger.info(f'making ema teacher') self.make_ema_teacher() elif (self.training and (self.ema is not None)): if (self.cfg.ema_decay != self.cfg.ema_end_decay): if (num_updates >= self.cfg.ema_anneal_end_step): decay = self.cfg.ema_end_decay else: decay = get_annealed_rate(self.cfg.ema_decay, self.cfg.ema_end_decay, num_updates, self.cfg.ema_anneal_end_step) self.ema.set_decay(decay) if (self.ema.get_decay() < 1): self.ema.step((self.encoder if self.cfg.ema_transformer_only else self)) self.num_updates = num_updates def state_dict(self, destination=None, prefix='', keep_vars=False): state = super().state_dict(destination, prefix, keep_vars) if (self.ema is not None): state[(prefix + '_ema')] = self.ema.fp32_params return state def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs): if (self.ema is not None): k = (prefix + '_ema') assert (k in state_dict) self.ema.restore(state_dict[k], True) del state_dict[k] return super()._load_from_state_dict(state_dict, prefix, *args, **kwargs) @classmethod def build_model(cls, cfg: Data2VecAudioConfig, task=None): 'Build a new model instance.' return cls(cfg) def apply_mask(self, x, padding_mask, mask_indices=None, mask_channel_indices=None): (B, T, C) = x.shape if ((self.mask_channel_prob > 0) and self.mask_channel_before): mask_channel_indices = compute_mask_indices((B, C), None, self.mask_channel_prob, self.mask_channel_length, self.mask_channel_selection, self.mask_channel_other, no_overlap=self.no_mask_channel_overlap, min_space=self.mask_channel_min_space) mask_channel_indices = torch.from_numpy(mask_channel_indices).to(x.device).unsqueeze(1).expand((- 1), T, (- 1)) x[mask_channel_indices] = 0 if (self.mask_prob > 0): if (mask_indices is None): mask_indices = compute_mask_indices((B, T), padding_mask, self.mask_prob, self.mask_length, self.mask_selection, self.mask_other, min_masks=1, no_overlap=self.no_mask_overlap, min_space=self.mask_min_space, require_same_masks=self.cfg.require_same_masks, mask_dropout=self.cfg.mask_dropout) mask_indices = torch.from_numpy(mask_indices).to(x.device) x = index_put(x, mask_indices, self.mask_emb) else: mask_indices = None if ((self.mask_channel_prob > 0) and (not self.mask_channel_before)): if (mask_channel_indices is None): mask_channel_indices = compute_mask_indices((B, C), None, self.mask_channel_prob, self.mask_channel_length, self.mask_channel_selection, self.mask_channel_other, no_overlap=self.no_mask_channel_overlap, min_space=self.mask_channel_min_space) mask_channel_indices = torch.from_numpy(mask_channel_indices).to(x.device).unsqueeze(1).expand((- 1), T, (- 1)) x = index_put(x, mask_channel_indices, 0) return (x, mask_indices) def _get_feat_extract_output_lengths(self, input_lengths: torch.LongTensor): '\n Computes the output length of the convolutional layers\n ' def _conv_out_length(input_length, kernel_size, stride): return torch.floor((((input_length - kernel_size) / stride) + 1)) conv_cfg_list = eval(self.cfg.conv_feature_layers) for i in range(len(conv_cfg_list)): input_lengths = _conv_out_length(input_lengths, conv_cfg_list[i][1], conv_cfg_list[i][2]) return input_lengths.to(torch.long) def forward(self, source, padding_mask=None, mask=True, features_only=False, layer=None, mask_indices=None, mask_channel_indices=None, padding_count=None): features = source if (self.feature_grad_mult > 0): features = self.feature_extractor(features) if (self.feature_grad_mult != 1.0): features = GradMultiply.apply(features, self.feature_grad_mult) else: with torch.no_grad(): features = self.feature_extractor(features) features = features.transpose(1, 2) features = self.layer_norm(features) orig_padding_mask = padding_mask if ((padding_mask is not None) and padding_mask.any()): input_lengths = (1 - padding_mask.long()).sum((- 1)) output_lengths = self._get_feat_extract_output_lengths(input_lengths) padding_mask = torch.zeros(features.shape[:2], dtype=features.dtype, device=features.device) padding_mask[(torch.arange(padding_mask.shape[0], device=padding_mask.device), (output_lengths - 1))] = 1 padding_mask = (1 - padding_mask.flip([(- 1)]).cumsum((- 1)).flip([(- 1)])).bool() else: padding_mask = None if (self.post_extract_proj is not None): features = self.post_extract_proj(features) pre_encoder_features = None if self.cfg.ema_transformer_only: pre_encoder_features = features.clone() features = self.dropout_input(features) if mask: (x, mask_indices) = self.apply_mask(features, padding_mask, mask_indices=mask_indices, mask_channel_indices=mask_channel_indices) else: x = features mask_indices = None (x, layer_results) = self.encoder(x, padding_mask=padding_mask, layer=layer) if features_only: return {'x': x, 'padding_mask': padding_mask, 'layer_results': layer_results} result = {'losses': {}} with torch.no_grad(): self.ema.model.eval() if self.cfg.ema_transformer_only: (y, layer_results) = self.ema.model.extract_features(pre_encoder_features, padding_mask=padding_mask, min_layer=(self.cfg.encoder_layers - self.average_top_k_layers)) y = {'x': y, 'padding_mask': padding_mask, 'layer_results': layer_results} else: y = self.ema.model.extract_features(source=source, padding_mask=orig_padding_mask, mask=False) target_layer_results = [l[2] for l in y['layer_results']] permuted = False if (self.cfg.instance_norm_target_layer or self.cfg.batch_norm_target_layer): target_layer_results = [tl.permute(1, 2, 0) for tl in target_layer_results] permuted = True if self.cfg.batch_norm_target_layer: target_layer_results = [F.batch_norm(tl.float(), running_mean=None, running_var=None, training=True) for tl in target_layer_results] if self.cfg.instance_norm_target_layer: target_layer_results = [F.instance_norm(tl.float()) for tl in target_layer_results] if permuted: target_layer_results = [tl.transpose(1, 2) for tl in target_layer_results] if self.cfg.group_norm_target_layer: target_layer_results = [F.layer_norm(tl.float(), tl.shape[(- 2):]) for tl in target_layer_results] if self.cfg.layer_norm_target_layer: target_layer_results = [F.layer_norm(tl.float(), tl.shape[(- 1):]) for tl in target_layer_results] y = (sum(target_layer_results) / len(target_layer_results)) if self.cfg.layer_norm_targets: y = F.layer_norm(y.float(), y.shape[(- 1):]) if self.cfg.instance_norm_targets: y = F.instance_norm(y.float().transpose(1, 2)).transpose(1, 2) if (not permuted): y = y.transpose(0, 1) y = y[mask_indices] x = x[mask_indices] x = self.final_proj(x) sz = x.size((- 1)) if (self.loss_beta == 0): loss = F.mse_loss(x.float(), y.float(), reduction='none').sum(dim=(- 1)) else: loss = F.smooth_l1_loss(x.float(), y.float(), reduction='none', beta=self.loss_beta).sum(dim=(- 1)) if (self.loss_scale is not None): scale = self.loss_scale else: scale = (1 / math.sqrt(sz)) result['losses']['regression'] = (loss.sum() * scale) if ('sample_size' not in result): result['sample_size'] = loss.numel() with torch.no_grad(): result['target_var'] = self.compute_var(y) result['pred_var'] = self.compute_var(x.float()) if ((self.num_updates > 5000) and (result['target_var'] < self.cfg.min_target_var)): logger.error(f"target var is {result['target_var'].item()} < {self.cfg.min_target_var}, exiting") raise Exception(f"target var is {result['target_var'].item()} < {self.cfg.min_target_var}, exiting") if ((self.num_updates > 5000) and (result['pred_var'] < self.cfg.min_pred_var)): logger.error(f"pred var is {result['pred_var'].item()} < {self.cfg.min_pred_var}, exiting") raise Exception(f"pred var is {result['pred_var'].item()} < {self.cfg.min_pred_var}, exiting") if (self.ema is not None): result['ema_decay'] = (self.ema.get_decay() * 1000) return result @staticmethod def compute_var(y): y = y.view((- 1), y.size((- 1))) if dist.is_initialized(): zc = torch.tensor(y.size(0)).cuda() zs = y.sum(dim=0) zss = (y ** 2).sum(dim=0) dist.all_reduce(zc) dist.all_reduce(zs) dist.all_reduce(zss) var = ((zss / (zc - 1)) - ((zs ** 2) / (zc * (zc - 1)))) return torch.sqrt((var + 1e-06)).mean() else: return torch.sqrt((y.var(dim=0) + 1e-06)).mean() def extract_features(self, source, padding_mask, mask=False, layer=None): res = self.forward(source, padding_mask, mask=mask, features_only=True, layer=layer) return res def remove_pretraining_modules(self, last_layer=None): self.final_proj = None self.ema = None if (last_layer is not None): self.encoder.layers = nn.ModuleList((l for (i, l) in enumerate(self.encoder.layers) if (i <= last_layer)))
def data2vec_custom(ckpt: str, refresh: bool=False, **kwargs): if ckpt.startswith('http'): ckpt = _urls_to_filepaths(ckpt, refresh=refresh) return _UpstreamExpert(ckpt, **kwargs)
def data2vec_local(*args, **kwargs): return data2vec_custom(*args, **kwargs)
def data2vec_url(*args, **kwargs): return data2vec_custom(*args, **kwargs)
def data2vec(refresh=False, *args, **kwargs): '\n The default model - Base\n refresh (bool): whether to download ckpt/config again if existed\n ' return data2vec_base_960(*args, refresh=refresh, **kwargs)
def data2vec_base_960(refresh=False, *args, **kwargs): '\n The Base model\n refresh (bool): whether to download ckpt/config again if existed\n ' kwargs['ckpt'] = 'https://huggingface.co/s3prl/converted_ckpts/resolve/main/audio_base_ls.pt' return data2vec_custom(*args, refresh=refresh, **kwargs)
def data2vec_large_ll60k(refresh=False, *args, **kwargs): '\n The Large model trained on Libri-light 60k hours of data\n refresh (bool): whether to download ckpt/config again if existed\n ' kwargs['ckpt'] = 'https://huggingface.co/s3prl/converted_ckpts/resolve/main/vox_pretrained.pt' return data2vec_custom(*args, refresh=refresh, **kwargs)
class CMVN(torch.jit.ScriptModule): __constants__ = ['mode', 'dim', 'eps'] def __init__(self, mode='global', dim=2, eps=1e-10): super(CMVN, self).__init__() if (mode != 'global'): raise NotImplementedError('Only support global mean variance normalization.') self.mode = mode self.dim = dim self.eps = eps @torch.jit.script_method def forward(self, x): if (self.mode == 'global'): return ((x - x.mean(self.dim, keepdim=True)) / (self.eps + x.std(self.dim, keepdim=True))) def extra_repr(self): return 'mode={}, dim={}, eps={}'.format(self.mode, self.dim, self.eps)
class FeatureExtractor(nn.Module): 'Feature extractor, transforming file path to Mel spectrogram' def __init__(self, mode='fbank', num_mel_bins=80, decode_wav=False, apply_cmvn=True, **kwargs): super(FeatureExtractor, self).__init__() assert (mode == 'fbank'), 'Only Mel-spectrogram implemented' self.mode = mode self.extract_fn = kaldi.fbank self.apply_cmvn = apply_cmvn if self.apply_cmvn: self.cmvn = CMVN() self.num_mel_bins = num_mel_bins self.kwargs = kwargs self.decode_wav = decode_wav if self.decode_wav: torchaudio.set_audio_backend('soundfile') def _load_file(self, filepath): if self.decode_wav: (waveform, sample_rate) = torchaudio.load_wav(filepath) else: (waveform, sample_rate) = torchaudio.load(filepath) return (waveform, sample_rate) def forward(self, waveform): y = self.extract_fn(waveform, num_mel_bins=self.num_mel_bins, sample_frequency=SAMPLE_RATE, window_type=WINDOW_TYPE, **self.kwargs) if self.apply_cmvn: y = y.transpose(0, 1).unsqueeze(0) y = self.cmvn(y) y = y.squeeze(0).transpose(0, 1) return y def extra_repr(self): return 'mode={}, num_mel_bins={}'.format(self.mode, self.num_mel_bins) def create_msg(self): 'List msg for verbose function' msg = 'Audio spec.| Audio feat. = {}\t\t| feat. dim = {}\t| CMVN = {}'.format(self.mode, self.num_mel_bins, self.apply_cmvn) return [msg]
def create_transform(): return FeatureExtractor()
def decoar_custom(ckpt: str, refresh=False, *args, **kwargs): if ckpt.startswith('http'): ckpt = _urls_to_filepaths(ckpt, refresh=refresh) return _UpstreamExpert(ckpt, *args, **kwargs)
def decoar_local(*args, **kwargs): return decoar_custom(*args, **kwargs)
def decoar_url(*args, **kwargs): return decoar_custom(*args, **kwargs)
def decoar(refresh=False, *args, **kwargs): kwargs['ckpt'] = 'https://huggingface.co/s3prl/converted_ckpts/resolve/main/checkpoint_decoar.pt' return decoar_url(*args, refresh=refresh, **kwargs)
def decoar2_custom(ckpt: str, refresh=False, *args, **kwargs): if ckpt.startswith('http'): ckpt = _urls_to_filepaths(ckpt, refresh=refresh) return _UpstreamExpert(ckpt, *args, **kwargs)
def decoar2_local(*args, **kwargs): "\n The model from local ckpt\n ckpt (str): PATH\n feature_selection (str): 'c' (default) or 'z'\n " return decoar2_custom(*args, **kwargs)
def decoar2_url(*args, **kwargs): '\n The model from URL\n ckpt (str): URL\n ' return decoar2_custom(*args, **kwargs)
def decoar2(*args, refresh=False, **kwargs): '\n The apc standard model on 360hr\n refresh (bool): whether to download ckpt/config again if existed\n ' kwargs['ckpt'] = 'https://huggingface.co/s3prl/converted_ckpts/resolve/main/checkpoint_decoar2.pt' return decoar2_url(*args, refresh=refresh, **kwargs)
class CMVN(torch.jit.ScriptModule): __constants__ = ['mode', 'dim', 'eps'] def __init__(self, mode='global', dim=2, eps=1e-10): super(CMVN, self).__init__() if (mode != 'global'): raise NotImplementedError('Only support global mean variance normalization.') self.mode = mode self.dim = dim self.eps = eps @torch.jit.script_method def forward(self, x): if (self.mode == 'global'): return ((x - x.mean(self.dim, keepdim=True)) / (self.eps + x.std(self.dim, keepdim=True))) def extra_repr(self): return 'mode={}, dim={}, eps={}'.format(self.mode, self.dim, self.eps)
class FeatureExtractor(nn.Module): 'Feature extractor, transforming file path to Mel spectrogram' def __init__(self, mode='fbank', num_mel_bins=80, decode_wav=False, apply_cmvn=True, **kwargs): super(FeatureExtractor, self).__init__() assert (mode == 'fbank'), 'Only Mel-spectrogram implemented' self.mode = mode self.extract_fn = kaldi.fbank self.apply_cmvn = apply_cmvn if self.apply_cmvn: self.cmvn = CMVN() self.num_mel_bins = num_mel_bins self.kwargs = kwargs self.decode_wav = decode_wav if self.decode_wav: torchaudio.set_audio_backend('soundfile') def _load_file(self, filepath): if self.decode_wav: (waveform, sample_rate) = torchaudio.load_wav(filepath) else: (waveform, sample_rate) = torchaudio.load(filepath) return (waveform, sample_rate) def forward(self, waveform): y = self.extract_fn(waveform, num_mel_bins=self.num_mel_bins, sample_frequency=SAMPLE_RATE, window_type=WINDOW_TYPE, **self.kwargs) if self.apply_cmvn: y = y.transpose(0, 1).unsqueeze(0) y = self.cmvn(y) y = y.squeeze(0).transpose(0, 1) return y def extra_repr(self): return 'mode={}, num_mel_bins={}'.format(self.mode, self.num_mel_bins) def create_msg(self): 'List msg for verbose function' msg = 'Audio spec.| Audio feat. = {}\t\t| feat. dim = {}\t| CMVN = {}'.format(self.mode, self.num_mel_bins, self.apply_cmvn) return [msg]