forked from PaddlePaddle/PaddleGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
579 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
total_iters: 1000000 | ||
output_dir: output_dir | ||
# tensor range for function tensor2img | ||
min_max: | ||
(0., 255.) | ||
|
||
model: | ||
name: DRN | ||
generator: | ||
name: DRNGenerator | ||
scale: (2, 4) | ||
n_blocks: 30 | ||
n_feats: 16 | ||
n_colors: 3 | ||
rgb_range: 255 | ||
negval: 0.2 | ||
pixel_criterion: | ||
name: L1Loss | ||
|
||
dataset: | ||
train: | ||
name: SRDataset | ||
gt_folder: data/DIV2K/DIV2K_train_HR_sub | ||
lq_folder: data/DIV2K/DIV2K_train_LR_bicubic/X4_sub | ||
num_workers: 4 | ||
batch_size: 8 | ||
scale: 4 | ||
preprocess: | ||
- name: LoadImageFromFile | ||
key: lq | ||
- name: LoadImageFromFile | ||
key: gt | ||
- name: Transforms | ||
input_keys: [lq, gt] | ||
output_keys: [lq, lqx2, gt] | ||
pipeline: | ||
- name: SRPairedRandomCrop | ||
gt_patch_size: 384 | ||
scale: 4 | ||
scale_list: True | ||
keys: [image, image] | ||
- name: PairedRandomHorizontalFlip | ||
keys: [image, image, image] | ||
- name: PairedRandomVerticalFlip | ||
keys: [image, image, image] | ||
- name: PairedRandomTransposeHW | ||
keys: [image, image, image] | ||
- name: Transpose | ||
keys: [image, image, image] | ||
- name: Normalize | ||
mean: [0., 0., 0.] | ||
std: [1., 1., 1.] | ||
keys: [image, image, image] | ||
test: | ||
name: SRDataset | ||
gt_folder: data/DIV2K/val_set14/Set14 | ||
lq_folder: data/DIV2K/val_set14/Set14_bicLRx4 | ||
scale: 4 | ||
preprocess: | ||
- name: LoadImageFromFile | ||
key: lq | ||
- name: LoadImageFromFile | ||
key: gt | ||
- name: Transforms | ||
input_keys: [lq, gt] | ||
pipeline: | ||
- name: Transpose | ||
keys: [image, image] | ||
- name: Normalize | ||
mean: [0., 0., 0.] | ||
std: [1., 1., 1.] | ||
keys: [image, image] | ||
|
||
lr_scheduler: | ||
name: CosineAnnealingRestartLR | ||
learning_rate: 0.0001 | ||
periods: [1000000] | ||
restart_weights: [1] | ||
eta_min: !!float 1e-7 | ||
|
||
optimizer: | ||
optimG: | ||
name: Adam | ||
net_names: | ||
- generator | ||
weight_decay: 0.0 | ||
beta1: 0.9 | ||
beta2: 0.999 | ||
optimD: | ||
name: Adam | ||
net_names: | ||
- dual_model_0 | ||
- dual_model_1 | ||
weight_decay: 0.0 | ||
beta1: 0.9 | ||
beta2: 0.999 | ||
|
||
validate: | ||
interval: 5000 | ||
save_img: false | ||
|
||
metrics: | ||
psnr: # metric name, can be arbitrary | ||
name: PSNR | ||
crop_border: 4 | ||
test_y_channel: True | ||
ssim: | ||
name: SSIM | ||
crop_border: 4 | ||
test_y_channel: True | ||
|
||
log_config: | ||
interval: 10 | ||
visiual_interval: 500 | ||
|
||
snapshot_config: | ||
interval: 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
# 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 paddle | ||
|
||
from .generators.builder import build_generator | ||
from .discriminators.builder import build_discriminator | ||
from .generators.drn import DownBlock | ||
from .sr_model import BaseSRModel | ||
from .builder import MODELS | ||
|
||
from .criterions import build_criterion | ||
from ..modules.init import init_weights | ||
from ..utils.visual import tensor2img | ||
|
||
|
||
@MODELS.register() | ||
class DRN(BaseSRModel): | ||
""" | ||
This class implements the DRN model. | ||
DRN paper: https://arxiv.org/pdf/1809.00219.pdf | ||
""" | ||
def __init__(self, | ||
generator, | ||
lq_loss_weight=0.1, | ||
dual_loss_weight=0.1, | ||
discriminator=None, | ||
pixel_criterion=None, | ||
perceptual_criterion=None, | ||
gan_criterion=None, | ||
params=None): | ||
"""Initialize the DRN class. | ||
Args: | ||
generator (dict): config of generator. | ||
discriminator (dict): config of discriminator. | ||
pixel_criterion (dict): config of pixel criterion. | ||
perceptual_criterion (dict): config of perceptual criterion. | ||
gan_criterion (dict): config of gan criterion. | ||
""" | ||
super(DRN, self).__init__(generator) | ||
self.lq_loss_weight = lq_loss_weight | ||
self.dual_loss_weight = dual_loss_weight | ||
self.params = params | ||
self.nets['generator'] = build_generator(generator) | ||
init_weights(self.nets['generator']) | ||
negval = generator.negval | ||
n_feats = generator.n_feats | ||
n_colors = generator.n_colors | ||
self.scale = generator.scale | ||
|
||
for i in range(len(self.scale)): | ||
dual_model = DownBlock(negval, n_feats, n_colors, 2) | ||
self.nets['dual_model_' + str(i)] = dual_model | ||
init_weights(self.nets['dual_model_' + str(i)]) | ||
|
||
if discriminator: | ||
self.nets['discriminator'] = build_discriminator(discriminator) | ||
|
||
if pixel_criterion: | ||
self.pixel_criterion = build_criterion(pixel_criterion) | ||
|
||
if perceptual_criterion: | ||
self.perceptual_criterion = build_criterion(perceptual_criterion) | ||
|
||
if gan_criterion: | ||
self.gan_criterion = build_criterion(gan_criterion) | ||
|
||
def setup_input(self, input): | ||
self.lq = paddle.fluid.dygraph.to_variable(input['lq']) | ||
self.visual_items['lq'] = self.lq | ||
|
||
if isinstance(self.scale, (list, tuple)) and len( | ||
self.scale) == 2 and 'lqx2' in input: | ||
self.lqx2 = input['lqx2'] | ||
|
||
if 'gt' in input: | ||
self.gt = paddle.fluid.dygraph.to_variable(input['gt']) | ||
self.visual_items['gt'] = self.gt | ||
self.image_paths = input['lq_path'] | ||
|
||
def train_iter(self, optimizers=None): | ||
lr = [self.lq] | ||
|
||
if hasattr(self, 'lqx2'): | ||
lr.append(self.lqx2) | ||
|
||
hr = self.gt | ||
|
||
sr = self.nets['generator'](self.lq) | ||
|
||
sr2lr = [] | ||
|
||
for i in range(len(self.scale)): | ||
sr2lr_i = self.nets['dual_model_' + str(i)](sr[i - len(self.scale)]) | ||
sr2lr.append(sr2lr_i) | ||
|
||
# compute primary loss | ||
loss_primary = self.pixel_criterion(sr[-1], hr) | ||
for i in range(1, len(sr)): | ||
if self.lq_loss_weight > 0.0: | ||
loss_primary += self.pixel_criterion( | ||
sr[i - 1 - len(sr)], lr[i - len(sr)]) * self.lq_loss_weight | ||
|
||
# compute dual loss | ||
loss_dual = self.pixel_criterion(sr2lr[0], lr[0]) | ||
for i in range(1, len(self.scale)): | ||
if self.dual_loss_weight > 0.0: | ||
loss_dual += self.pixel_criterion(sr2lr[i], | ||
lr[i]) * self.dual_loss_weight | ||
|
||
loss_total = loss_primary + loss_dual | ||
|
||
optimizers['optimG'].clear_grad() | ||
optimizers['optimD'].clear_grad() | ||
loss_total.backward() | ||
optimizers['optimG'].step() | ||
optimizers['optimD'].step() | ||
|
||
self.losses['loss_promary'] = loss_primary | ||
self.losses['loss_dual'] = loss_dual | ||
self.losses['loss_total'] = loss_total | ||
|
||
def test_iter(self, metrics=None): | ||
self.nets['generator'].eval() | ||
with paddle.no_grad(): | ||
self.output = self.nets['generator'](self.lq)[-1] | ||
self.visual_items['output'] = self.output | ||
self.nets['generator'].train() | ||
|
||
out_img = [] | ||
gt_img = [] | ||
for out_tensor, gt_tensor in zip(self.output, self.gt): | ||
out_img.append(tensor2img(out_tensor, (0., 255.))) | ||
gt_img.append(tensor2img(gt_tensor, (0., 255.))) | ||
|
||
if metrics is not None: | ||
for metric in metrics.values(): | ||
metric.update(out_img, gt_img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.