-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_features.py
More file actions
executable file
·130 lines (97 loc) · 4.21 KB
/
Copy pathtest_features.py
File metadata and controls
executable file
·130 lines (97 loc) · 4.21 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import torch
def test_deltas(device):
from speechbrain.processing.features import Deltas
size = [10, 101, 20]
inp = torch.ones(size, device=device)
compute_deltas = Deltas(input_size=20).to(device)
out = torch.zeros(size, device=device)
assert torch.sum(compute_deltas(inp) == out) == out.numel()
assert torch.jit.trace(compute_deltas, inp)
def test_context_window(device):
from speechbrain.processing.features import ContextWindow
inp = (
torch.tensor([1, 2, 3], device=device)
.unsqueeze(0)
.unsqueeze(-1)
.float()
)
compute_cw = ContextWindow(left_frames=1, right_frames=1).to(device)
out = (
torch.tensor([[0, 1, 2], [1, 2, 3], [2, 3, 0]], device=device)
.unsqueeze(0)
.float()
)
assert torch.sum(compute_cw(inp) == out) == 9
inp = torch.rand([2, 10, 5], device=device)
compute_cw = ContextWindow(left_frames=0, right_frames=0).to(device)
assert torch.sum(compute_cw(inp) == inp) == inp.numel()
assert torch.jit.trace(compute_cw, inp)
def test_istft(device):
from speechbrain.processing.features import ISTFT, STFT
fs = 16000
inp = torch.randn([10, 16000], device=device)
inp = torch.stack(3 * [inp], -1)
compute_stft = STFT(sample_rate=fs).to(device)
compute_istft = ISTFT(sample_rate=fs).to(device)
out = compute_istft(compute_stft(inp), sig_length=16000)
assert torch.sum(torch.abs(inp - out) < 5e-5) >= inp.numel() - 5
assert torch.jit.trace(compute_stft, inp)
assert torch.jit.trace(compute_istft, compute_stft(inp))
def test_filterbank(device):
from speechbrain.processing.features import Filterbank
compute_fbanks = Filterbank().to(device)
inputs = torch.ones([10, 101, 201], device=device)
assert torch.jit.trace(compute_fbanks, inputs)
# Check amin (-100 dB)
inputs = torch.zeros([10, 101, 201], device=device)
fbanks = compute_fbanks(inputs)
assert torch.equal(fbanks, torch.ones_like(fbanks) * -100)
# Check top_db
fbanks = torch.zeros([1, 1, 1], device=device)
expected = torch.Tensor([[[-100]]]).to(device)
fbanks_db = compute_fbanks._amplitude_to_DB(fbanks)
assert torch.equal(fbanks_db, expected)
# Making sure independent computation gives same results
# as the batch computation
input1 = torch.rand([1, 101, 201], device=device) * 10
input2 = torch.rand([1, 101, 201], device=device)
input3 = torch.cat([input1, input2], dim=0)
fbank1 = compute_fbanks(input1)
fbank2 = compute_fbanks(input2)
fbank3 = compute_fbanks(input3)
assert torch.sum(torch.abs(fbank1[0] - fbank3[0])) < 8e-05
assert torch.sum(torch.abs(fbank2[0] - fbank3[1])) < 8e-05
def test_dtc(device):
from speechbrain.processing.features import DCT
compute_dct = DCT(input_size=40)
inputs = torch.randn([10, 101, 40], device=device)
assert torch.jit.trace(compute_dct, inputs)
def test_input_normalization(device):
from speechbrain.processing.features import InputNormalization
# Check this can be traced after training is complete
norm = InputNormalization().to(device)
inputs = torch.randn([10, 101, 20], device=device)
inp_len = torch.arange(1, 11, device=device) / 10
# One pass to initialize, ensure it is correctly sized
_ = norm(inputs, inp_len)
assert norm.glob_mean.numel() == 20
assert norm.glob_std.numel() == 20
# Freeze and trace
norm = norm.eval()
assert torch.jit.trace(norm, (inputs, inp_len))
# Test default setup
norm = InputNormalization().to(device)
inputs = torch.FloatTensor([1, 3, 0, 0, 0]).view(1, -1, 1).to(device)
inp_len = torch.FloatTensor([0.4]).to(device)
out_norm = norm(inputs, inp_len).squeeze()
expected = torch.FloatTensor([-1, 1, -2, -2, -2]).to(device)
assert torch.equal(out_norm, expected)
def test_features_multimic(device):
from speechbrain.processing.features import Filterbank
compute_fbanks = Filterbank().to(device)
inputs = torch.rand([10, 101, 201], device=device)
output = compute_fbanks(inputs)
inputs_ch2 = torch.stack((inputs, inputs), -1)
output_ch2 = compute_fbanks(inputs_ch2)
output_ch2 = output_ch2[..., 0]
assert torch.sum(output - output_ch2) < 5e-05