mpr_model.py 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   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
import paddle.nn as nn

from .builder import MODELS
from .base_model import BaseModel
from .generators.builder import build_generator
from .criterions.builder import build_criterion
from ..modules.init import reset_parameters, init_weights
23
from ..utils.visual import tensor2img
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


@MODELS.register()
class MPRModel(BaseModel):
    """MPR Model.

    Paper: MPR: Multi-Stage Progressive Image Restoration (CVPR 2021).
    https://arxiv.org/abs/2102.02808
    """
    def __init__(self, generator, char_criterion=None, edge_criterion=None):
        """Initialize the MPR class.

        Args:
            generator (dict): config of generator.
            char_criterion (dict): config of char criterion.
            edge_criterion (dict): config of edge criterion.
        """
        super(MPRModel, self).__init__(generator)
        self.current_iter = 1

        self.nets['generator'] = build_generator(generator)
        init_weights(self.nets['generator'])

        if char_criterion:
            self.char_criterion = build_criterion(char_criterion)
        if edge_criterion:
            self.edge_criterion = build_criterion(edge_criterion)

    def setup_input(self, input):
        self.target = input[0]
54
        self.lq = input[1]
55 56 57 58

    def train_iter(self, optims=None):
        optims['optim'].clear_gradients()

59
        restored = self.nets['generator'](self.lq)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

        loss_char = []
        loss_edge = []

        for i in range(len(restored)):
            loss_char.append(self.char_criterion(restored[i], self.target))
            loss_edge.append(self.edge_criterion(restored[i], self.target))
        loss_char = paddle.stack(loss_char)
        loss_edge = paddle.stack(loss_edge)
        loss_char = paddle.sum(loss_char)
        loss_edge = paddle.sum(loss_edge)

        loss = (loss_char) + (0.05 * loss_edge)

        loss.backward()
        optims['optim'].step()
        self.losses['loss'] = loss.numpy()

    def forward(self):
        pass
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    def test_iter(self, metrics=None):
        self.nets['generator'].eval()
        with paddle.no_grad():
            self.output = self.nets['generator'](self.lq)[0]
            self.visual_items['output'] = self.output
        self.nets['generator'].train()

        out_img = []
        gt_img = []
        for out_tensor, gt_tensor in zip(self.output, self.target):
            out_img.append(tensor2img(out_tensor, (0., 1.)))
            gt_img.append(tensor2img(gt_tensor, (0., 1.)))

        if metrics is not None:
            for metric in metrics.values():
                metric.update(out_img, gt_img)