-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_cnn.py
132 lines (115 loc) · 4.2 KB
/
train_cnn.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
"""
EECS 445 - Introduction to Machine Learning
Fall 2018 - Project 2
Train CNN
Trains a convolutional neural network to classify images
Periodically outputs training information, and saves model checkpoints
Usage: python train_cnn.py
"""
import torch
import numpy as np
import random
from dataset import get_train_val_test_loaders
from model.cnn import CNN
from train_common import *
from utils import config
import utils
torch.manual_seed(42)
np.random.seed(42)
random.seed(42)
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`
"""
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):
"""
Evaluates the `model` on the train and validation set.
"""
with torch.no_grad():
y_true, y_pred = [], []
correct, total = 0, 0
running_loss = []
for X, y in tr_loader:
output = model(X) ## to be fixed
predicted = predictions(output.data) # predictions
y_true.append(y)
y_pred.append(predicted)
total += y.size(0)
correct += (predicted == y).sum().item()
running_loss.append(criterion(output, y).item()) # to be fixed
train_loss = np.mean(running_loss)
train_acc = correct / total
######################## added
cc = np.zeros(5)
tt = np.zeros(5)
####################### added
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) # predictions
y_true.append(y)
y_pred.append(predicted)
total += y.size(0)
correct += (predicted == y).sum().item()
######################### added
for i in range(5):
cc[i] += len(np.where((predicted == y) & (y == i))[0])
tt[i] += len(np.where(y == i)[0])
######################## added
running_loss.append(criterion(output, y).item())
val_loss = np.mean(running_loss)
val_acc = correct / total
#####################
for i in range(5):
print("accuracy for: ", i)
print(cc[i] / tt[i])
#####################
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('cnn.num_classes'))
# Model
model = CNN()
# TODO: define loss function, and optimizer
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
# optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
#
print('Number of float-valued parameters:', count_parameters(model))
# Attempts to restore the latest checkpoint if exists
print('Loading cnn...')
model, start_epoch, stats = restore_checkpoint(model, config('cnn.checkpoint'))
axes = utils.make_cnn_training_plot()
# Evaluate the randomly initialized model
_evaluate_epoch(axes, tr_loader, va_loader, model, criterion, start_epoch,
stats) # to be fixed
# Loop over the entire dataset multiple times
for epoch in range(start_epoch, config('cnn.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('cnn.checkpoint'), stats)
print('Finished Training')
# Save figure and keep plot open
utils.save_cnn_training_plot()
utils.hold_training_plot()
if __name__ == '__main__':
main()