-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloaddata.py
More file actions
328 lines (283 loc) · 15.4 KB
/
loaddata.py
File metadata and controls
328 lines (283 loc) · 15.4 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Data Loader implementation, specifically designed for in-house datasets. Code will be designed to reflect flexibility in
custom data loaders for new data.
"""
from torch.utils.data import Dataset
from torch import Tensor, empty, as_tensor, tensor, cat, unsqueeze, float32
import torch.nn.functional as F
import os
import math
from tqdm import tqdm
from tqdm.contrib import tzip
import tifffile
import cv2
import random
import numpy as np
from transforms import reformat, normalize1stto99th, Resize, random_horizontal_flip, labels_to_flows, generate_patches
class CellTransposeData(Dataset):
"""
Dataset subclass for loading in any tiff data, serving as a superclass to each dataset type for CellTranspose.
The dataset is expected to possess the following structure:
- /data
- vol1.tiff
...
- voln.tiff
- /labels
- lbl1.tiff
...
- lbln.tiff
*** NOTE: Data and labels are expected to be named in such a way that when sorted in ascending order,
the ith element of data corresponds to the ith label
"""
def __init__(self, split_name, data_dirs, n_chan, pf_dirs=None, do_3D=False, from_3D=False,
evaluate=False, batch_size=1, resize: Resize = None):
"""
Parameters
------------------------------------------------------------------------------------------------
split_name: name corresponding to the split (i.e. train, validation, test, target)
data_dirs: root directory/directories of the dataset, containing 'data' and 'labels' folders
n_chan: Maximum number of channels in input images (i.e. 2 for cytoplasm + nuclei images)
pf_dirs: root directory/directories of pre-calculated flows, if they exist
do_3D: whether or not to train 3D CellTranspose model (requires that from_3d is true)
from_3D: whether input samples are 2D images (False) or 3D volumes (True)
evaluate: if set to true, returns additional information when calling __getitem__()
batch_size: default 1
resize: Resize object containing parameters by which to resize input samples accordingly
"""
self.do_3D = do_3D
self.from_3D = from_3D
self.split_name = split_name
self.evaluate = evaluate
self.d_list_3D = []
self.l_list_3D = []
self.d_list = []
self.l_list = []
for dir_i in data_dirs:
self.d_list = self.d_list + sorted([dir_i + os.sep + 'data' + os.sep + f for f in
os.listdir(os.path.join(dir_i, 'data')) if f.lower()
.endswith('.tiff') or f.lower().endswith('.tif')
or f.lower().endswith('.png')])
if os.path.exists(os.path.join(dir_i,'labels')):
self.l_list = self.l_list + sorted([dir_i + os.sep + 'labels' + os.sep + f for f in
os.listdir(os.path.join(dir_i, 'labels')) if f.lower()
.endswith('.tiff') or f.lower().endswith('.tif')
or f.lower().endswith('.png')])
if pf_dirs is not None:
self.pf_list = []
for dir_i in pf_dirs:
self.pf_list = self.pf_list + sorted([dir_i + os.sep + 'labels' + os.sep + f for f in
os.listdir(os.path.join(dir_i, 'labels')) if f.lower()
.endswith('.tiff') or f.lower().endswith('.tif')])
self.data = []
self.labels = []
self.original_dims = []
self.lbl_len = len(self.l_list)
if self.lbl_len == 0:
assert self.evaluate == True,\
'>>> Folder containing labelled images does not exist, cannot continue without it for training OR validation purposes...'
print('>>> Folder containing labelled images does not exist, continueing without it for evaluation purposes...')
if from_3D:
print('>>> Utilizing 2D model for evaluation on 3D volumes...')
else:
for ind in tqdm(range(len(self.d_list)), desc='Loading {} Dataset...'.format(split_name)):
ext = os.path.splitext(self.d_list[ind])[-1]
if ext == '.tif' or ext == '.tiff':
new_data = as_tensor(tifffile.imread(self.d_list[ind]).astype('float'))
if self.lbl_len != 0:
new_label = as_tensor(tifffile.imread(self.l_list[ind]).astype('int16'))
else:
new_data = as_tensor(cv2.imread(self.d_list[ind], -1).astype('float'))
if self.lbl_len != 0:
new_label = as_tensor(cv2.imread(self.l_list[ind], -1).astype('int16'))
new_data = reformat(new_data, n_chan)
new_data = normalize1stto99th(new_data)
if self.lbl_len != 0:
new_label = reformat(new_label)
else:
new_label = []
if pf_dirs is not None:
new_pf = tifffile.imread(self.pf_list[ind])
new_pf = new_pf.reshape(1, new_pf.shape[0], new_pf.shape[1], new_pf.shape[2])
else:
new_pf = None
if resize is not None:
new_data, new_label, original_dim, _ = resize(new_data, new_label, new_pf)
self.original_dims.append(original_dim)
self.data.append(new_data)
self.labels.append(new_label)
self.target_data_samples = self.data
self.target_label_samples = self.labels
if self.split_name.lower() == 'target' and len(self.data) < batch_size and not from_3D and not do_3D:
for _ in range(1, math.ceil(batch_size / len(self.data))):
self.data = self.data + self.target_data_samples
if self.lbl_len != 0:
self.labels = self.labels + self.target_label_samples
self.data_samples = self.data
self.label_samples = self.labels
def __len__(self):
return len(self.d_list) if (self.do_3D or self.from_3D) else len(self.data)
class TrainCellTransposeData(CellTransposeData):
def __init__(self, split_name, data_dirs, n_chan, pf_dirs=None, do_3D=False, from_3D=False, evaluate=False,
crop_size=(112, 112), has_flows=False, batch_size=1, resize: Resize = None,
preprocessed_data=None, proc_every_epoch=True, result_dir=None):
self.resize = resize
self.crop_size = crop_size
self.has_flows = has_flows
self.from_3D = from_3D
self.preprocessed_data = preprocessed_data
self.do_every_epoch = proc_every_epoch
if self.preprocessed_data is None:
super().__init__(split_name, data_dirs, n_chan, pf_dirs=pf_dirs, do_3D=do_3D,
from_3D=from_3D, evaluate=evaluate, batch_size=batch_size, resize=None)
if self.preprocessed_data is not None:
print('Training preprocessed data provided...')
self.data = as_tensor(np.load(os.path.join(self.preprocessed_data, 'train_preprocessed_data.npy')))
self.labels = as_tensor(np.load(os.path.join(self.preprocessed_data, 'train_preprocessed_labels.npy')))
elif self.do_every_epoch is False and self.preprocessed_data is None:
data_samples = tensor([])
label_samples = tensor([])
for i in tqdm(range(len(self.data)), desc='Preprocessing training data once only...'):
try:
data, labels, dim, _ = self.resize(self.data[i], self.labels[i],
random_scale=random.uniform(0.75, 1.25))
data, labels = random_horizontal_flip(data, labels)
data, labels = train_generate_rand_crop(unsqueeze(data, 0), labels,
crop=crop_size, lbl_flows=has_flows)
if labels.ndim == 3:
labels = as_tensor(np.array([labels_to_flows(labels[i].numpy()) for i in range(len(labels))]),
dtype=float32)
data_samples = cat((data_samples, data))
label_samples = cat((label_samples, labels))
except RuntimeError:
print('Caught Size Mismatch.')
self.data = data_samples
self.labels = label_samples
if result_dir is not None:
np.save(os.path.join(result_dir, 'train_preprocessed_data.npy'), self.data.cpu().detach().numpy())
np.save(os.path.join(result_dir, 'train_preprocessed_labels.npy'), self.labels.cpu().detach().numpy())
# Augmentations and tiling applied to input data (for training and adaptation) -
# separated from DataLoader to allow for possibility of running only once or once per epoch
def process_training_data(self, index, crop_size, has_flows=False):
samples_generated = []
data, labels = self.data[index], self.labels[index]
try:
data, labels, dim, _ = self.resize(data, labels, random_scale=random.uniform(0.75, 1.25))
data, labels = random_horizontal_flip(data, labels)
data, labels = train_generate_rand_crop(unsqueeze(data, 0), labels, crop=crop_size, lbl_flows=has_flows)
if labels.ndim == 3:
labels = as_tensor(np.array([labels_to_flows(labels[i].numpy())
for i in range(len(labels))]), dtype=float32)
return data[0], labels[0]
except RuntimeError:
print('Caught Size Mismatch.')
samples_generated.append(-1)
def __getitem__(self, index):
if self.preprocessed_data is None and self.do_every_epoch:
return self.process_training_data(index, self.crop_size, has_flows=self.has_flows)
else:
return self.data[index], self.labels[index]
def __len__(self):
return len(self.data)
def train_generate_rand_crop(data, label=None, crop=(112, 112), lbl_flows=False):
if data.shape[3] < crop[0]:
pad_x = math.ceil((crop[0] - data.shape[3]) / 2)
data = F.pad(data, (pad_x, pad_x))
label = F.pad(label, (pad_x, pad_x))
if data.shape[2] < crop[1]:
pad_y = math.ceil((crop[1] - data.shape[2]) / 2)
data = F.pad(data, (0, 0, pad_y, pad_y))
label = F.pad(label, (0, 0, pad_y, pad_y))
x_max = data.shape[3] - crop[0]
y_max = data.shape[2] - crop[1]
patch_data = empty((data.shape[0] * 1 * 1, data.shape[1], crop[0], crop[1]))
if lbl_flows:
patch_label = empty((1 * 1, 3, crop[0], crop[1]))
else:
patch_label = empty((label.shape[0] * 1 * 1, crop[0], crop[1]))
i = random.randint(0, x_max)
j = random.randint(0, y_max)
d_patch = data[0, :, j:j + crop[1], i:i + crop[0]]
patch_data[0] = d_patch
if lbl_flows:
l_patch = label[:, j:j + crop[1], i:i + crop[0]]
else:
l_patch = label[0, j:j + crop[1], i:i + crop[0]]
patch_label[0] = l_patch
return patch_data, patch_label
class EvalCellTransposeData(CellTransposeData):
def __init__(self, split_name, data_dirs, n_chan, pf_dirs=None, do_3D=False, from_3D=False,
evaluate=False, resize: Resize = None):
self.from_3D = from_3D
super().__init__(split_name, data_dirs, n_chan, pf_dirs=pf_dirs, do_3D=do_3D, from_3D=from_3D,
evaluate=evaluate, resize=resize)
# Generates patches for validation dataset - only happens once
def pre_generate_validation_patches(self, patch_size, min_overlap):
self.data_samples = tensor([])
self.label_samples = tensor([])
new_d_list = []
new_original_dims = []
for (data, labels, data_fname, original_dim) in tzip(self.data, self.labels, self.d_list, self.original_dims,
desc='Processing Validation Dataset...'):
if data.shape[1] >= patch_size[0] and data.shape[2] >= patch_size[1]:
if len(labels) != 0:
data, labels = generate_patches(unsqueeze(data, 0), labels, patch=patch_size,
min_overlap=min_overlap, lbl_flows=False)
labels = as_tensor(np.array([labels_to_flows(labels[i].numpy()) for i in range(len(labels))]))
else:
data = generate_patches(unsqueeze(data, 0), patch=patch_size,
min_overlap=min_overlap, lbl_flows=False)
self.data_samples = cat((self.data_samples, data))
self.label_samples = cat((self.label_samples, labels))
for _ in range(len(data)):
new_d_list.append(data_fname)
new_original_dims.append(original_dim)
self.d_list = new_d_list
self.original_dims = new_original_dims
def __getitem__(self, index):
if len(self.label_samples) == 0:
label = []
else:
label = self.label_samples[index]
if self.evaluate and not self.from_3D:
return self.data_samples[index], label, self.d_list[index], self.original_dims[index]
else:
return self.data_samples[index], label
# final version of 3D validation dataloader
class EvalCellTransposeData3D(CellTransposeData):
def __init__(self, split_name, data_dirs, n_chan, do_3D=False,
from_3D=False, evaluate=False, resize: Resize = None):
self.resize = resize
self.n_chan = n_chan
super().__init__(split_name, data_dirs, n_chan, do_3D=do_3D, from_3D=from_3D, evaluate=evaluate, resize=resize)
def process_eval_3D(self, index):
ext = os.path.splitext(self.d_list[index])[-1]
if ext == '.tif' or ext == '.tiff':
raw_data_vol = tifffile.imread(self.d_list[index]).astype('float')
else:
raw_data_vol = cv2.imread(self.d_list[index], -1).astype('float')
axis = ('Z', 'Y', 'X')
plane = ('YX', 'ZX', 'ZY')
TP = [(0, 1, 2), (1, 0, 2), (2, 0, 1)]
data_vol = []
original_dim = []
print(f">>> Image path: {self.d_list[index]}")
for ind in range(len(plane)):
new_data = raw_data_vol.transpose(TP[ind])
print(f">>> Processing 3D data on {new_data.shape[0]} {plane[ind]} planes in {axis[ind]} direction...")
new_data_vol = []
new_origin_dim = []
for i in range(len(new_data)):
d = reformat(as_tensor(new_data[i]), self.n_chan)
data = normalize1stto99th(d)
label = []
if self.resize is not None:
data, label, dim, diam = self.resize(data, label)
else:
dim = (data[0], data[1])
new_data_vol.append(data)
new_origin_dim.append(dim)
data_vol.append(new_data_vol)
original_dim.append(new_origin_dim)
return data_vol, self.d_list[index], plane, original_dim, diam
def __getitem__(self, index):
return self.process_eval_3D(index)