-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
86 lines (69 loc) · 2.78 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
77
78
79
80
81
82
83
84
85
86
import time
from options.train_options import TrainOptions
from dataloader.data_loader import dataloader
from model import create_model
from util.visualizer import Visualizer
import torch
import numpy as np
import random
#fix seed
def fix_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
# get training options
opt = TrainOptions().parse()
# set seed
if not opt.seed is None:
fix_seed(opt.seed)
# create a dataset
dataset = dataloader(opt)
dataset_size = len(dataset) * opt.batchSize
print('training images = %d' % dataset_size)
# create a model
model = create_model(opt)
# 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)
#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)
# 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')