-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepisode_utils.py
193 lines (167 loc) · 6.85 KB
/
episode_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
# Use the original code
import logging
import os
from pathlib import Path
import re
from typing import Dict, Tuple
import numpy as np
import torch
logger = logging.getLogger(__name__)
def process_state(
episode: Dict[str, np.ndarray],
observation_space,
transforms: Dict,
proprio_state,
seq_idx: int = 0,
window_size: int = 0,
) -> Dict[str, torch.Tensor]:
state_obs_keys = observation_space["state_obs"]
state_obs_list_normalized = []
state_obs_list_unnormalized = []
for state_ob in state_obs_keys:
if window_size == 0 and seq_idx == 0: # single file loader
state_tensor = torch.from_numpy(episode[state_ob]).float()
else: # episode loader
state_tensor = torch.from_numpy(episode[state_ob][seq_idx : seq_idx + window_size]).float()
# expand dims for single environment obs
if len(state_tensor.shape) != 2:
state_tensor = state_tensor.unsqueeze(0)
# shape: (BxN_state_obs)
assert len(state_tensor.shape) == 2
if state_ob in transforms:
state_tensor_normalized = transforms[state_ob](state_tensor)
state_obs_list_normalized.append(state_tensor_normalized)
else:
state_obs_list_normalized.append(state_tensor)
state_obs_list_unnormalized.append(state_tensor)
seq_state_obs = torch.cat(state_obs_list_normalized, dim=1)
seq_state_obs_unnormalized = torch.cat(state_obs_list_unnormalized, dim=1)
if not proprio_state['normalize_robot_orientation'] and "robot_orientation_idx" in proprio_state.keys():
seq_state_obs[:, slice(*proprio_state.robot_orientation_idx)] = seq_state_obs_unnormalized[
:, slice(*proprio_state.robot_orientation_idx)
]
if not proprio_state['normalize']:
seq_state_obs = seq_state_obs_unnormalized
# slice the specified parts of the proprioception state
state_obs_sliced = []
for slice_ids in proprio_state['keep_indices']:
seq_state_obs_ = seq_state_obs[:, slice(*slice_ids)]
state_obs_sliced.append(seq_state_obs_)
seq_state_obs = torch.cat(state_obs_sliced, dim=1)
return {"robot_obs": seq_state_obs}
def process_rgb(
episode: Dict[str, np.ndarray],
observation_space,
transforms: Dict,
seq_idx: int = 0,
window_size: int = 0,
) -> Dict[str, Dict[str, torch.Tensor]]:
rgb_obs_keys = observation_space["rgb_obs"]
seq_rgb_obs_dict = {}
for _, rgb_obs_key in enumerate(rgb_obs_keys):
rgb_obs = episode[rgb_obs_key]
# expand dims for single environment obs
if len(rgb_obs.shape) != 4:
rgb_obs = np.expand_dims(rgb_obs, axis=0)
assert len(rgb_obs.shape) == 4
if window_size == 0 and seq_idx == 0: # single file loader
# To Square image
seq_rgb_obs_ = torch.from_numpy(rgb_obs).byte().permute(0, 3, 1, 2)
else: # episode loader
seq_rgb_obs_ = torch.from_numpy(rgb_obs[seq_idx : seq_idx + window_size]).byte().permute(0, 3, 1, 2)
# we might have different transformations for the different cameras
if rgb_obs_key in transforms:
seq_rgb_obs_ = transforms[rgb_obs_key](seq_rgb_obs_)
seq_rgb_obs_dict[rgb_obs_key] = seq_rgb_obs_
# shape: N_rgb_obs x (BxCxHxW)
return {"rgb_obs": seq_rgb_obs_dict}
def process_depth(
episode: Dict[str, np.ndarray],
observation_space,
transforms: Dict,
seq_idx: int = 0,
window_size: int = 0,
) -> Dict[str, Dict[str, torch.Tensor]]:
# expand dims for single environment obs
def exp_dim(depth_img):
if len(depth_img.shape) != 3:
depth_img = np.expand_dims(depth_img, axis=0)
return depth_img
depth_obs_keys = observation_space["depth_obs"]
seq_depth_obs_dict = {}
for _, depth_obs_key in enumerate(depth_obs_keys):
depth_ob = exp_dim(episode[depth_obs_key])
assert len(depth_ob.shape) == 3
if window_size == 0 and seq_idx == 0: # single file loader
depth_ob_ = torch.from_numpy(depth_ob).float()
else: # episode loader
depth_ob_ = torch.from_numpy(depth_ob[seq_idx : seq_idx + window_size]).float()
# we might have different transformations for the different cameras
if depth_obs_key in transforms:
depth_ob_ = transforms[depth_obs_key](depth_ob_)
seq_depth_obs_dict[depth_obs_key] = depth_ob_
# shape: N_depth_obs x(BxHxW)
return {"depth_obs": seq_depth_obs_dict}
def process_actions(
episode: Dict[str, np.ndarray],
observation_space,
transforms: Dict,
seq_idx: int = 0,
window_size: int = 0,
) -> Dict[str, torch.Tensor]:
# shape: (N_actions)
action_keys = observation_space["actions"]
if len(action_keys) != 1:
raise NotImplementedError
action_key = action_keys[0]
if window_size == 0 and seq_idx == 0: # single file loader
action = episode[action_key]
if "actions" in transforms:
action = transforms["actions"]((action, episode["robot_obs"]))
seq_acts = torch.from_numpy(action).float()
else: # episode loader
seq_acts = torch.from_numpy(episode[action_key][seq_idx : seq_idx + window_size]).float()
return {"actions": seq_acts}
def process_language(episode: Dict[str, np.ndarray], transforms: Dict, with_lang: bool) -> Dict[str, torch.Tensor]:
seq_lang = {"lang": torch.empty(0)}
if with_lang:
lang = torch.from_numpy(episode["language"]).float()
if "language" in transforms:
lang = transforms["language"](lang)
seq_lang["lang"] = lang
return seq_lang
def get_state_info_dict(episode: Dict[str, np.ndarray]) -> Dict[str, Dict[str, torch.Tensor]]:
"""
Create a dictionary with raw state observations for environment resets.
Args:
episode: Sequence dictionary.
Returns:
Info dict of full robot and scene state (for env resets).
"""
return {
"state_info": {
"robot_obs": torch.from_numpy(episode["robot_obs"]),
"scene_obs": torch.from_numpy(episode["scene_obs"]),
}
}
def lookup_naming_pattern(dataset_dir: Path, save_format: str) -> Tuple[Tuple[Path, str], int]:
"""
Check naming pattern of dataset files.
Args:
dataset_dir: Path to dataset.
save_format: File format (CALVIN default is npz).
Returns:
naming_pattern: 'file_0000001.npz' -> ('file_', '.npz')
n_digits: Zero padding of file enumeration.
"""
it = os.scandir(dataset_dir)
while True:
filename = Path(next(it))
if save_format in filename.suffix:
break
aux_naming_pattern = re.split(r"\d+", filename.stem)
naming_pattern = (filename.parent / aux_naming_pattern[0], filename.suffix)
n_digits = len(re.findall(r"\d+", filename.stem)[0])
assert len(naming_pattern) == 2
assert n_digits > 0
return naming_pattern, n_digits