-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.py
170 lines (141 loc) · 6.82 KB
/
data.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
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
import torch
from torch.utils import data
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data.sampler import SequentialSampler
import numpy as np
import matplotlib.pyplot as plt
def DatasetSplit(dataset, test_split, shuffle_dataset, random_seed, batchsize, testBatchsize, numworkers, pinmemory):
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(test_split * dataset_size))
if shuffle_dataset:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, test_indices = indices[split:], indices[:split]
# print(train_indices)
# print(test_indices)
# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
training_data_loader = torch.utils.data.DataLoader(dataset,
sampler=train_sampler,
batch_size=batchsize,
num_workers=numworkers,
pin_memory=pinmemory)
testing_data_loader = torch.utils.data.DataLoader(dataset,
sampler=test_sampler,
batch_size=testBatchsize,
num_workers=numworkers,
pin_memory=pinmemory)
return training_data_loader, testing_data_loader
def TestPredictionPlot(model, device, testing_data_loader, k, d, fout):
List_t = []
List_input = []
List_prediction = []
List_target = []
with torch.no_grad():
for batch in testing_data_loader:
input, target = batch[0].to(
device, torch.float), batch[1].to(device, torch.float)
prediction = model(input)
i = 0
for matrix in input:
if np.abs(matrix[2][0][0].item() - k) < 1e-8: # k
if np.abs(matrix[3][0][0].item() - d) < 1e-8: # d
List_t.append(matrix[1][0][0].item())
List_input.append(matrix)
List_prediction.append(prediction[i])
List_target.append(target[i])
i += 1
List_input = [x for y, x in sorted(zip(List_t, List_input))]
List_prediction = [x for y, x in sorted(zip(List_t, List_prediction))]
List_target = [x for y, x in sorted(zip(List_t, List_target))]
List_t = sorted(List_t)
fig, ax = plt.subplots(2, 10, figsize=(20, 3))
for i in range(10):
input = List_input[i].cpu().numpy()
target = List_target[i]
prediction = List_prediction[i]
im_tar = ax[0][i].imshow(target[0].cpu().numpy()[::-1], cmap="jet")
ax[0][i].axis('off')
ax[0][i].set_title("t = "+str(input[1][0][0]), size=14)
im_pre = ax[1][i].imshow(prediction[0].cpu().numpy()[::-1], cmap="jet")
ax[1][i].axis('off')
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.82, 0.13, 0.01, 0.75])
fig.colorbar(im_tar, cax=cbar_ax)
plt.gcf().text(0.07, 0.6, "K=" +
str.format('{0:.3f}', input[2][0][0]), fontsize=14)
plt.gcf().text(0.07, 0.4, "D=" +
str.format('{0:.3f}', input[3][0][0]), fontsize=14)
plt.show()
fig.savefig(fout, dpi=150, quality=100, format='svg')
def TestErrorPlot(model, device, testing_data_loader):
error_List = []
testID_List = []
count = 1
with torch.no_grad():
for batch in testing_data_loader:
input, target = batch[0].to(
device, torch.float), batch[1].to(device, torch.float)
prediction = model(input)
tmp_error = 0
for j in range(len(prediction)):
# tmp_error = torch.mean(torch.abs(prediction[j]-target[j]))/torch.abs(torch.max(target[j])-torch.min(target[j]))
# tmp_error = ComputeTestError(prediction[j], target[j]) * 0.0094 / 0.014284110054473812
tmp_error = ComputeTestError(prediction[j], target[j])
# if tmp_error < 0.3:
# error_List.append(tmp_error.item())
# testID_List.append(count)
# count += 1
error_List.append(tmp_error.item())
testID_List.append(count)
count += 1
testID_List = np.asarray(testID_List)
error_List = np.asarray(error_List)
avg_error = np.average(error_List)
# print(np.asarray(testID_List).type)
# print(np.asarray(error_List).size)
plt.plot(testID_List, error_List, 'ko', zorder=1, markersize=1)
# plt.scatter(np.asarray(testID_List), np.asarray(error_List), 'bo')
plt.hlines(avg_error, 1, count, colors='r', zorder=2)
fig = plt.gcf()
fig.savefig("./Figure/statistics_testdata.png",
dpi=300, quality=100, format='png')
print(avg_error)
def TestErrorCompute(model, device, testing_data_loader):
error_List = []
testID_List = []
count = 1
with torch.no_grad():
for batch in testing_data_loader:
input, target = batch[0].to(
device, torch.float), batch[1].to(device, torch.float)
prediction = model(input)
tmp_error = 0
for j in range(len(prediction)):
tmp_error = ComputeTestError(prediction[j], target[j])
error_List.append(tmp_error.item())
testID_List.append(count)
count += 1
testID_List = np.asarray(testID_List)
error_List = np.asarray(error_List)
avg_error = np.average(error_List)
return avg_error
def ComputeErrorVsEpoch(checkpoint_path, device, testing_data_loader):
error_list = []
for i in range(1, 101):
model = torch.load(checkpoint_path +
'model_epoch_' + str(int(i)) + '.pth')
tmp_error = TestErrorCompute(model, device, testing_data_loader)
error_list.append(tmp_error)
error_arr = np.asarray(error_list)
return error_arr
def ComputeTestError(prediction, target):
# tmp_error = torch.mean(torch.abs(prediction-target))/torch.abs(torch.max(target)-torch.min(target))
# tmp_error = torch.mean(torch.abs(prediction-target)**2)/torch.abs(torch.max(target))
# tmp_error = torch.sqrt(torch.abs(prediction-target).pow(2).sum())/prediction.numel()/torch.abs(torch.max(target)-torch.min(target))
# tmp_error = torch.sqrt(torch.mean(torch.abs(prediction-target)**2))/torch.abs(torch.max(target))
tmp_error = torch.sqrt(torch.mean((prediction-target)**2))/torch.abs(torch.max(target)-torch.min(target))
# tmp_error = torch.mean(torch.abs(prediction-target)**2)/torch.abs(torch.max(target)-torch.min(target))
return tmp_error