-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
282 lines (243 loc) · 9.91 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
'''A script to train a simple model
'''
import numpy as np
import pickle
import torch
import torch.optim as optim
import json
import argparse
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
from os import path as osp
from transformer import Models, Optim
from dataLoader import PathDataLoader, PaddedSequence, PathMixedDataLoader
from torch.utils.tensorboard import SummaryWriter
def focal_loss(predVals, trueLabels, gamma, eps=1e-8):
'''
A function to calculate the focal loss as mentioned in
https://arxiv.org/pdf/1708.02002.pdf
:param predVals: The output of the final linear layer.
:param trueLabels: The true labels
:param gamma: The hyperparameter of the loss function
:param eps: A scalar value to enforce numerical stability.
:returns float: The loss value
'''
input_soft = F.softmax(predVals, dim=1) + eps
target_one_hot = torch.zeros((trueLabels.shape[0], 2), device=trueLabels.device)
target_one_hot.scatter_(1, trueLabels.unsqueeze(1), 1.0)
weight = torch.pow(-input_soft + 1., gamma)
focal = -weight*torch.log(input_soft)
loss = torch.sum(target_one_hot*focal, dim=1).sum()
return loss
def cal_performance(predVals, anchorPoints, trueLabels, lengths):
'''
Return the loss and number of correct predictions.
:param predVals: the output of the final linear layer.
:param anchorPoints: The anchor points of interest
:param trueLabels: The expected clas of the corresponding anchor points.
:param lengths: The legths of each of sequence in the batch
:returns (loss, n_correct): The loss of the model and number of avg predictions.
'''
n_correct = 0
total_loss = 0
for predVal, anchorPoint, trueLabel, length in zip(predVals, anchorPoints, trueLabels, lengths):
predVal = predVal.index_select(0, anchorPoint[:length])
trueLabel = trueLabel[:length]
loss = F.cross_entropy(predVal, trueLabel)
total_loss += loss
classPred = predVal.max(1)[1]
n_correct +=classPred.eq(trueLabel[:length]).sum().item()/length
return total_loss, n_correct
def train_epoch(model, trainingData, optimizer, device):
'''
Train the model for 1-epoch with data from wds
'''
model.train()
total_loss = 0
total_n_correct = 0
# Train for a single epoch.
for batch in tqdm(trainingData, mininterval=2):
optimizer.zero_grad()
encoder_input = batch['map'].float().to(device)
predVal = model(encoder_input)
# Calculate the cross-entropy loss
loss, n_correct = cal_performance(
predVal, batch['anchor'].to(device),
batch['labels'].to(device),
batch['length'].to(device)
)
loss.backward()
optimizer.step_and_update_lr()
total_loss +=loss.item()
total_n_correct += n_correct
return total_loss, total_n_correct
def eval_epoch(model, validationData, device):
'''
Evaluation for a single epoch.
:param model: The Transformer Model to be trained.
:param validataionData: The set of validation data.
:param device: cpu/cuda to be used.
'''
model.eval()
total_loss = 0.0
total_n_correct = 0.0
with torch.no_grad():
for batch in tqdm(validationData, mininterval=2):
encoder_input = batch['map'].float().to(device)
predVal = model(encoder_input)
loss, n_correct = cal_performance(
predVal,
batch['anchor'].to(device),
batch['labels'].to(device),
batch['length'].to(device)
)
total_loss +=loss.item()
total_n_correct += n_correct
return total_loss, total_n_correct
def check_data_folders(folder):
'''
Checks if the folder is formatted properly for training.
The folder need to have a 'train' and 'val' folder
:param folder: The folder to test
'''
assert osp.isdir(osp.join(folder, 'train')), "Cannot find trainining data"
assert osp.isdir(osp.join(folder, 'val')), "Cannot find validation data"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--batchSize', help="Batch size per GPU", required=True, type=int)
parser.add_argument('--mazeDir', help="Directory with training and validation data for Maze", default=None)
parser.add_argument('--forestDir', help="Directory with training and validation data for Random Forest", default=None)
parser.add_argument('--fileDir', help="Directory to save training Data")
args = parser.parse_args()
maze=False
if args.mazeDir is not None:
check_data_folders(args.mazeDir)
maze=True
forest=False
if args.forestDir is not None:
check_data_folders(args.forestDir)
forest=True
assert forest or maze, "Need to provide data folder for atleast one kind of environment"
dataFolder = args.mazeDir if not(maze and forest) and maze else args.forestDir
print(f"Using data from {dataFolder}")
batch_size = args.batchSize
device = 'cpu'
if torch.cuda.is_available():
print("Using GPU....")
device = torch.device('cuda')
if torch.cuda.device_count() > 1:
batch_size = batch_size * torch.cuda.device_count()
print(f"Total batch size : {batch_size}")
torch_seed = np.random.randint(low=0, high=1000)
torch.manual_seed(torch_seed)
model_args = dict(
n_layers=6,
n_heads=3,
d_k=512,
d_v=256,
d_model=512,
d_inner=1024,
pad_idx=None,
n_position=40*40,
dropout=0.1,
train_shape=[24, 24],
)
transformer = Models.Transformer(**model_args)
if torch.cuda.device_count() > 1:
print("Using ", torch.cuda.device_count(), "GPUs")
transformer = nn.DataParallel(transformer)
transformer.to(device=device)
# Define the optimizer
# TODO: What does these parameters do ???
optimizer = Optim.ScheduledOptim(
optim.Adam(transformer.parameters(), betas=(0.9, 0.98), eps=1e-9),
lr_mul = 0.5,
d_model = 256,
n_warmup_steps = 3200
)
# Training with Mixed samples
if maze and forest:
from toolz.itertoolz import partition
trainDataset= PathMixedDataLoader(
envListForest=list(range(10)),
dataFolderForest=osp.join(args.forestDir, 'train'),
envListMaze=list(range(10)),
dataFolderMaze=osp.join(args.mazeDir, 'train')
)
allTrainingData = trainDataset.indexDictForest + trainDataset.indexDictMaze
batch_sampler_train = list(partition(batch_size, allTrainingData))
trainingData = DataLoader(trainDataset, num_workers=15, batch_sampler=batch_sampler_train, collate_fn=PaddedSequence)
valDataset = PathMixedDataLoader(
envListForest=list(range(10)),
dataFolderForest=osp.join(args.forestDir, 'val'),
envListMaze=list(range(10)),
dataFolderMaze=osp.join(args.mazeDir, 'val')
)
allValData = valDataset.indexDictForest+valDataset.indexDictMaze
batch_sampler_val = list(partition(batch_size, allValData))
validationData = DataLoader(valDataset, num_workers=5, batch_sampler=batch_sampler_val, collate_fn=PaddedSequence)
else:
trainDataset = PathDataLoader(
env_list=list(range(1750)),
dataFolder=osp.join(dataFolder, 'train')
)
trainingData = DataLoader(trainDataset, num_workers=15, collate_fn=PaddedSequence, batch_size=batch_size)
# Validation Data
valDataset = PathDataLoader(
env_list=list(range(2500)),
dataFolder=osp.join(dataFolder, 'val')
)
validationData = DataLoader(valDataset, num_workers=5, collate_fn=PaddedSequence, batch_size=batch_size)
# Increase number of epochs.
n_epochs = 70
results = {}
train_loss = []
val_loss = []
train_n_correct_list = []
val_n_correct_list = []
trainDataFolder = args.fileDir
# Save the model parameters as .json file
json.dump(
model_args,
open(osp.join(trainDataFolder, 'model_params.json'), 'w'),
sort_keys=True,
indent=4
)
writer = SummaryWriter(log_dir=trainDataFolder)
for n in range(n_epochs):
train_total_loss, train_n_correct = train_epoch(transformer, trainingData, optimizer, device)
val_total_loss, val_n_correct = eval_epoch(transformer, validationData, device)
print(f"Epoch {n} Loss: {train_total_loss}")
print(f"Epoch {n} Loss: {val_total_loss}")
print(f"Epoch {n} Accuracy {val_n_correct/len(valDataset)}")
# Log data.
train_loss.append(train_total_loss)
val_loss.append(val_total_loss)
train_n_correct_list.append(train_n_correct)
val_n_correct_list.append(val_n_correct)
if (n+1)%5==0:
if isinstance(transformer, nn.DataParallel):
state_dict = transformer.module.state_dict()
else:
state_dict = transformer.state_dict()
states = {
'state_dict': state_dict,
'optimizer': optimizer._optimizer.state_dict(),
'torch_seed': torch_seed
}
torch.save(states, osp.join(trainDataFolder, 'model_epoch_{}.pkl'.format(n)))
pickle.dump(
{
'trainLoss': train_loss,
'valLoss':val_loss,
'trainNCorrect':train_n_correct_list,
'valNCorrect':val_n_correct_list
},
open(osp.join(trainDataFolder, 'progress.pkl'), 'wb')
)
writer.add_scalar('Loss/train', train_total_loss, n)
writer.add_scalar('Loss/test', val_total_loss, n)
writer.add_scalar('Accuracy/train', train_n_correct/len(trainDataset), n)
writer.add_scalar('Accuracy/test', val_n_correct/len(valDataset), n)