train.py 2.4 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
#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
L
lvmengsi 已提交
27
import trainer
L
lvmengsi 已提交
28 29 30


def train(cfg):
L
lvmengsi 已提交
31 32 33 34 35 36 37

    MODELS = [
        "CGAN", "DCGAN", "Pix2pix", "CycleGAN", "StarGAN", "AttGAN", "STGAN"
    ]
    if cfg.model_net not in MODELS:
        raise NotImplementedError("{} is not support!".format(cfg.model_net))

L
lvmengsi 已提交
38
    reader = data_reader(cfg)
L
lvmengsi 已提交
39 40

    if cfg.model_net in ['CycleGAN']:
L
lvmengsi 已提交
41 42 43
        a_reader, b_reader, a_reader_test, b_reader_test, batch_num = reader.make_data(
        )
    else:
L
lvmengsi 已提交
44
        if cfg.dataset in ['mnist']:
L
lvmengsi 已提交
45 46 47 48
            train_reader = reader.make_data()
        else:
            train_reader, test_reader, batch_num = reader.make_data()

L
lvmengsi 已提交
49
    if cfg.model_net in ['CGAN', 'DCGAN']:
L
lvmengsi 已提交
50
        if cfg.dataset != 'mnist':
L
lvmengsi 已提交
51 52 53 54 55
            raise NotImplementedError("CGAN/DCGAN only support MNIST now!")
        model = trainer.__dict__[cfg.model_net](cfg, train_reader)
    elif cfg.model_net in ['CycleGAN']:
        model = trainer.__dict__[cfg.model_net](
            cfg, a_reader, b_reader, a_reader_test, b_reader_test, batch_num)
L
lvmengsi 已提交
56
    else:
L
lvmengsi 已提交
57 58
        model = trainer.__dict__[cfg.model_net](cfg, train_reader, test_reader,
                                                batch_num)
L
lvmengsi 已提交
59 60 61 62 63 64 65

    model.build_model()


if __name__ == "__main__":
    cfg = config.parse_args()
    config.print_arguments(cfg)
L
lvmengsi 已提交
66
    utility.check_gpu(cfg.use_gpu)
L
lvmengsi 已提交
67 68
    if cfg.profile:
        if cfg.use_gpu:
L
lvmengsi 已提交
69 70
            with fluid.profiler.profiler('All', 'total',
                                         '/tmp/profile') as prof:
L
lvmengsi 已提交
71 72
                train(cfg)
        else:
L
lvmengsi 已提交
73
            with fluid.profiler.profiler("CPU", sorted_key='total') as cpuprof:
L
lvmengsi 已提交
74 75 76
                train(cfg)
    else:
        train(cfg)