-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
400 lines (320 loc) · 12.2 KB
/
utils.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import numpy as np
import imageio
from pdb import set_trace
import os
import shelve
from collections import deque
# noinspection PyTypeChecker
def open_file_and_save(file_path, data):
"""
:param file_path: type==string
:param data:
"""
if os.path.exists(file_path):
os.remove(file_path)
# Create the directory for the file
dir_path = file_path.rsplit('/', 1)[0]
if not os.path.exists(dir_path):
os.mkdir(dir_path)
with open(file_path, 'wb') as f_handle:
np.savetxt(f_handle, data, fmt='%s')
def save_frames_as_video(frames, filename='temp'):
"""
Displays a list of frames as a gif, with controls
"""
# down sample frames to appropriate size
if frames[0].shape[0] > 500:
print("downsampling")
for i, frame in enumerate(frames):
frames[i] = frame[::3, ::3, :]
frames = np.asarray(frames)
imageio.mimwrite('./cached_videos/'+filename+'.mp4', frames, fps=60)
def print_metrics(domain, readouts):
'''
Printing the losses from a sess.run() call
Args:
readouts: losses and train_ops : dict
domain: domain : str
Returns:
'''
spacing = 17
print_str = '\n' + domain.capitalize() + '> \n'
print_str += 'reward: '.rjust(spacing) + '{:0.1f}'.format(readouts['total_reward']) + '\n'
if 'total_fake_reward' in readouts:
print_str += 'fake reward: '.rjust(spacing) + '{:0.1f}'.format(readouts['total_fake_reward']) + '\n'
for k_, v_ in readouts.items():
if 'loss' in k_:
value = np.around(np.mean(v_, axis=0), decimals=6)
print_str += (k_ + ': ').rjust(spacing) + str(value) + '\n'
print_str = print_str[:-2]
print(print_str)
def render_policy(sess, graph, ph, env, domain, num_rollout=20, save_video=True, save_dir='temp'):
frames = []
tot_reward = []
if save_video:
print("Saving video")
else:
print("Evaluating expert performance")
for idx in range(num_rollout):
done = False
obs = env[domain]['env'].reset()
steps = 0
ep_reward = 0.
while not done:
if save_video:
frames.append(env[domain]['env'].render(mode='rgb_array'))
action, = sess.run(graph[domain]['action'], feed_dict={ph[domain]['state']: obs[None],
ph[domain]['is_training']: False})
obs, reward, done, info = env[domain]['env'].step(action)
# print(np.around(action, 4))
ep_reward += reward
steps += 1
tot_reward.append(ep_reward)
if save_video:
save_frames_as_video(frames, save_dir)
avg_reward = np.mean(tot_reward)
print("Steps: {}".format(steps))
print("Avg Reward: {}".format(avg_reward))
return avg_reward
def create_dataset(sess, graph, ph, env, save_dir, num_rollout=20, save_video=True, vid_name='demonstrations'):
frames = []
tot_reward = []
total_obs = []
total_acs = []
if save_video:
print("Saving video")
else:
print("Creating transfer dataset")
for idx in range(num_rollout):
done = False
obs = env['expert']['env'].reset()
steps = 0
ep_reward = 0.
ep_obs = []
ep_acs = []
while not done:
# Get next action
action, = sess.run(graph['expert']['action'], feed_dict={ph['expert']['state']: obs[None],
ph['expert']['is_training']: False})
ep_obs.append(np.squeeze(obs))
ep_acs.append(np.squeeze(action))
# Save dataset as video
if save_video:
eimg = env['expert']['env'].render(mode='rgb_array')
frames.append(eimg)
# Step in environment
## Add slight noise to the action space
action += np.random.normal(0, 0.05)
obs, reward, done, info = env['expert']['env'].step(action)
ep_reward += reward
steps += 1
tot_reward.append(ep_reward)
total_obs.append(np.array(ep_obs))
total_acs.append(np.array(ep_acs))
# Print metrics
print("Steps: {}".format(steps))
print("Avg Reward: {}".format(np.mean(tot_reward)))
# Create a video of the dataset
if save_video:
save_frames_as_video(frames, filename=vid_name)
# Save into dataset
print("Saved {} demonstrations to {}".format(num_rollout, save_dir))
# shape [num_demo, ep_len, data_dim]
total_obs = np.array(total_obs, dtype='object')
total_acs = np.array(total_acs, dtype='object')
np.savez(save_dir,
obs=total_obs,
acs=total_acs)
def create_hybrid_dataset(sess, graph, ph, env, save_dir, num_transitions=20, save_video=True):
expert_deque = deque(maxlen=num_transitions)
learner_deque = deque(maxlen=num_transitions)
#================ EXPERT DATASET ================
frames = []
tot_reward = []
steps = 0
while steps < num_transitions:
done = False
ep_reward = 0.
obs = env['expert']['env'].reset()
while not done:
# Save dataset as video
if save_video:
img = env['expert']['env'].render(mode='rgb_array')
frames.append(img)
# Get next action
raw_action = sess.run(graph['expert']['action'], feed_dict={ph['expert']['state']: obs[None],
ph['expert']['is_training']: False})
raw_action = raw_action[0]
# Step in environment
## Add slight noise to the action space
noisy_action = raw_action + np.random.normal(0, 0.05, size=raw_action.shape)
# noisy_action = raw_action
next_obs, reward, done, info = env['expert']['env'].step(noisy_action)
ep_reward += reward
steps += 1
# Fill up the expert deque
expert_transition = (obs, noisy_action, reward, next_obs, 0.0 if done else 1.0, raw_action, 0., 0., 0.)
expert_deque.append(expert_transition)
obs = next_obs
# Keep track of reward
tot_reward.append(ep_reward)
# Create a video of the dataset
if save_video:
print("Saving video")
save_frames_as_video(frames, filename='expert_dataset')
print("Expert")
print("Num Transitions: {}".format(steps))
print("Avg Reward: {}".format(np.mean(tot_reward)))
# #================ IDENTITY DATASET ================
# frames = []
# tot_reward = []
# steps = 0
#
# while steps < num_transitions:
# done = False
# ep_reward = 0.
# obs = env['expert']['env'].reset()
#
#
# while not done:
# # Save dataset as video
# if save_video:
# img = env['expert']['env'].render(mode='rgb_array')
# frames.append(img)
#
# # Get next action
# raw_action = sess.run(graph['expert']['action'], feed_dict={ph['expert']['state']: obs[None],
# ph['expert']['is_training']: False})
#
# raw_action = raw_action[0]
#
#
# # Step in environment
# ## Add slight noise to the action space
# noisy_action = raw_action + np.random.normal(0, 0.05, size=raw_action.shape)
## noisy_action = raw_action
# next_obs, reward, done, info = env['expert']['env'].step(noisy_action)
#
#
# ep_reward += reward
# steps += 1
#
# # Fill up the expert deque
# learner_transition = (obs, noisy_action, reward, next_obs, 0.0 if done else 1.0, raw_action, 0., 0., 0.)
# learner_deque.append(learner_transition)
#
# obs = next_obs
#
# # Keep track of reward
# tot_reward.append(ep_reward)
#
#
# print("-----------------------")
# print("Learner")
# print("Num Transitions: {}".format(steps))
# print("Avg Reward: {}".format(np.mean(tot_reward)))
#
# # Create a video of the dataset
# if save_video:
# print("Saving video")
# save_frames_as_video(frames, filename='learner_dataset')
# #================ D_R2R DATASET ================
# frames = []
# tot_reward = []
# steps = 0
#
# while steps < num_transitions:
# done = False
# ep_reward = 0.
# obs = env['learner']['env'].reset()
#
#
# while not done:
# # Save dataset as video
# if save_video:
# img = env['learner']['env'].render(mode='rgb_array')
# frames.append(img)
#
# # Get next action
# raw_action = sess.run(graph['expert']['action'], feed_dict={ph['expert']['state']: obs[None],
# ph['expert']['is_training']: False})
#
# raw_action = raw_action[0] / 10.
#
#
# # Step in environment
# ## Add slight noise to the action space
# noisy_action = raw_action + np.random.normal(0, 0.05, size=raw_action.shape) / 10.
## noisy_action = raw_action
# next_obs, reward, done, info = env['learner']['env'].step(noisy_action)
#
#
# ep_reward += reward
# steps += 1
#
# # Fill up the expert deque
# learner_transition = (obs, noisy_action, reward, next_obs, 0.0 if done else 1.0, raw_action, 0., 0., 0.)
# learner_deque.append(learner_transition)
#
# obs = next_obs
#
# # Keep track of reward
# tot_reward.append(ep_reward)
#
#
# print("-----------------------")
# print("Learner")
# print("Num Transitions: {}".format(steps))
# print("Avg Reward: {}".format(np.mean(tot_reward)))
#
# # Create a video of the dataset
# if save_video:
# print("Saving video")
# save_frames_as_video(frames, filename='learner_dataset')
#========================== LEARNER DATASET ==========================
frames = []
tot_reward = []
steps = 0
while steps < num_transitions:
done = False
ep_reward = 0
env['expert']['env'].reset()
obs = env['learner']['env'].reset()
while not done:
mapped_state_raw, raw_action = sess.run([graph['learner']['mapped_state'], graph['learner']['action']],
feed_dict={ph['learner']['state']: obs[None],
ph['learner']['is_training']: False})
raw_action = raw_action[0]
mapped_state_raw = mapped_state_raw[0]
env['expert']['env'].env.set_state_from_obs(mapped_state_raw)
# self.env['expert']['env'].set_state_from_obs(mapped_state_raw)
# Render
if save_video:
# Concatenate learner and expert images
limg = env['learner']['env'].render(mode='rgb_array')
eimg = env['expert']['env'].render(mode='rgb_array')
img = np.concatenate([limg, eimg], axis=1)
frames.append(img)
# Step
noisy_action = raw_action + np.random.normal(0, 0.05, size=raw_action.shape)
# noisy_action = raw_action
next_obs, reward, done, info = env['learner']['env'].step(noisy_action)
ep_reward += reward
steps += 1
# Fill up the expert deque
learner_transition = (obs, noisy_action, reward, next_obs, 0.0 if done else 1.0, raw_action, 0., 0., 0.)
learner_deque.append(learner_transition)
obs = next_obs
tot_reward.append(ep_reward)
print("-----------------------")
print("Learner")
print("Num Transitions: {}".format(steps))
print("Avg Reward: {}".format(np.mean(tot_reward)))
# Create a video of the dataset
if save_video:
print("Saving learner video")
save_frames_as_video(frames, filename='learner_dataset')
hybrid_dataset = shelve.open(save_dir, writeback=True)
hybrid_dataset['expert'] = expert_deque
hybrid_dataset['learner'] = learner_deque
hybrid_dataset.close()