This repository was archived by the owner on Oct 18, 2023. It is now read-only.
forked from Lornatang/SRGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_srgan.py
More file actions
471 lines (389 loc) · 20.4 KB
/
train_srgan.py
File metadata and controls
471 lines (389 loc) · 20.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Copyright 2022 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import os
import time
import torch
from torch import nn
from torch import optim
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import model
import srgan_config
from dataset import CUDAPrefetcher, TrainValidImageDataset, TestImageDataset
from image_quality_assessment import PSNR, SSIM
from utils import load_state_dict, make_directory, save_checkpoint, AverageMeter, ProgressMeter
model_names = sorted(
name for name in model.__dict__ if
name.islower() and not name.startswith("__") and callable(model.__dict__[name]))
def main():
# Initialize the number of training epochs
start_epoch = 0
# Initialize training to generate network evaluation indicators
best_psnr = 0.0
best_ssim = 0.0
train_prefetcher, test_prefetcher = load_dataset()
print("Load all datasets successfully.")
d_model, g_model = build_model()
print(f"Build `{srgan_config.g_arch_name}` model successfully.")
pixel_criterion, content_criterion, adversarial_criterion = define_loss()
print("Define all loss functions successfully.")
d_optimizer, g_optimizer = define_optimizer(d_model, g_model)
print("Define all optimizer functions successfully.")
d_scheduler, g_scheduler = define_scheduler(d_optimizer, g_optimizer)
print("Define all optimizer scheduler functions successfully.")
print("Check whether to load pretrained d model weights...")
if srgan_config.pretrained_d_model_weights_path:
d_model = load_state_dict(d_model, srgan_config.pretrained_d_model_weights_path)
print(f"Loaded `{srgan_config.pretrained_d_model_weights_path}` pretrained model weights successfully.")
else:
print("Pretrained d model weights not found.")
print("Check whether to load pretrained g model weights...")
if srgan_config.pretrained_g_model_weights_path:
g_model = load_state_dict(g_model, srgan_config.pretrained_g_model_weights_path)
print(f"Loaded `{srgan_config.pretrained_g_model_weights_path}` pretrained model weights successfully.")
else:
print("Pretrained g model weights not found.")
print("Check whether the pretrained d model is restored...")
if srgan_config.resume_d_model_weights_path:
d_model, _, start_epoch, best_psnr, best_ssim, optimizer, scheduler = load_state_dict(
d_model,
srgan_config.resume_d_model_weights_path,
optimizer=d_optimizer,
scheduler=d_scheduler,
load_mode="resume")
print("Loaded pretrained model weights.")
else:
print("Resume training d model not found. Start training from scratch.")
print("Check whether the pretrained g model is restored...")
if srgan_config.resume_g_model_weights_path:
g_model, _, start_epoch, best_psnr, best_ssim, optimizer, scheduler = load_state_dict(
g_model,
srgan_config.resume_g_model_weights_path,
optimizer=g_optimizer,
scheduler=g_scheduler,
load_mode="resume")
print("Loaded pretrained model weights.")
else:
print("Resume training g model not found. Start training from scratch.")
# Create a experiment results
samples_dir = os.path.join("samples", srgan_config.exp_name)
results_dir = os.path.join("results", srgan_config.exp_name)
make_directory(samples_dir)
make_directory(results_dir)
# Create training process log file
writer = SummaryWriter(os.path.join("samples", "logs", srgan_config.exp_name))
# Create an IQA evaluation model
psnr_model = PSNR(srgan_config.upscale_factor, srgan_config.only_test_y_channel)
ssim_model = SSIM(srgan_config.upscale_factor, srgan_config.only_test_y_channel)
# Transfer the IQA model to the specified device
psnr_model = psnr_model.to(device=srgan_config.device)
ssim_model = ssim_model.to(device=srgan_config.device)
for epoch in range(start_epoch, srgan_config.epochs):
train(d_model,
g_model,
train_prefetcher,
pixel_criterion,
content_criterion,
adversarial_criterion,
d_optimizer,
g_optimizer,
epoch,
writer)
psnr, ssim = validate(g_model,
test_prefetcher,
epoch,
writer,
psnr_model,
ssim_model,
"Test")
print("\n")
# Update LR
d_scheduler.step()
g_scheduler.step()
# Automatically save the model with the highest index
is_best = psnr > best_psnr and ssim > best_ssim
is_last = (epoch + 1) == srgan_config.epochs
best_psnr = max(psnr, best_psnr)
best_ssim = max(ssim, best_ssim)
save_checkpoint({"epoch": epoch + 1,
"best_psnr": best_psnr,
"best_ssim": best_ssim,
"state_dict": d_model.state_dict(),
"optimizer": d_optimizer.state_dict(),
"scheduler": d_scheduler.state_dict()},
f"d_epoch_{epoch + 1}.pth.tar",
samples_dir,
results_dir,
"d_best.pth.tar",
"d_last.pth.tar",
is_best,
is_last)
save_checkpoint({"epoch": epoch + 1,
"best_psnr": best_psnr,
"best_ssim": best_ssim,
"state_dict": g_model.state_dict(),
"optimizer": g_optimizer.state_dict(),
"scheduler": g_scheduler.state_dict()},
f"g_epoch_{epoch + 1}.pth.tar",
samples_dir,
results_dir,
"g_best.pth.tar",
"g_last.pth.tar",
is_best,
is_last)
def load_dataset() -> [CUDAPrefetcher, CUDAPrefetcher]:
# Load train, test and valid datasets
train_datasets = TrainValidImageDataset(srgan_config.train_gt_images_dir,
srgan_config.gt_image_size,
srgan_config.upscale_factor,
"Train")
test_datasets = TestImageDataset(srgan_config.test_gt_images_dir, srgan_config.test_lr_images_dir)
# Generator all dataloader
train_dataloader = DataLoader(train_datasets,
batch_size=srgan_config.batch_size,
shuffle=True,
num_workers=srgan_config.num_workers,
pin_memory=True,
drop_last=True,
persistent_workers=True)
test_dataloader = DataLoader(test_datasets,
batch_size=1,
shuffle=False,
num_workers=1,
pin_memory=True,
drop_last=False,
persistent_workers=True)
# Place all data on the preprocessing data loader
train_prefetcher = CUDAPrefetcher(train_dataloader, srgan_config.device)
test_prefetcher = CUDAPrefetcher(test_dataloader, srgan_config.device)
return train_prefetcher, test_prefetcher
def build_model() -> [nn.Module, nn.Module, nn.Module]:
d_model = model.__dict__[srgan_config.d_arch_name]()
g_model = model.__dict__[srgan_config.g_arch_name](in_channels=srgan_config.in_channels,
out_channels=srgan_config.out_channels,
channels=srgan_config.channels,
num_rcb=srgan_config.num_rcb)
d_model = d_model.to(device=srgan_config.device)
g_model = g_model.to(device=srgan_config.device)
return d_model, g_model
def define_loss() -> [nn.MSELoss, model.content_loss, nn.BCEWithLogitsLoss]:
pixel_criterion = nn.MSELoss()
content_criterion = model.content_loss(feature_model_extractor_node=srgan_config.feature_model_extractor_node,
feature_model_normalize_mean=srgan_config.feature_model_normalize_mean,
feature_model_normalize_std=srgan_config.feature_model_normalize_std)
adversarial_criterion = nn.BCEWithLogitsLoss()
# Transfer to CUDA
pixel_criterion = pixel_criterion.to(device=srgan_config.device)
content_criterion = content_criterion.to(device=srgan_config.device)
adversarial_criterion = adversarial_criterion.to(device=srgan_config.device)
return pixel_criterion, content_criterion, adversarial_criterion
def define_optimizer(d_model, g_model) -> [optim.Adam, optim.Adam]:
d_optimizer = optim.Adam(d_model.parameters(),
srgan_config.model_lr,
srgan_config.model_betas,
srgan_config.model_eps,
srgan_config.model_weight_decay)
g_optimizer = optim.Adam(g_model.parameters(),
srgan_config.model_lr,
srgan_config.model_betas,
srgan_config.model_eps,
srgan_config.model_weight_decay)
return d_optimizer, g_optimizer
def define_scheduler(
d_optimizer: optim.Adam,
g_optimizer: optim.Adam
) -> [lr_scheduler.StepLR, lr_scheduler.StepLR]:
d_scheduler = lr_scheduler.StepLR(d_optimizer,
srgan_config.lr_scheduler_step_size,
srgan_config.lr_scheduler_gamma)
g_scheduler = lr_scheduler.StepLR(g_optimizer,
srgan_config.lr_scheduler_step_size,
srgan_config.lr_scheduler_gamma)
return d_scheduler, g_scheduler
def train(
d_model: nn.Module,
g_model: nn.Module,
train_prefetcher: CUDAPrefetcher,
pixel_criterion: nn.MSELoss,
content_criterion: model.content_loss,
adversarial_criterion: nn.BCEWithLogitsLoss,
d_optimizer: optim.Adam,
g_optimizer: optim.Adam,
epoch: int,
writer: SummaryWriter
) -> None:
# Calculate how many batches of data are in each Epoch
batches = len(train_prefetcher)
# Print information of progress bar during training
batch_time = AverageMeter("Time", ":6.3f")
data_time = AverageMeter("Data", ":6.3f")
pixel_losses = AverageMeter("Pixel loss", ":6.6f")
content_losses = AverageMeter("Content loss", ":6.6f")
adversarial_losses = AverageMeter("Adversarial loss", ":6.6f")
d_gt_probabilities = AverageMeter("D(GT)", ":6.3f")
d_sr_probabilities = AverageMeter("D(SR)", ":6.3f")
progress = ProgressMeter(batches,
[batch_time, data_time,
pixel_losses, content_losses, adversarial_losses,
d_gt_probabilities, d_sr_probabilities],
prefix=f"Epoch: [{epoch + 1}]")
# Put the adversarial network model in training mode
d_model.train()
g_model.train()
# Initialize the number of data batches to print logs on the terminal
batch_index = 0
# Initialize the data loader and load the first batch of data
train_prefetcher.reset()
batch_data = train_prefetcher.next()
# Get the initialization training time
end = time.time()
while batch_data is not None:
# Calculate the time it takes to load a batch of data
data_time.update(time.time() - end)
# Transfer in-memory data to CUDA devices to speed up training
gt = batch_data["gt"].to(device=srgan_config.device, non_blocking=True)
lr = batch_data["lr"].to(device=srgan_config.device, non_blocking=True)
# Set the real sample label to 1, and the false sample label to 0
batch_size, _, height, width = gt.shape
real_label = torch.full([batch_size, 1], 1.0, dtype=gt.dtype, device=srgan_config.device)
fake_label = torch.full([batch_size, 1], 0.0, dtype=gt.dtype, device=srgan_config.device)
# Start training the discriminator model
# During discriminator model training, enable discriminator model backpropagation
for d_parameters in d_model.parameters():
d_parameters.requires_grad = True
# Initialize the discriminator model gradients
d_model.zero_grad(set_to_none=True)
# Calculate the classification score of the discriminator model for real samples
gt_output = d_model(gt)
d_loss_gt = adversarial_criterion(gt_output, real_label)
# Call the gradient scaling function in the mixed precision API to
# back-propagate the gradient information of the fake samples
d_loss_gt.backward(retain_graph=True)
# Calculate the classification score of the discriminator model for fake samples
# Use the generator model to generate fake samples
sr = g_model(lr)
sr_output = d_model(sr.detach().clone())
d_loss_sr = adversarial_criterion(sr_output, fake_label)
# Call the gradient scaling function in the mixed precision API to
# back-propagate the gradient information of the fake samples
d_loss_sr.backward()
# Calculate the total discriminator loss value
d_loss = d_loss_gt + d_loss_sr
# Improve the discriminator model's ability to classify real and fake samples
d_optimizer.step()
# Finish training the discriminator model
# Start training the generator model
# During generator training, turn off discriminator backpropagation
for d_parameters in d_model.parameters():
d_parameters.requires_grad = False
# Initialize generator model gradients
g_model.zero_grad(set_to_none=True)
# Calculate the perceptual loss of the generator, mainly including pixel loss, feature loss and adversarial loss
pixel_loss = srgan_config.pixel_weight * pixel_criterion(sr, gt)
content_loss = srgan_config.content_weight * content_criterion(sr, gt)
adversarial_loss = srgan_config.adversarial_weight * adversarial_criterion(d_model(sr), real_label)
# Calculate the generator total loss value
g_loss = pixel_loss + content_loss + adversarial_loss
# Call the gradient scaling function in the mixed precision API to
# back-propagate the gradient information of the fake samples
g_loss.backward()
# Encourage the generator to generate higher quality fake samples, making it easier to fool the discriminator
g_optimizer.step()
# Finish training the generator model
# Calculate the score of the discriminator on real samples and fake samples,
# the score of real samples is close to 1, and the score of fake samples is close to 0
d_gt_probability = torch.sigmoid_(torch.mean(gt_output.detach()))
d_sr_probability = torch.sigmoid_(torch.mean(sr_output.detach()))
# Statistical accuracy and loss value for terminal data output
pixel_losses.update(pixel_loss.item(), lr.size(0))
content_losses.update(content_loss.item(), lr.size(0))
adversarial_losses.update(adversarial_loss.item(), lr.size(0))
d_gt_probabilities.update(d_gt_probability.item(), lr.size(0))
d_sr_probabilities.update(d_sr_probability.item(), lr.size(0))
# Calculate the time it takes to fully train a batch of data
batch_time.update(time.time() - end)
end = time.time()
# Write the data during training to the training log file
if batch_index % srgan_config.train_print_frequency == 0:
iters = batch_index + epoch * batches + 1
writer.add_scalar("Train/D_Loss", d_loss.item(), iters)
writer.add_scalar("Train/G_Loss", g_loss.item(), iters)
writer.add_scalar("Train/Pixel_Loss", pixel_loss.item(), iters)
writer.add_scalar("Train/Content_Loss", content_loss.item(), iters)
writer.add_scalar("Train/Adversarial_Loss", adversarial_loss.item(), iters)
writer.add_scalar("Train/D(GT)_Probability", d_gt_probability.item(), iters)
writer.add_scalar("Train/D(SR)_Probability", d_sr_probability.item(), iters)
progress.display(batch_index + 1)
# Preload the next batch of data
batch_data = train_prefetcher.next()
# After training a batch of data, add 1 to the number of data batches to ensure that the
# terminal print data normally
batch_index += 1
def validate(
g_model: nn.Module,
data_prefetcher: CUDAPrefetcher,
epoch: int,
writer: SummaryWriter,
psnr_model: nn.Module,
ssim_model: nn.Module,
mode: str
) -> [float, float]:
# Calculate how many batches of data are in each Epoch
batch_time = AverageMeter("Time", ":6.3f")
psnres = AverageMeter("PSNR", ":4.2f")
ssimes = AverageMeter("SSIM", ":4.4f")
progress = ProgressMeter(len(data_prefetcher), [batch_time, psnres, ssimes], prefix=f"{mode}: ")
# Put the adversarial network model in validation mode
g_model.eval()
# Initialize the number of data batches to print logs on the terminal
batch_index = 0
# Initialize the data loader and load the first batch of data
data_prefetcher.reset()
batch_data = data_prefetcher.next()
# Get the initialization test time
end = time.time()
with torch.no_grad():
while batch_data is not None:
# Transfer the in-memory data to the CUDA device to speed up the test
gt = batch_data["gt"].to(device=srgan_config.device, non_blocking=True)
lr = batch_data["lr"].to(device=srgan_config.device, non_blocking=True)
# Use the generator model to generate a fake sample
sr = g_model(lr)
# Statistical loss value for terminal data output
psnr = psnr_model(sr, gt)
ssim = ssim_model(sr, gt)
psnres.update(psnr.item(), lr.size(0))
ssimes.update(ssim.item(), lr.size(0))
# Calculate the time it takes to fully test a batch of data
batch_time.update(time.time() - end)
end = time.time()
# Record training log information
if batch_index % srgan_config.valid_print_frequency == 0:
progress.display(batch_index + 1)
# Preload the next batch of data
batch_data = data_prefetcher.next()
# After training a batch of data, add 1 to the number of data batches to ensure that the
# terminal print data normally
batch_index += 1
# print metrics
progress.display_summary()
if mode == "Valid" or mode == "Test":
writer.add_scalar(f"{mode}/PSNR", psnres.avg, epoch + 1)
writer.add_scalar(f"{mode}/SSIM", ssimes.avg, epoch + 1)
else:
raise ValueError("Unsupported mode, please use `Valid` or `Test`.")
return psnres.avg, ssimes.avg
if __name__ == "__main__":
main()