-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
42 lines (27 loc) · 943 Bytes
/
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
import numpy as np
import torch
from torch.utils.data import Dataset
class DnRDataset(Dataset):
def __init__(self, filename, transform=None):
self.filename = filename
self.transform = transform
self.data = np.load(filename, allow_pickle=True)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
sample = self.data[idx]
if self.transform:
sample = self.transform(sample)
return sample
class ApplyOnKey(object):
def __init__(self, on_key, func):
self.on_key = on_key
self.func = func
def __call__(self, sample):
data = sample[self.on_key]
sample[self.on_key] = self.func(data)
return sample
def __repr__(self):
return '{}(on_key={})'.format(self.__class__.__name__, self.on_key) + ' - ' + self.func.__repr__()