-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwake_sleep.py
204 lines (154 loc) · 5.05 KB
/
wake_sleep.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
import torch
from torch import nn, optim
from torch.nn import functional as F
import numpy as np
import time
import utils
from tqdm import tqdm
import random
WS_TRAIN_LOG_INFO = [
('Train Loss', 'train_loss', 'nc'),
('Val Loss', 'val_loss', 'nc'),
]
class WSDataGen:
def __init__(
self,
domain,
gen_ex,
pbest,
):
args = domain.args
self.args = args
self.domain = domain
self.batch_size = args.os_batch_size
self.keys = []
self.data = []
for keys, (_, d) in pbest.data.items():
self.keys.append(keys)
self.data.append(d)
self.train_size = len(self.keys)
with torch.no_grad():
self.ex_data = gen_ex.make_os_batch(self.data, self.args)
def train_iter(self):
inds = torch.randperm(len(self.data))
while len(inds) > 0:
binds = inds[:self.batch_size]
inds = inds[self.batch_size:]
with torch.no_grad():
g_batch = {
k: v[binds].to(self.domain.device) for k,v in
self.ex_data.items() if k != 'vdata'
}
g_batch['vdata'] = len(binds)
yield g_batch
def make_ws_gens(
domain, gen_model, train_pbest, val_pbest
):
gen_model, ge = train_gen_model(
domain, gen_model, train_pbest, val_pbest
)
with torch.no_grad():
print("Sampling gen model")
gen_data = sample_ws_gens(domain, gen_model)
images = []
for g in gen_data[:50]:
img = gen_model.ex.execute(' '.join(g))
images.append(img)
num_rows = 5
if domain.args.num_write > 0:
try:
domain.executor.render_group(
images,
name=f'{domain.args.ws_save_path}/drm_render_{gen_model.gen_epoch}',
rows=num_rows
)
except Exception as e:
utils.log_print("Failed to save dream images with {e}", domain.args)
gen_model.gen_epoch += 1
return gen_model, gen_data, ge
def train_gen_model(
domain, gen_model, train_pbest, val_pbest
):
args = domain.args
path = args.ws_save_path
epochs = args.epochs
train_gen = WSDataGen(
domain,
gen_model.ex,
train_pbest,
)
val_gen = WSDataGen(
domain,
gen_model.ex,
val_pbest
)
opt = optim.Adam(
gen_model.parameters(),
lr=args.lr
)
best_test_metric = 100.
utils.save_model(gen_model.state_dict(), f"{path}/best_gen_dict.pt")
patience = args.gen_train_patience
num_worse = 0
for epoch in range(epochs):
start = time.time()
train_losses = []
val_losses = []
gen_model.train()
for batch in train_gen.train_iter():
loss, _ = gen_model.model_train_batch(batch)
opt.zero_grad()
loss.backward()
opt.step()
train_losses.append(loss.item())
gen_model.eval()
with torch.no_grad():
for batch in val_gen.train_iter():
loss, _ = gen_model.model_train_batch(batch)
val_losses.append(loss.item())
eval_res = {
'train_loss': torch.tensor(train_losses).float().mean().item(),
'val_loss': torch.tensor(val_losses).float().mean().item(),
'nc': 1.0
}
results = utils.print_results(
WS_TRAIN_LOG_INFO,
eval_res,
args,
ret_early=True
)
## EVAL
METRIC = eval_res['val_loss']
if METRIC >= best_test_metric:
num_worse += 1
else:
num_worse = 0
best_test_metric = METRIC
utils.save_model(gen_model.state_dict(), f"{path}/best_gen_dict.pt")
# early stopping on validation set
if num_worse >= patience:
# load the best model and stop training
gen_model.load_state_dict(torch.load(f"{path}/best_gen_dict.pt"))
break
end = time.time()
utils.log_print(
f"Epoch {epoch}/{epochs} => Train / Val : {round(eval_res['train_loss'], 3)} / {round(eval_res['val_loss'], 3)} "
f"| {end-start}"
,args
)
return gen_model, epochs
def sample_ws_gens(domain, gen_model):
gen_data = []
pbar = tqdm(total = domain.args.ws_train_size)
while len(gen_data) < domain.args.ws_train_size:
batch_size = domain.args.os_batch_size
try:
samples = gen_model.gen_sample_progs(
batch_size
)
except Exception as e:
utils.log_print(f"FAILED WAKE SLEEP batch with {e}", domain.args)
continue
gen_data += samples
pbar.update(len(samples))
return gen_data[:domain.args.ws_train_size]