forked from Darth-Kronos/terrain-identification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
144 lines (114 loc) · 4.21 KB
/
dataloader.py
File metadata and controls
144 lines (114 loc) · 4.21 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
import pandas as pd
import numpy as np
import os
from collections import Counter
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import torch
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import WeightedRandomSampler
# train_data_root = "./data/train_data.csv"
train_data_root = "terrain-identification/data/train_data.csv"
SUBJECT_X_TEMPLATE = "subject_{}_{}__x.csv"
SUBJECT_X_TIME_TEMPLATE = "subject_{}_{}__x_time.csv"
SUBJECT_Y_TEMPLATE = "subject_{}_{}__y.csv"
SUBJECT_Y_TIME_TEMPLATE = "subject_{}_{}__y_time.csv"
X_HEADER = ["acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z"]
Y_HEADER = ["label"]
BATCH_SIZE = 128
def parse_uid(uid):
subject_id, session_num = uid.split("_")
# return int(subject_id), int(session_num)
return subject_id, session_num
class SubjectDataset(Dataset):
def __init__(self, datapath: str, ids: list):
self.ids = ids
self.datapath = datapath
self.y_files = {uid: os.path.join(self.datapath, SUBJECT_Y_TEMPLATE.format(parse_uid(uid)[0], parse_uid(uid)[1])) for uid in self.ids}
self.x_files = {uid: os.path.join(self.datapath, SUBJECT_X_TEMPLATE.format(parse_uid(uid)[0], parse_uid(uid)[1])) for uid in self.ids}
# Generate a list of samples and determine the number of datapoints in the dataset
# and build up the cache
self.build_cache_and_datalen()
def __len__(self):
return self.num_samples
def __getitem__(self, index):
inputs = self.X[index]
labels = np.array([self.y[index]])
return torch.from_numpy(inputs), torch.from_numpy(labels)
def build_cache_and_datalen(self):
num_samples = 0
timesteps = None
X_list = []
y_list = []
for uid, y_file in self.y_files.items():
# print(f"Converting uid {uid}")
y = pd.read_csv(y_file)
n_samples = len(y)
num_samples += n_samples
x_file = self.x_files[uid]
X_dataframe = pd.read_csv(x_file)
if timesteps is None:
_sample = X_dataframe[X_dataframe["timestamp"] == 0]
timesteps = len(_sample)
# Convert to numpy
X = self.dataframe_to_numpy(X_dataframe, timesteps, y)
X_list.append(X)
y_list.append(y["label"].values)
self.X = np.concatenate(X_list, axis=0).astype(np.float32)
self.y = np.concatenate(y_list, axis=0).astype(int)
assert self.X.shape[0] == self.y.shape[0]
self.num_samples = self.X.shape[0]
def dataframe_to_numpy(self, df, timesteps, y_df):
"""Convert from pandas to numpy for faster access
"""
len_array = int(len(df) / timesteps)
assert len_array == len(y_df)
values = df[X_HEADER].values
X = values.reshape(len_array, timesteps, len(X_HEADER))
return np.transpose(X, axes=(0, 2, 1)).copy()
split_ids = {'train': ['005_02',
'001_06',
'003_02',
'001_05',
'002_02',
'003_01',
'003_03',
'005_01',
'001_07',
'002_05',
'004_02',
'002_03',
'001_02',
'002_04',
'001_03',
'004_01',
'005_03',
'006_01',
'006_02',
'007_01',
'007_03',
'007_04',
],
'val': ['001_08', '002_01', '001_01', '001_04', '006_03','007_02','008_01'],
}
train_data_path = 'terrain-identification/data/TrainingData/filtered_window_1'
train_dataset = SubjectDataset(
train_data_path,
split_ids["train"]
)
ys = train_dataset.y.tolist()
counts = Counter(ys)
weights = np.array([1./counts[_y] for _y in ys])
sample_weights = torch.from_numpy(weights).float()
sampler = WeightedRandomSampler(
weights=sample_weights,
num_samples=len(sample_weights),
replacement=True)
train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, sampler=sampler, drop_last=True)
train_iterations = (len(train_dataset) // BATCH_SIZE) + ((len(train_dataset) % BATCH_SIZE) != 0)
val_dataset = SubjectDataset(
train_data_path,
split_ids["val"]
)
val_dataloader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)
val_iterations = (len(val_dataset) // BATCH_SIZE) + ((len(val_dataset) % BATCH_SIZE) != 0)