-
Notifications
You must be signed in to change notification settings - Fork 17
/
spectrogramEstimate_inference.py
121 lines (103 loc) · 3.42 KB
/
spectrogramEstimate_inference.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
torch.manual_seed(1)
testY = np.load("norm_cnn_timit_test_Y.npy")
Xtest = np.load("norm_cnn_timit_test_X.npy").astype(np.float32)
Ytest = testY[:,1:5].astype(np.float32)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
D = Xtest.shape
print(D)
print(Xtest.shape[1], len(Ytest))
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.Conv1 = nn.Conv2d(in_channels=1, out_channels=96, kernel_size=(3, 3), stride=1, padding=0)
self.Conv2 = nn.Conv2d(in_channels=96, out_channels=32, kernel_size=(3, 3), stride=1, padding=0)
self.Conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), stride=1, padding=0)
self.Conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(5, 5), stride=1, padding=0)
self.Dense5 = nn.Linear(43*38*64, 512)
self.out = nn.Linear(512, 4)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.Conv1(x))
x = F.relu(self.Conv2(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
x = F.relu(self.Conv3(x))
x = F.relu(self.Conv4(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
#print(in_size)
x = x.view(x.size(0), -1)
x = F.relu(self.Dense5(x))
return self.out(x)
def train(model, loss, optimizer, inputs, labels):
inputs = Variable(inputs.to(device))
labels = Variable(labels.to(device))
optimizer.zero_grad()
logits = model.forward(inputs)
output = loss.forward(logits, labels)
output.backward()
optimizer.step()
return output.item()
def predict(model, inputs):
inputs = Variable(inputs)
with torch.no_grad():
logits = model.forward(inputs.to(device))
return logits.data.cpu().numpy()
Xtest = torch.from_numpy(Xtest).float().to(device)
Ytest = torch.from_numpy(Ytest).float().to(device)
model = Net().to(device)
loss = nn.L1Loss()
optimizer = optim.Adagrad(model.parameters())
model.load_state_dict(torch.load("CNN_estimate.pt"))
model.eval()
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
print('predicting...')
Ypred1 = predict(model, Xtest[:1000])
Ypred2 = predict(model, Xtest[1000:2000])
Ypred3 = predict(model, Xtest[2000:])
Ypred = np.concatenate((Ypred1, Ypred2, Ypred3))
for k in range(0, len(Ytest)):
# print(y_hat[i])
l1 = np.abs(float(Ytest[k, 0]) - Ypred[k, 0])
l2 = np.abs(float(Ytest[k, 1]) - Ypred[k, 1])
l3 = np.abs(float(Ytest[k, 2]) - Ypred[k, 2])
l4 = np.abs(float(Ytest[k, 3]) - Ypred[k, 3])
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1, l1)
max_2 = max(max_2, l2)
max_3 = max(max_3, l3)
max_4 = max(max_4, l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
loss1 /= len(Ytest)
loss2 /= len(Ytest)
loss3 /= len(Ytest)
loss4 /= len(Ytest)
total_loss = loss1 + loss2 + loss3 + loss4
total_loss /= 4.0
print('median: %.3f %.3f %.3f %.3f' % (np.median(list_1), np.median(list_2), np.median(list_3), np.median(list_4)))
print('max loss: %.3f %.3f %.3f %.3f' % (max_1, max_2, max_3, max_4))
print('Real test score: %.3f %.3f %.3f %.3f' % (loss1, loss2, loss3, loss4))
print("acc: %.3f" % (total_loss))