-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
52 lines (37 loc) · 1.5 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
import torch
from torch import utils, nn
import numpy as np
from preprocess import Tokenizer, preproc_timeseries
import os, sys, json
class MotionDataset(utils.data.Dataset):
def __init__(self, tokenizer, test=False, val=False):
self.tokenizer = tokenizer
with open('config.json', 'r') as config_file:
config = json.load(config_file)
data_folder = config['formatted_data_folder']
with open('dataset_split.json', 'r') as dataset_split:
data = json.load(dataset_split)
if not test and not val:
self.indices = data['train']
elif test:
self.indices = data['test']
else:
self.indices = data['val']
if len(self.indices) == 0:
return
inputs, labels = [], []
for idx in self.indices:
input = np.load(os.path.join(data_folder, str(idx) + '.npy'))
with open(os.path.join(data_folder, str(idx) + '.txt'), 'r') as f:
label = f.read()
inputs.append(input)
labels.append(label)
self.X = preproc_timeseries(inputs)
self.Y, self.lengths = self.tokenizer.get_tokenized(labels)
def __len__(self):
return len(self.indices)
def __getitem__(self, index):
X = self.X[index].type(torch.float32)
Y = self.Y[index].type(torch.long)
len = torch.tensor(self.lengths[index]).type(torch.long)
return X, Y, len