-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
49 lines (40 loc) · 1.52 KB
/
Copy pathdata_loader.py
File metadata and controls
49 lines (40 loc) · 1.52 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
from __future__ import print_function
import os
from glob import glob
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
# Ignore warnings
import warnings
warnings.filters("ignore")
# Chroma dataset
# root_dir can vary according to the dataset we want to use(CENS or naive chroma)
class chromaDataset(Dataset):
def __init__(self, root_dir):
self.root_dir = root_dir
if not os.path.exists(self.root_dir):
raise Exception("[!] {} not exists.".format(self.root_dir))
self.paths = glob(os.path.join(self.root_dir, '*.csv'))
if len(self.paths) == 0:
raise Exception("[!] No data is found in {}".format(self.root_dir))
self.chromas = []
for path in self.paths:
chroma = pd.read_csv(path)
self.chromas.append(chroma)
self.transform = transforms.Compose([
transforms.ToTensor()
])
def __getitem__(self, index):
chroma = self.chromas[index].as_matrix().astype('float')
out = self.transform(chroma)
return out
def __len__(self):
return len(self.paths)
# Data loader (will be used in main.py)
def get_loader(root_dir, batch_size, num_workers=1, shuffle=True):
print("[*] Loading Data from {}...".format(root_dir))
chroma_data = chromaDataset(root_dir)
data_loader = DataLoader(dataset=chroma_data, batch_size=batch_size,
shuffle=True, num_workers=num_workers)
return data_loader