srgan_model.py 2.0 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

L
LielinJiang 已提交
15 16 17
from collections import OrderedDict
import paddle
import paddle.nn as nn
L
LielinJiang 已提交
18

L
LielinJiang 已提交
19 20 21 22 23
from .generators.builder import build_generator
from .base_model import BaseModel
from .losses import GANLoss
from .builder import MODELS

L
LielinJiang 已提交
24

L
LielinJiang 已提交
25 26 27 28 29
@MODELS.register()
class SRGANModel(BaseModel):
    def __init__(self, cfg):
        super(SRGANModel, self).__init__(cfg)

L
LielinJiang 已提交
30
        # define networks
L
LielinJiang 已提交
31
        self.model_names = ['G']
L
LielinJiang 已提交
32

L
LielinJiang 已提交
33 34 35
        self.netG = build_generator(cfg.model.generator)
        self.visual_names = ['LQ', 'GT', 'fake_H']

L
LielinJiang 已提交
36 37 38
        # TODO: support srgan train.
        if False:
            # self.netD = build_discriminator(cfg.model.discriminator)
L
LielinJiang 已提交
39
            self.netG.train()
L
LielinJiang 已提交
40
            # self.netD.train()
L
LielinJiang 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

    def set_input(self, input):
        """Unpack input data from the dataloader and perform necessary pre-processing steps.

        Parameters:
            input (dict): include the data itself and its metadata information.

        The option 'direction' can be used to swap images in domain A and domain B.
        """

        # AtoB = self.opt.dataset.train.direction == 'AtoB'
        if 'A' in input:
            self.LQ = paddle.to_tensor(input['A'])
        if 'B' in input:
            self.GT = paddle.to_tensor(input['B'])
        if 'A_paths' in input:
            self.image_paths = input['A_paths']

    def forward(self):
        self.fake_H = self.netG(self.LQ)

    def optimize_parameters(self, step):
        pass