MCPcopy Create free account
hub / github.com/SpatialVLA/SpatialVLA / restructure

Function restructure

data/rlds.py:130–191  ·  view source on GitHub ↗
(traj)

Source from the content-addressed store, hash-verified

128 REQUIRED_KEYS.add(language_key)
129
130 def restructure(traj):
131 # apply a standardization function, if provided
132 if standardize_fn is not None:
133 traj = standardize_fn(traj)
134
135 if not all(k in traj for k in REQUIRED_KEYS):
136 raise ValueError(f"Trajectory is missing keys: {REQUIRED_KEYS - set(traj.keys())}. " "Did you write a `standardize_fn`?")
137
138 # extracts images, depth images and proprio from the "observation" dict
139 traj_len = tf.shape(traj["action"])[0]
140 old_obs = traj["observation"]
141 new_obs = {}
142 for new, old in image_obs_keys.items():
143 if old is None:
144 new_obs[f"image_{new}"] = tf.repeat("", traj_len) # padding
145 else:
146 new_obs[f"image_{new}"] = old_obs[old]
147
148 for new, old in depth_obs_keys.items():
149 if old is None:
150 new_obs[f"depth_{new}"] = tf.repeat("", traj_len) # padding
151 else:
152 new_obs[f"depth_{new}"] = old_obs[old]
153
154 if state_obs_keys:
155 new_obs["proprio"] = tf.concat(
156 [
157 (tf.zeros((traj_len, 1), dtype=tf.float32) if key is None else tf.cast(old_obs[key], tf.float32)) # padding
158 for key in state_obs_keys
159 ],
160 axis=1,
161 )
162
163 # add timestep info
164 new_obs["timestep"] = tf.range(traj_len)
165
166 # extracts `language_key` into the "task" dict
167 task = {}
168 if language_key is not None:
169 if traj[language_key].dtype != tf.string:
170 raise ValueError(f"Language key {language_key} has dtype {traj[language_key].dtype}, " "but it must be tf.string.")
171 task["language_instruction"] = traj.pop(language_key)
172
173 traj = {
174 "observation": new_obs,
175 "task": task,
176 "action": tf.cast(traj["action"], tf.float32),
177 "dataset_name": tf.repeat(name, traj_len),
178 "traj_index": traj["_traj_index"],
179 }
180
181 if absolute_action_mask is not None:
182 if len(absolute_action_mask) != traj["action"].shape[-1]:
183 raise ValueError(
184 f"Length of absolute_action_mask ({len(absolute_action_mask)}) " f"does not match action dimension ({traj['action'].shape[-1]})."
185 )
186 traj["absolute_action_mask"] = tf.tile(
187 tf.convert_to_tensor(absolute_action_mask, dtype=tf.bool)[None],

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected