-
Notifications
You must be signed in to change notification settings - Fork 14
/
dataloader.py
71 lines (61 loc) · 2.17 KB
/
dataloader.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
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader
import config as cfg
# # If you don't set the data type to object when saving the data...
# np_load_old = np.load
# np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)
def create_dataloader(mode, type=0, snr=0):
if mode == 'train':
return DataLoader(
dataset=Wave_Dataset(mode, type, snr),
batch_size=cfg.batch,
shuffle=True,
num_workers=0,
pin_memory=True,
drop_last=True,
sampler=None
)
elif mode == 'valid':
return DataLoader(
dataset=Wave_Dataset(mode, type, snr),
batch_size=cfg.batch, shuffle=False, num_workers=0
)
elif mode == 'test':
return DataLoader(
dataset=Wave_Dataset(mode, type, snr),
batch_size=cfg.batch, shuffle=False, num_workers=0
)
class Wave_Dataset(Dataset):
def __init__(self, mode, type, snr):
# load data
if mode == 'train':
self.mode = 'train'
print('<Training dataset>')
print('Load the data...')
self.input_path = "DATASET_FILE_PATH"
self.input = np.load(self.input_path)
elif mode == 'valid':
self.mode = 'valid'
print('<Validation dataset>')
print('Load the data...')
self.input_path = "DATASET_FILE_PATH"
self.input = np.load(self.input_path)
# # if you want to use a part of the dataset
# self.input = self.input[:500]
elif mode == 'test':
self.mode = 'test'
print('<Test dataset>')
print('Load the data...')
self.input_path = "DATASET_FILE_PATH"
self.input = np.load(self.input_path)
self.input = self.input[type][snr]
def __len__(self):
return len(self.input)
def __getitem__(self, idx):
inputs = self.input[idx][0]
targets = self.input[idx][1]
# transform to torch from numpy
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
return inputs, targets