forked from Vandermode/ERRNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
132 lines (101 loc) · 4.23 KB
/
engine.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
import torch
import util.util as util
import models
import time
import os
import sys
from os.path import join
from util.visualizer import Visualizer
class Engine(object):
def __init__(self, opt):
self.opt = opt
self.writer = None
self.visualizer = None
self.model = None
self.best_val_loss = 1e6
self.__setup()
def __setup(self):
self.basedir = join('checkpoints', self.opt.name)
if not os.path.exists(self.basedir):
os.mkdir(self.basedir)
opt = self.opt
"""Model"""
self.model = models.__dict__[self.opt.model]()
self.model.initialize(opt)
if not opt.no_log:
self.writer = util.get_summary_writer(os.path.join(self.basedir, 'logs'))
self.visualizer = Visualizer(opt)
def train(self, train_loader, **kwargs):
print('\nEpoch: %d' % self.epoch)
avg_meters = util.AverageMeters()
opt = self.opt
model = self.model
epoch = self.epoch
epoch_start_time = time.time()
for i, data in enumerate(train_loader):
iter_start_time = time.time()
iterations = self.iterations
model.set_input(data, mode='train')
model.optimize_parameters(**kwargs)
errors = model.get_current_errors()
avg_meters.update(errors)
util.progress_bar(i, len(train_loader), str(avg_meters))
if not opt.no_log:
util.write_loss(self.writer, 'train', avg_meters, iterations)
if iterations % opt.display_freq == 0 and opt.display_id != 0:
save_result = iterations % opt.update_html_freq == 0
self.visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)
if iterations % opt.print_freq == 0 and opt.display_id != 0:
t = (time.time() - iter_start_time)
self.iterations += 1
self.epoch += 1
if not self.opt.no_log:
if self.epoch % opt.save_epoch_freq == 0:
print('saving the model at epoch %d, iters %d' %
(self.epoch, self.iterations))
model.save()
print('saving the latest model at the end of epoch %d, iters %d' %
(self.epoch, self.iterations))
model.save(label='latest')
print('Time Taken: %d sec' %
(time.time() - epoch_start_time))
# model.update_learning_rate()
train_loader.reset()
def eval(self, val_loader, dataset_name, savedir=None, loss_key=None, **kwargs):
avg_meters = util.AverageMeters()
model = self.model
opt = self.opt
with torch.no_grad():
for i, data in enumerate(val_loader):
index = model.eval(data, savedir=savedir, **kwargs)
avg_meters.update(index)
util.progress_bar(i, len(val_loader), str(avg_meters))
if not opt.no_log:
util.write_loss(self.writer, join('eval', dataset_name), avg_meters, self.epoch)
if loss_key is not None:
val_loss = avg_meters[loss_key]
if val_loss < self.best_val_loss:
self.best_val_loss = val_loss
print('saving the best model at the end of epoch %d, iters %d' %
(self.epoch, self.iterations))
model.save(label='best_{}_{}'.format(loss_key, dataset_name))
return avg_meters
def test(self, test_loader, savedir=None, **kwargs):
model = self.model
opt = self.opt
with torch.no_grad():
for i, data in enumerate(test_loader):
model.test(data, savedir=savedir, **kwargs)
util.progress_bar(i, len(test_loader))
@property
def iterations(self):
return self.model.iterations
@iterations.setter
def iterations(self, i):
self.model.iterations = i
@property
def epoch(self):
return self.model.epoch
@epoch.setter
def epoch(self, e):
self.model.epoch = e