-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle_gan_style_transfer.py
193 lines (153 loc) · 7.53 KB
/
cycle_gan_style_transfer.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
185
186
187
188
189
190
191
192
193
from cycle_gan import CycleGAN
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import random
from collections import deque
from utils import save_model, load_yaml
import copy
# Set the configuration
config = load_yaml("./config/cycle_gan_config.yml")
# Training setting
device = 'cuda' if torch.cuda.is_available() else 'cpu'
torch.manual_seed(config['data']['seed'])
if device == 'cuda':
torch.cuda.manual_seed_all(config['data']['seed'])
# Set the transform
transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(config['data']['img_size'])])
# https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/
# apple2orange
# Set the training data
data = datasets.ImageFolder(root=config['data']['data_path'], transform=transform)
test_dataA = copy.deepcopy(data)
test_dataA.targets = test_dataA.targets[test_dataA.targets == 0]
test_dataA.imgs = test_dataA.imgs[test_dataA.targets == 0]
test_dataB = copy.deepcopy(data)
test_dataB.targets = test_dataB.targets[test_dataB.targets == 0]
test_dataB.imgs = test_dataB.imgs[test_dataB.targets == 0]
train_dataA = copy.deepcopy(data)
train_dataA.targets = train_dataA.targets[train_dataA.targets == 0]
train_dataA.imgs = train_dataA.imgs[train_dataA.targets == 0]
train_dataB = copy.deepcopy(data)
train_dataB.targets = train_dataB.targets[train_dataB.targets == 0]
train_dataB.imgs = train_dataB.imgs[train_dataB.targets == 0]
train_loaderA = torch.utils.data.DataLoader(train_dataA, batch_size=config['data']['batch_size'],
shuffle=config['data']['shuffle'],
num_workers=config['data']['num_workers'],
drop_last=config['data']['drop_last'])
train_loaderB = torch.utils.data.DataLoader(train_dataB, batch_size=config['data']['batch_size'],
shuffle=config['data']['shuffle'],
num_workers=config['data']['num_workers'],
drop_last=config['data']['drop_last'])
test_loaderA = torch.utils.data.DataLoader(test_dataA, batch_size=config['data']['batch_size'],
shuffle=config['data']['shuffle'],
num_workers=config['data']['num_workers'],
drop_last=config['data']['drop_last'])
test_loaderB = torch.utils.data.DataLoader(test_dataB, batch_size=config['data']['batch_size'],
shuffle=config['data']['shuffle'],
num_workers=config['data']['num_workers'],
drop_last=config['data']['drop_last'])
# Set the model
model = CycleGAN(gen_input_dim=config['model']['gen_input_dim'], gen_output_dim=config['model']['gen_output_dim'],
dis_input_dim=config['model']['dis_input_dim'], dis_conv_filters=config['model']['dis_conv_filters'],
dis_conv_kernels=config['model']['dis_conv_kernels'], dis_conv_strides=config['model']['dis_conv_strides'],
dis_conv_pads=config['model']['dis_conv_pads'], dis_norm=config['model']['dis_norm']).to(device)
print(model, device)
# Set the criterion and optimizer
gxy_optimizer = optim.Adam(model.Gxy.parameters(),
lr=config['train']['lr'],
betas=config['train']['betas'])
gyx_optimizer = optim.Adam(model.Gyx.parameters(),
lr=config['train']['lr'],
betas=config['train']['betas'])
dx_optimizer = optim.Adam(model.Dx.parameters(),
lr=config['train']['lr'],
betas=config['train']['betas'])
dy_optimizer = optim.Adam(model.Dy.parameters(),
lr=config['train']['lr'],
betas=config['train']['betas'])
mse_loss = nn.MSELoss()
l1_loss = nn.L1Loss()
fakeA_buffer = deque(maxlen=100)
fakeB_buffer = deque(maxlen=100)
w_valid = config['train']['w_valid']
w_recon = config['train']['w_recon']
w_iden = config['train']['w_iden']
# Training
def train(epoch, model, train_loaderA, train_loaderB, gxy_optimizer, gyx_optimizer, dx_optimizer, dy_optimizer):
model.train()
batch_size = config['data']['batch_size']
num_data = batch_size * config['others']['log_period']
valid = torch.ones(batch_size, 1, 16, 16, device=device)
fake = torch.ones(batch_size, 1, 16, 16, device=device)
losses = [0, 0, 0, 0]
i = 0
for dataA, dataB in zip(train_loaderA, train_loaderB):
# Data
realA, _ = dataA
realA = realA.to(device)
realB, _ = dataB
realB = realB.to(device)
fakeA = model.Gyx(realB)
fakeB = model.Gxy(realA)
# fakeA_buffer.append(fakeA)
# fakeB_buffer.append(fakeB)
# fakeA = fakeA_buffer[random.randrange(len(fakeA_buffer))]
# fakeB = fakeB_buffer[random.randrange(len(fakeB_buffer))]
realA_score = model.Dx(realA)
fakeA_score = model.Dx(fakeA)
realB_score = model.Dy(realB)
fakeB_score = model.Dy(fakeB)
# Discriminator Dx (Validity)
# (torch.mean((realA_score - 1)**2) + torch.mean(fakeA_score**2))
realA_loss = mse_loss(realA_score, valid)
fakeA_loss = mse_loss(fakeA_score, fake)
dx_loss = (realA_loss + fakeA_loss).mean()
dx_optimizer.zero_grad()
dx_loss.backward()
dx_optimizer.step()
# Discriminator Dy (Validity)
realB_loss = mse_loss(realB_score, valid)
fakeB_loss = mse_loss(fakeB_score, fake)
dy_loss = (realB_loss + fakeB_loss).mean()
dy_optimizer.zero_grad()
dy_loss.backward()
dy_optimizer.step()
# Generator Gxy (Reconstruction and identity)
scoreB = model.Dy(model.Gxy(realA))
reconB = model.Gxy(model.Gyx(realB))
idenB = model.Gxy(realB)
gxy_loss = w_valid * mse_loss(scoreB, valid) + w_recon * l1_loss(realB, reconB) + w_iden * l1_loss(realB, idenB)
gxy_optimizer.zero_grad()
gxy_loss.backward()
gxy_optimizer.step()
# Generator Gyx (Reconstruction and identity)
scoreA = model.Dx(model.Gyx(realB))
reconA = model.Gyx(model.Gxy(realA))
idenA = model.Gyx(realA)
gyx_loss = w_valid * mse_loss(scoreA, valid) + w_recon * l1_loss(realA, reconA) + w_iden * l1_loss(realA, idenA)
gyx_optimizer.zero_grad()
gyx_loss.backward()
gyx_optimizer.step()
losses[0] += dx_loss
losses[1] += dy_loss
losses[2] += gxy_loss
losses[3] += gyx_loss
i = i + 1
if i % config['others']['log_period'] == 0 and i != 0:
print(f'[{epoch}, {i}]\t dx loss: {losses[0]/num_data:.5f}\t dy loss: {losses[1]/num_data:.5f}\t gxy loss: {losses[2]/num_data:.5f}\t gyx loss: {losses[3]/num_data:.5f}')
losses = [0, 0, 0, 0]
return losses[0]/num_data, losses[1]/num_data, losses[2]/num_data, losses[3]/num_data
# Main
if __name__ == '__main__':
for epoch in range(config['train']['epochs']): # loop over the dataset multiple times
# Training
dx_loss, dy_loss, gxy_loss, gyx_loss = train(epoch, model, train_loaderA, train_loaderB, gxy_optimizer, gyx_optimizer, dx_optimizer, dy_optimizer)
# Print the log
print(f'Epoch: {epoch}\t dx loss: {dx_loss:.5f}\t dy loss: {dy_loss:.5f}\t gxy loss: {gxy_loss:.5f}\t gyx loss: {gyx_loss:.5f}')
# Save the model
save_model(model_name=config['save']['model_name'], epoch=epoch, model=model, optimizer=gxy_optimizer, loss=dx_loss, config=config)