-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_GAN_states_x.py
276 lines (195 loc) · 9.79 KB
/
main_GAN_states_x.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import os
import random
import time
import warnings
warnings.filterwarnings("ignore")
from math import log10
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from lpips import lpips
from pytorch_msssim import SSIM as pytorch_ssim
from torch.utils.data import DataLoader
from tqdm import tqdm
from data import get_training_set
from image_utils import TVLoss
from laploss import LapLoss
from model_x import *
from network import decoder3, decoder4, encoder3, encoder4
# Training settings
parser = argparse.ArgumentParser(description='Example')
parser.add_argument('--data_dir', type=str, default="D:/Users/CVAE/datasets/STATES_x4_3D")
parser.add_argument('--up_factor', type=int, default=4, help='upsampling factor')
parser.add_argument('--batchSize', type=int, default=1, help='training batch size')
parser.add_argument('--nEpochs', type=int, default=50, help='number of epochs to train for')
parser.add_argument('--snapshots', type=int, default=1, help='Snapshots')
parser.add_argument('--start_iter', type=int, default=1, help='Starting Epoch')
parser.add_argument('--lr', type=float, default=1e-4, help='Learning Rate. Default=0.0001')
parser.add_argument('--gpu_mode', type=bool, default=True)
parser.add_argument('--threads', type=int, default=4, help='number of threads for data loader to use')
parser.add_argument('--seed', type=int, default=123, help='random seed to use. Default=123')
parser.add_argument('--gpus', default=1, type=int, help='number of gpu')
parser.add_argument('--data_augmentation', type=bool, default=False)
parser.add_argument('--model_type', type=str, default='GAN')
parser.add_argument('--patch_size', type=int, default=8, help='Size of cropped LR image')
parser.add_argument('--pretrained', type=bool, default=False)
parser.add_argument('--save_folder', default='D:/Users/CVAE/states_models_x4_3D_x1/', help='Location to save checkpoint models')
opt = parser.parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
def train(epoch):
G_epoch_loss = 0
D_epoch_loss = 0
G.train()
D.train()
for iteration, batch in enumerate(training_data_loader):
input, target, ref = batch['LR'], batch['HR'], batch['Ref']
# ref = ref[0]
minibatch = input.size()[0]
real_label = torch.ones((minibatch, 7917))
fake_label = torch.zeros((minibatch, 7917))
input = input.to(device)
target = target.to(device)
ref = ref.to(device)
real_label = real_label.to(device)
fake_label = fake_label.to(device)
# Reset gradient
for p in D.parameters():
p.requires_grad = False
G_optimizer.zero_grad()
# encoder
bic = F.interpolate(input, scale_factor=opt.up_factor, mode='bilinear')
ref_feat = enc(target)
LR_feat = enc(bic)
predict, KL = G(input,target, LR_feat['r41'], ref_feat['r41'])
pre_LR = F.interpolate(predict, scale_factor=1.0 / opt.up_factor, mode='bilinear')
LR_loss = L1_criterion(pre_LR, input)
SR_L1 = L1_criterion(predict, target) + \
L1_criterion(F.interpolate(predict, scale_factor=0.5, mode='bilinear'), F.interpolate(target, scale_factor=0.5, mode='bilinear')) + \
L1_criterion(F.interpolate(predict, scale_factor=0.25, mode='bilinear'), F.interpolate(target, scale_factor=0.25, mode='bilinear'))
ssim_loss = 1 - ssim(predict, target)
lap_recon = lap_loss(predict, target)
ContentLoss, StyleLoss = VGG_feat(predict, bic)
lpips_sp = loss_fn_alex_sp(2 * predict - 1, 2 * target - 1)
lpips_sp = lpips_sp.mean()
D_fake_feat, D_fake_decision = D(predict)
D_real_feat, D_real_decision = D(target)
GAN_feat_loss = L1_criterion(D_fake_feat, D_real_feat)
GAN_loss = L1_criterion(D_fake_decision, real_label)
G_loss = 16*LR_loss + 1 * SR_L1 + 0.001 * ContentLoss + StyleLoss + 1e4*lpips_sp +1e4*ssim_loss+lap_recon+1e4*KL
G_loss.backward()
G_optimizer.step()
G_epoch_loss += G_loss.data
print("===> Epoch[{}]({}/{}): \
G_loss: {:.4f} || "
"LR_loss: {:.4f} || "
"SR_L1: {:.4f} || "
"GAN_loss: {:.4f} || "
"ContentLoss: {:.4f} || "
"StyleLoss: {:.4f} ||"
"lpips_sp: {:.4f} ||"
"GAN_feat_loss: {:.4f} ||".format(epoch, iteration,
len(training_data_loader),
G_loss.data,
LR_loss.data,
SR_L1.data,
GAN_loss.data,
ContentLoss.data,
StyleLoss.data,
lpips_sp.data,
GAN_feat_loss.data))
print("===> Epoch {} Complete: Avg. G Loss: {:.4f} || D Loss: {:.4f}".format(epoch, G_epoch_loss / len(training_data_loader),
D_epoch_loss / len(training_data_loader)))
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
def checkpoint(epoch):
G_model_out_path = opt.save_folder + opt.model_type + "_generator_{}.pth".format(epoch)
D_model_out_path = opt.save_folder + opt.model_type + "_discriminator_{}.pth".format(epoch)
enc_model_out_path = opt.save_folder + opt.model_type + "_enc_{}.pth".format(epoch)
dec_model_out_path = opt.save_folder + opt.model_type + "_dec_{}.pth".format(epoch)
torch.save(G.state_dict(), G_model_out_path)
torch.save(D.state_dict(), D_model_out_path)
torch.save(enc.state_dict(), enc_model_out_path)
torch.save(dec.state_dict(), dec_model_out_path)
print("Checkpoint saved to {} and {}".format(G_model_out_path, D_model_out_path))
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
if __name__ =="__main__":
setup_seed(123)
print('===> Loading datasets')
train_set = get_training_set(opt.data_dir, opt.patch_size, opt.up_factor,
opt.data_augmentation)
training_data_loader = DataLoader(dataset=train_set, num_workers=0, batch_size=opt.batchSize, shuffle=True)
print('===> Building model ', opt.model_type)
enc = encoder4(1)
dec = decoder4()
G = VAE_v3_4x(up_factor=opt.up_factor)
D = discriminator_v2(num_channels=1, base_filter=32)
L1_criterion = nn.L1Loss(size_average=False)
L2_criterion = nn.MSELoss(size_average=False)
TV = TVLoss()
ssim = pytorch_ssim()
lap_loss = LapLoss(max_levels=5, k_size=5, sigma=2.0)
VGG_feat = Vgg19_feat(device)
loss_fn_alex_sp = lpips.LPIPS(spatial=True)
print('---------- Generator architecture -------------')
print_network(G)
print('----------------------------------------------')
print('---------- Discriminator architecture -------------')
print_network(D)
print('----------------------------------------------')
enc_model_name = r"D:/Users/CVAE/models/vgg_r41.pth"
if os.path.exists(enc_model_name):
pretrained_dict = torch.load(enc_model_name, map_location=lambda storage, loc: storage)
enc_model_dict = enc.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in enc_model_dict}
enc_model_dict.update(pretrained_dict)
enc.load_state_dict(enc_model_dict)
print('encoder model is loaded!')
if not os.path.exists(opt.save_folder):
os.mkdir(opt.save_folder)
if opt.pretrained:
G_model_name = os.path.join(opt.save_folder + opt.pretrained_G_model)
D_model_name = os.path.join(opt.save_folder + opt.pretrained_D_model)
if os.path.exists(G_model_name):
pretrained_dict = torch.load(G_model_name, map_location=lambda storage, loc: storage)
model_dict = G.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
G.load_state_dict(model_dict)
print('Pre-trained Generator is loaded.')
if os.path.exists(D_model_name):
D.load_state_dict(torch.load(D_model_name, map_location=lambda storage, loc: storage))
print('Pre-trained Discriminator is loaded.')
enc = enc.to(device)
dec = dec.to(device)
G = G.to(device)
D = D.to(device)
VGG_feat = VGG_feat.to(device)
L1_criterion = L1_criterion.to(device)
ssim = ssim.to(device)
lap_loss = lap_loss.to(device)
TV = TV.to(device)
loss_fn_alex_sp = loss_fn_alex_sp.to(device)
G_optimizer = optim.Adam(G.parameters(), lr=opt.lr, betas=(0.9, 0.999), eps=1e-8)
D_optimizer = optim.Adam(D.parameters(), lr=opt.lr, betas=(0.9, 0.999), eps=1e-8)
for epoch in tqdm(range(opt.start_iter, opt.nEpochs + 1)):
train(epoch)
print(G_optimizer.param_groups[0]['lr'])
if epoch % (opt.snapshots) == 0:
checkpoint(epoch)
print("end")