-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconvert_to_lerobot.py
More file actions
299 lines (262 loc) · 12.7 KB
/
convert_to_lerobot.py
File metadata and controls
299 lines (262 loc) · 12.7 KB
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
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import json
from collections import OrderedDict
from pathlib import Path
from lerobot.common.datasets.lerobot_dataset import LeRobotDataset
import numpy as np
import PIL
MAX_PATH_LENGTH = 200
MAX_DEPTH_VALUE = 100_000
# Get the paths of subdirectories within a directory.
def get_subdirectories(path: Path) -> list[Path]:
return sorted([p for p in path.iterdir() if p.is_dir()])
# Get the paths of files within a directory.
def get_files_in_dir(path: Path) -> list[Path]:
return sorted([p for p in path.iterdir() if p.is_file()])
# Read an image file into a numpy array.
def load_image(file_path: Path) -> np.ndarray:
file_extension = str(file_path).split(".")[-1]
if file_extension not in ["png", "jpg", "jpeg"]:
raise Exception(f"Unsupported file format: {file_extension}")
return np.array(PIL.Image.open(file_path))
# Reshape an array from (n, 2) to (new_length, 2) and fill the extra space with zeros.
def pad_array(
arr : np.ndarray,
new_length: int
) -> np.ndarray:
n = arr.shape[0]
if n > new_length:
raise ValueError(f"Input array has more than {new_length} rows.")
padding = ((0, new_length - n), (0, 0)) # Pad rows only, not columns
return np.pad(arr, padding, mode='constant', constant_values=0)
# Returns true if feature is a segmentation info, false otherwise.
def is_feature_segmentation_info(state_name: str) -> bool:
return state_name.split(".")[-1] == "segmentation_info"
# Returns true if feature is an RGB image, false otherwise.
def is_feature_rgb_image(state_name: str) -> bool:
return state_name.split(".")[-1] == "rgb_image"
# Returns true if feature is a segmentation image, false otherwise.
def is_feature_segmentation_image(state_name: str) -> bool:
return state_name.split(".")[-1] == "segmentation_image"
# Returns true if feature is a segmentation image, false otherwise.
def is_feature_instance_id_segmentation_image(state_name: str) -> bool:
return state_name.split(".")[-1] == "instance_id_segmentation_image"
# Returns true if feature is a depth image, false otherwise.
def is_feature_depth_image(state_name: str) -> bool:
return state_name.split(".")[-1] == "depth_image"
# Returns true if feature is a normal image, false otherwise.
def is_feature_normal_image(state_name: str) -> bool:
return state_name.split(".")[-1] == "normals_image"
# Converts an idToLabel dictionary into a multi-hot encoding of the labels.
def idToLabel_to_bool_array(
old_id_to_label: dict,
label_to_new_id: dict
) -> np.ndarray:
result = np.zeros(len(label_to_new_id), dtype=bool)
for value in old_id_to_label.values():
id = label_to_new_id[value["class"]]
result[id] = True
return np.array(result)
# Maps the id within the segmentation image to the new global id.
def remap_segmentation_image(
image : np.ndarray,
label_to_new_id: dict,
old_id_to_label: dict
) -> np.ndarray:
max_id = 0
for old_id, label in old_id_to_label.items():
assert int(old_id) >= 0
max_id = max(max_id, int(old_id))
if label["class"] == "UNLABELLED":
old_id_of_unlabeled = int(old_id)
# TODO: Try the map method instead and compare performance.
mapping_array = np.zeros(max_id + 1, dtype=np.uint8)
for old_id, label in old_id_to_label.items():
new_id = label_to_new_id[label["class"]]
mapping_array[int(old_id)] = new_id
# There are some incosistencies in the dataset, where some pixels indicate a certain
# label but that label wasn't present in the old_id_to_label dictionary. We change all
# those erroenous pixels to the unlabeled class
if image.max() > max_id:
image[image > max_id] = old_id_of_unlabeled
return mapping_array[image]
# Traverse all frames and find out all possible labels, and give each an id.
def get_segmentation_label_lookup(dataset_paths: list[Path]) -> dict:
labels = set()
for dataset_dir in dataset_paths:
for common_state_file in get_files_in_dir(dataset_dir / "state" / "common"):
common_state = np.load(common_state_file, allow_pickle=True).item()
for state_name, state_value in common_state.items():
if is_feature_segmentation_info(state_name):
for val in state_value["idToLabels"].values():
labels.add(val["class"])
label_to_id_lookup = OrderedDict()
for id, label in enumerate(sorted(list(labels))):
label_to_id_lookup[label] = id
return label_to_id_lookup
# Load sample files to obtain necessary metadata for each feature.
def get_feature_info(
dataset_path : str,
segmentation_label_to_id: dict
) -> tuple[dict, dict, dict]:
feature_files_list = {} # Maps feature name to list of files under that feature.
features = {}
state_category_dirs = get_subdirectories(dataset_path / "state")
# For each state, load a sample file to obtain metadata.
for state_category_dir in state_category_dirs:
if state_category_dir.name == "common":
feature_files_list["common"] = get_files_in_dir(state_category_dir)
sample_common_file = np.load(feature_files_list["common"][0], allow_pickle=True).item()
for key, val in sample_common_file.items():
if isinstance(val, np.ndarray):
if key == "target_path": # Temporary solution to deal with inconsistent shape of target_path.
features["target_path_length"] = {
"dtype" : "uint32",
"shape" : np.array([0], dtype=np.uint32).shape,
"names" : None
}
val = pad_array(val, MAX_PATH_LENGTH)
features[key] = {
"dtype" : str(val.dtype),
"shape" : val.shape,
"names" : None
}
if is_feature_segmentation_info(key): # Feature containing the segmentation info.
segmentation_info_array = idToLabel_to_bool_array(val["idToLabels"], segmentation_label_to_id)
features[key] = {
"dtype" : str(segmentation_info_array.dtype),
"shape" : segmentation_info_array.shape,
"names" : list(segmentation_label_to_id.keys())
}
else: # If it's not a common state, it is an image state.
image_state_dirs = get_subdirectories(state_category_dir)
assert len(image_state_dirs) > 0
for image_state_dir in image_state_dirs:
image_state_name = image_state_dir.name
if is_feature_normal_image(image_state_name) or \
is_feature_instance_id_segmentation_image(image_state_name): # don't need thoes two states
continue
feature_files = get_files_in_dir(image_state_dir)
img = load_image(feature_files[0])
height, width = img.shape[0], img.shape[1]
features[image_state_name] = {
"dtype" : "image",
"shape" : (height, width, 3),
"names" : None
}
feature_files_list[image_state_name] = feature_files
extra_info = {}
extra_info["frame_count"] = len(feature_files_list["common"])
return features, feature_files_list, extra_info
# Write data frames into the episode.
def write_frames(
dataset : LeRobotDataset,
feature_files_list : dict,
segmentation_label_to_id: dict,
frame_count : int,
task : str
) -> None:
for i in range(frame_count):
frame = {}
current_frame_idToLabel = None
common_state = np.load(feature_files_list["common"][i], allow_pickle=True).item()
for state_name, state_value in common_state.items():
if state_name == "target_path": # Temporary solution to deal with inconsistent shape of target_path.
frame["target_path_length"] = np.array([state_value.shape[0]], dtype=np.uint32)
state_value = pad_array(state_value, MAX_PATH_LENGTH)
if isinstance(state_value, np.ndarray):
frame[state_name] = state_value
if is_feature_segmentation_info(state_name):
current_frame_idToLabel = state_value["idToLabels"]
frame[state_name] = idToLabel_to_bool_array(current_frame_idToLabel, segmentation_label_to_id)
# Parse the states other than `common`, which are the image states.
for feature, file_list in feature_files_list.items():
if feature == "common":
continue
img = load_image(file_list[i])
if is_feature_rgb_image(feature):
assert img.dtype == np.uint8
frame[feature] = img
elif is_feature_depth_image(feature):
assert img.dtype == np.uint16
img = img / MAX_DEPTH_VALUE
img = np.stack([img] * 3, axis=-1) # reshape from (h,w) to (h,w,3)
frame[feature] = img
elif is_feature_segmentation_image(feature):
assert img.dtype == np.uint16
img = remap_segmentation_image(
img,
segmentation_label_to_id,
current_frame_idToLabel)
img = np.stack([img] * 3, axis=-1) # reshape from (h,w) to (h,w,3)
frame[feature] = img
frame["task"] = task
dataset.add_frame(frame)
# Convert a collection of MobilityGen datasets into a single LeRobot dataset.
def convert_to_lerobot_dataset(
dataset_paths: list[str],
output_path : str,
fps : int,
num_processes: int
) -> None:
num_episodes = len(dataset_paths)
print(f"Number of episodes: {num_episodes}")
sample_dataset_path = Path(dataset_paths[0])
config = json.load(open(sample_dataset_path / "config.json", "r"))
segmentation_label_to_id = get_segmentation_label_lookup(dataset_paths)
features, _, _ = get_feature_info(sample_dataset_path, segmentation_label_to_id)
dataset = LeRobotDataset.create(
repo_id = "",
fps = fps,
root = output_path,
robot_type = config["robot_type"],
features = features,
use_videos = False,
image_writer_processes = num_processes,
image_writer_threads = 1,
)
for i, dataset_path in enumerate(dataset_paths):
print(f"Processing {i + 1}/{num_episodes} - {dataset_path.name}")
_, feature_files_list, extra_info = get_feature_info(dataset_path, segmentation_label_to_id)
dataset.clear_episode_buffer()
write_frames(dataset, feature_files_list, segmentation_label_to_id, extra_info["frame_count"], config["scenario_type"])
dataset.save_episode()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=str, default=None, help="Path to the source MobilityGen dataset(s)")
parser.add_argument("--output", type=str, default=None, help="Output path for the converted LeRobot dataset")
parser.add_argument('--batch', action=argparse.BooleanOptionalAction, help="Whether the input directory contains more than one dataset")
parser.add_argument("--fps", type=int, default=30, help="Frames per second for the source dataset")
parser.add_argument("--num_processes", type=int, default=8, help="Number of processes for the image writer")
args = parser.parse_args()
args.input = os.path.expanduser(args.input)
args.output = os.path.expanduser(args.output)
if args.batch:
convert_to_lerobot_dataset(
dataset_paths = get_subdirectories(Path(args.input)),
output_path = args.output,
fps = args.fps,
num_processes = args.num_processes
)
else:
convert_to_lerobot_dataset(
dataset_paths = [Path(args.input)],
output_path = args.output,
fps = args.fps,
num_processes = args.num_processes
)