-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
184 lines (148 loc) · 6.22 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
import torch
from utils.dataset import ABDataset, my_transform
from utils.utils import save_checkpoint, load_checkpoint
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim
import config
from tqdm import tqdm
from torchvision.utils import save_image
from utils.discriminator import Discriminator
from utils.generator import Generator
from utils.brush_ink import HED, no_sigmoid_cross_entropy, gauss_kernel, erode
'''
In this case, A stands for real world image and B stands for ink wash painting.
disc_A: If it is A or not.
gen_A: To generate fake A.
'''
def train(disc_A, disc_B, disc_ink, gen_A, gen_B, hed, loader, opt_disc, opt_gen, l1, mse, d_scaler, g_scaler):
B_reals = 0
B_fakes = 0
loop = tqdm(loader, leave=True)
for idx, (A, B) in enumerate(loop):
A = A.to(config.DEVICE)
B = B.to(config.DEVICE)
fake_A = gen_A(B)
fake_B = gen_B(A)
if idx % 200 == 0:
save_image(A, f'saved_images/A_{idx}.png')
save_image(B, f'saved_images/B_{idx}.png')
save_image(fake_A, f'saved_images/Fake_A_{idx}.png')
save_image(fake_B, f'saved_images/Fake_B_{idx}.png')
# TODO: Train Discriminator A, B and ink
# Train Discriminator A
D_A_real = disc_A(A)
D_A_fake = disc_A(fake_A.detach()) # Here we don't want to touch generator
D_A_real_loss = mse(D_A_real, torch.ones_like(D_A_real))
D_A_fake_loss = mse(D_A_fake, torch.zeros_like(D_A_fake))
D_A_loss = D_A_real_loss + D_A_fake_loss
# Train Discriminator B
D_B_real = disc_B(B)
D_B_fake = disc_B(fake_B.detach())
B_reals += D_B_real.mean().item()
B_fakes += D_B_fake.mean().item()
D_B_real_loss = mse(D_B_real, torch.ones_like(D_B_real))
D_B_fake_loss = mse(D_B_fake, torch.zeros_like(D_B_fake))
D_B_loss = D_B_real_loss + D_B_fake_loss
# Train Discriminator ink
ink_B = gauss_kernel(erode(B))
ink_fake_B = gauss_kernel(erode(fake_B.detach()))
D_ink_real = disc_ink(ink_B)
D_ink_fake = disc_ink(ink_fake_B)
D_ink_real_loss = mse(D_ink_real, torch.ones_like(D_ink_real))
D_ink_fake_loss = mse(D_ink_fake, torch.zeros_like(D_ink_fake))
D_ink_loss = D_ink_real_loss + D_ink_fake_loss
# put it together
D_loss = D_A_loss + D_B_loss + D_ink_loss * config.LAMBDA_INK
opt_disc.zero_grad()
d_scaler.scale(D_loss).backward()
d_scaler.step(opt_disc)
d_scaler.update()
# TODO: Train Generator A and B
# adversarial loss for both generators
D_A_fake = disc_A(fake_A)
D_B_fake = disc_B(fake_B)
ink_fake_B = gauss_kernel(erode(fake_B))
loss_G_A = mse(D_A_fake, torch.ones_like(D_A_fake))
loss_G_B = mse(D_B_fake, torch.ones_like(D_B_fake))
loss_G_B_ink = mse(ink_fake_B, torch.ones_like(ink_fake_B))
# cycle loss
cycle_B = gen_B(fake_A)
cycle_A = gen_A(fake_B)
cycle_B_loss = l1(B, cycle_B)
cycle_A_loss = l1(A, cycle_A)
# brush loss
edge_real_A = torch.sigmoid(hed(A))
edge_fake_B = torch.sigmoid(hed(fake_B))
loss_edge = no_sigmoid_cross_entropy(edge_fake_B, edge_real_A)
# add all together
G_loss = (
loss_G_B
+ loss_G_A
+ loss_G_B_ink * config.LAMBDA_INK
+ cycle_B_loss * config.LAMBDA_CYCLE
+ cycle_A_loss * config.LAMBDA_CYCLE
+ loss_edge * config.LAMBDA_BRUSH
)
opt_gen.zero_grad()
g_scaler.scale(G_loss).backward()
g_scaler.step(opt_gen)
g_scaler.update()
loop.set_postfix(B_real=B_reals/(idx+1), B_fake=B_fakes/(idx+1))
def main():
disc_A = Discriminator(in_channels=3).to(config.DEVICE)
disc_B = Discriminator(in_channels=3).to(config.DEVICE)
disc_ink = Discriminator(in_channels=3).to(config.DEVICE)
gen_A = Generator(img_channels=3).to(config.DEVICE)
gen_B = Generator(img_channels=3).to(config.DEVICE)
hed = HED().to(config.DEVICE)
opt_disc = optim.Adam(
list(disc_A.parameters()) + list(disc_B.parameters()) + list(disc_ink.parameters()),
lr=config.LEARNING_RATE,
betas=(0.5, 0.999),
)
opt_gen = optim.Adam(
list(gen_A.parameters()) + list(gen_B.parameters()),
lr=config.LEARNING_RATE,
betas=(0.5, 0.999),
)
L1 = nn.L1Loss()
mse = nn.MSELoss()
if config.LOAD_MODEL:
load_checkpoint(
config.CHECKPOINT_GEN_A, gen_A, opt_gen, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_GEN_B, gen_B, opt_gen, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_CRITIC_A, disc_A, opt_disc, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_CRITIC_B, disc_B, opt_disc, config.LEARNING_RATE,
)
load_checkpoint(
config.CHECKPOINT_CRITIC_INK, disc_ink, opt_disc, config.LEARNING_RATE,
)
dataset = ABDataset(
root_A=config.TRAIN_DIR + "/trainA", root_B=config.TRAIN_DIR + "/trainB", transform=my_transform
)
loader = DataLoader(
dataset,
batch_size=config.BATCH_SIZE,
shuffle=True,
num_workers=config.NUM_WORKERS,
pin_memory=True
)
g_scaler = torch.cuda.amp.GradScaler()
d_scaler = torch.cuda.amp.GradScaler()
for epoch in range(config.NUM_EPOCHS):
train(disc_A, disc_B, disc_ink, gen_A, gen_B, hed, loader, opt_disc, opt_gen, L1, mse, d_scaler, g_scaler)
if config.SAVE_MODEL:
save_checkpoint(gen_A, opt_gen, filename=config.CHECKPOINT_GEN_A)
save_checkpoint(gen_B, opt_gen, filename=config.CHECKPOINT_GEN_B)
save_checkpoint(disc_A, opt_disc, filename=config.CHECKPOINT_CRITIC_A)
save_checkpoint(disc_B, opt_disc, filename=config.CHECKPOINT_CRITIC_B)
save_checkpoint(disc_ink, opt_disc, filename=config.CHECKPOINT_CRITIC_INK)
if __name__ == "__main__":
main()