-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
136 lines (111 loc) · 3.84 KB
/
train.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
import torch
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
from torch import nn
import torch.optim as optim
import numpy as np
from model import TrackmaniaNet
from dataset import TrackManiaDataset
import matplotlib.pyplot as plt
# LEARNING_RATE = 0.048749
# DECAY = 0.00013067
# BATCH_SIZE = 64
# DROP_OUT = 0.175
LEARNING_RATE = 0.025557158229886558
DECAY = 0.0004193392285214784
BATCH_SIZE = 64
DROP_OUT = 0.15
def load_data(batch_size=64):
training_data = TrackManiaDataset(
"data",
"train.csv",
transform=transforms.Compose([transforms.ConvertImageDtype(torch.float)]),
)
test_data = TrackManiaDataset(
"data",
"test.csv",
transform=transforms.Compose([transforms.ConvertImageDtype(torch.float)]),
)
train_dataloader = DataLoader(
training_data,
batch_size=batch_size,
shuffle=True,
num_workers=4,
pin_memory=True,
)
test_dataloader = DataLoader(
test_data, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True
)
return train_dataloader, test_dataloader
def train(net, dataloader, epochs=1, lr=0.01, momentum=0.9, decay=0.0, verbose=True):
net.to(device)
losses = []
criterion = nn.MSELoss()
optimizer = optim.SGD(
net.parameters(), lr=lr, momentum=momentum, weight_decay=decay
)
for epoch in range(epochs):
sum_loss = 0.0
for i, batch in enumerate(dataloader, 0):
inputs, labels = batch[0].to(device), batch[1].to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
losses.append(loss.item())
sum_loss += loss.item()
if i % 100 == 99:
if verbose:
print("[%d, %5d] loss: %.6f" % (epoch + 1, i + 1, sum_loss / 100))
sum_loss = 0.0
return losses
def accuracy_and_loss(net, dataloader, err_thresh=0.1):
loss_sum = 0.0
correct = 0
correct_speed = 0
correct_steer = 0
size = len(dataloader.dataset)
criterion = nn.MSELoss()
with torch.no_grad():
for batch in dataloader:
images, labels = batch[0].to(device), batch[1].to(device)
outputs = net(images)
loss_sum += criterion(outputs, labels).item()
err = torch.abs(torch.sub(outputs, labels))
correct_speed += torch.sum(torch.le(err, err_thresh).long()[:, 0])
correct_steer += torch.sum(torch.le(err, err_thresh).long()[:, 1])
correct += torch.sum(
torch.ge(torch.sum(torch.le(err, err_thresh).long(), 1), 2).long()
)
return (
correct / size * 100,
correct_speed / size * 100,
correct_steer / size * 100,
loss_sum / size,
)
def smooth(x, size):
return np.convolve(x, np.ones(size) / size, mode="valid")
if __name__ == "__main__":
torch.multiprocessing.freeze_support()
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
train_dataloader, test_dataloader = load_data(BATCH_SIZE)
trackmania_net = TrackmaniaNet(drop_out=DROP_OUT)
conv_losses = train(
trackmania_net, train_dataloader, epochs=15, lr=LEARNING_RATE, decay=DECAY
)
torch.save(trackmania_net.state_dict(), "models/model_large_tune.pth")
plt.title('Loss vs Num Batches')
plt.ylabel('Loss')
plt.xlabel('Num Batches')
plt.plot(smooth(conv_losses, 50))
print(
"Training accuracy & MSE loss: %f%%, %f%%, %f%%, %f"
% accuracy_and_loss(trackmania_net, train_dataloader)
)
print(
"Testing accuracy & MSE loss: %f%%, %f%%, %f%%, %f"
% accuracy_and_loss(trackmania_net, test_dataloader)
)
plt.show()