-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlicesDataset.py
48 lines (37 loc) · 1.29 KB
/
SlicesDataset.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
"""
Module for Pytorch dataset representations
"""
import torch
from torch.utils.data import Dataset
class SlicesDataset(Dataset):
"""
This class represents an indexable Torch dataset
which could be consumed by the PyTorch DataLoader class
"""
def __init__(self, data):
self.data = data
self.slices = []
for i, d in enumerate(data):
for j in range(d["image"].shape[0]):
self.slices.append((i, j))
def __getitem__(self, idx):
"""
This method is called by PyTorch DataLoader class to return a sample with id idx
Arguments:
idx {int} -- id of sample
Returns:
Dictionary of 2 Torch Tensors of dimensions [1, W, H]
"""
slc = self.slices[idx]
sample = dict()
sample["id"] = idx
sample['image'] = torch.from_numpy(self.data[slc[0]]['image'][slc[1]]).type(torch.FloatTensor).unsqueeze(0)
sample['seg'] = torch.from_numpy(self.data[slc[0]]['seg'][slc[1]]).type(torch.FloatTensor).unsqueeze(0)
return sample
def __len__(self):
"""
This method is called by PyTorch DataLoader class to return number of samples in the dataset
Returns:
int
"""
return len(self.slices)