File size: 2,053 Bytes
21e9df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import os
import torch
import numpy as np

sys.path.insert(0, os.path.join(sys.path[0], "../../../.."))






def pad(x, max_len, dim, n_dim):
    if x.shape[dim] < max_len:
        if n_dim == 1:
            x = np.pad(x, (0, max_len - x.shape[dim]),
                       "constant", constant_values=(0, 0))
        elif n_dim == 2:
            if dim == 0:
                x = np.pad(x, ((0, max_len - x.shape[dim]), (0, 0)),
                           "constant", constant_values=(0, 0))
            elif dim == 1:
                x = np.pad(x, ((0, 0), (0, max_len - x.shape[dim])),
                           "constant", constant_values=(0, 0))
    return x


def detect_silence(path, mode="wav"):
    x = AudioSegment.from_file(path, mode)
    dBFS = x.dBFS
    sil = silence.detect_silence(x, min_silence_len=1000, silence_thresh=dBFS - 16)
    if len(sil) == 0:
        return 0, -1
    x_len = x.duration_seconds
    st = 0 if sil[0][0] > 0 else sil[0][1] / 1000.
    ed = sil[-1][0] / 1000. if sil[-1][1] / 1000. >= x_len - 1 else -1
    return st, ed








def mkdir(folder):
    if not os.path.exists(folder):
        os.mkdir(folder)


def listdir(folder, suffix=None):
    outs = []
    for f in os.listdir(folder):
        if suffix is None or str.endswith(f, suffix):
            outs.append(f)
    return outs






def print_trainable_parameters(model):
    trainable_params = 0
    all_param = 0
    for k, param in model.named_parameters():

        num_params = param.numel()
        # if using DS Zero 3 and the weights are initialized empty
        if num_params == 0 and hasattr(param, "ds_numel"):
            num_params = param.ds_numel

        all_param += num_params
        if param.requires_grad:
            print(k)
            trainable_params += num_params

    print(
        f"trainable params: {trainable_params:,d} || all params: {all_param:,d} || trainable%: {100 * trainable_params / all_param}"
    )
def freeze(model):
    for n, p in model.named_parameters():
        p.requires_grad = False