forked from ilennaj/ktree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraintestloop.py
269 lines (222 loc) · 9.6 KB
/
traintestloop.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
263
264
265
266
267
268
269
import torch
from torch.utils.data import DataLoader
import torch.optim as optim
import torch.nn as nn
import numpy as np
import math
from torch.optim.optimizer import required
from torch.utils.data.dataset import random_split
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import Optimizer
from pytorchtools import EarlyStopping
def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, randorder=False, patience=60):
'''
Trains and tests k-tree models
Inputs: model, trainloader, validloader, testloader, epochs, randorder, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []
# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []
# if randorder == True, generate the randomizer index array for randomizing the input image pixel order
if randorder == True:
ordering = torch.randperm(len(trainloader.dataset.tensors[0][0]))
# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)
for epoch in range(epochs): # loop over the dataset multiple times
######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
labels = labels.cuda()
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, labels.float().reshape(-1,1))
loss.backward()
#### # Freeze select weights by zeroing out gradients
for child in model.children():
for param in child.parameters():
for freeze_mask in model.freeze_mask_set:
if param.grad.shape == freeze_mask.shape:
param.grad[freeze_mask] = 0
optimizer.step()
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
# Generate loss and accuracy curves by saving average every 4th minibatch
if (i % 4) == 3:
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0
######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())
valid_loss = np.average(valid_losses)
# early_stopping needs the validation loss to check if it has decreased,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))
print('Finished Training, %d epochs' % (epoch+1))
######################
# test the model #
######################
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels, _ = data
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()
# Calculate test accuracy
accuracy = correct/total
print('Accuracy of the network on the test images: %2f %%' % (
100 * accuracy))
if randorder == True:
return(loss_curve, acc_curve, loss, accuracy, model, ordering)
else:
return(loss_curve, acc_curve, loss, accuracy, model)
def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patience=60):
'''
Trains and tests fcnn models
Inputs: model, trainloader, validloader, testloader, epochs, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []
# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []
# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)
for epoch in range(epochs): # loop over the dataset multiple times
######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, labels.float().reshape(-1,1))
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
if i % 4 == 3: # Generate loss and accuracy curves by saving average every 4th minibatch
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0
######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())
valid_loss = np.average(valid_losses)
# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)
if early_stopping.early_stop:
print("Early stopping")
break
# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))
print('Finished Training, %d epochs' % (epoch+1))
correct = 0
all_loss = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels, _ = data
images = images.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()
all_loss += loss
# Calculate test accuracy
accuracy = correct/total
# Calculate average loss
ave_loss = all_loss.item()/total
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * accuracy))
return(loss_curve, acc_curve, ave_loss, accuracy, model)