-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.py
More file actions
371 lines (317 loc) · 15.2 KB
/
manager.py
File metadata and controls
371 lines (317 loc) · 15.2 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import time
import numpy as np
import data
import model as m
import metrics
import pred
import transform
class Manager:
def __init__(self):
self.datasets = dict()
self.models = []
def get_datasets_index(self):
"""
return a list of existing datasets.
"""
return list(self.datasets.keys())
def get_datasets_number_labels(self, name):
return self.datasets[name].attrs['labels']
def get_datasets_labels_aliases(self, name, asdictionary=True):
if 'labels_aliases' in self.datasets[name].attrs.keys():
if asdictionary:
return {**{f'label_{i:04}': item for i, item in enumerate(self.datasets[name].attrs['labels_aliases'])},
**{f'prediction_{i:04}': item for i, item in enumerate(self.datasets[name].attrs['labels_aliases'])}}
else:
return self.datasets[name].attrs['labels_aliases']
return {} if asdictionary else []
def get_sample(self, dataset, sample):
return self.datasets[dataset][sample]
def get_dataset_samples(self, name, info=True):
sample_info = []
for sample in list(self.datasets[name].keys()):
if info:
sample_info.append((sample, list(self.datasets[name][sample].keys())))
else:
sample_info.append(sample)
return sample_info
def get_dataset_data_for_train(self, name):
images, labels = [], []
for sample in list(self.datasets[name].keys()):
image = None
label = []
for data in list(self.datasets[name][sample].keys()):
if 'image' in data:
image = np.expand_dims(np.array(self.datasets[name][sample][data], dtype=np.float32), axis=-1)
elif 'label' in data:
label.append(np.array(self.datasets[name][sample][data], dtype=np.float32))
else:
raise NotImplementedError
if image is None or len(label) <= 0:
raise ValueError
label = np.moveaxis(np.array(label), 0, -1)
images.append(image)
labels.append(label)
return images, labels
def get_models_list(self, string=True):
models = []
for model in self.models:
if string:
models.append(f'{model.name} {model.input_shape} > {model.output_shape}')
else:
models.append((model.name, model.input_shape, model.output_shape))
return models
def load_dataset(self, filename, labels=None):
"""
load a dataset from an existing file.
"""
dataset = data.read_dataset(filename)
if labels is not None and int(dataset.attrs['labels']) != labels:
raise AssertionError(f'Labels attribute in dataset do not match expected labels.\n{filename}')
if dataset.attrs['name'] in self.datasets.keys():
raise ValueError(f'A dataset with same name already exist.\n{filename}')
self.datasets[dataset.attrs['name']] = dataset
return dataset.attrs['name']
def new_dataset(self, name, labels=0, filename=None):
"""
create a new dataset.
"""
if filename is None:
filename = name + '_DeepSCEM.hdf5'
self.datasets[name] = data.create_dataset(name, labels, filename)
def rename_dataset(self, name, new_name):
dataset = self.datasets.pop(name)
dataset.attrs['name'] = new_name
self.datasets[new_name] = dataset
def rename_sample(self, dataset_name, sample_name, new_name):
dataset = self.datasets[dataset_name]
# https://docs.h5py.org/en/stable/high/group.html#h5py.Group.move
dataset.move(sample_name, new_name)
dataset.flush()
def rename_labels_aliases(self, dataset_name, aliases_table):
dataset = self.datasets[dataset_name]
dataset.attrs['labels_aliases'] = aliases_table
def add_sample(self, name, sample_name, sample_image_filename, sample_labels_filenames):
"""
add a sample composed of multiple files to an existing dataset.
"""
dataset = self.datasets[name]
# read the image
try:
sample_image = data.read_image(sample_image_filename, dtype=np.float32, normalize=True)
except Exception as e:
raise Exception(f'Sample image load error.\n{e}')
# read the labels
sample_labels = []
for choice_label in sample_labels_filenames:
# if label is empty or None, label is considered as valid but blank.
if choice_label == '' or choice_label is None:
sample_label = np.zeros(sample_image.shape, dtype=np.uint8)
else:
try:
sample_label = data.read_image(choice_label, dtype=np.uint8, binarize=True)
if not sample_image.shape == sample_label.shape:
raise Exception(f'Label shape does not match image shape.')
except Exception as e:
raise Exception(f'Sample load error.\n{e}')
sample_labels.append(sample_label)
data.add_sample_to_dataset(dataset, sample_name, sample_image, sample_labels)
def export_sample(self, dataset, sample, folder):
sample_ = self.datasets[dataset][sample]
label_aliases = self.get_datasets_labels_aliases(dataset)
for imgk in sample_.keys():
name = imgk
if imgk in label_aliases.keys():
name += f'_{label_aliases.get(imgk)}'
data.write_image(f'{folder}/{name}', sample_[imgk], extension='tiff')
def crop_sample(self, name, sample_name, z_min, z_max, y_min, y_max, x_min, x_max, new_name=None):
"""
crop an existing sample.
"""
dataset = self.datasets[name]
if new_name is None or new_name == '':
new_name = f'{sample_name}_crop_{z_min}_{z_max}_{y_min}_{y_max}_{x_min}_{x_max}'
data.crop_sample(dataset, sample_name, new_name, z_min, z_max, y_min, y_max, x_min, x_max)
def remove_dataset(self, name):
self.datasets[name].close()
del self.datasets[name]
self.datasets.pop(name, None)
def remove_labels_aliases(self, name):
if 'labels_aliases' in self.datasets[name].attrs.keys():
del self.datasets[name].attrs['labels_aliases']
def remove_sample(self, dataset, sample):
# del self.datasets[dataset][sample]
data.remove_sample_from_dataset(self.datasets[dataset], sample)
def saveas_dataset(self, name, filename):
data.copy_dataset(self.datasets[name], filename)
def eval_dataset(self, reference_name, segmentation_name, f1=True, iou=False):
results = []
ref_ds = self.datasets[reference_name]
seg_ds = self.datasets[segmentation_name]
if not ref_ds.attrs['labels'] == seg_ds.attrs['labels']:
raise ValueError('Datasets number of labels does not match')
for i in range(ref_ds.attrs['labels']):
score_samples = {}
for sample in ref_ds.keys():
if sample in seg_ds.keys():
ref_samples_data = '\t'.join(list(ref_ds[sample].keys()))
if 'label_' in ref_samples_data:
ref = np.array(ref_ds[sample][f'label_{i:04}'])
elif 'prediction_' in ref_samples_data:
ref = np.array(ref_ds[sample][f'prediction_{i:04}'])
else:
raise ValueError('Reference do not have label or prediction')
seg_samples_data = '\t'.join(list(seg_ds[sample].keys()))
if 'prediction_' in seg_samples_data:
seg = np.array(seg_ds[sample][f'prediction_{i:04}'])
elif 'label_' in seg_samples_data:
seg = np.array(seg_ds[sample][f'label_{i:04}'])
else:
raise ValueError('Segmentation do not have label or prediction')
scores = {'f1': None, 'iou': None}
if f1:
scores['f1'] = metrics.f1(ref, seg)
if iou:
scores['iou'] = metrics.iou(ref, seg)
score_samples[sample] = scores
results.append(score_samples)
return results
def distance_transform(self, name):
dataset = self.datasets[name]
for sample in list(self.datasets[name].keys()):
for data in list(self.datasets[name][sample].keys()):
if 'label' in data:
self.datasets[name][sample][data+'_dt'] = transform.label_dt(np.array(self.datasets[name][sample][data]))
def load_model(self, filename, labels=None):
import tensorflow as tf
model = tf.keras.models.load_model(filename, compile=False)
if len(model.input_shape) != len(model.output_shape):
raise AssertionError(f'Model shapes error. Input and output shapes does not match.\n{filename}')
if not (4 <= len(model.input_shape) <= 5):
raise AssertionError(f'Model shapes error. Input shape is not 2D or 3D.\n{filename}')
if not model.input_shape[-1] == 1:
raise AssertionError(f'Model shapes error. Input shape chan is not equal to 1.\n{filename}')
if labels is not None and model.output_shape[-1] != labels:
raise AssertionError(f'Model output label do not match expected labels.\n{filename}')
self.models.append(model)
def save_model(self, index, filename):
import tensorflow as tf
model = self.models[index]
model.save(filename)
def new_model(self,
dimension=2,
architecture='u-net',
backbone='residual',
kernel_size=3,
block_filters=32,
block_per_level=2,
normalization='batchnorm',
depth=5,
outputs=1,
activation='sigmoid',
name=None):
if normalization == 'None':
normalization = False
else:
normalization = normalization.lower()
model = m.create(dimension, architecture.lower(), backbone.lower(), kernel_size, block_filters, block_per_level,
normalization, depth, outputs, activation.lower(), name)
self.models.append(model)
def train_model(self,
model_index,
dataset_name_train,
dataset_name_valid,
loss,
batch_size,
patch_size_z,
patch_size_y,
patch_size_x,
steps_per_epoch,
epochs,
validation_steps,
keep_best=True,
early_stop=False,
augmentations=(False, False),
label_focus=0):
import tensorflow as tf
model = self.models[model_index]
# Get model information and set options
is2d = len(model.input_shape) == 4 # (batch_size, y, x, chan=1)
if not is2d and len(model.input_shape) != 5: # (batch_size, z, y, x, chan=1)
raise NotImplementedError("Input model is not 2D or 3D")
n_classes = self.datasets[dataset_name_train].attrs['labels']
patch_size = (patch_size_y, patch_size_x) if is2d else (patch_size_z, patch_size_y, patch_size_x)
# Load all data in RAM
train_img, train_lbl = self.get_dataset_data_for_train(dataset_name_train)
# Don't load validation data if no validation steps
if validation_steps is None or validation_steps <= 0:
valid_img, valid_lbl = None, None
else:
# Avoid copy of same data
if dataset_name_train == dataset_name_valid:
valid_img, valid_lbl = train_img, train_lbl
else:
valid_img, valid_lbl = self.get_dataset_data_for_train(dataset_name_valid)
# Create callbacks
train_id = int(time.time())
callbacks = [tf.keras.callbacks.CSVLogger(f'{model.name}_{train_id}_logs.csv')]
if keep_best:
callbacks.append(
tf.keras.callbacks.ModelCheckpoint(f'{model.name}_{train_id}_best.h5',
save_best_only=True,
save_weights_only=False))
if early_stop:
callbacks.append(tf.keras.callbacks.EarlyStopping(patience=max(5, epochs//20)))
callbacks.append(tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1))
# Train model
import train
loss = train.get_loss(loss, n_classes)
t0 = time.time()
model = train.train_model(model, (train_img, train_lbl), (valid_img, valid_lbl),
loss, batch_size, patch_size, steps_per_epoch, epochs,
validation_steps, callbacks, augmentations, label_focus)
t1 = time.time()
print(f'training duration: {int(t1 - t0)} s')
self.models[model_index] = model
def pred_model(self,
model_index,
dataset_name,
patch_size_z,
patch_size_y,
patch_size_x,
overlapping,
threshold=None):
import tensorflow as tf
model = self.models[model_index]
dataset = self.datasets[dataset_name]
# Get model information and set options
is2d = len(model.input_shape) == 4 # (batch_size, y, x, chan=1)
if not is2d and len(model.input_shape) != 5: # (batch_size, z, y, x, chan=1)
raise NotImplementedError("Input model is not 2D or 3D")
patch_size = (1, patch_size_y, patch_size_x) if is2d else (patch_size_z, patch_size_y, patch_size_x)
overlap = 1
if overlapping:
overlap = (1, 2, 2) if is2d else (2, 2, 2)
# Prediction
if is2d:
def predict(x):
tf.keras.backend.clear_session()
x = x[0]
return np.expand_dims(model.predict(x, verbose=0), 0)
else:
def predict(x):
tf.keras.backend.clear_session()
return model.predict(x, verbose=0)
# Create output dataset
pred_dataset_name = f'{dataset_name} pred_' + f'{time.time():.4f}'.replace('.', '_')
self.new_dataset(pred_dataset_name, labels=dataset.attrs['labels'])
# Load data, predict and store result
for sample in list(dataset.keys()):
image = np.array(dataset[sample]['image'])
prediction = pred.infer_pad(image,
patch_size,
predict,
overlap=overlap,
verbose=1)
if threshold is not None:
prediction = (prediction > threshold).astype(np.uint8)
data.add_prediction_to_dataset(self.datasets[pred_dataset_name], sample, dataset[sample]['image'], prediction)