-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
349 lines (280 loc) · 12.2 KB
/
train.py
File metadata and controls
349 lines (280 loc) · 12.2 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from utils.data_lightning import preloading
from utils.data_lightning import otf
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
import matplotlib as mat
import argparse
import torch as th
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
import random
import os
import sys
from datetime import datetime
import matplotlib as mpl
import torch.optim as optim
import time
from models.ae import seq2seq_ConvLSTM
mat.use("Agg") # headless mode
# -------------- Functions
def accuracy(prediction, target, threshold = 1e-2):
# totale celle bagnate del fiume nel target
total = (target * prediction).cpu().detach().numpy()
total = np.array(total > 0).astype(int) # TP + TN + FP + FN
# totale celle bagnate del fiume nella previsione
diff = np.abs((target - prediction).cpu().detach().numpy())
correct_cells = (diff < threshold).astype(int)
correct_cells = correct_cells*total # TP + TN
accuracy = np.sum(correct_cells)/np.sum(total)
return accuracy
def rss_loss(input, target):
return th.sum((target - input) ** 2)
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
# -------------------------------
parser = argparse.ArgumentParser(description='Trains a given model on a given dataset')
parser.add_argument('-accuracy_threshold', dest='accuracy_threshold', default = 1e-1, type=float,
help='Delta threshold to consider true positives, [0,1] ')
parser.add_argument('-test_size', dest='test_size', default = 0, type=float,
help='Test size for the split')
parser.add_argument('-val_size', dest='val_size', default = 0, type=float,
help='Val size for the split')
parser.add_argument('-shuffle', dest='shuffle', default=True, type=str2bool,
help='Shuffle the dataset')
parser.add_argument('-root', dest='root',
help='root path with the simulation files (cropped and stored in folders)')
parser.add_argument('-past_frames', dest='past_frames', default=4, type=int,
help='number of past frames')
parser.add_argument('-future_frames', dest='future_frames', default=1, type=int,
help='number of future frames')
parser.add_argument('-partial', dest='partial', default=None, type=float,
help='percentage of portion of dataset (to load partial, lighter chunks)')
parser.add_argument('-filters', dest='filters', default=16, type=int,
help='Number of network kernels in hidden layers')
parser.add_argument('-image_size', dest='image_size', default=256, type=int,
help='Width=height of image')
parser.add_argument('-batch_size', dest='batch_size', default=4, type=int,
help='Batch size')
parser.add_argument('-lr', dest='learning_rate', default=0.0001, type=float,
help='learning rate')
parser.add_argument('-epochs', dest='epochs', default=200, type=int,
help='training iterations')
parser.add_argument('-in_channels', dest='in_channels', default=4, type=int,
help='number of input channels')
parser.add_argument('-out_channels', dest='out_channels', default=3, type=int,
help='number of input channels')
parser.add_argument('-filtering', dest='filtering', default=True, type=str2bool,
help='Enable filtering dynamic sequences only')
parser.add_argument('-workers', dest='workers', default=4, type=int,
help='Dataloader threads')
parser.add_argument('-dynamicity', dest='dynamicity', default=0.1, type=float,
help='Filtering thershold in [0,1]')
parser.add_argument('-otf', dest='otf', default=False, type=str2bool,
help='Use on the fly data loading')
parser.add_argument('-caching', dest='caching', default=False, type=str2bool,
help='Use cache in dataloader')
parser.add_argument('-downsampling', dest='downsampling', default=False, type=str2bool,
help='Use 4xdownsampling')
args = parser.parse_args()
# -------------- Setting up the run
num_run = len(os.listdir("runs/")) + 1
now = datetime.now()
foldername = "train_{}_{}".format(num_run, now.strftime("%d_%m_%Y_%H_%M_%S"))
os.mkdir("runs/" + foldername)
weights_path = "runs/" + foldername + "/model.weights"
print("[!] Session folder: {}".format("runs/" + foldername))
writer = SummaryWriter("runs/" + foldername)
# -------------------------------
plotsize = 15
if args.otf:
dataset = otf.SWEDataModule(
root=args.root,
test_size=args.test_size,
val_size=args.val_size,
past_frames=args.past_frames,
future_frames=args.future_frames,
partial=args.partial,
filtering=False,
batch_size=args.batch_size,
workers=args.workers,
image_size=args.image_size,
shuffle=False,
dynamicity=100,
caching=args.caching,
downsampling=args.downsampling
)
else:
dataset = preloading.SWEDataModule(
root=args.root,
test_size=args.test_size,
val_size=args.val_size,
past_frames=args.past_frames,
future_frames=args.future_frames,
partial=args.partial,
filtering=args.filtering,
batch_size=args.batch_size,
workers=args.workers,
image_size=args.image_size,
shuffle=False,
dynamicity=args.dynamicity,
caching=args.caching,
downsampling=args.downsampling
)
dataset.prepare_data()
batches = len(dataset.train_dataloader())
sequences = batches * args.batch_size
seq_matrices = (args.past_frames * 4 + args.future_frames * 3)
ram_db_size = (sequences * seq_matrices * (args.image_size**2) * 4) / (1024**3)
batch_gbytes_size = (args.batch_size * (seq_matrices * (args.image_size**2) * 4)) / (1024**3)
print("Dataset size:\n\t{} sequences\n\t{} batches\n\t{:.2f} GB RAM dataset size\n\t{:.2f} GB RAM batch size".format(sequences, batches, ram_db_size, batch_gbytes_size))
# ---- Model
net = seq2seq_ConvLSTM.EncoderDecoderConvLSTM(nf=args.filters, in_chan=args.in_channels, out_chan=args.out_channels)
# Parallelism
if th.cuda.is_available():
dev = "cuda:0"
else:
dev = "cpu"
device = th.device(dev)
if th.cuda.device_count() > 1:
print("[!] Yay! Using ", th.cuda.device_count(), "GPUs!")
net = nn.DataParallel(net)
net = net.to(device)
# ---- Training time!
optimizer = optim.AdamW(net.parameters(), lr=args.learning_rate, weight_decay=1e-5) # L2, Ridge Regression
# L1 Lasso Regression --> https://medium.com/analytics-vidhya/understanding-regularization-with-pytorch-26a838d94058
losses = []
avg_losses = []
errors = []
test_errors = []
print("\n[!] It's training time!")
epochs = args.epochs
plot_graph = False
for epoch in range(epochs): # loop over the dataset multiple times
print("---- Epoch {}".format(epoch))
epoch_start = time.time()
training_times = []
query_times = []
query_start = time.time()
iter_dataset = tqdm(enumerate(dataset.train_dataloader()), file=sys.stdout)
for i, batch in iter_dataset:
query_end = time.time()
query_times.append(query_end-query_start)
x, y = batch
optimizer.zero_grad()
if args.otf: # otf batches have an extra useless dimension
x = x[:,0]
y = y[:,0]
x = x.float().to(device)
y = y.float().to(device)
# first time plot the graph
if not plot_graph:
writer.add_graph(net, x)
writer.close()
plot_graph = True
# ---- Predicting
start = time.time()
outputs = net(x, args.future_frames) # 0 for layer index, 0 for h index
center = outputs.shape[3]//3
# ---- Batch Loss
loss = rss_loss(outputs[:, :args.out_channels, 0, center:2*center, center:2*center], y[:, 0, :args.out_channels, center:2*center, center:2*center])
acc = accuracy(outputs[:, :args.out_channels, 0, center:2*center, center:2*center], y[:, 0, :args.out_channels, center:2*center, center:2*center], threshold=args.accuracy_threshold)
loss.backward()
optimizer.step()
end = time.time()
training_times.append(end - start)
losses.append(loss.item())
query_start = time.time()
# ----- Testing
if args.test_size > 0:
size = len(dataset.datasets[1])
random_test_batch = dataset.datasets[1][random.randint(0, size - 1)]
x_test, y_test = batch
x_test = x_test.float().to(device)
y_test = y_test.float().to(device)
test_outputs = net(x_test, 1)
test_acc = accuracy(test_outputs[:, :3, 0, center:2*center, center:2*center], y_test[:, 0, :3, center:2*center, center:2*center], threshold=args.accuracy_threshold)
writer.add_scalars('accuracy', {'train': acc,
'test': test_acc,
}, epoch * len(dataset.train_dataloader()) + i)
else:
test_acc = 0
writer.add_scalar('train_accuracy',
acc,
epoch * len(dataset.train_dataloader()) + i)
# Item loss
writer.add_scalar('training loss',
loss.item(),
epoch)
# Plot values
if i % 3:
writer.add_scalar('avg training loss',
np.mean(losses),
epoch)
iter_dataset.set_postfix(
loss=np.mean(losses),
train_acc=acc,
test_acc=test_acc,
fwd_time=np.mean(training_times),
query_time=np.mean(query_times)
)
epoch_end = time.time()
print("\navg.loss {:.2f}\ttook {:.2f} s\tavg. inference time {:.2f} s\tavg.query time/batch {:.2f} s"
.format(epoch, np.mean(losses), epoch_end-epoch_start, np.mean(training_times), np.mean(query_times)))
avg_losses.append(np.mean(losses))
# checkpoint weights
th.save(net.state_dict(), weights_path)
end = time.time()
print(end - start)
print('[!] Finished Training, storing final weights...')
# Loss plot
mpl.rcParams['text.color'] = 'k'
plt.title("average loss")
plt.plot(range(len(avg_losses)), avg_losses)
plt.savefig("runs/" + foldername + "/avg_loss.png")
plt.clf()
print("Avg.training time: {}".format(np.mean(training_times)))
'''
---- CUT OUT PLOTTING EACH EPOCH
if epoch % 2 == 0:
k = np.random.randint(len(X_test))
outputs = net(X_test[k], 1) # 0 for layer index, 0 for h index
#test_loss = criterion(outputs[0], y_test[k,:,:,0,:,:])
#print("test loss: {}".format(test_loss.item()))
#------------------------------
fig, axs = plt.subplots(1, X_test.shape[2] + 2, figsize=(plotsize, plotsize))
for ax in axs:
ax.set_yticklabels([])
ax.set_xticklabels([])
# pick random datapoint from batch
x = np.random.randint(X_test[k].shape[0])
for i, frame in enumerate(X_test[k, x]):
axs[i].title.set_text('t={}'.format(i))
axs[i].matshow(frame[0].cpu().detach().numpy())
rect = patches.Rectangle((256, 256), 256, 256, linewidth=1, edgecolor='r', facecolor='none')
axs[i].add_patch(rect)
axs[i+1].matshow(outputs[x][0][0].cpu().detach().numpy())
rect = patches.Rectangle((256, 256), 256, 256, linewidth=1, edgecolor='r', facecolor='none')
axs[i+1].add_patch(rect)
axs[i+1].title.set_text('Predicted')
axs[i+2].matshow(y_test[k,x][0][0].cpu().detach().numpy())
rect = patches.Rectangle((256, 256), 256, 256, linewidth=1, edgecolor='r', facecolor='none')
axs[i+2].add_patch(rect)
axs[i+2].title.set_text('Ground Truth')
plt.show()
if args.test_flight is None:
plt.savefig("runs/" + foldername + "/{}_{}_plot.png".format(epoch, k))
plt.clf()
#------------------------------
#if epoch % 10 == 0:
# print('[%d, %5d] loss: %.3f' %
# (epoch + 1, i + 1, running_loss / 2000))
# running_loss = 0.0
'''