-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_enet.py
69 lines (56 loc) · 2.26 KB
/
train_enet.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
from torch.autograd import Variable
"""
From https://github.com/davidtvs/PyTorch-ENet/blob/master/train.py
"""
class Train():
"""Performs the training of ``model`` given a training dataset data
loader, the optimizer, and the loss criterion.
Keyword arguments:
- model (``nn.Module``): the model instance to train.
- data_loader (``Dataloader``): Provides single or multi-process
iterators over the dataset.
- optim (``Optimizer``): The optimization algorithm.
- criterion (``Optimizer``): The loss criterion.
- metric (```Metric``): An instance specifying the metric to return.
- use_cuda (``bool``): If ``True``, the training is performed using
CUDA operations (GPU).
"""
def __init__(self, model, data_loader, optim, criterion, metric, use_cuda):
self.model = model
self.data_loader = data_loader
self.optim = optim
self.criterion = criterion
self.metric = metric
self.use_cuda = use_cuda
def run_epoch(self, iteration_loss=False):
"""Runs an epoch of training.
Keyword arguments:
- iteration_loss (``bool``, optional): Prints loss at every step.
Returns:
- The epoch loss (float).
"""
epoch_loss = 0.0
self.metric.reset()
for step, batch_data in enumerate(self.data_loader):
# Get the inputs and labels
inputs, labels = batch_data
# Wrap them in a Varaible
inputs, labels = Variable(inputs), Variable(labels)
if self.use_cuda:
inputs = inputs.cuda()
labels = labels.cuda()
# Forward propagation
outputs = self.model(inputs)
# Loss computation
loss = self.criterion(outputs, labels)
# Backpropagation
self.optim.zero_grad()
loss.backward()
self.optim.step()
# Keep track of loss for current epoch
epoch_loss += loss.data[0]
# Keep track of the evaluation metric
self.metric.add(outputs.data, labels.data)
if iteration_loss:
print("[Step: %d] Iteration loss: %.4f" % (step, loss.data[0]))
return epoch_loss / len(self.data_loader), self.metric.value()