-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
97 lines (70 loc) · 2.64 KB
/
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
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
import torch
import torch.nn.functional as F
import torchaudio
import torchaudio.transforms as aT
import torchvision.transforms as vT
def audio_loader(path, max_length_in_seconds=4):
waveform, sample_rate = torchaudio.load(path)
_, num_frames = waveform.shape
max_frames = sample_rate * max_length_in_seconds
# ? Pad audio with zeros if too short or cut audio if too long
if num_frames < max_frames:
waveform = torch.nn.functional.pad(waveform, (0, max_frames - num_frames))
elif num_frames > max_frames:
waveform = waveform.narrow(dim=1, start=0, length=max_frames)
transforms = vT.Compose(
[
aT.Resample(44100, 22050),
aT.MFCC(sample_rate=sample_rate, n_mfcc=64),
aT.AmplitudeToDB(),
]
)
waveform = transforms(waveform)
return waveform
def train(model, train_loader, criterion, optimizer, device="cpu"):
model.train()
train_loss = 0
num_correct = 0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
# ? zero the parameter gradients
optimizer.zero_grad()
# ? forward pass, backward pass & optimize
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
num_correct += (predicted == labels).sum().item()
train_loss /= len(train_loader.dataset)
accuracy = 100 * num_correct / len(train_loader.dataset)
return train_loss, accuracy
def valid(model, valid_loader, criterion, device="cpu"):
model.eval()
valid_loss = 0
num_correct = 0
with torch.no_grad():
for inputs, labels in valid_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
loss = criterion(outputs, labels)
valid_loss += loss.item()
num_correct += (predicted == labels).sum().item()
valid_loss /= len(valid_loader)
accuracy = 100 * num_correct / len(valid_loader.dataset)
return valid_loss, accuracy
def predict(model, input_tensor, classes, device="cpu"):
model.eval()
inputs = input_tensor.unsqueeze(1)
inputs = inputs.to(device)
with torch.no_grad():
output = model(inputs)
output = output.squeeze()
output = F.softmax(output, dim=-1)
accuracy, predicted = torch.max(output.data, -1)
accuracy *= 100
# ? provide class labels
predicted = classes[predicted]
return predicted, accuracy