-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
70 lines (57 loc) · 2.17 KB
/
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
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
from pathlib import Path
import torch
import numpy as np
import imgaug
import imgaug.augmenters as iaa
from imgaug.augmentables.segmaps import SegmentationMapsOnImage
class CardiacDataset(torch.utils.data.Dataset):
def __init__(self, root, augment_params):
self.all_files = self.extract_files(root)
self.augment_params = augment_params
@staticmethod
def extract_files(root):
"""
Extract the paths to all slices given the root path (ends with train or val)
"""
files = []
for subject in root.glob("*"): # Iterate over the subjects
slice_path = subject/"data" # Get the slices for current subject
for slice in slice_path.glob("*"):
files.append(slice)
return files
@staticmethod
def change_img_to_label_path(path):
"""
Replace data with mask to get the masks
"""
parts = list(path.parts)
parts[parts.index("data")] = "masks"
return Path(*parts)
def augment(self, slice, mask):
"""
Augments slice and segmentation mask in the exact same way
Note the manual seed initialization
"""
random_seed = torch.randint(0, 1000000, (1,))[0].item()
imgaug.seed(random_seed)
mask = SegmentationMapsOnImage(mask, mask.shape)
slice_aug, mask_aug = self.augment_params(image=slice, segmentation_maps=mask)
mask_aug = mask_aug.get_arr()
return slice_aug, mask_aug
def __len__(self):
"""
Return the length of the dataset (length of all files)
"""
return len(self.all_files)
def __getitem__(self, idx):
"""
Given an index return the (augmented) slice and corresponding mask
Add another dimension for pytorch
"""
file_path = self.all_files[idx]
mask_path = self.change_img_to_label_path(file_path)
slice = np.load(file_path).astype(np.float32)
mask = np.load(mask_path)
if self.augment_params:
slice, mask = self.augment(slice, mask)
return np.expand_dims(slice, 0), np.expand_dims(mask, 0)