forked from mila-iqia/babyai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_concepts_GoToImpUnlock.py
313 lines (246 loc) · 11.6 KB
/
generate_concepts_GoToImpUnlock.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
import gym
from babyai.levels.levelgen import *
register_levels('iclr19_levels', globals())
env = gym.make('BabyAI-GoToImpUnlock-v0')
observation_space = env.observation_space
action_space = env.action_space
import copy
import gym
import time
import datetime
import numpy as np
import sys
import itertools
import torch
from babyai.evaluate import batch_evaluate
import babyai.utils as utils
from babyai.rl import DictList
from babyai.model import ACModel
import multiprocessing
import os
import json
import logging
import numpy
import tqdm
import pickle
d_path = "/data/graceduansu/demos/BabyAI-GoToImpUnlock-v0"
demos_path = utils.get_demos_path(d_path, None, None, valid=False)
train_demos = utils.load_demos(demos_path)
rand_inds = np.random.choice(len(train_demos), size=6400)
some_demos = [train_demos[i] for i in rand_inds]
demo_list = utils.demos.transform_demos(some_demos)
from gym_minigrid.minigrid import *
def get_concept_data_from_batch(batch, batch_concept_inds, concept_path):
cuda6 = torch.device('cuda:6')
flat_batch = []
inds = [0]
flat_batch_concept_inds = [(0,0)]
for j in tqdm.tqdm(range(len(batch))):
demo = batch[j]
flat_batch += demo
flat_batch_concept_inds.append((inds[-1] + batch_concept_inds[j][0],
inds[-1] + batch_concept_inds[j][1]))
inds.append(inds[-1] + len(demo))
flat_batch = np.array(flat_batch)
inds = inds[:-1]
flat_batch_concept_inds = flat_batch_concept_inds[:-1]
flat_batch_concept_inds = flat_batch_concept_inds[1:]
mask = np.ones([len(flat_batch)], dtype=np.float64)
mask[inds] = 0
mask = torch.tensor(mask, device=cuda6, dtype=torch.float).unsqueeze(1)
# get batch with only concepts
flat_concept_batch = []
concept_inds = [0]
for pair in flat_batch_concept_inds:
concept_data = flat_batch[pair[0]:pair[1]]
flat_concept_batch.extend(concept_data)
concept_inds.append(concept_inds[-1] + len(concept_data))
print(concept_inds[-10:])
concept_inds = [concept_inds[i] for i in range(len(concept_inds)) if concept_inds[i] < len(flat_concept_batch)]
print('--------------------------------------------------------------------')
print(concept_inds[-10:])
concept_mask = np.ones([len(flat_concept_batch)], dtype=np.float64)
concept_mask[concept_inds] = 0
concept_mask = torch.tensor(concept_mask, device=cuda6, dtype=torch.float).unsqueeze(1)
# Observations, true action, values and done for each of the stored demostration
obss, action_true, done = flat_batch[:, 0], flat_batch[:, 1], flat_batch[:, 2]
episode_ids = np.zeros(len(flat_batch))
inds_copy = inds.copy()
# Loop terminates when every observation in the flat_batch has been handled
while True:
# taking observations and done located at inds
done_step = done[inds]
episode_ids[inds] = range(len(inds))
# Updating inds, by removing those indices corresponding to which the demonstrations have finished
inds = inds[:len(inds) - sum(done_step)]
if len(inds) == 0:
break
# Incrementing the remaining indices
inds = [index + 1 for index in inds]
# make concept_episode_ids
flat_concept_batch = np.array(flat_concept_batch)
obss, action_true, done = flat_concept_batch[:, 0], flat_concept_batch[:, 1], flat_concept_batch[:, 2]
concept_episode_ids = np.zeros(len(flat_concept_batch))
concept_inds_copy = concept_inds.copy()
# Loop terminates when every observation in the flat_batch has been handled
while True:
# taking observations and done located at inds
done_step = done[concept_inds]
concept_episode_ids[concept_inds] = range(len(concept_inds))
# Updating inds, by removing those indices corresponding to which the demonstrations have finished
concept_inds = concept_inds[:len(concept_inds) - sum(done_step)]
if len(concept_inds) == 0:
break
# Incrementing the remaining indices
concept_inds = [index + 1 for index in concept_inds]
# {flat_batch, mask, episode_ids, inds_copy,
# flat_batch_concept_inds,
# concept_mask, concept_episode_ids, concept_inds} <-- inds after batch only has concepts
batch_dict = {'flat_batch': flat_batch,
'mask': mask,
'episode_ids': episode_ids,
'inds': inds_copy,
'batch_concept_inds': batch_concept_inds,
'flat_batch_concept_inds': flat_batch_concept_inds,
'concept_mask': concept_mask,
'concept_episode_ids': concept_episode_ids,
'concept_inds': concept_inds_copy}
path = concept_path + str(i)
with open(path, 'wb') as handle:
pickle.dump(batch_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
def get_concept3_data_from_batch(batch, batch_concept_inds):
cuda6 = torch.device('cuda:3')
flat_batch = []
inds = [0]
flat_batch_concept_inds = [(0,0)]
for j in tqdm.tqdm(range(len(batch))):
demo = batch[j]
flat_batch += demo
flat_batch_concept_inds.append((inds[-1] + batch_concept_inds[j][0],
inds[-1] + batch_concept_inds[j][1]))
flat_batch_concept_inds.append((inds[-1] + batch_concept_inds[j][2],
inds[-1] + batch_concept_inds[j][3]))
inds.append(inds[-1] + len(demo))
flat_batch = np.array(flat_batch)
inds = inds[:-1]
flat_batch_concept_inds = flat_batch_concept_inds[:-1]
flat_batch_concept_inds = flat_batch_concept_inds[1:]
mask = np.ones([len(flat_batch)], dtype=np.float64)
mask[inds] = 0
mask = torch.tensor(mask, device=cuda6, dtype=torch.float).unsqueeze(1)
# get batch with only concepts
flat_concept_batch = []
concept_inds = [0]
for pair in flat_batch_concept_inds:
concept_data = flat_batch[pair[0]:pair[1]]
flat_concept_batch.extend(concept_data)
concept_inds.append(concept_inds[-1] + len(concept_data))
print(concept_inds[-10:])
concept_inds = [concept_inds[i] for i in range(len(concept_inds)) if concept_inds[i] < len(flat_concept_batch)]
print('--------------------------------------------------------------------')
print(concept_inds[-10:])
concept_mask = np.ones([len(flat_concept_batch)], dtype=np.float64)
concept_mask[concept_inds] = 0
concept_mask = torch.tensor(concept_mask, device=cuda6, dtype=torch.float).unsqueeze(1)
# Observations, true action, values and done for each of the stored demostration
obss, action_true, done = flat_batch[:, 0], flat_batch[:, 1], flat_batch[:, 2]
episode_ids = np.zeros(len(flat_batch))
inds_copy = inds.copy()
# Loop terminates when every observation in the flat_batch has been handled
while True:
# taking observations and done located at inds
done_step = done[inds]
episode_ids[inds] = range(len(inds))
# Updating inds, by removing those indices corresponding to which the demonstrations have finished
inds = inds[:len(inds) - sum(done_step)]
if len(inds) == 0:
break
# Incrementing the remaining indices
inds = [index + 1 for index in inds]
# make concept_episode_ids
flat_concept_batch = np.array(flat_concept_batch)
obss, action_true, done = flat_concept_batch[:, 0], flat_concept_batch[:, 1], flat_concept_batch[:, 2]
concept_episode_ids = np.zeros(len(flat_concept_batch))
concept_inds_copy = concept_inds.copy()
# Loop terminates when every observation in the flat_batch has been handled
while True:
# taking observations and done located at inds
done_step = done[concept_inds]
concept_episode_ids[concept_inds] = range(len(concept_inds))
# Updating inds, by removing those indices corresponding to which the demonstrations have finished
concept_inds = concept_inds[:len(concept_inds) - sum(done_step)]
if len(concept_inds) == 0:
break
# Incrementing the remaining indices
concept_inds = [index + 1 for index in concept_inds]
# {flat_batch, mask, episode_ids, inds_copy,
# flat_batch_concept_inds,
# concept_mask, concept_episode_ids, concept_inds} <-- inds after batch only has concepts
batch_dict = {'flat_batch': flat_batch,
'mask': mask,
'episode_ids': episode_ids,
'inds': inds_copy,
'batch_concept_inds': batch_concept_inds,
'flat_batch_concept_inds': flat_batch_concept_inds,
'concept_mask': concept_mask,
'concept_episode_ids': concept_episode_ids,
'concept_inds': concept_inds_copy}
path = '/data/graceduansu/GoToImpUnlock_concepts/3_search_for_target/batch' + str(i)
with open(path, 'wb') as handle:
pickle.dump(batch_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
################################ RUN ###################################################
for i in range(0, 128):
print(i)
batch = []
concept1_inds = []
concept2_inds = []
concept3_inds = []
# filter for 1280 demos that have unlock action
count = 0
search_begin_idx = None
unlock_idx = None
pickup_idx = None
dem_transformed = None
while count < 128:
search_begin_idx = None
unlock_idx = None
pickup_idx = None
dem_transformed = None
while unlock_idx is None or pickup_idx is None or search_begin_idx is None:
search_begin_idx = None
unlock_idx = None
pickup_idx = None
dem_transformed = None
rand_idx = np.random.choice(len(some_demos))
dem = some_demos[rand_idx]
dem_transformed = demo_list[rand_idx]
# if demo has Toggle (unlock) action
if env.actions.toggle in dem[3] and env.actions.pickup in dem[3]:
color = None
for t in range(len(dem_transformed)-1, -1, -1):
isdoor = (dem_transformed[t][0]['image'][3][5][0] == 4)
istoggle = (dem[3][t] == env.actions.toggle)
if isdoor and istoggle and unlock_idx is None:
unlock_idx = t
color = dem_transformed[unlock_idx][0]['image'][3][5][1]
if color:
iskey = (dem_transformed[t][0]['image'][3][5][0] == 5)
iscolor = (dem_transformed[t][0]['image'][3][5][1] == color)
ispickup = (dem[3][t] == env.actions.pickup)
if iskey and iscolor and ispickup and pickup_idx is None:
pickup_idx = t
if pickup_idx is not None:
if isdoor and iscolor:
search_begin_idx = t
break
# (start, end) idx
batch.append(dem_transformed)
last_idx = len(dem_transformed)
concept1_inds.append((search_begin_idx, pickup_idx))
concept2_inds.append((pickup_idx, unlock_idx))
concept3_inds.append((0, search_begin_idx, unlock_idx, last_idx))
count += 1
batch.sort(key=len, reverse=True)
get_concept_data_from_batch(batch, concept1_inds, '/data/graceduansu/GoToImpUnlock_concepts/1_search_for_key/batch')
get_concept_data_from_batch(batch, concept2_inds, '/data/graceduansu/GoToImpUnlock_concepts/2_take_key_to_door/batch')
get_concept3_data_from_batch(batch, concept3_inds)