-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
276 lines (232 loc) · 10.5 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
## Copyright (c) Meta Platforms, Inc. and affiliates
import numpy as np
import torch
import os
import h5py
from torch.utils.data import DataLoader
from constants import CAMERA_NAMES, TEXT_EMBEDDINGS
import random
import glob
class EpisodicDatasetRobopen(torch.utils.data.Dataset):
def __init__(self, episode_ids, dataset_dir, norm_stats,num_episodes):
super(EpisodicDatasetRobopen).__init__()
self.episode_ids = episode_ids
self.dataset_dir = dataset_dir
self.norm_stats = norm_stats
self.num_episodes = num_episodes
self.is_sim = None
self.h5s = []
self.trials = []
self.task_emb_per_trial = []
self.verbose = True
self.h5s = {}
lens = []
files = list()
randfiles = list()
n = 100
for i in glob.glob("{}/*/*/".format(dataset_dir)):
print(i)
randfiles.append(sorted(glob.glob(os.path.join(i, "*.h5"))))
for i in randfiles:
l = len(i)
num = int(n/250 * l)
for file in range(num):
files.append(i[file])
files = sorted(files)
# files = sorted(glob.glob(os.path.join(dataset_dir + "*/*/", '*.h5')))
for filename in files:
#for 20 tasks hardcoded, modify as needed
if 'open_drawer' in filename:
task_emb = TEXT_EMBEDDINGS[0]
elif 'close_drawer' in filename:
task_emb = TEXT_EMBEDDINGS[1]
elif 'pick_butter' in filename:
task_emb = TEXT_EMBEDDINGS[2]
elif 'place_butter' in filename:
task_emb = TEXT_EMBEDDINGS[3]
elif 'pick_toast' in filename:
task_emb = TEXT_EMBEDDINGS[4]
elif 'place_toast' in filename:
task_emb = TEXT_EMBEDDINGS[5]
elif 'cap_lid' in filename:
task_emb = TEXT_EMBEDDINGS[6]
elif 'pick_lid' in filename:
task_emb = TEXT_EMBEDDINGS[7]
elif 'pick_tea' in filename:
task_emb = TEXT_EMBEDDINGS[8]
elif 'place_lid' in filename:
task_emb = TEXT_EMBEDDINGS[9]
elif 'place_tea' in filename:
task_emb = TEXT_EMBEDDINGS[10]
elif 'uncap_lid' in filename:
task_emb = TEXT_EMBEDDINGS[11]
elif 'close_oven' in filename:
task_emb = TEXT_EMBEDDINGS[12]
elif 'open_oven' in filename:
task_emb = TEXT_EMBEDDINGS[13]
elif 'place_bowl' in filename:
task_emb = TEXT_EMBEDDINGS[14]
elif 'slide_out' in filename:
task_emb = TEXT_EMBEDDINGS[15]
elif "cap_mug" in filename:
task_emb = TEXT_EMBEDDINGS[16]
elif "pick_mug" in filename:
task_emb = TEXT_EMBEDDINGS[17]
elif "pick_towel" in filename:
task_emb = TEXT_EMBEDDINGS[18]
elif "wipe_towel" in filename:
task_emb = TEXT_EMBEDDINGS[19]
elif "pick_cup" in filename:
task_emb = TEXT_EMBEDDINGS[20]
elif "place_cup" in filename:
task_emb = TEXT_EMBEDDINGS[21]
else:
task_emb = TEXT_EMBEDDINGS[0]
'SINGLE TASK embedding wont be used'
h5 = h5py.File(filename, 'r')
for key, trial in h5.items():
if(trial['data']['time'].shape[0] != 42):
continue
# Open the trial and extract metadata
lens.append(trial['data']['ctrl_arm'].shape[0])
# Bookkeeping for all the trials
self.trials.append(trial)
self.task_emb_per_trial.append(task_emb)
self.trial_lengths = np.cumsum(lens)
self.max_idx = self.trial_lengths[-1]
print("TOTAL TRIALS",len(self.trials))
self.trials = self.trials[:num_episodes]
assert self.num_episodes == len(self.trials) ## sanity check that all files are loaded, remove if needed
print('TOTAL TRIALS = num_episodes = ',len(self.trials))
self.__getitem__(0)
def __len__(self):
return len(self.episode_ids)
def __getitem__(self, idx):
sample_full_episode = False # hardcode
trial_idx = self.episode_ids[idx]
trial = self.trials[trial_idx]
task_emb = self.task_emb_per_trial[trial_idx]
camera_names = CAMERA_NAMES
action = np.concatenate([trial['data']['ctrl_arm'], trial['data']['ctrl_ee']], axis=1).astype(np.float32)
original_action_shape = action.shape
cutoff = 2 #10#5
episode_len = original_action_shape[0] -cutoff ## cutoff last few
if sample_full_episode:
start_ts = 0
else:
start_ts = np.random.choice(episode_len)
# get observation at start_ts only
qpos = trial['data']['qp_arm'][start_ts].astype(np.float32)
qvel = trial['data']['qv_arm'][start_ts].astype(np.float32)
image_dict = dict()
for cam_name in camera_names:
image_dict[cam_name] = trial['data'][f'{cam_name}'][start_ts]
# get all actions after and including start_ts
action = np.concatenate([trial['data']['ctrl_arm'], trial['data']['ctrl_ee']], axis=1)[max(0, start_ts - 1):].astype(np.float32) # hack, to make timesteps more aligned
action_len = episode_len - max(0, start_ts - 1) # hack, to make timesteps more aligned
padded_action = np.zeros(original_action_shape, dtype=np.float32)
padded_action[:action_len] = action[:-cutoff]
is_pad = np.zeros(episode_len)
is_pad[action_len:] = 1
# new axis for different cameras
all_cam_images = []
for cam_name in camera_names:
all_cam_images.append(image_dict[cam_name])
all_cam_images = np.stack(all_cam_images, axis=0)
# construct observations
image_data = torch.from_numpy(all_cam_images)
qpos_data = torch.from_numpy(qpos).float()
action_data = torch.from_numpy(padded_action).float()
is_pad = torch.from_numpy(is_pad).bool()
# channel last
image_data = torch.einsum('k h w c -> k c h w', image_data)
# normalize image and change dtype to float
image_data = image_data / 255.0
action_data = (action_data - self.norm_stats["action_mean"]) / self.norm_stats["action_std"]
qpos_data = (qpos_data - self.norm_stats["qpos_mean"]) / self.norm_stats["qpos_std"]
task_emb = torch.from_numpy(np.asarray(task_emb)).float()
return image_data, qpos_data, action_data, is_pad, task_emb
def get_norm_stats_robopen(dataset_dir,num_epsiodes):
# files = []
# for directory in dataset_dir:
# files.append()
files = list()
randfiles = list()
n = 100
for i in glob.glob("{}/*/*/".format(dataset_dir)):
print(i)
randfiles.append(sorted(glob.glob(os.path.join(i, "*.h5"))))
for i in randfiles:
l = len(i)
num = int(n/250 * l)
for file in range(num):
files.append(i[file])
files = sorted(files)
print('files',files)
all_qpos_data = []
all_action_data = []
cutoff = 2 #10#5
for filename in files:
# Check each file to see how many entires it has
h5 = h5py.File(filename, 'r')
# with h5py.File(filename, 'r') as h5:
for key, trial in h5.items():
# Open the trial and extract metadata
qpos = trial['data']['qp_arm'][()].astype(np.float32)
qvel = trial['data']['qv_arm'][()].astype(np.float32)
camera_names = CAMERA_NAMES
action = np.concatenate([trial['data']['ctrl_arm'], trial['data']['ctrl_ee']], axis=1).astype(np.float32)
if(trial['data']['time'].shape[0] != 42):
continue
all_qpos_data.append(torch.from_numpy(qpos[:-cutoff]))
all_action_data.append(torch.from_numpy(action[:-cutoff]))
# if len(qpos)==41:
# all_qpos_data.append(torch.from_numpy(qpos[:-(cutoff-1)]))
# all_action_data.append(torch.from_numpy(action[:-(cutoff-1)]))
all_qpos_data = torch.stack(all_qpos_data)
all_action_data = torch.stack(all_action_data)
all_action_data = all_action_data
# normalize action data
action_mean = all_action_data.mean(dim=[0, 1], keepdim=True)
action_std = all_action_data.std(dim=[0, 1], keepdim=True)
action_std = torch.clip(action_std, 1e-2, 10) # clipping
# normalize qpos data
qpos_mean = all_qpos_data.mean(dim=[0, 1], keepdim=True)
qpos_std = all_qpos_data.std(dim=[0, 1], keepdim=True)
qpos_std = torch.clip(qpos_std, 1e-2, 10) # clipping
stats = {"action_mean": action_mean.numpy().squeeze(), "action_std": action_std.numpy().squeeze(),
"qpos_mean": qpos_mean.numpy().squeeze(), "qpos_std": qpos_std.numpy().squeeze(),
"example_qpos": qpos}
return stats
def load_data(dataset_dir, num_episodes, batch_size_train, batch_size_val):
# obtain train test split
train_ratio = 0.8 # change as needed
shuffled_indices = np.random.permutation(num_episodes)
train_indices = shuffled_indices[:int(train_ratio * num_episodes)]
val_indices = shuffled_indices[int(train_ratio * num_episodes):]
# obtain normalization stats for qpos and action
norm_stats = get_norm_stats_robopen(dataset_dir, num_episodes)
# construct dataset and dataloader
train_dataset = EpisodicDatasetRobopen(train_indices, dataset_dir, norm_stats,num_episodes)
val_dataset = EpisodicDatasetRobopen(val_indices, dataset_dir, norm_stats,num_episodes)
train_dataloader = DataLoader(train_dataset, batch_size=batch_size_train, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size_val, shuffle=True, pin_memory=True, num_workers=1, prefetch_factor=1)
return train_dataloader, val_dataloader, norm_stats, train_dataset.is_sim
### helper functions
def compute_dict_mean(epoch_dicts):
result = {k: None for k in epoch_dicts[0]}
num_items = len(epoch_dicts)
for k in result:
value_sum = 0
for epoch_dict in epoch_dicts:
value_sum += epoch_dict[k]
result[k] = value_sum / num_items
return result
def detach_dict(d):
new_d = dict()
for k, v in d.items():
new_d[k] = v.detach()
return new_d
def set_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)