-
Notifications
You must be signed in to change notification settings - Fork 8
/
train.py
executable file
·82 lines (66 loc) · 2.16 KB
/
train.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import argparse
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import matplotlib
matplotlib.use('Agg')
import tensorflow as tf
from models import *
from datasets import load_data, mnist, svhn
models = {
'vae': VAE,
'dcgan': DCGAN,
'improved': ImprovedGAN,
'resnet': ResNetGAN,
'began': BEGAN,
'wgan': WGAN,
'lsgan': LSGAN,
'cvae': CVAE,
'cvaegan': CVAEGAN
}
def main(_):
# Parsing arguments
parser = argparse.ArgumentParser(description='Training GANs or VAEs')
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--datasize', type=int, default=-1)
parser.add_argument('--epoch', type=int, default=200)
parser.add_argument('--batchsize', type=int, default=50)
parser.add_argument('--output', default='output')
parser.add_argument('--zdims', type=int, default=256)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--resume', type=str, default=None)
parser.add_argument('--testmode', action='store_true')
args = parser.parse_args()
# select gpu
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
# Make output direcotiry if not exists
if not os.path.isdir(args.output):
os.mkdir(args.output)
# Load datasets
if args.dataset == 'mnist':
datasets = mnist.load_data()
elif args.dataset == 'svhn':
datasets = svhn.load_data()
else:
datasets = load_data(args.dataset, args.datasize)
# Construct model
if args.model not in models:
raise Exception('Unknown model:', args.model)
model = models[args.model](
batchsize=args.batchsize,
input_shape=datasets.shape[1:],
attr_names=None or datasets.attr_names,
z_dims=args.zdims,
output=args.output,
resume=args.resume
)
if args.testmode:
model.test_mode = True
tf.set_random_seed(12345)
# Training loop
datasets.images = datasets.images.astype('float32') * 2.0 - 1.0
model.main_loop(datasets,
epochs=args.epoch)
if __name__ == '__main__':
tf.app.run(main)