train.py 3.1 KB
Newer Older
L
lvmengsi 已提交
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
#copyright (c) 2019 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.

from __future__ import division
from __future__ import print_function

from util import config, utility
from data_reader import data_reader
import os
import sys
import six
import time
import numpy as np
import paddle
import paddle.fluid as fluid


def train(cfg):
    reader = data_reader(cfg)
    if cfg.model_net == 'CycleGAN':
        a_reader, b_reader, a_reader_test, b_reader_test, batch_num = reader.make_data(
        )
Z
zhumanyu 已提交
34 35
    elif cfg.model_net == 'Pix2pix':
        train_reader, test_reader, batch_num = reader.make_data()
Z
zhumanyu 已提交
36 37
    elif cfg.model_net == 'StarGAN':
        train_reader, test_reader, batch_num = reader.make_data()
L
lvmengsi 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    else:
        if cfg.dataset == 'mnist':
            train_reader = reader.make_data()
        else:
            train_reader, test_reader, batch_num = reader.make_data()

    if cfg.model_net == 'CGAN':
        from trainer.CGAN import CGAN
        if cfg.dataset != 'mnist':
            raise NotImplementedError('CGAN only support mnist now!')
        model = CGAN(cfg, train_reader)
    elif cfg.model_net == 'DCGAN':
        from trainer.DCGAN import DCGAN
        if cfg.dataset != 'mnist':
            raise NotImplementedError('DCGAN only support mnist now!')
        model = DCGAN(cfg, train_reader)
    elif cfg.model_net == 'CycleGAN':
        from trainer.CycleGAN import CycleGAN
        model = CycleGAN(cfg, a_reader, b_reader, a_reader_test, b_reader_test,
                         batch_num)
Z
zhumanyu 已提交
58 59 60
    elif cfg.model_net == 'Pix2pix':
        from trainer.Pix2pix import Pix2pix
        model = Pix2pix(cfg, train_reader, test_reader, batch_num)
Z
zhumanyu 已提交
61 62 63
    elif cfg.model_net == 'StarGAN':
        from trainer.StarGAN import StarGAN
        model = StarGAN(cfg, train_reader, test_reader, batch_num)
L
lvmengsi 已提交
64 65 66 67 68 69
    elif cfg.model_net == 'AttGAN':
        from trainer.AttGAN import AttGAN
        model = AttGAN(cfg, train_reader, test_reader, batch_num)
    elif cfg.model_net == 'STGAN':
        from trainer.STGAN import STGAN
        model = STGAN(cfg, train_reader, test_reader, batch_num)
L
lvmengsi 已提交
70 71 72 73 74 75 76 77 78
    else:
        pass

    model.build_model()


if __name__ == "__main__":
    cfg = config.parse_args()
    config.print_arguments(cfg)
L
lvmengsi 已提交
79
    #assert cfg.load_size >= cfg.crop_size, "Load Size CANNOT less than Crop Size!"
L
lvmengsi 已提交
80 81 82 83 84 85 86 87 88
    if cfg.profile:
        if cfg.use_gpu:
            with profiler.profiler('All', 'total', '/tmp/profile') as prof:
                train(cfg)
        else:
            with profiler.profiler("CPU", sorted_key='total') as cpuprof:
                train(cfg)
    else:
        train(cfg)