-
Notifications
You must be signed in to change notification settings - Fork 18
/
FRSRGAN_train.py
206 lines (174 loc) · 8.36 KB
/
FRSRGAN_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import argparse
import os
from math import log10
import gc
import pandas as pd
import torch.optim as optim
import torch.utils.data
import torchvision.utils as utils
from torch.autograd import Variable
from torch.utils.data import DataLoader
from tqdm import tqdm
import Dataset_OnlyHR
from FRVSR_models import FRVSR
from SRGAN.data_utils import TrainDatasetFromFolder, ValDatasetFromFolder, display_transform
from FRVSR_models import GeneratorLoss
from SRGAN.model import Generator, Discriminator
import SRGAN.pytorch_ssim as pts
parser = argparse.ArgumentParser(description='Train Super Resolution Models')
parser.add_argument('--num_epochs', default=1000, type=int, help='train epoch number')
parser.add_argument('--width', default=112, type=int, help='lr pic width')
parser.add_argument('--height', default=64, type=int, help='lr pic height')
parser.add_argument('--dataset_size', default=0, type=int, help='dataset_size, 0 to use all')
parser.add_argument('--batch_size', default=2, type=int, help='batch_size, default 2')
parser.add_argument('--lr', default=1e-5, type=float, help='learning rate, default 1e-5')
opt = parser.parse_args()
UPSCALE_FACTOR = 4
NUM_EPOCHS = opt.num_epochs
WIDTH = opt.width
HEIGHT = opt.height
batch_size = opt.batch_size
dataset_size = opt.dataset_size
lr = opt.lr
# train_set = TrainDatasetFromFolder('data/VOC2012/train', crop_size=CROP_SIZE, upscale_factor=UPSCALE_FACTOR)
# val_set = ValDatasetFromFolder('data/VOC2012/val', upscale_factor=UPSCALE_FACTOR)
# train_loader = DataLoader(dataset=train_set, num_workers=4, batch_size=64, shuffle=True)
# val_loader = DataLoader(dataset=val_set, num_workers=4, batch_size=1, shuffle=False)
train_loader, val_loader = Dataset_OnlyHR.get_data_loaders(batch_size, dataset_size=dataset_size, validation_split=0.2)
num_train_batches = len(train_loader)
num_val_batches = len(val_loader)
netG = FRVSR(batch_size, lr_width=WIDTH, lr_height=HEIGHT)
print('# generator parameters:', sum(param.numel() for param in netG.parameters()))
netD = Discriminator()
print('# discriminator parameters:', sum(param.numel() for param in netD.parameters()))
generator_criterion = GeneratorLoss()
if torch.cuda.is_available():
netG.cuda()
netD.cuda()
generator_criterion.cuda()
optimizerG = optim.Adam(netG.parameters(), lr=lr)
optimizerD = optim.Adam(netD.parameters(), lr=lr)
results = {'d_loss': [], 'g_loss': [], 'd_score': [], 'g_score': [], 'psnr': [], 'ssim': []}
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
for epoch in range(1, NUM_EPOCHS + 1):
train_bar = tqdm(train_loader)
running_results = {'batch_sizes': 0, 'd_loss': 0, 'g_loss': 0, 'd_score': 0, 'g_score': 0}
netG.train()
netD.train()
for data, target in train_bar:
g_update_first = True
batch_size = data.size(0)
running_results['batch_sizes'] += batch_size
############################
# (1) Update D network: maximize D(x)-1-D(G(z))
###########################
fake_hrs = []
fake_lrs = []
fake_scrs = []
real_scrs = []
d_loss = 0
netD.zero_grad()
netG.init_hidden(device)
for lr_img, hr_img in zip(data, target):
# if torch.cuda.is_available():
hr_img = hr_img.to(device)
# if torch.cuda.is_available():
lr_img = lr_img.to(device)
fake_hr, fake_lr = netG(lr_img)
real_out = netD(hr_img).mean()
fake_out = netD(fake_hr).mean()
fake_hrs.append(fake_hr)
fake_lrs.append(fake_lr)
fake_scrs.append(fake_out)
real_scrs.append(real_out)
d_loss += 1 - real_out + fake_out
d_loss /= len(data)
d_loss.backward(retain_graph=True)
optimizerD.step()
############################
# (2) Update G network: minimize 1-D(G(z)) + Perception Loss + Image Loss + TV Loss
###########################
g_loss = 0
netG.zero_grad()
idx = 0
for fake_hr, fake_lr, fake_scr, hr_img, lr_img \
in zip(fake_hrs, fake_lrs, fake_scrs, target, data):
fake_hr = fake_hr.to(device)
fake_lr = fake_lr.to(device)
fake_scr = fake_scr.to(device)
hr_img = hr_img.to(device)
lr_img = lr_img.to(device)
g_loss += generator_criterion(fake_scr, fake_hr, hr_img, fake_lr, lr_img, idx)
idx += 1
g_loss /= len(data)
g_loss.backward()
optimizerG.step()
real_out = torch.Tensor(real_scrs).mean()
fake_out = torch.Tensor(fake_scrs).mean()
running_results['g_loss'] += g_loss.data.item() * batch_size
running_results['d_loss'] += d_loss.data.item() * batch_size
running_results['d_score'] += real_out.data.item() * batch_size
running_results['g_score'] += fake_out.data.item() * batch_size
train_bar.set_description(desc='[%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f' % (
epoch, NUM_EPOCHS, running_results['d_loss'] / running_results['batch_sizes'],
running_results['g_loss'] / running_results['batch_sizes'],
running_results['d_score'] / running_results['batch_sizes'],
running_results['g_score'] / running_results['batch_sizes']))
gc.collect()
netG.eval()
# out_path = 'training_results/SRF_' + str(UPSCALE_FACTOR) + '/'
# if not os.path.exists(out_path):
# os.makedirs(out_path)
val_bar = tqdm(val_loader)
valing_results = {'mse': 0, 'ssims': 0, 'psnr': 0, 'ssim': 0, 'batch_sizes': 0}
val_images = []
for val_lr, val_hr in val_bar:
batch_size = val_lr.size(0)
valing_results['batch_sizes'] += batch_size
netG.init_hidden(device)
batch_mse = []
batch_ssim = []
for lr, hr in zip(val_lr, val_hr):
lr = lr.to(device)
hr = hr.to(device)
hr_est, lr_est = netG(lr)
batch_mse.append(((hr_est - hr) ** 2).data.mean())
batch_ssim.append(pts.ssim(hr_est, hr).item())
batch_mse = torch.Tensor(batch_mse).mean()
valing_results['mse'] += batch_mse * batch_size
batch_ssim = torch.Tensor(batch_ssim).mean()
valing_results['ssims'] += batch_ssim * batch_size
valing_results['psnr'] = 10 * log10(1 / (valing_results['mse'] / valing_results['batch_sizes']))
valing_results['ssim'] = valing_results['ssims'] / valing_results['batch_sizes']
val_bar.set_description(
desc='[converting LR images to SR images] PSNR: %.4f dB SSIM: %.4f' % (
valing_results['psnr'], valing_results['ssim']))
gc.collect()
# val_images.extend(
# [display_transform()(val_hr_restore.squeeze(0)), display_transform()(hr.data.cpu().squeeze(0)),
# display_transform()(sr.data.cpu().squeeze(0))])
# val_images = torch.stack(val_images)
# val_images = torch.chunk(val_images, val_images.size(0) // 15)
# val_save_bar = tqdm(val_images, desc='[saving training results]')
# index = 1
# for image in val_save_bar:
# image = utils.make_grid(image, nrow=3, padding=5)
# utils.save_image(image, out_path + 'epoch_%d_index_%d.png' % (epoch, index), padding=5)
# index += 1
# save model parameters
torch.save(netG.state_dict(), 'epochs/netG_epoch_%d_%d.pth' % (UPSCALE_FACTOR, epoch))
torch.save(netD.state_dict(), 'epochs/netD_epoch_%d_%d.pth' % (UPSCALE_FACTOR, epoch))
# save loss\scores\psnr\ssim
results['d_loss'].append(running_results['d_loss'] / running_results['batch_sizes'])
results['g_loss'].append(running_results['g_loss'] / running_results['batch_sizes'])
results['d_score'].append(running_results['d_score'] / running_results['batch_sizes'])
results['g_score'].append(running_results['g_score'] / running_results['batch_sizes'])
results['psnr'].append(valing_results['psnr'])
results['ssim'].append(valing_results['ssim'])
if epoch % 1 == 0 and epoch != 0:
out_path = 'statistics/'
data_frame = pd.DataFrame(
data={'Loss_D': results['d_loss'], 'Loss_G': results['g_loss'], 'Score_D': results['d_score'],
'Score_G': results['g_score'], 'PSNR': results['psnr'], 'SSIM': results['ssim']},
index=range(1, epoch + 1))
data_frame.to_csv(out_path + 'srf_' + str(UPSCALE_FACTOR) + '_train_results.csv', index_label='Epoch')