-
Notifications
You must be signed in to change notification settings - Fork 6
/
loader.py
64 lines (53 loc) · 2.04 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from torch.utils.data import Dataset, DataLoader
import torch
import numpy as np
import random
from einops.layers.torch import Rearrange
from scipy.ndimage.morphology import binary_dilation
# ===== normalize over the dataset
def dataset_normalized(imgs):
imgs_normalized = np.empty(imgs.shape)
imgs_std = np.std(imgs)
imgs_mean = np.mean(imgs)
imgs_normalized = (imgs-imgs_mean)/imgs_std
for i in range(imgs.shape[0]):
imgs_normalized[i] = ((imgs_normalized[i] - np.min(imgs_normalized[i])) / (np.max(imgs_normalized[i])-np.min(imgs_normalized[i])))*255
return imgs_normalized
## Temporary
class isic_loader(Dataset):
""" dataset class for Brats datasets
"""
def __init__(self, path_Data, train = True, Test = False):
super(isic_loader, self)
self.train = train
if train:
self.data = np.load(path_Data+'data_train.npy')
self.mask = np.load(path_Data+'mask_train.npy')
else:
if Test:
self.data = np.load(path_Data+'data_test.npy')
self.mask = np.load(path_Data+'mask_test.npy')
else:
self.data = np.load(path_Data+'data_val.npy')
self.mask = np.load(path_Data+'mask_val.npy')
self.data = dataset_normalized(self.data)
self.mask = np.expand_dims(self.mask, axis=3)
self.mask = self.mask /255.
def __getitem__(self, indx):
img = self.data[indx]
seg = self.mask[indx]
if self.train:
img, seg = self.apply_augmentation(img, seg)
seg = torch.tensor(seg.copy())
img = torch.tensor(img.copy())
img = img.permute( 2, 0, 1)
seg = seg.permute( 2, 0, 1)
return {'image': img,
'mask' : seg}
def apply_augmentation(self, img, seg):
if random.random() < 0.5:
img = np.flip(img, axis=1)
seg = np.flip(seg, axis=1)
return img, seg
def __len__(self):
return len(self.data)