-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_challenge.py
115 lines (97 loc) · 3.51 KB
/
train_challenge.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
'''
EECS 445 - Introduction to Machine Learning
Fall 2018 - Project 2
Train Challenge
Trains a neural network to classify images
Periodically outputs training information, and saves model checkpoints
Usage: python train_challenge.py
'''
import torch
import numpy as np
import utils
from dataset import get_train_val_test_loaders
from model.challenge import Challenge, ResidualBlock
from train_common import *
from utils import config
def _train_epoch(data_loader, model, criterion, optimizer):
"""
Train the `model` for one epoch of data from `data_loader`
Use `optimizer` to optimize the specified `criterion`
"""
# TODO: complete the training step
for i, (X, y) in enumerate(data_loader):
# clear parameter gradients
optimizer.zero_grad()
#
# forward + backward + optimize
output = model(X)
loss = criterion(output, y)
loss.backward()
optimizer.step()
#
#
def _evaluate_epoch(axes, tr_loader, val_loader, model, criterion, epoch, stats):
with torch.no_grad():
y_true, y_pred = [], []
correct, total = 0, 0
running_loss = []
for X, y in tr_loader:
output = model(X)
predicted = predictions(output.data)
y_true.append(y)
y_pred.append(predicted)
total += y.size(0)
correct += (predicted == y).sum().item()
running_loss.append(criterion(output, y).item())
train_loss = np.mean(running_loss)
train_acc = correct / total
with torch.no_grad():
y_true, y_pred = [], []
correct, total = 0, 0
running_loss = []
for X, y in val_loader:
output = model(X)
predicted = predictions(output.data)
y_true.append(y)
y_pred.append(predicted)
total += y.size(0)
correct += (predicted == y).sum().item()
running_loss.append(criterion(output, y).item())
val_loss = np.mean(running_loss)
val_acc = correct / total
stats.append([val_acc, val_loss, train_acc, train_loss])
utils.log_cnn_training(epoch, stats)
utils.update_cnn_training_plot(axes, epoch, stats)
def main():
# data loaders
tr_loader, va_loader, te_loader, _ = get_train_val_test_loaders(
num_classes=config('challenge.num_classes'))
# TODO: define model, loss function, and optimizer
# TODO: define loss function, and optimizer
####
model = Challenge(ResidualBlock, [2,2,2,2])
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
####
# Attempts to restore the latest checkpoint if exists
print('Loading challenge...')
model, start_epoch, stats = restore_checkpoint(model,
config('challenge.checkpoint'))
axes = utils.make_cnn_training_plot()
# Evaluate model
_evaluate_epoch(axes, tr_loader, va_loader, model, criterion, start_epoch,
stats)
# Loop over the entire dataset multiple times
for epoch in range(start_epoch, config('challenge.num_epochs')):
# Train model
_train_epoch(tr_loader, model, criterion, optimizer)
# Evaluate model
_evaluate_epoch(axes, tr_loader, va_loader, model, criterion, epoch+1,
stats)
# Save model parameters
save_checkpoint(model, epoch+1, config('challenge.checkpoint'), stats)
print('Finished Training')
# Keep plot open
utils.hold_training_plot()
if __name__ == '__main__':
main()