forked from RenYurui/Global-Flow-Local-Attention
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
76 lines (64 loc) · 2.93 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
import time
from options.train_options import TrainOptions
import data as Dataset
from model import create_model
from util.visualizer import Visualizer
if __name__ == '__main__':
# get training options
opt = TrainOptions().parse()
# create a dataset
dataset = Dataset.create_dataloader(opt)
dataset_size = len(dataset) * opt.batchSize
print('training images = %d' % dataset_size)
# create a model
model = create_model(opt)
# model = model.to()
# create a visualizer
visualizer = Visualizer(opt)
# training flag
keep_training = True
max_iteration = opt.niter+opt.niter_decay
epoch = 0
total_iteration = opt.iter_count
# training process
while(keep_training):
epoch_start_time = time.time()
epoch+=1
print('\n Training epoch: %d' % epoch)
for i, data in enumerate(dataset):
iter_start_time = time.time()
total_iteration += 1
model.set_input(data)
model.optimize_parameters()
# display images on visdom and save images
if total_iteration % opt.display_freq == 0:
visualizer.display_current_results(model.get_current_visuals(), epoch)
if hasattr(model, 'distribution'):
visualizer.plot_current_distribution(model.get_current_dis())
# print training loss and save logging information to the disk
if total_iteration % opt.print_freq == 0:
losses = model.get_current_errors()
t = (time.time() - iter_start_time) / opt.batchSize
visualizer.print_current_errors(epoch, total_iteration, losses, t)
if opt.display_id > 0:
visualizer.plot_current_errors(total_iteration, losses)
if total_iteration % opt.eval_iters_freq == 0:
model.eval()
if hasattr(model, 'eval_metric_name'):
eval_results = model.get_current_eval_results()
visualizer.print_current_eval(epoch, total_iteration, eval_results)
if opt.display_id > 0:
visualizer.plot_current_score(total_iteration, eval_results)
# save the latest model every <save_latest_freq> iterations to the disk
if total_iteration % opt.save_latest_freq == 0:
print('saving the latest model (epoch %d, total_steps %d)' % (epoch, total_iteration))
model.save_networks('latest')
# save the model every <save_iter_freq> iterations to the disk
if total_iteration % opt.save_iters_freq == 0:
print('saving the model of iterations %d' % total_iteration)
model.save_networks(total_iteration)
if total_iteration > max_iteration:
keep_training = False
break
model.update_learning_rate()
print('\nEnd training')