-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
174 lines (144 loc) · 7.07 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""An example of training SPIRAL with A3C.
"""
import argparse
import logging
import os
import chainer
import yaml
from chainerrl import experiments, misc
from chainerrl.optimizers.nonbias_weight_decay import NonbiasWeightDecay
from chainer_spiral.agents import SPIRAL, SpiralStepHook
from chainer_spiral.dataset import (EMnistDataset, JikeiDataset, MnistDataset, QuickdrawDataset,
ToyDataset)
from chainer_spiral.environments import MyPaintEnv, ToyEnv
from chainer_spiral.models import (SpiralDiscriminator, SpiralModel,
SpiralToyDiscriminator, SpiralToyModel)
from chainer_spiral.utils.arg_utils import print_args
# This prevents numpy from using multiple threads
os.environ['OMP_NUM_THREADS'] = '1' # NOQA
def main():
parser = argparse.ArgumentParser()
parser.add_argument('config', help='YAML config file')
parser.add_argument('outdir', type=str, help='directory to put training log')
parser.add_argument('--profile', action='store_true')
parser.add_argument('--load', type=str, default='')
parser.add_argument('--logger_level', type=int, default=logging.INFO)
args = parser.parse_args()
print_args(args)
# init a logger
logging.basicConfig(level=args.logger_level)
# load yaml config file
with open(args.config) as f:
config = yaml.load(f)
# set random seed
misc.set_random_seed(config['seed'])
# create directory to put the results
args.outdir = experiments.prepare_output_dir(args, args.outdir)
# save config file to outdir
with open(os.path.join(args.outdir, 'config.yaml'), 'w') as f:
yaml.dump(config, f, indent=4, default_flow_style=False)
# define func to create env, target data sampler, and models
if config['problem'] == 'toy':
assert config['imsize'] == 3, 'invalid imsize'
assert config['in_channel'] == 1, 'invalid in_channel'
def make_env(process_idx, test):
env = ToyEnv(config['imsize'])
return env
gen = SpiralToyModel(imsize, config['conditional'])
dis = SpiralToyDiscriminator(imsize, config['conditional'])
if config['conditional']:
train_patterns = [(1, 4, 7), (0, 1, 2), (3, 4, 5), (2, 5, 8)]
test_patterns = [(6, 7, 8)]
else:
train_patterns = [(1, 4, 7)]
test_patterns = train_patterns
dataset = ToyDataset(config['imsize'], train_patterns, test_patterns)
else:
# my paint env
def make_env(process_idx, test):
env = MyPaintEnv(max_episode_steps=config['max_episode_steps'],
imsize=config['imsize'],
pos_resolution=config['pos_resolution'],
brush_info_file=config['brush_info_file'])
return env
# generator
gen = SpiralModel(config['imsize'], config['conditional'])
dis = SpiralDiscriminator(config['imsize'], config['conditional'])
if config['problem'] == 'mnist':
single_label = config['mnist_target_label'] is not None
dataset = MnistDataset(config['imsize'], single_label, config['mnist_target_label'],
config['mnist_binarization'])
elif config['problem'] == 'emnist':
dataset = EMnistDataset(config['emnist_gz_images'], config['emnist_gz_labels'],
config['emnist_single_label'])
elif config['problem'] == 'jikei':
dataset = JikeiDataset(config['jikei_npz'])
elif config['problem'] == 'quickdraw':
dataset = QuickdrawDataset(config['quickdraw_npz'])
else:
raise NotImplementedError()
# initialize optimizers
gen_opt = chainer.optimizers.Adam(alpha=config['lr'], beta1=0.5)
dis_opt = chainer.optimizers.Adam(alpha=config['lr'], beta1=0.5)
gen_opt.setup(gen)
dis_opt.setup(dis)
gen_opt.add_hook(chainer.optimizer.GradientClipping(40))
dis_opt.add_hook(chainer.optimizer.GradientClipping(40))
if config['weight_decay'] > 0:
gen_opt.add_hook(NonbiasWeightDecay(config['weight_decay']))
dis_opt.add_hook(NonbiasWeightDecay(config['weight_decay']))
# init an spiral agent
agent = SPIRAL(generator=gen,
discriminator=dis,
gen_optimizer=gen_opt,
dis_optimizer=dis_opt,
dataset=dataset,
conditional=config['conditional'],
reward_mode=config['reward_mode'],
imsize=config['imsize'],
max_episode_steps=config['max_episode_steps'],
rollout_n=config['rollout_n'],
gamma=config['gamma'],
beta=config['beta'],
gp_lambda=config['gp_lambda'],
lambda_R=config['lambda_R'],
staying_penalty=config['staying_penalty'],
empty_drawing_penalty=config['empty_drawing_penalty'],
n_save_final_obs_interval=config['n_save_final_obs_interval'],
outdir=args.outdir)
# load from a snapshot
if args.load:
agent.load(args.load)
# training mode
max_episode_len = config['max_episode_steps'] * config['rollout_n']
steps = config['processes'] * config['n_update'] * max_episode_len
save_interval = config['processes'] * config['n_save_interval'] * max_episode_len
eval_interval = config['processes'] * config['n_eval_interval'] * max_episode_len
step_hook = SpiralStepHook(config['max_episode_steps'], save_interval, args.outdir)
if config['processes'] == 1:
# single process for easy to debug
agent.process_idx = 0
env = make_env(0, False)
experiments.train_agent_with_evaluation(agent=agent,
outdir=args.outdir,
env=env,
steps=steps,
eval_n_runs=config['eval_n_runs'],
eval_interval=eval_interval,
max_episode_len=max_episode_len,
step_hooks=[step_hook],
save_best_so_far_agent=False)
else:
experiments.train_agent_async(agent=agent,
outdir=args.outdir,
processes=config['processes'],
make_env=make_env,
profile=args.profile,
steps=steps,
eval_n_runs=config['eval_n_runs'],
eval_interval=eval_interval,
max_episode_len=max_episode_len,
global_step_hooks=[step_hook],
save_best_so_far_agent=False)
if __name__ == '__main__':
main()