-
Notifications
You must be signed in to change notification settings - Fork 16
/
train_model.py
executable file
·487 lines (379 loc) · 19.1 KB
/
train_model.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""
Train the fully convolutional neural network model with PyTorch.
"""
# -----------------------------------------------------------------------------
# IMPORTS
# -----------------------------------------------------------------------------
import argparse
import numpy as np
import time
import torch
from tensorboardX import SummaryWriter
from typing import Any
from utils.checkpointing import CheckpointManager
from utils.datasets import InjectionDataset
from utils.models import FCNN
from utils.training import AverageMeter, get_log_dir, update_lr
# -----------------------------------------------------------------------------
# FUNCTION DEFINITIONS
# -----------------------------------------------------------------------------
def get_arguments() -> argparse.Namespace:
"""
Set up an ArgumentParser to get the command line arguments.
Returns:
A Namespace object containing all the command line arguments
for the script.
"""
# Set up parser
parser = argparse.ArgumentParser()
# Add arguments
parser.add_argument('--batch-size',
default=64,
type=int,
metavar='N',
help='Size of the mini-batches during training. '
'Default: 64.')
parser.add_argument('--epochs',
default=64,
type=int,
metavar='N',
help='Total number of training epochs. Default: 64.')
parser.add_argument('--learning-rate',
default=3e-4,
type=float,
metavar='LR',
help='Initial learning rate. Default: 3e-4.')
parser.add_argument('--log-interval',
default=32,
type=int,
metavar='N',
help='Logging interval during training. Default: 32.')
parser.add_argument('--resume',
default=None,
type=str,
metavar='PATH',
help='Path to checkpoint to be used when resuming '
'training. Default: None.')
parser.add_argument('--tensorboard',
action='store_true',
default=True,
help='Use TensorBoard to log training progress? '
'Default: True.')
parser.add_argument('--use-cuda',
action='store_true',
default=True,
help='Train on GPU, if available? Default: True.')
parser.add_argument('--workers',
default=4,
type=int,
metavar='N',
help='Number of workers for DataLoaders. Default: 4.')
# Parse and return the arguments (as a Namespace object)
arguments = parser.parse_args()
return arguments
def train(dataloader: torch.utils.data.DataLoader,
model: torch.nn.Module,
loss_func: Any,
optimizer: torch.optim.Optimizer,
epoch: int,
args: argparse.Namespace):
"""
Train the given model for a single epoch using the given dataloader.
Args:
dataloader: The dataloader containing the training data.
model: Instance of the model that is being trained.
loss_func: A loss function to compute the error between the
actual and the desired output of the model.
optimizer: An instance of an optimizer that is used to compute
and perform the updates to the weights of the network.
epoch: The current training epoch.
args: Namespace object containing some global variable (e.g.,
command line arguments, such as the batch size)
"""
# -------------------------------------------------------------------------
# Preliminaries
# -------------------------------------------------------------------------
# Activate training mode
model.train()
# Keep track the time to process a batch, as well as the batch losses
batch_times = AverageMeter()
batch_losses = AverageMeter()
# -------------------------------------------------------------------------
# Process the training dataset in mini-batches
# -------------------------------------------------------------------------
for batch_idx, (data, target) in enumerate(dataloader):
# Initialize start time of the batch
batch_start = time.time()
# Fetch data and move to device
data, target = data.to(args.device), target.to(args.device)
target = target.squeeze()
# Clear gradients
optimizer.zero_grad()
# Compute forward pass through model
output = model.forward(data).squeeze()
# Calculate the loss for the batch
loss = loss_func(output, target)
# Back-propagate the loss and update the weights
loss.backward()
optimizer.step(closure=None)
# ---------------------------------------------------------------------
# Log information about current batch to TensorBoard
# ---------------------------------------------------------------------
if args.tensorboard:
# Compute how many examples we have processed already and log the
# loss value for the current batch
global_step = ((epoch - 1) * args.n_train_batches + batch_idx) * \
args.batch_size
args.logger.add_scalar(tag='loss/train',
scalar_value=loss.item(),
global_step=global_step)
# ---------------------------------------------------------------------
# Additional logging to console
# ---------------------------------------------------------------------
# Store the loss and processing time for the current batch
batch_losses.update(loss.item())
batch_times.update(time.time() - batch_start)
# Print information to console, if applicable
if batch_idx % args.log_interval == 0:
# Which fraction of batches have we already processed this epoch?
percent = 100. * batch_idx / args.n_train_batches
# Print some information about how the training is going
print(f'Epoch: {epoch:>3}/{args.epochs}', end=' | ', flush=True)
print(f'Batch: {batch_idx:>3}/{args.n_train_batches}',
flush=True, end=' ')
print(f'({percent:>4.1f}%)', end=' | ', flush=True)
print(f'Loss: {loss.item():.6f}', end=' | ', flush=True)
print(f'Time: {batch_times.value:>6.3f}s', flush=True)
def validate(dataloader: torch.utils.data.DataLoader,
model: torch.nn.Module,
loss_func: Any,
epoch: int,
args: argparse.Namespace) -> float:
"""
At the end of each epoch, run the model on the validation dataset.
Args:
dataloader: The dataloader containing the validation data.
model: Instance of the model that is being trained.
loss_func: A loss function to compute the error between the
actual and the desired output of the model.
epoch: The current training epoch.
args: Namespace object containing some global variable (e.g.,
command line arguments, such as the batch size).
Returns:
The average loss on the validation dataset.
"""
# -------------------------------------------------------------------------
# Preliminaries
# -------------------------------------------------------------------------
# Activate model evaluation mode
model.eval()
# Initialize validation loss as 0, because we need to sum it up over all
# mini batches for the validation dataset
validation_loss = 0
# Ensure the loss function uses 'sum' as the reduction method (we don't
# want batch averages, but just one global validation average)
reduction = loss_func.reduction
loss_func.reduction = 'sum'
# -------------------------------------------------------------------------
# Process the validation dataset in mini-batches
# -------------------------------------------------------------------------
# At test time, we do not need to compute gradients
with torch.no_grad():
# Loop in mini batches over the validation dataset
for data, target in dataloader:
# Fetch batch data and move to device
data, target = data.to(args.device), target.to(args.device)
target = target.squeeze()
# Compute the forward pass through the model
output = model.forward(data).squeeze()
# Compute the loss for the batch
validation_loss += loss_func(output, target).item()
# -------------------------------------------------------------------------
# Compute the average validation loss
# -------------------------------------------------------------------------
validation_loss /= np.prod(dataloader.dataset.labels.shape)
print(f'\nAverage loss on validation set:\t {validation_loss:.5f}\n')
# -------------------------------------------------------------------------
# Log stuff to TensorBoard
# -------------------------------------------------------------------------
if args.tensorboard:
# Compute the current global_step (i.e., the total number examples
# that we've seen during training so far)
global_step = epoch * args.n_train_batches * args.batch_size
# Log the validation loss
args.logger.add_scalar(tag='loss/validation',
scalar_value=validation_loss,
global_step=global_step)
# -------------------------------------------------------------------------
# Postliminaries
# -------------------------------------------------------------------------
# Finally, restore the original reduction method of the loss function
loss_func.reduction = reduction
# Return the validation loss
return validation_loss
# -----------------------------------------------------------------------------
# MAIN CODE
# -----------------------------------------------------------------------------
if __name__ == '__main__':
# -------------------------------------------------------------------------
# Preliminaries
# -------------------------------------------------------------------------
print('')
print('TRAIN A FULLY CONVOLUTIONAL NEURAL NETWORK')
print('')
# Start the stopwatch
script_start = time.time()
# Read in command line arguments
args = get_arguments()
print('Preparing the training process:')
print(80 * '-')
# -------------------------------------------------------------------------
# Set up CUDA for GPU support
# -------------------------------------------------------------------------
if torch.cuda.is_available() and args.use_cuda:
args.device = 'cuda'
device_count = torch.cuda.device_count()
device_name = torch.cuda.get_device_name(0)
print(f'device: \t\t GPU ({device_count} x {device_name})')
else:
args.device = 'cpu'
print('device: \t\t CPU [CUDA not requested or unavailable]')
# -------------------------------------------------------------------------
# Set up the network model
# -------------------------------------------------------------------------
# Create a new instance of the model we want to train
model = FCNN()
print('model: \t\t\t', model.__class__.__name__)
# DataParallel will divide and allocate batch_size to all available GPUs
if args.device == 'cuda':
model = torch.nn.DataParallel(model)
# Move model to the correct device
model.to(args.device)
# -------------------------------------------------------------------------
# Instantiate an optimizer, a loss function and a LR scheduler
# -------------------------------------------------------------------------
# Instantiate the specified optimizer
optimizer = torch.optim.Adam(params=model.parameters(),
lr=args.learning_rate,
amsgrad=True)
print('optimizer: \t\t', optimizer.__class__.__name__)
# Define the loss function (we use Binary Cross-Entropy)
loss_func = torch.nn.BCELoss().to(args.device)
print('loss_function: \t\t', loss_func.__class__.__name__)
# Reduce the LR by a factor of 0.5 if the validation loss did not
# go down for at least 10 training epochs
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
factor=0.5,
patience=8,
min_lr=1e-6)
# -------------------------------------------------------------------------
# Instantiate a CheckpointManager and load checkpoint (if desired)
# -------------------------------------------------------------------------
# Instantiate a new CheckpointManager
checkpoint_manager = CheckpointManager(model=model,
optimizer=optimizer,
scheduler=scheduler,
mode='min',
step_size=-1)
# Check if we are resuming training, and if so, load the checkpoint
if args.resume is not None:
# Load the checkpoint from the provided checkpoint file
checkpoint_manager.load_checkpoint(args.resume)
args.start_epoch = checkpoint_manager.last_epoch + 1
# Print which checkpoint we are using and where we start to train
print(f'checkpoint:\t\t {args.resume} '
f'(epoch: {checkpoint_manager.last_epoch})')
# Other, simply print that we're not using any checkpoint
else:
args.start_epoch = 1
print('checkpoint: \t\t None')
# -------------------------------------------------------------------------
# Load datasets for training and validation and create DataLoader objects
# -------------------------------------------------------------------------
# Load the training and the validation dataset
training_dataset = InjectionDataset(mode='training')
validation_dataset = InjectionDataset(mode='validation')
# Compute size of training / validation set and number of training batches
args.train_size = len(training_dataset)
args.validation_size = len(validation_dataset)
args.n_train_batches = int(np.ceil(args.train_size / args.batch_size))
print('train_set_size: \t', args.train_size)
print('validation_set_size: \t', args.validation_size)
print('n_training_batches: \t', args.n_train_batches)
# Create DataLoaders for training and validation
training_dataloader = \
torch.utils.data.DataLoader(dataset=training_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.workers,
pin_memory=True)
validation_dataloader = \
torch.utils.data.DataLoader(dataset=validation_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True)
# -------------------------------------------------------------------------
# Create a TensorBoard logger and log some basics
# -------------------------------------------------------------------------
if args.tensorboard:
# Create TensorBoard logger
args.logger = SummaryWriter(log_dir=get_log_dir())
# Add all args to as text objects (to epoch 0)
for key, value in dict(vars(args)).items():
args.logger.add_text(tag=key,
text_string=str(value),
global_step=0)
# -------------------------------------------------------------------------
# Train the network for the given number of epochs
# -------------------------------------------------------------------------
print(80 * '-' + '\n\n' + 'Training the Model:\n' + 80 * '-')
for epoch in range(args.start_epoch, args.epochs):
print('')
epoch_start = time.time()
# ---------------------------------------------------------------------
# Train the model for one epoch
# ---------------------------------------------------------------------
train(dataloader=training_dataloader,
model=model,
loss_func=loss_func,
optimizer=optimizer,
epoch=epoch,
args=args)
# ---------------------------------------------------------------------
# Evaluate on the validation set
# ---------------------------------------------------------------------
validation_loss = validate(dataloader=validation_dataloader,
model=model,
loss_func=loss_func,
epoch=epoch,
args=args)
# ---------------------------------------------------------------------
# Take a step with the CheckpointManager
# ---------------------------------------------------------------------
# This will create checkpoint if the current model is the best we've
# seen yet, and also once every `step_size` number of epochs.
checkpoint_manager.step(metric=validation_loss,
epoch=epoch)
# ---------------------------------------------------------------------
# Update the learning rate of the optimizer (using the LR scheduler)
# ---------------------------------------------------------------------
# Take a step with the LR scheduler; print message when LR changes
current_lr = update_lr(scheduler, optimizer, validation_loss)
# Log the current value of the LR to TensorBoard
if args.tensorboard:
args.logger.add_scalar(tag='learning_rate',
scalar_value=current_lr,
global_step=epoch)
# ---------------------------------------------------------------------
# Print epoch duration
# ---------------------------------------------------------------------
print(f'Total Epoch Time: {time.time() - epoch_start:.3f}s\n')
# ---------------------------------------------------------------------
print(80 * '-' + '\n\n' + 'Training complete!')
# -------------------------------------------------------------------------
# Postliminaries
# -------------------------------------------------------------------------
print('')
print(f'This took {time.time() - script_start:.1f} seconds!')
print('')