-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_unified_dataloader.py
More file actions
147 lines (134 loc) · 4.77 KB
/
creating_unified_dataloader.py
File metadata and controls
147 lines (134 loc) · 4.77 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
"""
This example demonstrates how to create a unified DataLoader for multiple datasets.
Particularly, it focuses on the Waymo End-to-End and Waymo Perception datasets.
"""
import argparse
import os
import numpy as np
import pandas as pd
from torch.utils.data import DataLoader
from tqdm import tqdm
from standard_e2e import UnifiedE2EDataset
from standard_e2e.dataset_utils.augmentation import (
MultipleFramesImageAugmentation,
TrajectoryResampling,
)
from standard_e2e.dataset_utils.frame_loader import (
FrameLoader,
)
from standard_e2e.dataset_utils.modality_defaults import IntentDefaults
from standard_e2e.dataset_utils.selector import (
ClosestTimestampSelector,
CurrentSelector,
)
from standard_e2e.enums import Intent, Modality
from standard_e2e.enums import TrajectoryComponent as TC
from standard_e2e.indexing import get_multi_dataset_index
def parse_args():
parser = argparse.ArgumentParser(description="Create a unified DataLoader")
parser.add_argument(
"--processed_data_path",
type=str,
help="Path to the processed data",
required=True,
)
return parser.parse_args()
def main():
args = parse_args()
processed_data_path = args.processed_data_path
waymo_e2e_train_index = pd.read_parquet(
os.path.join(processed_data_path, "waymo_e2e/training/index.parquet")
)
waymo_perception_train_index = pd.read_parquet(
os.path.join(processed_data_path, "waymo_perception/training/index.parquet")
)
print("Waymo End-to-end dataset size:", waymo_e2e_train_index.shape[0])
print("Waymo Perception dataset size:", waymo_perception_train_index.shape[0])
unified_index = get_multi_dataset_index(
[waymo_e2e_train_index, waymo_perception_train_index]
)
feature_loaders = [
FrameLoader(
frame_name="current_sensors",
required_modalities=[
Modality.CAMERAS,
Modality.INTENT,
Modality.PAST_STATES,
],
frame_selector=CurrentSelector(location="features"),
)
]
label_loaders = [
FrameLoader(
frame_name="current_states",
required_modalities=[
Modality.FUTURE_STATES,
],
frame_selector=CurrentSelector(location="labels"),
),
FrameLoader(
frame_name="future_1second",
required_modalities=[Modality.CAMERAS],
frame_selector=ClosestTimestampSelector(location="labels", delta_t=1.0),
),
]
history_time_lattice = np.linspace(-3, 0, 6, endpoint=False)
future_time_lattice = np.linspace(0, 10, 21)[1:]
dataset = UnifiedE2EDataset(
index_data=unified_index,
processed_data_path=processed_data_path,
regime="train",
feature_loaders=feature_loaders,
label_loaders=label_loaders,
augmentations=[
MultipleFramesImageAugmentation("train"),
TrajectoryResampling(
history_target_timestamps=history_time_lattice,
target_frame_names=["current_sensors"],
),
TrajectoryResampling(
future_target_timestamps=future_time_lattice,
target_frame_names=["current_states"],
),
],
modality_defaults={Modality.INTENT: IntentDefaults()},
)
dataloader = DataLoader(
dataset,
batch_size=16,
shuffle=True,
collate_fn=dataset.collate_fn,
num_workers=16,
)
# Further processing with the dataset
num_e2e, num_perc = 0, 0
intent_counter = {intent: 0 for intent in Intent}
pbar = tqdm(dataloader, total=len(dataloader), desc="Processing batches")
for batch in pbar:
num_e2e += sum(e == "waymo_e2e" for e in batch["current_sensors"].dataset_name)
num_perc += sum(
e == "waymo_perception" for e in batch["current_sensors"].dataset_name
)
pbar.set_description(
f"Waymo E2E samples: {num_e2e}, Waymo Perception samples: {num_perc}"
)
for intent in Intent:
intent_counter[intent] += sum(
e == intent
for e in batch["current_sensors"].get_modality_data(Modality.INTENT)
)
past_states = (
batch["current_sensors"]
.get_modality_data(Modality.PAST_STATES)
.get([TC.TIMESTAMP, TC.X, TC.Y])
)
future_states = (
batch["current_states"]
.get_modality_data(Modality.FUTURE_STATES)
.get([TC.TIMESTAMP, TC.X, TC.Y])
)
assert past_states.shape[1:] == (len(history_time_lattice), 3)
assert future_states.shape[1:] == (len(future_time_lattice), 3)
print("Intent counts:", intent_counter)
if __name__ == "__main__":
main()