-
Notifications
You must be signed in to change notification settings - Fork 43
/
train.py
executable file
·123 lines (112 loc) · 4.75 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
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
import pdb
import sys
import torch
import numpy as np
from collections import OrderedDict
from options.train_options import TrainOptions
import data
from util.iter_counter import IterationCounter
from logger import Logger
from torchvision.utils import make_grid
from trainers import create_trainer
# parse options
opt = TrainOptions().parse()
# print options to help debugging
print(' '.join(sys.argv))
# load the dataset
if opt.dataset_mode_val is not None:
dataloader_train, dataloader_val = data.create_dataloader_trainval(opt)
else:
dataloader_train = data.create_dataloader(opt)
dataloader_val = None
# create trainer for our model
trainer = create_trainer(opt)
model = trainer.pix2pix_model
# create tool for counting iterations
iter_counter = IterationCounter(opt, len(dataloader_train))
# create tool for visualization
writer = Logger(f"output/{opt.name}")
trainer.save('latest')
for epoch in iter_counter.training_epochs():
iter_counter.record_epoch_start(epoch)
for i, data_i in enumerate(dataloader_train, start=iter_counter.epoch_iter):
iter_counter.record_one_iteration()
# train discriminator
if not opt.freeze_D:
trainer.run_discriminator_one_step(data_i, i)
# Training
# train generator
if i % opt.D_steps_per_G == 0:
trainer.run_generator_one_step(data_i, i)
if iter_counter.needs_displaying():
losses = trainer.get_latest_losses()
for k,v in losses.items():
writer.add_scalar(k,v.mean().item(), iter_counter.total_steps_so_far)
writer.write_console(epoch, iter_counter.epoch_iter, iter_counter.time_per_iter)
num_print = min(4, data_i['image'].size(0))
writer.add_single_image('inputs',
(make_grid(trainer.get_latest_inputs()[:num_print])+1)/2,
iter_counter.total_steps_so_far)
infer_out,inp = trainer.pix2pix_model.forward(data_i, mode='inference')
vis = (make_grid(inp[:num_print])+1)/2
writer.add_single_image('infer_in',
vis,
iter_counter.total_steps_so_far)
vis = (make_grid(infer_out[:num_print])+1)/2
vis = torch.clamp(vis, 0,1)
writer.add_single_image('infer_out',
vis,
iter_counter.total_steps_so_far)
generated = trainer.get_latest_generated()
for k,v in generated.items():
if v is None:
continue
if 'label' in k:
vis = make_grid(v[:num_print].expand(-1,3,-1,-1))[0]
writer.add_single_label(k,
vis,
iter_counter.total_steps_so_far)
else:
if v.size(1) == 3:
vis = (make_grid(v[:num_print])+1)/2
vis = torch.clamp(vis, 0,1)
else:
vis = make_grid(v[:num_print])
writer.add_single_image(k,
vis,
iter_counter.total_steps_so_far)
writer.write_html()
if iter_counter.needs_validation():
print('saving the latest model (epoch %d, total_steps %d)' %
(epoch, iter_counter.total_steps_so_far))
trainer.save('epoch%d_step%d'%
(epoch, iter_counter.total_steps_so_far))
trainer.save('latest')
iter_counter.record_current_iter()
if dataloader_val is not None:
print("doing validation")
model.eval()
num = 0
psnr_total = 0
for ii, data_ii in enumerate(dataloader_val):
with torch.no_grad():
generated,_ = model(data_ii, mode='inference')
generated = generated.cpu()
generated = (generated+1)/2*255
gt = data_ii['image']
bsize, c, h, w = gt.shape
gt = (gt+1)/2*255
mse = ((generated-gt)**2).sum(3).sum(2).sum(1)
mse /= (c*h*w)
psnr = 10*torch.log10(255.0*255.0 / (mse+1e-8))
psnr_total += psnr.sum().item()
num += bsize
psnr_total /= num
writer.add_scalar("val.psnr", psnr_total, iter_counter.total_steps_so_far)
writer.write_scalar("val.psnr", psnr_total, iter_counter.total_steps_so_far)
writer.write_html()
model.train()
trainer.update_learning_rate(epoch)
iter_counter.record_epoch_end()
trainer.save('latest')
print('Training was successfully finished.')