-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdataset.py
150 lines (124 loc) · 5.01 KB
/
dataset.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
# %%
import os
from typing import List, Dict, Any
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from torch_geometric.data import InMemoryDataset
from torch_geometric.data import Data, DataLoader
import torch
from utils.feature_utils import compute_feature_for_one_seq, encoding_features, save_features
from utils.config import DATA_DIR, LANE_RADIUS, OBJ_RADIUS, OBS_LEN, INTERMEDIATE_DATA_DIR
from tqdm import tqdm
# %%
def get_fc_edge_index(num_nodes, start=0):
"""
return a tensor(2, edges), indicing edge_index
"""
to_ = np.arange(num_nodes, dtype=np.int64)
edge_index = np.empty((2, 0))
for i in range(num_nodes):
from_ = np.ones(num_nodes, dtype=np.int64) * i
# FIX BUG: no self loop in ful connected nodes graphs
edge_index = np.hstack((edge_index, np.vstack((np.hstack([from_[:i], from_[i+1:]]), np.hstack([to_[:i], to_[i+1:]])))))
edge_index = edge_index + start
return edge_index.astype(np.int64), num_nodes + start
# %%
class GraphData(Data):
"""
override key `cluster` indicating which polyline_id is for the vector
"""
def __inc__(self, key, value):
if key == 'edge_index':
return self.x.size(0)
elif key == 'cluster':
return int(self.cluster.max().item()) + 1
else:
return 0
# %%
class GraphDataset(InMemoryDataset):
"""
dataset object similar to `torchvision`
"""
def __init__(self, root, transform=None, pre_transform=None):
super(GraphDataset, self).__init__(root, transform, pre_transform)
self.data, self.slices = torch.load(self.processed_paths[0])
@property
def raw_file_names(self):
return []
@property
def processed_file_names(self):
return ['dataset.pt']
def download(self):
pass
def process(self):
def get_data_path_ls(dir_):
return [os.path.join(dir_, data_path) for data_path in os.listdir(dir_)]
# make sure deterministic results
data_path_ls = sorted(get_data_path_ls(self.root))
valid_len_ls = []
valid_len_ls = []
data_ls = []
for data_p in tqdm(data_path_ls):
if not data_p.endswith('pkl'):
continue
x_ls = []
y = None
cluster = None
edge_index_ls = []
data = pd.read_pickle(data_p)
all_in_features = data['POLYLINE_FEATURES'].values[0]
add_len = data['TARJ_LEN'].values[0]
cluster = all_in_features[:, -1].reshape(-1).astype(np.int32)
valid_len_ls.append(cluster.max())
y = data['GT'].values[0].reshape(-1).astype(np.float32)
traj_mask, lane_mask = data["TRAJ_ID_TO_MASK"].values[0], data['LANE_ID_TO_MASK'].values[0]
agent_id = 0
edge_index_start = 0
assert all_in_features[agent_id][
-1] == 0, f"agent id is wrong. id {agent_id}: type {all_in_features[agent_id][4]}"
for id_, mask_ in traj_mask.items():
data_ = all_in_features[mask_[0]:mask_[1]]
edge_index_, edge_index_start = get_fc_edge_index(
data_.shape[0], start=edge_index_start)
x_ls.append(data_)
edge_index_ls.append(edge_index_)
for id_, mask_ in lane_mask.items():
data_ = all_in_features[mask_[0]+add_len: mask_[1]+add_len]
edge_index_, edge_index_start = get_fc_edge_index(
data_.shape[0], edge_index_start)
x_ls.append(data_)
edge_index_ls.append(edge_index_)
edge_index = np.hstack(edge_index_ls)
x = np.vstack(x_ls)
data_ls.append([x, y, cluster, edge_index])
# [x, y, cluster, edge_index, valid_len]
g_ls = []
padd_to_index = np.max(valid_len_ls)
feature_len = data_ls[0][0].shape[1]
for ind, tup in enumerate(data_ls):
tup[0] = np.vstack(
[tup[0], np.zeros((padd_to_index - tup[-2].max(), feature_len), dtype=tup[0].dtype)])
tup[-2] = np.hstack(
[tup[2], np.arange(tup[-2].max()+1, padd_to_index+1)])
g_data = GraphData(
x=torch.from_numpy(tup[0]),
y=torch.from_numpy(tup[1]),
cluster=torch.from_numpy(tup[2]),
edge_index=torch.from_numpy(tup[3]),
valid_len=torch.tensor([valid_len_ls[ind]]),
time_step_len=torch.tensor([padd_to_index + 1])
)
g_ls.append(g_data)
data, slices = self.collate(g_ls)
torch.save((data, slices), self.processed_paths[0])
# %%
if __name__ == "__main__":
for folder in os.listdir(DATA_DIR):
dataset_input_path = os.path.join(
INTERMEDIATE_DATA_DIR, f"{folder}_intermediate")
dataset = GraphDataset(dataset_input_path)
batch_iter = DataLoader(dataset, batch_size=256)
batch = next(iter(batch_iter))
# %%