-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatasets.py
More file actions
200 lines (166 loc) · 5.92 KB
/
datasets.py
File metadata and controls
200 lines (166 loc) · 5.92 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import lightning as pl
import numpy as np
import sklearn
import tifffile
import torch
from torch.utils.data import DataLoader
def load_mask(mask_path):
mask = np.load(mask_path) # (4, H, W), uint8
assert mask.shape == (4, 1024, 1024)
mask = mask.transpose(1, 2, 0) # (H, W, 4)
return mask.astype(np.float32) / 255.0 # normalize to [0, 1]
def load_image(image_path, channels="full"):
image = tifffile.imread(image_path) # (H, W, 12), float64
assert image.shape == (1024, 1024, 12)
image = np.nan_to_num(image) # replace NaN with 0
if channels == "rgb":
image = np.take(image, [1, 2, 3], 2)
return image.astype(np.float32)
def normalize_image(image, channels="full"):
# mean of train images
mean = np.array(
[
285.8190561180765,
327.22091430696577,
552.9305957826701,
392.1575148484924,
914.3138803812591,
2346.1184507500043,
2884.4831706095824,
2886.442429854111,
3176.7501338557763,
3156.934442092072,
1727.1940075511282,
848.573373995044,
],
dtype=np.float32,
)
# std of train images
std = np.array(
[
216.44975668759372,
269.8880248304874,
309.92790753407064,
397.45655590699,
400.22078920482215,
630.3269651264278,
789.8006920468097,
810.4773696969773,
852.9031432100967,
807.5976198303886,
631.7808113929271,
502.66788721341396,
],
dtype=np.float32,
)
if channels == "rgb":
mean = np.take(mean, [1, 2, 3], 0)
std = np.take(std, [1, 2, 3], 0)
mean = mean.reshape(3, 1, 1)
std = std.reshape(3, 1, 1)
else:
mean = mean.reshape(12, 1, 1)
std = std.reshape(12, 1, 1)
return (image - mean) / std
class TrainValDataset(torch.utils.data.Dataset):
def __init__(self, data_root, sample_indices, augmentations=None, channels="full"):
self.image_paths, self.mask_paths = [], []
self.channels = channels
for i in sample_indices:
self.image_paths.append(data_root / "train_images" / f"train_{i}.tif")
self.mask_paths.append(data_root / "train_masks" / f"train_{i}.npy")
self.augmentations = augmentations
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
sample = {
"image": load_image(self.image_paths[idx], self.channels),
"mask": load_mask(self.mask_paths[idx]),
}
if self.augmentations is not None:
sample = self.augmentations(**sample)
sample["image"] = sample["image"].transpose(2, 0, 1) # (12, H, W) or (3, H, W)
sample["mask"] = sample["mask"].transpose(2, 0, 1) # (4, H, W)
sample["image"] = normalize_image(sample["image"], self.channels)
# add metadata
sample["image_path"] = str(self.image_paths[idx])
sample["mask_path"] = str(self.mask_paths[idx])
return sample
class TestDataset(torch.utils.data.Dataset):
def __init__(self, data_root, channels="full"):
self.channels = channels
self.image_paths = []
for i in range(118): # evaluation_0.tif to evaluation_117.tif
self.image_paths.append(
data_root / "evaluation_images" / f"evaluation_{i}.tif"
)
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
sample = {
"image": load_image(self.image_paths[idx], self.channels),
}
sample["image"] = sample["image"].transpose(2, 0, 1) # (12, H, W) or (3, H, W)
sample["image"] = normalize_image(sample["image"], self.channels)
# add metadata
sample["image_path"] = str(self.image_paths[idx])
return sample
class IDDDataModule(pl.LightningDataModule):
def __init__(
self,
data_root,
augmentations=None,
train_batch_size=8,
val_batch_size=8,
test_batch_size=8,
num_workers=8,
channels="full",
) -> None:
super().__init__()
self.data_dir = data_root
self.train_batch_size = train_batch_size
self.val_batch_size = val_batch_size
self.test_batch_size = test_batch_size
self.augmentations = augmentations
self.num_workers = num_workers
self.channels = channels
def setup(self, stage=None) -> None:
if stage == "fit":
sample_indices = list(range(176)) # train_0.tif to train_175.tif
train_indices, val_indices = sklearn.model_selection.train_test_split(
sample_indices, test_size=0.2, random_state=42
)
self.idd_train = TrainValDataset(
self.data_dir,
train_indices,
augmentations=self.augmentations,
channels=self.channels,
)
self.idd_val = TrainValDataset(
self.data_dir, val_indices, augmentations=None, channels=self.channels
)
if stage == "test":
self.idd_test = TestDataset(self.data_dir, channels=self.channels)
def train_dataloader(self):
return DataLoader(
self.idd_train,
batch_size=self.train_batch_size,
num_workers=self.num_workers,
persistent_workers=True,
pin_memory=True,
)
def val_dataloader(self):
return DataLoader(
self.idd_val,
batch_size=self.val_batch_size,
num_workers=self.num_workers,
persistent_workers=True,
pin_memory=True,
)
def test_dataloader(self):
return DataLoader(
self.idd_test,
batch_size=self.test_batch_size,
num_workers=self.num_workers,
pin_memory=True,
)