-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
184 lines (174 loc) · 9.71 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from unet import *
from datasets.cityscapes import *
from datasets.cycle import *
from datasets.folder import *
from discriminator import Discriminator
from torch import nn
from torch.utils.data import DataLoader
import itertools
from tensorboardX import SummaryWriter
from tqdm import tqdm
from utils import *
import os
def datasets_by_name(name, params):
del params['name']
if name == 'cityscapes':
return CityScapes(**params), CityScapes('val', **params)
elif name == 'edges2shoes':
data_train = CycleDataset('data/edges2shoes/', 'train')
data_test = CycleDataset('data/edges2shoes/', 'val')
return data_train, data_test
elif name == 'folder2folder':
data_train = Folder2FolderDataset(params['folder_left'], params['folder_right'])
data_test = Folder2FolderDataset(params['folder_left'], params['folder_right'], phase='val')
return data_train, data_test
else:
raise NotImplementedError('Unknown dataset')
def train(config):
genAB = UNet(3, 3, bilinear=config.model.bilinear_upsample).cuda()
init_weights(genAB, 'normal')
genBA = UNet(3, 3, bilinear=config.model.bilinear_upsample).cuda()
init_weights(genBA, 'normal')
discrA = Discriminator(3).cuda()
init_weights(discrA, 'normal')
discrB = Discriminator(3).cuda()
init_weights(discrB, 'normal')
writer = SummaryWriter(config.name)
data_train, data_test = datasets_by_name(config.dataset.name, config.dataset)
train_dataloader = DataLoader(data_train, batch_size=config.bs, shuffle=True, num_workers=config.num_workers)
test_dataloader = DataLoader(data_test, batch_size=config.bs, shuffle=True, num_workers=config.num_workers)
idt_loss = nn.L1Loss()
cycle_consistency = nn.L1Loss()
l2_loss = nn.MSELoss()
discriminator_loss = nn.BCELoss()
lambda_idt, lambda_C, lambda_D = config.loss.lambda_idt, config.loss.lambda_C, config.loss.lambda_D
optG = torch.optim.Adam(itertools.chain(genAB.parameters(), genBA.parameters()), lr=config.train.lr, betas=(config.train.beta1, 0.999))
optD = torch.optim.Adam(itertools.chain(discrA.parameters(), discrB.parameters()), lr=config.train.lr, betas=(config.train.beta1, 0.999))
genAB, genBA, discrA, discrB, optG, optD, start_epoch = load_if_exsists(config, genAB, genBA, discrA, discrB, optG, optD)
for epoch in range(start_epoch, config.train.epochs):
set_train([genAB, genBA, discrA, discrB])
set_requires_grad([genAB, genBA, discrA, discrB], True)
for i, (batch_A, batch_B) in enumerate(tqdm(train_dataloader)):
batch_A, batch_B = batch_A.cuda(), batch_B.cuda()
optG.zero_grad()
loss_G, loss_D = 0, 0
fake_B = genAB(batch_A)
cycle_A = genBA(fake_B)
fake_A = genBA(batch_B)
cycle_B = genAB(fake_A)
if lambda_idt > 0:
loss_G += idt_loss(fake_B, batch_B) * lambda_idt
loss_G += idt_loss(fake_A, batch_A) * lambda_idt
if lambda_C > 0:
loss_G += cycle_consistency(cycle_A, batch_A) * lambda_C
loss_G += cycle_consistency(cycle_B, batch_B) * lambda_C
if lambda_D > 0:
set_requires_grad([discrA, discrB], False)
discr_feedbackA = discrA(fake_A)
discr_feedbackB = discrB(fake_B)
loss_G += discriminator_loss(discr_feedbackA, torch.ones_like(discr_feedbackA)) * lambda_D
loss_G += discriminator_loss(discr_feedbackB, torch.ones_like(discr_feedbackB)) * lambda_D
loss_G.backward()
torch.nn.utils.clip_grad_norm_(itertools.chain(genAB.parameters(), genBA.parameters()), 15)
optG.step()
if lambda_D > 0:
set_requires_grad([discrA, discrB], True)
loss_D_fake, loss_D_true = 0, 0
optD.zero_grad()
logits = discrA(fake_A.detach())
loss_D_fake += discriminator_loss(logits, torch.zeros_like(logits))
logits = discrB(fake_B.detach())
loss_D_fake += discriminator_loss(logits, torch.zeros_like(logits))
loss_D_fake.backward()
torch.nn.utils.clip_grad_norm_(itertools.chain(discrA.parameters(), discrB.parameters()), 15)
optD.step()
optD.zero_grad()
logits = discrA(batch_A)
loss_D_true += discriminator_loss(logits, torch.ones_like(logits))
logits = discrB(batch_B)
loss_D_true += discriminator_loss(logits, torch.ones_like(logits))
loss_D_true.backward()
torch.nn.utils.clip_grad_norm_(itertools.chain(discrA.parameters(), discrB.parameters()), 15)
optD.step()
loss_D = loss_D_fake + loss_D_true
if (i % config.train.verbose_period == 0):
writer.add_scalar('train/loss_G', loss_G.item(), len(train_dataloader) * epoch + i)
writer.add_scalar('train/pixel_error_A', l2_loss(fake_A, batch_A).mean().item(), len(train_dataloader) * epoch + i)
writer.add_scalar('train/pixel_error_B', l2_loss(fake_B, batch_B).mean().item(), len(train_dataloader) * epoch + i)
if lambda_D > 0:
writer.add_scalar('train/loss_D', loss_D.item(), len(train_dataloader) * epoch + i)
writer.add_scalar('train/mean_D_A', discr_feedbackA.mean().item(), len(train_dataloader) * epoch + i)
writer.add_scalar('train/mean_D_B', discr_feedbackB.mean().item(), len(train_dataloader) * epoch + i)
for batch_i in range(fake_A.shape[0]):
concat = (torch.cat([fake_A[batch_i], batch_B[batch_i]], dim=-1) + 1.) / 2.
writer.add_image('train/fake_A_' + str(batch_i), concat, len(train_dataloader) * epoch + i)
for batch_i in range(fake_B.shape[0]):
concat = (torch.cat([fake_B[batch_i], batch_A[batch_i]], dim=-1) + 1.) / 2.
writer.add_image('train/fake_B_' + str(batch_i), concat, len(train_dataloader) * epoch + i)
if not config.validate:
continue
set_eval([genAB, genBA, discrA, discrB])
set_requires_grad([genAB, genBA, discrA, discrB], False)
loss_G, loss_D, discr_feedbackA_mean, discr_feedbackB_mean = 0, 0, 0, 0
pixel_error_A, pixel_error_B = 0, 0
for i, (batch_A, batch_B) in enumerate(tqdm(test_dataloader)):
batch_A, batch_B = batch_A.cuda(), batch_B.cuda()
fake_B = genAB(batch_A)
cycle_A = genBA(fake_B)
fake_A = genBA(batch_B)
cycle_B = genAB(fake_A)
pixel_error_A += l2_loss(fake_A, batch_A).mean()
pixel_error_B += l2_loss(fake_B, batch_B).mean()
if lambda_idt > 0:
loss_G += idt_loss(fake_B, batch_B) * lambda_idt
loss_G += idt_loss(fake_A, batch_A) * lambda_idt
if lambda_C > 0:
loss_G += cycle_consistency(cycle_A, batch_A) * lambda_C
loss_G += cycle_consistency(cycle_B, batch_B) * lambda_C
if lambda_D > 0:
discr_feedbackA = discrA(fake_A)
discr_feedbackB = discrB(fake_B)
loss_G += discriminator_loss(discr_feedbackA, torch.ones_like(discr_feedbackA)) * lambda_D
loss_G += discriminator_loss(discr_feedbackB, torch.ones_like(discr_feedbackB)) * lambda_D
discr_feedbackA_mean += discr_feedbackA.mean()
discr_feedbackB_mean += discr_feedbackB.mean()
if lambda_D > 0:
loss_D_fake, loss_D_true = 0, 0
logits = discrA(fake_A.detach())
loss_D_fake += discriminator_loss(logits, torch.zeros_like(logits))
logits = discrB(fake_B.detach())
loss_D_fake += discriminator_loss(logits, torch.zeros_like(logits))
logits = discrA(batch_A)
loss_D_true += discriminator_loss(logits, torch.ones_like(logits))
logits = discrB(batch_B)
loss_D_true += discriminator_loss(logits, torch.ones_like(logits))
loss_D += loss_D_fake + loss_D_true
if i == 0:
for batch_i in range(fake_A.shape[0]):
concat = (torch.cat([fake_A[batch_i], batch_B[batch_i]], dim=-1) + 1.) / 2.
writer.add_image('val/fake_A_' + str(batch_i), concat, epoch)
for batch_i in range(fake_B.shape[0]):
concat = (torch.cat([fake_B[batch_i], batch_A[batch_i]], dim=-1) + 1.) / 2.
writer.add_image('val/fake_B_' + str(batch_i), concat, epoch)
loss_G /= len(test_dataloader)
pixel_error_A /= len(test_dataloader)
pixel_error_B /= len(test_dataloader)
writer.add_scalar('val/loss_G', loss_G.item(), epoch)
writer.add_scalar('val/pixel_error_A', pixel_error_A.item(), epoch)
writer.add_scalar('val/pixel_error_B', pixel_error_B.item(), epoch)
if lambda_D > 0:
loss_D /= len(test_dataloader)
discr_feedbackA_mean /= len(test_dataloader)
discr_feedbackB_mean /= len(test_dataloader)
writer.add_scalar('val/loss_D', loss_D.item(), epoch)
writer.add_scalar('val/mean_D_A', discr_feedbackA_mean.item(), epoch)
writer.add_scalar('val/mean_D_B', discr_feedbackB_mean.item(), epoch)
torch.save({
'genAB': genAB.state_dict(),
'genBA': genBA.state_dict(),
'discrA': discrA.state_dict(),
'discrB': discrB.state_dict(),
'optG': optG.state_dict(),
'optD': optD.state_dict(),
'epoch': epoch
}, os.path.join(config.name, 'model.pth'))