-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
56 lines (41 loc) · 1.43 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
import numpy as np
import sys
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
sys.path.append('./utils')
sys.path.append('./networks')
import logging
import logging_utils
import GANbuild
import parameters
configtag = sys.argv[1]
run_num = sys.argv[2]
baseDir = './expts/'+configtag+'/'
expDir = baseDir+'run'+str(run_num)+'/'
if not os.path.isdir(baseDir):
os.mkdir(baseDir)
if not os.path.isdir(expDir):
os.mkdir(expDir)
os.mkdir(expDir+'models')
else:
print("Experiment directory %s already exists, exiting"%expDir)
sys.exit()
#Set up logger
logging_utils.config_logger(log_level=logging.INFO)
logging_utils.log_to_file(logger_name=None, log_filename=expDir+'train.log')
# Build GAN
GAN = GANbuild.DCGAN(configtag, expDir)
Nepochs = GAN.Nepochs
Nbatches = GAN.n_imgs // GAN.batchsize
for epoch in np.arange(GAN.start, Nepochs+GAN.start):
logging.info("| ******************************* Epoch %d of %d ******************************* |"%(epoch+1, Nepochs+GAN.start))
shuff_idxs = np.random.permutation(GAN.n_imgs)
GAN.train_epoch(shuff_idxs, Nbatches, epoch)
if (epoch+1)%10==0:
GAN.genrtor.save_weights(GAN.expDir+'models/g_cosmo%04d.h5'%(epoch))
GAN.discrim.save_weights(GAN.expDir+'models/d_cosmo%04d.h5'%(epoch))
GAN.genrtor.save_weights(GAN.expDir+'models/g_cosmo_last.h5')
GAN.discrim.save_weights(GAN.expDir+'models/d_cosmo_last.h5')
logging.info('DONE')