-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader.py
executable file
·48 lines (42 loc) · 1.43 KB
/
data_loader.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
import os
import numpy as np
import pandas as pd
import torch
import torch.utils.data as data_utils
# read csv file
def readcsv(filename):
data = pd.read_csv(filename)
c = []
data = np.array(data)
for i in range(0, data.shape[0]):
a = data[i][0]
b = np.array(list(a.split(" ")))
c.append(b)
return (np.array(c))
def get_loader(features, batch_size, train_test, num_workers=1):
"""
Build and return a data loader.
"""
if train_test == "train":
dataset = data_utils.TensorDataset(torch.Tensor(features))
loader = data_utils.DataLoader(dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers
)
else:
dataset = data_utils.TensorDataset(torch.Tensor(features))
loader = data_utils.DataLoader(dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers
)
return loader
def create_dirs_if_not_exist(dir_list):
if isinstance(dir_list, list):
for dir in dir_list:
if not os.path.exists(dir):
os.makedirs(dir)
else:
if not os.path.exists(dir_list):
os.makedirs(dir_list)