-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
313 lines (263 loc) · 8.08 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Args should be imported before everything to cover https://discuss.pytorch.org/t/cuda-visible-device-is-of-no-use/10018
from utils.args import args
import numpy as np
import os
import sys
import torch
import torchvision
from PIL import Image
from progress.bar import Bar
from tensorboard_wrapper.tensorboard import Tensorboard as Board
from torch import nn, optim
from models.idm import IDM
from models.idm import train as train_idm
from models.idm import validation as validate_idm
from models.policy import Policy
from models.policy import train as train_policy
from models.policy import validation as validate_policy
from utils.utils import create_alpha_dataset
from utils.utils import domain
from utils.utils import policy_infer
from utils.utils import save_policy_model
# ARGS: GPU and Pretrained
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
environment = domain[args.domain]
# Tensorboard
parent_folder = "_".join(args.run_name.split('_')[:-1])
st_char = environment['name'][0].upper()
rest_char = environment['name'][1:]
if 'maze' in environment['name']:
env_name = f'{st_char}{rest_char}{args.maze_size}'
else:
env_name = f'{st_char}{rest_char}'
name = f'./checkpoint/alpha/{env_name}/{parent_folder}/{args.run_name}'
if os.path.exists(name) is False:
os.makedirs(name)
parent_folder = "_".join(args.run_name.split('_')[:-1])
path = f'./runs/alpha/{env_name}/{parent_folder}/{args.run_name}'
if os.path.exists(path) is False:
os.makedirs(path)
board = Board(name, path)
# Datasets
print('\nCreating PyTorch IDM Datasets')
print(f'Using dataset: {args.data_path} with batch size: {args.batch_size}')
get_idm_dataset = environment['idm_dataset']
idm_train, idm_validation = get_idm_dataset(
args.data_path,
args.batch_size,
downsample_size=5000,
reducted=args.reducted,
no_hit=args.no_hit
)
print('\nCreating PyTorch Policy Datasets')
print(f'Using dataset: {args.expert_path} with batch size: {args.policy_batch_size}')
get_policy_dataset = environment['policy_dataset']
policy_train, policy_validation = get_policy_dataset(
args.expert_path,
args.policy_batch_size,
maze_size=args.maze_size,
maze_type=args.maze_type,
)
# Model and action size
print('\nCreating Models')
action_dimension = environment['action']
inputs = environment['input_size'] * 2 if environment['input_size'] is not None else None
policy_model = Policy(
action_dimension,
net=args.encoder,
pretrained=args.pretrained,
input=environment['input_size']
)
idm_model = IDM(
action_dimension,
net=args.encoder,
pretrained=args.pretrained,
input=inputs
)
policy_model.to(device)
idm_model.to(device)
# Optimizer and loss
print('\nCreating Optimizer and Loss')
print(f'IDM learning rate: {args.lr}\nPolicy learning rate: {args.policy_lr}')
idm_lr = args.lr
idm_criterion = nn.CrossEntropyLoss()
idm_optimizer = optim.Adam(idm_model.parameters(), lr=idm_lr)
policy_lr = args.policy_lr
policy_criterion = nn.CrossEntropyLoss()
policy_optimizer = optim.Adam(policy_model.parameters(), lr=policy_lr)
# Learning rate decay
print('Setting up Learning Rate Decay function and Schedulers')
idm_lr_decay = lambda epoch: args.lr_decay_rate**epoch
policy_lr_decay = lambda epoch: args.policy_lr_decay_rate**epoch
idm_scheduler = optim.lr_scheduler.LambdaLR(idm_optimizer, idm_lr_decay)
policy_scheduler = optim.lr_scheduler.LambdaLR(policy_optimizer, policy_lr_decay)
# Train
print('Starting Train\n')
best_epoch_acc = 0
early_stop_count = 0
max_epochs = args.idm_epochs
max_iter = len(idm_train) + len(idm_validation) + len(policy_train) + len(policy_validation)
for epoch in range(max_epochs):
board.add_scalar('Learning Rate', idm_lr_decay(epoch - 1))
############################ IDM Train ############################
if args.verbose is True:
bar = Bar(f'EPOCH {epoch:3d}', max=max_iter, suffix='%(percent).1f%% - %(eta)ds')
batch_acc = []
batch_loss = []
for itr, mini_batch in enumerate(idm_train):
loss, acc = train_idm(
idm_model,
mini_batch,
idm_criterion,
idm_optimizer,
device,
board,
)
batch_acc.append(acc)
batch_loss.append(loss.item())
if args.verbose is True:
bar.next()
if args.debug:
break
############################ IDM Validation ############################
board.add_scalars(
train=True,
IDM_Loss=np.mean(batch_loss),
IDM_Accuracy=np.mean(batch_acc)
)
batch_acc = []
for itr, sample_batched in enumerate(idm_validation):
acc = validate_idm(
idm_model,
mini_batch,
device,
board,
)
batch_acc.append(acc)
if args.verbose is True:
bar.next()
if args.debug:
break
board.add_scalars(
train=False,
IDM_Accuracy=np.mean(batch_acc)
)
############################ Policy Train ############################
batch_acc = []
batch_loss = []
for itr, mini_batch in enumerate(policy_train):
loss, acc = train_policy(
policy_model,
idm_model,
mini_batch,
policy_criterion,
policy_optimizer,
device,
board
)
batch_acc.append(acc)
batch_loss.append(loss.item())
if args.verbose is True:
bar.next()
if args.debug:
break
board.add_scalars(
train=True,
Policy_Loss=np.mean(batch_loss),
Policy_Accuracy=np.mean(batch_acc)
)
############################ Policy Validation ############################
batch_acc = []
for itr, mini_batch in enumerate(policy_validation):
acc = validate_policy(
policy_model,
idm_model,
mini_batch,
device,
board
)
batch_acc.append(acc)
bar.next()
board.add_scalars(
train=False,
Policy_Accuracy=np.mean(batch_acc)
)
############################ Policy Eval ############################
if args.verbose is True:
bar = Bar(
f'VALID Sample {epoch:3d}',
max=100,
suffix='%(percent).1f%% - %(eta)ds'
)
else:
bar = None
if args.debug:
amount = 1
else:
amount = 100
infer, performance, solved = policy_infer(
policy_model,
dataloader=policy_train,
device=device,
domain=environment,
size=(args.maze_size, args.maze_size),
bar=bar,
episodes=amount,
gif=False,
alpha_location=args.alpha,
dataset=True,
)
board.add_scalars(
train=False,
AER_Sample=infer,
Sample_Solved=solved,
Performance_Sample=performance
)
if args.verbose is True:
bar = Bar(
f'VALID Random {epoch:3d}',
max=10,
suffix='%(percent).1f%% - %(eta)ds'
)
else:
bar = None
if args.debug:
amount = 1
else:
amount = 10
infer_random, _, random_solved = policy_infer(
policy_model,
dataloader=policy_train,
device=device,
domain=environment,
random=True,
size=(args.maze_size, args.maze_size),
episodes=amount,
bar=bar,
gif=False,
)
board.add_scalars(
train=False,
AER_Random=infer_random,
Random_Solved=random_solved
)
print(f'\nSample Infer {infer}\tRandom Infer {infer_random}\n')
print(f'Using dataset: {args.alpha} with batch size: {args.batch_size}')
create_alpha = environment['alpha']
create_alpha(
args.data_path,
args.alpha,
environment,
solved,
)
idm_train, idm_validation = get_idm_dataset(
args.alpha,
args.batch_size,
downsample_size=np.inf,
reducted=args.reducted,
replace=False,
sampling=False,
no_hit=args.no_hit,
)
############################ Necessary updates ############################
board.step()