-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataset.py
184 lines (142 loc) · 6.09 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import albumentations as A
import cv2
import numpy as np
import skimage
import torch
from torch.utils.data import Dataset
from utilities import convert_from_color, get_id
class SemSeg_Custom(Dataset):
"""
Outputs image, mask, boun_mask with the shape of (C,H,W) where C equals to N (number of classes).
On-the-fly cropping and boundary extraction operations.
"""
def __init__(
self,
psp_dir,
masks_dir,
mode='train',
augmentation=None,
preprocessing=None,
image_mask = False,
inference = False):
self.masks_dir = masks_dir
self.psp_dir = psp_dir
self.augmentation = augmentation
self.preprocessing = preprocessing
self.class_values = [0,1] # class values to encode
self.image_mask = image_mask
self.inference = inference
train_ids, validation_ids, test_ids = get_id(psp_dir, verbose=False)
print("Len train_ids: {}, Len val_ids {}, Len test_ids {}".format(len(train_ids),len(validation_ids),len(test_ids)))
masks_dir_ = masks_dir + '\{}'
psp_dir_ = psp_dir + '\SN6_Train_AOI_11_Rotterdam_PS-RGB_{}'
if mode == 'train':
self.psp_list = [psp_dir_.format(id) for id in train_ids]
self.mask_list = [masks_dir_.format(id) for id in train_ids]
elif mode == 'validation':
self.psp_list = [psp_dir_.format(id) for id in validation_ids]
self.mask_list = [masks_dir_.format(id) for id in validation_ids]
elif mode == 'test':
self.psp_list = [psp_dir_.format(id) for id in test_ids]
self.mask_list = [masks_dir_.format(id) for id in test_ids]
@staticmethod
def get_boundary(label, kernel_size = (3,3)):
tlabel = label.astype(np.uint8)
temp = cv2.Canny(tlabel,0,1)
tlabel = cv2.dilate(
temp,
cv2.getStructuringElement(
cv2.MORPH_CROSS,
kernel_size),
iterations = 2)
tlabel = tlabel.astype(np.float32)
tlabel /= 255.
return tlabel
@staticmethod
def _read_img(image_path):
img = skimage.io.imread(image_path, plugin='tifffile')
return img
def __len__(self):
return len(self.mask_list)
def __getitem__(self, idx):
psp_filepath = self.psp_list[idx]
mask_filepath = self.mask_list[idx]
image = self._read_img(psp_filepath)
mask = self._read_img(mask_filepath)
mask_raw = convert_from_color(mask)
masks = [(mask_raw == v) for v in [0,1]]
mask = np.stack(masks, axis=-1).astype('uint8') #(480, 480, 2))
boun_mask = self.get_boundary(mask_raw) #(480, 480, 2))
boun_mask = [(boun_mask == v) for v in [1,0]]
boun_mask = np.stack(boun_mask, axis=-1).astype('uint8') #(480, 480, 2))
if self.augmentation:
transformed = A.Compose(self.augmentation, p=1)(image=image, masks=[mask, boun_mask])
image, mask, boun_mask = transformed['image'], transformed['masks'][0], transformed['masks'][1]
if self.preprocessing:
preprocessed = self.preprocessing(image=image, mask=mask, boundary_mask = boun_mask)
image, mask, boun_mask = preprocessed['image'], preprocessed['mask'], preprocessed['boundary_mask']
image = image[...] / 255.0
image = np.asarray(image).transpose(2,0,1)
mask = np.asarray(mask).transpose(2,0,1)
boun_mask = np.asarray(boun_mask).transpose(2,0,1)
image = torch.as_tensor(image, dtype=torch.float32)
mask = torch.as_tensor(mask, dtype=torch.float32)
boun_mask = torch.as_tensor(boun_mask, dtype=torch.float32)
if self.inference == True:
return image,mask,boun_mask,self.psp_list
if self.image_mask == True :
return image, mask
if self.image_mask == False:
return image, mask, boun_mask
def get_training_augmentation(crop_size):
train_transform = [
A.OneOf([A.RandomCrop(crop_size[0], crop_size[1], p=1.0)
], p=1.0),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.IAAAdditiveGaussianNoise(p=0.2),
A.IAAPerspective(p=0.5),
A.OneOf(
[
A.CLAHE(p=1),
A.RandomBrightness(p=1),
A.RandomGamma(p=1),
],
p=0.7,
),
A.OneOf(
[
A.IAASharpen(p=1),
A.Blur(blur_limit=3, p=1),
A.MotionBlur(blur_limit=3, p=1),
],
p=0.7,
),
A.OneOf(
[
A.RandomContrast(p=1),
A.HueSaturationValue(p=1),
],
p=0.7,
),
]
return A.Compose(train_transform)
def get_val_augmentation(crop_size):
val_transform = [
A.OneOf([A.RandomCrop(crop_size[0], crop_size[1], p=1.0)
], p=1.0),]
return A.Compose(val_transform)
def get_test_augmentation(crop_size): # ensure determinism. replace random crop with center crop.
val_transform = [
A.OneOf([A.CenterCrop(crop_size[0], crop_size[1], p=1.0)
], p=1.0),]
return A.Compose(val_transform)
def to_tensor(x, **kwargs):
return x.transpose(2,0,1).astype('float32')
#preprocessing_fn = get_preprocessing_fn(params['encoder'], params['encoder_weights'])
def get_preprocessing(preprocessing_fn):
_transform = [
A.Lambda(image=preprocessing_fn),
A.Lambda(image=to_tensor, mask= to_tensor),
]
return A.Compose(_transform)