-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain_results.py
262 lines (225 loc) · 8.02 KB
/
train_results.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'''
Classes and methods related to abstracting and handling results later (saved in .pkl files)
Author: Diedre Carmo
https://github.com/dscarmo
'''
from matplotlib import pyplot as plt
import numpy as np
import pickle
import torch
import torch.nn as nn
class TrainResults():
'''
Stores training results
'''
@staticmethod
def load(filepath):
tr = None
with open(filepath, "rb") as f:
tr = pickle.load(f)
return tr
def __init__(self, val_loss, val_acc, train_loss, train_acc, nepochs, loss_name, metric_name, plot_title="", val_class=None,
train_class=None, metric_name_class="Class Accuracy"):
self.val_loss = val_loss
self.val_acc = val_acc
self.train_loss = train_loss
self.train_acc = train_acc
self.nepochs = nepochs
self.loss_name = loss_name
self.metric_name = metric_name
self.plot_title = plot_title
self.metric_name_class = metric_name_class
self.train_class = train_class
self.val_class = val_class
def plot(self, plot_title=None, show=True, loss_only=False, o="", generate_new_figure=False, ylim=1.0, classify=False,
lower_ylim=0.0):
# epoch_range = range(1, self.nepochs + 1)
epoch_range = range(len(self.train_acc))
plt_title = ''
if show:
if plot_title is None:
plt_title = self.plot_title
else:
plt_title = plot_title
if not generate_new_figure:
plt.figure(num=plt_title)
# old pkls support
if classify:
classify = (self.train_class is not None) and (self.val_class is not None)
if not loss_only:
if generate_new_figure:
plt.figure(plt_title + " metrics")
else:
plt.subplot(1, 2+1*classify, 1)
plt.ylabel("Dice")
plt.xlabel("Epoch")
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.val_acc, '-', label=o + ' val')
plt.legend()
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.train_acc, '-', label=o + ' train')
plt.legend()
if classify:
if generate_new_figure:
plt.figure(num=plt_title + " classification metrics")
else:
plt.subplot(1, 2+1*classify, 2)
plt.ylabel(self.metric_name_class)
plt.xlabel("Epoch")
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.val_class, '-', label=o + ' val')
plt.legend()
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.train_class, '-', label=o + ' train')
plt.legend()
if generate_new_figure:
plt.figure(plt_title + " loss")
else:
plt.subplot(1, 3, 3)
plt.ylabel(self.loss_name)
plt.xlabel("Epoch")
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.val_loss, '-', label=o + ' val')
plt.legend()
if ylim is not None:
plt.ylim(lower_ylim, ylim)
plt.plot(epoch_range, self.train_loss, '-', label=o + ' train')
plt.legend()
if show:
plt.show()
def save(self, filepath):
'''
Saves itself
'''
if filepath[-4:] != ".pkl":
print("WARNING: save path not valid! should be a .pkl file, trying to save anyway")
with open(filepath, "wb") as output_file:
pickle.dump(self, output_file)
# DEPRECATED, will be removed once backwards compatibility is assured with new class
class OLD_TrainResults():
'''
Stores training results
'''
@staticmethod
def load(filepath):
tr = None
with open(filepath, "rb") as f:
tr = pickle.load(f)
return tr
def __init__(self, val_loss, val_acc, train_loss, train_acc, nepochs, loss_name, metric_name, plot_title=""):
self.val_loss = val_loss
self.val_acc = val_acc
self.train_loss = train_loss
self.train_acc = train_acc
self.nepochs = nepochs
self.loss_name = loss_name
self.metric_name = metric_name
self.plot_title = plot_title
def plot(self, plot_title=None, show=True, loss_only=False, o=""):
# epoch_range = range(1, self.nepochs + 1)
epoch_range = range(len(self.train_acc))
if show:
if plot_title is None:
plt.figure(num=self.plot_title)
else:
plt.figure(num=plot_title)
if not loss_only:
plt.subplot(1, 2, 1)
plt.ylabel(self.metric_name)
plt.xlabel("Epoch")
plt.ylim(0.0, 1.0)
plt.plot(epoch_range, self.val_acc, '-', label=o + ' val')
plt.legend()
plt.ylim(0.0, 1.0)
plt.plot(epoch_range, self.train_acc, '-', label=o + ' train')
plt.legend()
plt.subplot(1, 2, 2)
plt.ylabel(self.loss_name)
plt.xlabel("Epoch")
plt.ylim(0.0, 1.0)
plt.plot(epoch_range, self.val_loss, '-', label=o + ' val')
plt.legend()
plt.ylim(0.0, 1.0)
plt.plot(epoch_range, self.train_loss, '-', label=o + ' train')
plt.legend()
if show:
plt.show()
def save(self, filepath):
'''
Saves itself
'''
if filepath[-4:] != ".pkl":
print("WARNING: save path not valid! should be a .pkl file, trying to save anyway")
with open(filepath, "wb") as output_file:
pickle.dump(self, output_file)
class TestModel(nn.Module):
'''
Single convolution test model
'''
def __init__(self, in_ch, out_ch):
super(TestModel, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 3)
def forward(self, x):
x = self.conv(x)
return x
def test_display(wintitle, input, output):
plt.figure(num=wintitle)
plt.subplot(1, 2, 1)
plt.title("Input")
plt.imshow(input.squeeze().numpy(), cmap='gray')
plt.subplot(1, 2, 2)
plt.title("Output")
plt.imshow(output.squeeze().detach().numpy(), cmap='gray')
def tr_test():
'''
Tests train results with test model
'''
testimg = torch.rand((1, 1, 1000, 1000))
test_model = TestModel(1, 1)
test_output = test_model(testimg)
test_display("Pre-save", testimg, test_output)
nepochs = 10
vl = np.random.rand(nepochs)
va = np.random.rand(nepochs)
tl = np.random.rand(nepochs)
ta = np.random.rand(nepochs)
tr = TrainResults(test_model, vl, va, tl, ta, nepochs, "test loss", "test metric")
tr.plot(plot_title="Pre-save plot", show=False)
test_path = "./test.pkl"
tr.save(test_path)
loaded_tr = TrainResults.load(test_path)
loaded_tr.plot(plot_title="Post-load plot", show=False)
loaded_test_output = loaded_tr.best_model(testimg)
test_display("Post-load", testimg, loaded_test_output)
plt.show()
def openall(maindir='.'):
import glob
from os.path import join as add_path
from os.path import basename
opened = 0
for filename in glob.iglob(add_path(maindir, "*.pkl")):
plt.figure(basename(filename))
tr = TrainResults.load(filename)
tr.plot(show=False)
opened += 1
if opened == 0:
print("No .pkl TrainResults files found on {}".format(maindir if maindir != '.' else "current directory"))
else:
print("Finished. Opened {} results.".format(opened))
plt.show()
if __name__ == "__main__":
from sys import argv
if argv[1] == "tr_test":
tr_test()
elif argv[1] == "openall":
try:
maindir = argv[2]
except IndexError:
print("No directory passed, using current directory")
maindir = '.'
openall(maindir)