Repository navigation
Expand file tree
/
Copy pathmodeltester.py
More file actions
170 lines (136 loc) · 5.4 KB
/
Copy pathmodeltester.py
File metadata and controls
170 lines (136 loc) · 5.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from src.config import *
import numpy as np
import numpy
from scipy.signal import savgol_filter
from sklearn.metrics import mean_squared_error
import pandas as pd
skip=False
def double_conv(in_channels, out_channels):
return nn.Sequential(
nn.Conv1d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1,padding=1),
nn.BatchNorm1d(out_channels),
nn.GELU(),
nn.Conv1d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1,padding=1),
nn.BatchNorm1d(out_channels),
nn.GELU(),
nn.Conv1d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1,padding=1),
nn.BatchNorm1d(out_channels),
nn.GELU()
)
class Unet1d(nn.Module):
def __init__(self, n_feature, hidden_dim, n_output):
super(Unet1d, self).__init__()
self.n_feature = n_feature
self.hidden_dim = hidden_dim
self.n_output = n_output
self.dconv_down1 = double_conv(n_feature,hidden_dim)
self.dconv_down2 = double_conv(hidden_dim,hidden_dim*2)
self.dconv_down3 = double_conv(hidden_dim*2,hidden_dim*4)
self.dconv_down4 = double_conv(hidden_dim*4,hidden_dim*8)
self.dconv_down5 = double_conv(hidden_dim*8,hidden_dim*16)
self.downsample = nn.MaxPool1d(kernel_size=2, stride=2)
self.upsample = nn.Upsample(scale_factor=2, mode='linear', align_corners=True)
if skip:
self.dconv_up5 = double_conv(hidden_dim*(16+8),hidden_dim*8)
self.dconv_up4 = double_conv(hidden_dim*(8+4),hidden_dim*4)
self.dconv_up3 = double_conv(hidden_dim*(4+2),hidden_dim*2)
self.dconv_up2 = double_conv(hidden_dim*(2+1),hidden_dim*1)
else:
self.dconv_up5 = double_conv(hidden_dim*(16),hidden_dim*8)
self.dconv_up4 = double_conv(hidden_dim*(8),hidden_dim*4)
self.dconv_up3 = double_conv(hidden_dim*(4),hidden_dim*2)
self.dconv_up2 = double_conv(hidden_dim*(2),hidden_dim*1)
self.dropout = nn.Dropout(0.5)
self.lastlayer = nn.Conv1d(in_channels=hidden_dim, out_channels=2, kernel_size=1, stride=1,padding=0)
self.flatten = Flatten()
#change this to reshape the input! reshape hidenlayer
def forward(self, x):
conv1 = self.dconv_down1(x)
x = self.downsample(conv1)
conv2 = self.dconv_down2(x)
x = self.downsample(conv2)
conv3 = self.dconv_down3(x)
x = self.downsample(conv3)
conv4 = self.dconv_down4(x)
x = self.downsample(conv4)
x = self.dconv_down5(x)
#x = self.dropout(x)
x = self.upsample(x)
if skip:
x = torch.cat([x,conv4], dim=1)
x = self.dconv_up5(x)
x = self.upsample(x)
if skip:
x = torch.cat([x,conv3], dim=1)
x = self.dconv_up4(x)
x = self.upsample(x)
if skip:
x = torch.cat([x,conv2], dim=1)
x = self.dconv_up3(x)
x = self.upsample(x)
if skip:
x = torch.cat([x,conv1], dim=1)
x = self.dconv_up2(x)
x = self.lastlayer(x)
x = self.flatten(x)
return x
old = False
blind = 1
if blind ==0:
inputcsv="./raw_data/traininput.csv"
labelcsv="./raw_data/trainoutput.csv"
elif blind ==1:
inputcsv="./raw_data/blindtestinput.csv"
labelcsv="./raw_data/blindtestoutput.csv"
modelpath="./results/0.1model.pt"
peaknum=0
ndiff=0
maxtest=5000
from numpy import genfromtxt
features=genfromtxt(inputcsv, delimiter=',')
labels=genfromtxt(labelcsv, delimiter=',')
#need to add minibatch
if len(features)>maxtest:
labels=labels[1+50000:maxtest+50000]
features=features[1+50000:maxtest+50000]
features = torch.tensor(features).type(torch.FloatTensor).cuda()
features = features.view(-1,10,400)
with torch.no_grad():
if old:
net = Unet1d_old(features.shape[1], opt.hidden_dim, opt.n_classes).cuda()
else:
net = Unet1d(features.shape[1], opt.hidden_dim, opt.n_classes).cuda()
net.load_state_dict(torch.load(modelpath))
net.eval()
#net.train()
train_predicted = net(features)
labels=labels
train_n=train_predicted.data.cpu().numpy()[:,0:800]
print(len(labels))
print(peaknum,ndiff)
crosscoef_test_n=[]
crosscoef_test_k=[]
for i in range (len(labels)):
crosscoef_test_n.append(numpy.corrcoef(labels[i][0:400],train_n[i][0:400])[1,0])
crosscoef_test_k.append(numpy.corrcoef(labels[i][399:-1],train_n[i][399:-1])[1,0])
#print(mean_squared_error(labels[:,0:400],train_n[:,0:400]))
train_distant_n = (labels[:,0:400]-train_n[:,0:400])**2
train_distant_n = np.average(train_distant_n, axis=1)
train_distant_n = np.sqrt(train_distant_n)
train_distant_n = np.average(train_distant_n)
print(train_distant_n)
#print(mean_squared_error(labels[:,399:-1],train_n[:,399:-1]))
train_distant_k = (labels[:,399:-1]-train_n[:,399:-1])**2
train_distant_k = np.average(train_distant_k, axis=1)
train_distant_k = np.sqrt(train_distant_k)
train_distant_k = np.average(train_distant_k)
print(train_distant_k)
print('crosscoef: test Accn=%.4f' % (sum(crosscoef_test_n) / len(crosscoef_test_n)))
print('crosscoef: test Acck=%.4f' % (sum(crosscoef_test_k) / len(crosscoef_test_k)))
plt.plot(train_n[0],'r')
plt.plot(labels[0],'g')
if blind ==1:
numpy.savetxt("blindpredicted.csv", train_n, delimiter=",")
elif blind ==0:
numpy.savetxt("trainpredicted.csv", train_n, delimiter=",")
plt.show()