-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_common.py
More file actions
231 lines (194 loc) · 6.83 KB
/
train_common.py
File metadata and controls
231 lines (194 loc) · 6.83 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
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
"""
Helper file for common training functions.
"""
from utils import config
import numpy as np
import itertools
import os
import torch
from torch.nn.functional import softmax
from sklearn import metrics
import utils
def count_parameters(model):
"""Count number of learnable parameters."""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def save_checkpoint(model, epoch, checkpoint_dir, stats):
"""Save a checkpoint file to `checkpoint_dir`."""
state = {
"epoch": epoch,
"state_dict": model.state_dict(),
"stats": stats,
}
filename = os.path.join(checkpoint_dir, "epoch={}.checkpoint.pth.tar".format(epoch))
torch.save(state, filename)
def check_for_augmented_data(data_dir):
"""Ask to use augmented data if `augmented_landmarks.csv` exists in the data directory."""
if "augmented_landmarks.csv" in os.listdir(data_dir):
print("Augmented data found, would you like to use it? y/n")
print(">> ", end="")
rep = str(input())
return rep == "y"
return False
def restore_checkpoint(model, checkpoint_dir, cuda=False, force=False, pretrain=False):
"""Restore model from checkpoint if it exists.
Returns the model and the current epoch.
"""
try:
cp_files = [
file_
for file_ in os.listdir(checkpoint_dir)
if file_.startswith("epoch=") and file_.endswith(".checkpoint.pth.tar")
]
except FileNotFoundError:
cp_files = None
os.makedirs(checkpoint_dir)
if not cp_files:
print("No saved model parameters found")
if force:
raise Exception("Checkpoint not found")
else:
return model, 0, []
# Find latest epoch
for i in itertools.count(1):
if "epoch={}.checkpoint.pth.tar".format(i) in cp_files:
epoch = i
else:
break
if not force:
print(
"Which epoch to load from? Choose in range [0, {}].".format(epoch),
"Enter 0 to train from scratch.",
)
print(">> ", end="")
inp_epoch = int(input())
if inp_epoch not in range(epoch + 1):
raise Exception("Invalid epoch number")
if inp_epoch == 0:
print("Checkpoint not loaded")
clear_checkpoint(checkpoint_dir)
return model, 0, []
else:
print("Which epoch to load from? Choose in range [1, {}].".format(epoch))
inp_epoch = int(input())
if inp_epoch not in range(1, epoch + 1):
raise Exception("Invalid epoch number")
filename = os.path.join(
checkpoint_dir, "epoch={}.checkpoint.pth.tar".format(inp_epoch)
)
print("Loading from checkpoint {}?".format(filename))
if cuda:
checkpoint = torch.load(filename)
else:
# Load GPU model on CPU
checkpoint = torch.load(filename, map_location=lambda storage, loc: storage)
try:
start_epoch = checkpoint["epoch"]
stats = checkpoint["stats"]
if pretrain:
model.load_state_dict(checkpoint["state_dict"], strict=False)
else:
model.load_state_dict(checkpoint["state_dict"])
print(
"=> Successfully restored checkpoint (trained for {} epochs)".format(
checkpoint["epoch"]
)
)
except:
print("=> Checkpoint not successfully restored")
raise
return model, inp_epoch, stats
def clear_checkpoint(checkpoint_dir):
"""Remove checkpoints in `checkpoint_dir`."""
filelist = [f for f in os.listdir(checkpoint_dir) if f.endswith(".pth.tar")]
for f in filelist:
os.remove(os.path.join(checkpoint_dir, f))
print("Checkpoint successfully removed")
def early_stopping(stats, curr_count_to_patience, global_min_loss):
"""Calculate new patience and validation loss.
Returns: new values of curr_patience and global_min_loss
"""
# Implement early stopping
curr_val_loss = stats[-1][1] # the newest epoch - val_loss, idx 0 is val_acc
if curr_val_loss >= global_min_loss:
curr_count_to_patience += 1
else:
global_min_loss = curr_val_loss
curr_count_to_patience = 0
return curr_count_to_patience, global_min_loss
def evaluate_epoch(
axes,
tr_loader,
val_loader,
te_loader,
model,
criterion,
epoch,
stats,
include_test=False,
update_plot=True,
multiclass=False,
):
"""Evaluate the `model` on the train and validation set."""
def _get_metrics(loader):
y_true, y_pred, y_score = [], [], []
correct, total = 0, 0
running_loss = []
for X, y in loader:
with torch.no_grad():
output = model(X)
predicted = predictions(output.data)
y_true.append(y)
y_pred.append(predicted)
if not multiclass:
y_score.append(softmax(output.data, dim=1)[:, 1])
else:
y_score.append(softmax(output.data, dim=1))
total += y.size(0)
correct += (predicted == y).sum().item()
running_loss.append(criterion(output, y).item())
y_true = torch.cat(y_true)
y_pred = torch.cat(y_pred)
y_score = torch.cat(y_score)
loss = np.mean(running_loss)
acc = correct / total
if not multiclass:
auroc = metrics.roc_auc_score(y_true, y_score)
else:
auroc = metrics.roc_auc_score(y_true, y_score, multi_class="ovo")
return acc, loss, auroc
train_acc, train_loss, train_auc = _get_metrics(tr_loader)
val_acc, val_loss, val_auc = _get_metrics(val_loader)
stats_at_epoch = [
val_acc,
val_loss,
val_auc,
train_acc,
train_loss,
train_auc,
]
if include_test:
stats_at_epoch += list(_get_metrics(te_loader))
stats.append(stats_at_epoch)
utils.log_training(epoch, stats)
if update_plot:
utils.update_training_plot(axes, epoch, stats)
def train_epoch(data_loader, model, criterion, optimizer):
"""Train the `model` for one epoch of data from `data_loader`."""
for i, (X, y) in enumerate(data_loader):
optimizer.zero_grad()
outputs = model(X)
loss = criterion(outputs, y)
loss.backward()
optimizer.step() # update
# total_loss += loss.item() # detach?
# avg_loss = total_loss / len(data_loader)
# print(f"Average training loss: {avg_loss}")
def predictions(logits):
"""Determine predicted class index given a tensor of logits.
Example: Given tensor([[0.2, -0.8], [-0.9, -3.1], [0.5, 2.3]]), return tensor([0, 0, 1])
Returns:
the predicted class output as a PyTorch Tensor
"""
# Implement predictions
pred = torch.argmax(logits, dim=1) # logits: (batch_size,num_classes)
return pred