-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
32 lines (21 loc) · 855 Bytes
/
utils.py
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
import torch
import numpy as np
from scipy.io.wavfile import read
from sklearn.metrics import confusion_matrix, accuracy_score
def get_mask_from_lengths(lengths):
max_len = torch.max(lengths).item()
ids = torch.arange(0, max_len, out=torch.cuda.LongTensor(max_len))
mask = (ids < lengths.unsqueeze(1)).bool()
return mask
def load_filepaths_and_label(filename, split=","):
with open(filename, encoding='utf-8') as f:
filepaths_text_and_speaker = [line.strip().split(split) for line in f]
return filepaths_text_and_speaker
def to_device(x, device):
x = x.contiguous()
x = x.to(device)
return torch.autograd.Variable(x)
def get_acc_and_confusion_matrix(labels, predictions):
acc = accuracy_score(labels, predictions)
c_matrix = confusion_matrix(labels, predictions)
return acc, c_matrix