-
Notifications
You must be signed in to change notification settings - Fork 51
/
customdatasets.py
262 lines (209 loc) · 7.36 KB
/
customdatasets.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import numpy as np
import torch
from skimage.io import imread
from torch.utils import data
from tqdm.notebook import tqdm
class SegmentationDataSet1(data.Dataset):
"""Most basic image segmentation dataset."""
def __init__(self, inputs: list, targets: list, transform=None):
self.inputs = inputs
self.targets = targets
self.transform = transform
self.inputs_dtype = torch.float32
self.targets_dtype = torch.long
def __len__(self):
return len(self.inputs)
def __getitem__(self, index: int):
# Select the sample
input_ID = self.inputs[index]
target_ID = self.targets[index]
# Load input and target
x, y = imread(str(input_ID)), imread(str(target_ID))
# Preprocessing
if self.transform is not None:
x, y = self.transform(x, y)
# Typecasting
x, y = torch.from_numpy(x).type(self.inputs_dtype), torch.from_numpy(y).type(
self.targets_dtype
)
return x, y
class SegmentationDataSet2(data.Dataset):
"""Image segmentation dataset with caching and pretransforms."""
def __init__(
self,
inputs: list,
targets: list,
transform=None,
use_cache: bool = False,
pre_transform=None,
):
self.inputs = inputs
self.targets = targets
self.transform = transform
self.inputs_dtype = torch.float32
self.targets_dtype = torch.long
self.use_cache = use_cache
self.pre_transform = pre_transform
if self.use_cache:
self.cached_data = []
progressbar = tqdm(range(len(self.inputs)), desc="Caching")
for i, img_name, tar_name in zip(progressbar, self.inputs, self.targets):
img, tar = imread(str(img_name)), imread(str(tar_name))
if self.pre_transform is not None:
img, tar = self.pre_transform(img, tar)
self.cached_data.append((img, tar))
def __len__(self):
return len(self.inputs)
def __getitem__(self, index: int):
if self.use_cache:
x, y = self.cached_data[index]
else:
# Select the sample
input_ID = self.inputs[index]
target_ID = self.targets[index]
# Load input and target
x, y = imread(str(input_ID)), imread(str(target_ID))
# Preprocessing
if self.transform is not None:
x, y = self.transform(x, y)
# Typecasting
x, y = torch.from_numpy(x).type(self.inputs_dtype), torch.from_numpy(y).type(
self.targets_dtype
)
return x, y
class SegmentationDataSet3(data.Dataset):
"""Image segmentation dataset with caching, pretransforms and multiprocessing."""
def __init__(
self,
inputs: list,
targets: list,
transform=None,
use_cache: bool = False,
pre_transform=None,
):
self.inputs = inputs
self.targets = targets
self.transform = transform
self.inputs_dtype = torch.float32
self.targets_dtype = torch.long
self.use_cache = use_cache
self.pre_transform = pre_transform
if self.use_cache:
from itertools import repeat
from multiprocessing import Pool
with Pool() as pool:
self.cached_data = pool.starmap(
self.read_images, zip(inputs, targets, repeat(self.pre_transform))
)
def __len__(self):
return len(self.inputs)
def __getitem__(self, index: int):
if self.use_cache:
x, y = self.cached_data[index]
else:
# Select the sample
input_ID = self.inputs[index]
target_ID = self.targets[index]
# Load input and target
x, y = imread(str(input_ID)), imread(str(target_ID))
# Preprocessing
if self.transform is not None:
x, y = self.transform(x, y)
# Typecasting
x, y = torch.from_numpy(x).type(self.inputs_dtype), torch.from_numpy(y).type(
self.targets_dtype
)
return x, y
@staticmethod
def read_images(inp, tar, pre_transform):
inp, tar = imread(str(inp)), imread(str(tar))
if pre_transform:
inp, tar = pre_transform(inp, tar)
return inp, tar
class SegmentationDataSet4(data.Dataset):
"""Image segmentation dataset with caching, pretransforms and multiprocessing. Output is a dict."""
def __init__(
self,
inputs: list,
targets: list,
transform=None,
use_cache: bool = False,
pre_transform=None,
):
self.inputs = inputs
self.targets = targets
self.transform = transform
self.inputs_dtype = torch.float32
self.targets_dtype = torch.long
self.use_cache = use_cache
self.pre_transform = pre_transform
if self.use_cache:
from itertools import repeat
from multiprocessing import Pool
with Pool() as pool:
self.cached_data = pool.starmap(
self.read_images, zip(inputs, targets, repeat(self.pre_transform))
)
def __len__(self):
return len(self.inputs)
def __getitem__(self, index: int):
if self.use_cache:
x, y = self.cached_data[index]
else:
# Select the sample
input_ID = self.inputs[index]
target_ID = self.targets[index]
# Load input and target
x, y = imread(str(input_ID)), imread(str(target_ID))
# Preprocessing
if self.transform is not None:
x, y = self.transform(x, y)
# Typecasting
x, y = torch.from_numpy(x).type(self.inputs_dtype), torch.from_numpy(y).type(
self.targets_dtype
)
return {
"x": x,
"y": y,
"x_name": self.inputs[index].name,
"y_name": self.targets[index].name,
}
@staticmethod
def read_images(inp, tar, pre_transform):
inp, tar = imread(str(inp)), imread(str(tar))
if pre_transform:
inp, tar = pre_transform(inp, tar)
return inp, tar
class SegmentationDataSetRandom(data.Dataset):
"""Random image segmentation dataset for testing purposes."""
def __init__(
self,
num_samples,
size,
num_classes: int = 4,
inputs_dtype=torch.float32,
targets_dtype=torch.long,
):
self.num_samples = num_samples
self.size = size
self.num_classes = num_classes
self.inputs_dtype = inputs_dtype
self.targets_dtype = targets_dtype
self.cached_data = []
# Generate some random input target pairs
for num in range(self.num_samples):
inp = torch.from_numpy(np.random.uniform(low=0, high=1, size=size))
tar = torch.randint(low=0, high=num_classes, size=size[1:])
self.cached_data.append((inp, tar))
def __len__(self):
return self.num_samples
def __getitem__(self, index: int):
x, y = self.cached_data[index]
# Typecasting
x, y = x.type(self.inputs_dtype), y.type(self.targets_dtype)
return {
"x": x,
"y": y,
"x_name": f"x_name_{index}",
"y_name": f"y_name_{index}",
}