-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFromScratch.py
1266 lines (956 loc) · 47.8 KB
/
FromScratch.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import, division, print_function, unicode_literals
#@markdown ##Load key 3D U-Net dependencies and instantiate network
Notebook_version = '2.2.1'
Network = 'U-Net (3D)'
from builtins import any as b_any
#Put the imported code and libraries here
# !pip install fpdf
import elasticdeform
import tifffile
import imgaug.augmenters as iaa
import os
import csv
import random
import h5py
import imageio
import math
import shutil
import argparse
import neptune
from neptune.types import File
from neptune.integrations.tensorflow_keras import NeptuneCallback
import pandas as pd
from glob import glob
from tqdm import tqdm
from skimage import transform
from skimage import exposure
from skimage import color
from skimage import io
from scipy.ndimage import zoom
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
print("TensorFlow version: {}".format(tf.__version__))
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Conv3D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import ReLU
from tensorflow.keras.layers import MaxPooling3D
from tensorflow.keras.layers import Conv3DTranspose
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.utils import Sequence
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.callbacks import CSVLogger
from tensorflow.keras.callbacks import Callback
from tensorflow.keras.metrics import RootMeanSquaredError
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
from datetime import datetime
import subprocess
from pip._internal.operations.freeze import freeze
import time
from skimage import io
import matplotlib
from skimage import io
from shutil import rmtree
print("Dependencies installed and imported.")
# neptune setup
EXPERIMENT_DESCRIPTION = 'Track the training details'
neptune_run = neptune.init_run(
project="BroadImagingPlatform/DBP-Doe",
api_token="eyJhcGlfYWRkcmVzcyI6Imh0dHBzOi8vYXBwLm5lcHR1bmUuYWkiLCJhcGlfdXJsIjoiaHR0cHM6Ly9hcHAubmVwdHVuZS5haSIsImFwaV9rZXkiOiIyMWU0MmFiZS0yZGVkLTQwMGItYTczNC0yNzdiNTljMTExY2QifQ==",
)
neptune_callback = NeptuneCallback(run=neptune_run)
#Create a variable to get and store relative base path
base_path = os.getcwd()
# Define MultiPageTiffGenerator class
class MultiPageTiffGenerator(Sequence):
def __init__(self,
source_path,
target_path,
batch_size=1,
shape=(128,128,32,1),
augment=False,
augmentations=[],
deform_augment=False,
deform_augmentation_params=(5,3,4),
val_split=0.2,
is_val=False,
random_crop=True,
downscale=1,
binary_target=False):
# If directory with various multi-page tiffiles is provided read as list
if os.path.isfile(source_path):
self.dir_flag = False
self.source = tifffile.imread(source_path)
if binary_target:
self.target = tifffile.imread(target_path).astype(bool)
else:
self.target = tifffile.imread(target_path)
elif os.path.isdir(source_path):
self.dir_flag = True
self.source_dir_list = glob(os.path.join(source_path, '*'))
self.target_dir_list = glob(os.path.join(target_path, '*'))
self.source_dir_list.sort()
self.target_dir_list.sort()
self.shape = shape
self.batch_size = batch_size
self.augment = augment
self.val_split = val_split
self.is_val = is_val
self.random_crop = random_crop
self.downscale = downscale
self.binary_target = binary_target
self.deform_augment = deform_augment
self.on_epoch_end()
if self.augment:
# pass list of augmentation functions
self.seq = iaa.Sequential(augmentations, random_order=True) # apply augmenters in random order
if self.deform_augment:
self.deform_sigma, self.deform_points, self.deform_order = deform_augmentation_params
def __len__(self):
# If various multi-page tiff files provided sum all images within each
if self.augment:
augment_factor = 4
else:
augment_factor = 1
if self.dir_flag:
num_of_imgs = 0
for tiff_path in self.source_dir_list:
num_of_imgs += tifffile.imread(tiff_path).shape[0]
xy_shape = tifffile.imread(self.source_dir_list[0]).shape[1:]
if self.is_val:
if self.random_crop:
crop_volume = self.shape[0] * self.shape[1] * self.shape[2]
volume = xy_shape[0] * xy_shape[1] * self.val_split * num_of_imgs
return math.floor(augment_factor * volume / (crop_volume * self.batch_size * self.downscale))
else:
return math.floor(self.val_split * num_of_imgs / self.batch_size)
else:
if self.random_crop:
crop_volume = self.shape[0] * self.shape[1] * self.shape[2]
volume = xy_shape[0] * xy_shape[1] * (1 - self.val_split) * num_of_imgs
return math.floor(augment_factor * volume / (crop_volume * self.batch_size * self.downscale))
else:
return math.floor(augment_factor*(1 - self.val_split) * num_of_imgs/self.batch_size)
else:
if self.is_val:
if self.random_crop:
crop_volume = self.shape[0] * self.shape[1] * self.shape[2]
volume = self.source.shape[0] * self.source.shape[1] * self.val_split * self.source.shape[2]
return math.floor(augment_factor * volume / (crop_volume * self.batch_size * self.downscale))
else:
return math.floor((self.val_split * self.source.shape[0] / self.batch_size))
else:
if self.random_crop:
crop_volume = self.shape[0] * self.shape[1] * self.shape[2]
volume = self.source.shape[0] * self.source.shape[1] * (1 - self.val_split) * self.source.shape[2]
return math.floor(augment_factor * volume / (crop_volume * self.batch_size * self.downscale))
else:
return math.floor(augment_factor * (1 - self.val_split) * self.source.shape[0] / self.batch_size)
def __getitem__(self, idx):
source_batch = np.empty((self.batch_size,
self.shape[0],
self.shape[1],
self.shape[2],
self.shape[3]))
target_batch = np.empty((self.batch_size,
self.shape[0],
self.shape[1],
self.shape[2],
self.shape[3]))
for batch in range(self.batch_size):
# Modulo operator ensures IndexError is avoided
stack_start = self.batch_list[(idx+batch*self.shape[2])%len(self.batch_list)]
if self.dir_flag:
self.source = tifffile.imread(self.source_dir_list[stack_start[0]])
if self.binary_target:
self.target = tifffile.imread(self.target_dir_list[stack_start[0]]).astype(bool)
else:
self.target = tifffile.imread(self.target_dir_list[stack_start[0]])
src_list = []
tgt_list = []
for i in range(stack_start[1], stack_start[1]+self.shape[2]):
src = self.source[i]
src = transform.downscale_local_mean(src, (self.downscale, self.downscale))
if not self.random_crop:
src = transform.resize(src, (self.shape[0], self.shape[1]), mode='constant', preserve_range=True)
src = self._min_max_scaling(src)
src_list.append(src)
tgt = self.target[i]
tgt = transform.downscale_local_mean(tgt, (self.downscale, self.downscale))
if not self.random_crop:
tgt = transform.resize(tgt, (self.shape[0], self.shape[1]), mode='constant', preserve_range=True)
if not self.binary_target:
tgt = self._min_max_scaling(tgt)
tgt_list.append(tgt)
if self.random_crop:
if src.shape[0] == self.shape[0]:
x_rand = 0
if src.shape[1] == self.shape[1]:
y_rand = 0
if src.shape[0] > self.shape[0]:
x_rand = np.random.randint(src.shape[0] - self.shape[0])
if src.shape[1] > self.shape[1]:
y_rand = np.random.randint(src.shape[1] - self.shape[1])
if src.shape[0] < self.shape[0] or src.shape[1] < self.shape[1]:
raise ValueError('Patch shape larger than (downscaled) source shape')
for i in range(self.shape[2]):
if self.random_crop:
src = src_list[i]
tgt = tgt_list[i]
src_crop = src[x_rand:self.shape[0]+x_rand, y_rand:self.shape[1]+y_rand]
tgt_crop = tgt[x_rand:self.shape[0]+x_rand, y_rand:self.shape[1]+y_rand]
else:
src_crop = src_list[i]
tgt_crop = tgt_list[i]
source_batch[batch,:,:,i,0] = src_crop
target_batch[batch,:,:,i,0] = tgt_crop
print("The shape of the tgt_crop is:", tgt_crop.shape)
print("The shape of the target_batch is:", target_batch[batch,:,:,i,0].shape)
if self.augment:
# On-the-fly data augmentation
source_batch, target_batch = self.augment_volume(source_batch, target_batch)
# Data augmentation by reversing stack
if np.random.random() > 0.5:
source_batch, target_batch = source_batch[::-1], target_batch[::-1]
# Data augmentation by elastic deformation
if np.random.random() > 0.5 and self.deform_augment:
source_batch, target_batch = self.deform_volume(source_batch, target_batch)
if not self.binary_target:
target_batch = self._min_max_scaling(target_batch)
return self._min_max_scaling(source_batch), target_batch
else:
return source_batch, target_batch
def on_epoch_end(self):
# Validation split performed here
self.batch_list = []
# Create batch_list of all combinations of tifffile and stack position
if self.dir_flag:
for i in range(len(self.source_dir_list)):
num_of_pages = tifffile.imread(self.source_dir_list[i]).shape[0]
if self.is_val:
start_page = num_of_pages-math.floor(self.val_split*num_of_pages)
for j in range(start_page, num_of_pages-self.shape[2]):
self.batch_list.append([i, j])
else:
last_page = math.floor((1-self.val_split)*num_of_pages)
for j in range(last_page-self.shape[2]):
self.batch_list.append([i, j])
else:
num_of_pages = self.source.shape[0]
if self.is_val:
start_page = num_of_pages-math.floor(self.val_split*num_of_pages)
for j in range(start_page, num_of_pages-self.shape[2]):
self.batch_list.append([0, j])
else:
last_page = math.floor((1-self.val_split)*num_of_pages)
for j in range(last_page-self.shape[2]):
self.batch_list.append([0, j])
if self.is_val and (len(self.batch_list) <= 0):
raise ValueError('validation_split too small! Increase val_split or decrease z-depth')
random.shuffle(self.batch_list)
def _min_max_scaling(self, data):
n = data - np.min(data)
d = np.max(data) - np.min(data)
return n/d
def class_weights(self):
ones = 0
pixels = 0
if self.dir_flag:
for i in range(len(self.target_dir_list)):
tgt = tifffile.imread(self.target_dir_list[i]).astype(bool)
ones += np.sum(tgt)
pixels += tgt.shape[0]*tgt.shape[1]*tgt.shape[2]
else:
ones = np.sum(self.target)
pixels = self.target.shape[0]*self.target.shape[1]*self.target.shape[2]
p_ones = ones/pixels
p_zeros = 1-p_ones
# Return swapped probability to increase weight of unlikely class
return p_ones, p_zeros
def deform_volume(self, src_vol, tgt_vol):
[src_dfrm, tgt_dfrm] = elasticdeform.deform_random_grid([src_vol, tgt_vol],
axis=(1, 2, 3),
sigma=self.deform_sigma,
points=self.deform_points,
order=self.deform_order)
if self.binary_target:
tgt_dfrm = tgt_dfrm > 0.1
return self._min_max_scaling(src_dfrm), tgt_dfrm
def augment_volume(self, src_vol, tgt_vol):
src_vol_aug = np.empty(src_vol.shape)
tgt_vol_aug = np.empty(tgt_vol.shape)
for i in range(src_vol.shape[3]):
src_aug_z, tgt_aug_z = self.seq(images=src_vol[:,:,:,i,0].astype('float16'),
segmentation_maps=np.expand_dims(tgt_vol[:,:,:,i,0].astype(bool), axis=-1))
src_vol_aug[:,:,:,i,0] = src_aug_z
tgt_vol_aug[:,:,:,i,0] = np.squeeze(tgt_aug_z)
return self._min_max_scaling(src_vol_aug), tgt_vol_aug
def sample_augmentation(self, idx):
src, tgt = self.__getitem__(idx)
src_aug, tgt_aug = self.augment_volume(src, tgt)
if self.deform_augment:
src_aug, tgt_aug = self.deform_volume(src_aug, tgt_aug)
return src_aug, tgt_aug
# Define custom loss and dice coefficient
def dice_coefficient(y_true, y_pred):
eps = 1e-6
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f*y_pred_f)
return (2.*intersection)/(K.sum(y_true_f*y_true_f)+K.sum(y_pred_f*y_pred_f)+eps)
def weighted_binary_crossentropy(zero_weight, one_weight):
def _weighted_binary_crossentropy(y_true, y_pred):
binary_crossentropy = K.binary_crossentropy(y_true, y_pred)
weight_vector = y_true*one_weight+(1.-y_true)*zero_weight
weighted_binary_crossentropy = weight_vector*binary_crossentropy
return K.mean(weighted_binary_crossentropy)
return _weighted_binary_crossentropy
# Custom callback showing sample prediction
class SampleImageCallback(Callback):
def __init__(self, model, sample_data, model_path, save=False):
self.model = model
self.sample_data = sample_data
self.model_path = model_path
self.save = save
if self.save:
plt.savefig(self.model_path + '/epoch_' + str(epoch+1) + '.png')
# Define Unet3D class
class Unet3D:
def __init__(self,
shape=(256,256,16,1)):
if isinstance(shape, str):
shape = eval(shape)
self.shape = shape
input_tensor = Input(self.shape, name='input')
self.model = self.unet_3D(input_tensor)
def down_block_3D(self, input_tensor, filters):
x = Conv3D(filters=filters, kernel_size=(3,3,3), padding='same')(input_tensor)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv3D(filters=filters*2, kernel_size=(3,3,3), padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
def up_block_3D(self, input_tensor, concat_layer, filters):
x = Conv3DTranspose(filters, kernel_size=(2,2,2), strides=(2,2,2))(input_tensor)
x = Concatenate()([x, concat_layer])
x = Conv3D(filters=filters, kernel_size=(3,3,3), padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv3D(filters=filters*2, kernel_size=(3,3,3), padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
def unet_3D(self, input_tensor, filters=32):
d1 = self.down_block_3D(input_tensor, filters=filters)
p1 = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), data_format='channels_last')(d1)
d2 = self.down_block_3D(p1, filters=filters*2)
p2 = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), data_format='channels_last')(d2)
d3 = self.down_block_3D(p2, filters=filters*4)
p3 = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), data_format='channels_last')(d3)
d4 = self.down_block_3D(p3, filters=filters*8)
u1 = self.up_block_3D(d4, d3, filters=filters*4)
u2 = self.up_block_3D(u1, d2, filters=filters*2)
u3 = self.up_block_3D(u2, d1, filters=filters)
output_tensor = Conv3D(filters=1, kernel_size=(1,1,1), activation='sigmoid')(u3)
return Model(inputs=[input_tensor], outputs=[output_tensor])
def summary(self):
return self.model.summary()
# Pass generators instead
def train(self,
epochs,
batch_size,
train_generator,
val_generator,
model_path,
model_name,
optimizer='adam',
learning_rate=0.001,
loss='weighted_binary_crossentropy',
metrics='dice',
ckpt_period=1,
save_best_ckpt_only=False,
ckpt_path=None, neptune_run = neptune_run):
class_weight_zero, class_weight_one = train_generator.class_weights()
if loss == 'weighted_binary_crossentropy':
loss = weighted_binary_crossentropy(class_weight_zero, class_weight_one)
if metrics == 'dice':
metrics = dice_coefficient
if optimizer == 'adam':
optimizer = Adam(learning_rate=learning_rate)
elif optimizer == 'sgd':
optimizer = SGD(learning_rate=learning_rate)
elif optimizer == 'rmsprop':
optimizer = RMSprop(learning_rate=learning_rate)
self.model.compile(optimizer=optimizer,
loss=loss,
metrics=[metrics])
if ckpt_path is not None:
self.model.load_weights(ckpt_path)
full_model_path = os.path.join(model_path, model_name)
if not os.path.exists(full_model_path):
os.makedirs(full_model_path)
log_dir = full_model_path + '/Quality Control'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
ckpt_dir = full_model_path + '/ckpt'
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir)
csv_out_name = log_dir + '/training_evaluation.csv'
if ckpt_path is None:
csv_logger = CSVLogger(csv_out_name)
else:
csv_logger = CSVLogger(csv_out_name, append=True)
if save_best_ckpt_only:
ckpt_name = ckpt_dir + '/' + model_name + '.hdf5'
else:
ckpt_name = ckpt_dir + '/' + model_name + '_epoch_{epoch:02d}_val_loss_{val_loss:.4f}.hdf5'
model_ckpt = ModelCheckpoint(ckpt_name,
verbose=1,
save_freq=ckpt_period,
save_best_only=save_best_ckpt_only,
save_weights_only=True)
sample_batch, __ = val_generator.__getitem__(random.randint(0, len(val_generator)))
sample_img = SampleImageCallback(self.model,
sample_batch,
model_path)
#validation_steps = math.floor(len(val_generator)/batch_size)
#print(f'validation_steps:{validation_steps}')
#print(f'length of validation gen = {len(val_generator)}')
history_callback = self.model.fit(train_generator,
validation_data=val_generator,
#validation_steps=math.floor(len(val_generator)/batch_size),
validation_steps=max(1,math.floor(len(val_generator)/batch_size)),
epochs=epochs,
callbacks=[neptune_callback, csv_logger])
#callbacks=[csv_logger,
#model_ckpt,
#sample_img])
last_ckpt_name = ckpt_dir + '/' + model_name + '_last.hdf5'
self.model.save_weights(last_ckpt_name)
loss_history = []
val_loss_history = []
epoch_history = []
dice_coefficient_history = []
val_dice_coefficient_history = []
# Open the CSV file in append mode
with open(csv_out_name, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
loss_history.append(float(row['loss']))
val_loss_history.append(float(row['val_loss']))
epoch_history.append(int(row['epoch']))
dice_coefficient_history.append(float(row['dice_coefficient']))
val_dice_coefficient_history.append(float(row['val_dice_coefficient']))
print(row)
# Log loss values to Neptune
if neptune_run is not None:
neptune_run['loss'].log(loss_history)
neptune_run['val_loss'].log(val_loss_history)
neptune_run['epoch'].log(epoch_history)
neptune_run['dice_coefficient'].log(dice_coefficient_history)
neptune_run['val_dice_coefficient'].log(val_dice_coefficient_history)
def _min_max_scaling(self, data):
n = data - np.min(data)
d = np.max(data) - np.min(data)
return n/d
def predict(self,
input,
ckpt_path,
z_range=None,
downscaling=None,
true_patch_size=None):
self.model.load_weights(ckpt_path)
if isinstance(downscaling, str):
downscaling = eval(downscaling)
if math.isnan(downscaling):
downscaling = None
if isinstance(true_patch_size, str):
true_patch_size = eval(true_patch_size)
if not isinstance(true_patch_size, tuple):
if math.isnan(true_patch_size):
true_patch_size = None
if isinstance(input, str):
src_volume = tifffile.imread(input)
elif isinstance(input, np.ndarray):
src_volume = input
else:
raise TypeError('Input is not path or numpy array!')
in_size = src_volume.shape
if downscaling or true_patch_size is not None:
x_scaling = 0
y_scaling = 0
if true_patch_size is not None:
x_scaling += true_patch_size[0]/self.shape[0]
y_scaling += true_patch_size[1]/self.shape[1]
if downscaling is not None:
x_scaling += downscaling
y_scaling += downscaling
src_list = []
for i in range(src_volume.shape[0]):
src_list.append(transform.downscale_local_mean(src_volume[i], (int(x_scaling), int(y_scaling))))
src_volume = np.array(src_list)
if z_range is not None:
src_volume = src_volume[z_range[0]:z_range[1]]
src_volume = self._min_max_scaling(src_volume)
src_array = np.zeros((1,
math.ceil(src_volume.shape[1]/self.shape[0])*self.shape[0],
math.ceil(src_volume.shape[2]/self.shape[1])*self.shape[1],
math.ceil(src_volume.shape[0]/self.shape[2])*self.shape[2],
self.shape[3]))
for i in range(src_volume.shape[0]):
src_array[0,:src_volume.shape[1],:src_volume.shape[2],i,0] = src_volume[i]
pred_array = np.empty(src_array.shape)
print(src_volume.dtype)
for i in range(math.ceil(src_volume.shape[1]/self.shape[0])):
for j in range(math.ceil(src_volume.shape[2]/self.shape[1])):
for k in range(math.ceil(src_volume.shape[0]/self.shape[2])):
pred_temp = self.model.predict(src_array[:,
i*self.shape[0]:i*self.shape[0]+self.shape[0],
j*self.shape[1]:j*self.shape[1]+self.shape[1],
k*self.shape[2]:k*self.shape[2]+self.shape[2]])
pred_array[:,
i*self.shape[0]:i*self.shape[0]+self.shape[0],
j*self.shape[1]:j*self.shape[1]+self.shape[1],
k*self.shape[2]:k*self.shape[2]+self.shape[2]] = pred_temp
pred_volume = np.rollaxis(np.squeeze(pred_array), -1)[:src_volume.shape[0],:src_volume.shape[1],:src_volume.shape[2]]
if downscaling is not None:
pred_list = []
for i in range(pred_volume.shape[0]):
pred_list.append(transform.resize(pred_volume[i], (in_size[1], in_size[2]), preserve_range=True))
pred_volume = np.array(pred_list)
return pred_volume
# -------------- Other definitions -----------
W = '\033[0m' # white (normal)
R = '\033[31m' # red
prediction_prefix = 'Predicted_'
print('-------------------')
print('U-Net 3D and dependencies installed.')
# Colors for the warning messages
class bcolors:
WARNING = '\033[31m'
NORMAL = '\033[0m' # white (normal)
All_notebook_versions = pd.read_csv("https://raw.githubusercontent.com/HenriquesLab/ZeroCostDL4Mic/master/Colab_notebooks/Latest_Notebook_versions.csv", dtype=str)
print('Notebook version: '+Notebook_version)
Latest_Notebook_version = All_notebook_versions[All_notebook_versions["Notebook"] == Network]['Version'].iloc[0]
print('Latest notebook version: '+Latest_Notebook_version)
if Notebook_version == Latest_Notebook_version:
print("This notebook is up-to-date.")
else:
print(bcolors.WARNING +"A new version of this notebook has been released. We recommend that you download it at https://github.com/HenriquesLab/ZeroCostDL4Mic/wiki")
if tf.test.gpu_device_name()=='':
print('You do not have GPU access.')
print('Did you change your runtime?')
print('If the runtime setting is correct then Google did not allocate a GPU for your session')
print('Expect slow performance. To access GPU try reconnecting later')
else:
print('You have GPU access')
#using the argparse for options to add flags
parser = argparse.ArgumentParser(description = "train")
parser.add_argument("--number_of_epochs",nargs="?", default=10, type=int, required=False)
parser.add_argument("--model_name",nargs="?", default='model1', type=str, required=False)
parser.add_argument("--learning_rate",nargs="?", default=0.001, type=float, required=False)
parser.add_argument("--training_source", type=str, required=False)
parser.add_argument("--training_target", type=str, required=False)
parser.add_argument("--model_path", type=str, required=False)
parser.add_argument("--testing_source", type=str, required=False)
parser.add_argument("--testing_target", type=str, required=False)
parser.add_argument("--source_path", type=str, required=False)
parser.add_argument("--output_directory", type=str, required=False)
parser.add_argument("--batch_size", type=int, default=24, required=False)
parser.add_argument("--loss_function", type=str, default='weighted_binary_crossentropy', required=False)
parser.add_argument("--optimizer", type=str, default='adam', required=False)
args = parser.parse_args()
#@markdown ###Path to training data:
training_source = args.training_source #@param {type:"string"}
training_target = args.training_target #@param {type:"string"}
#@markdown ---
#@markdown ###Model name and path to model folder:
model_name = args.model_name #@param {type:"string"}
model_path = args.model_path #@param {type:"string"}
full_model_path = os.path.join(model_path, model_name)
#@markdown ---
#@markdown ###Training parameters
number_of_epochs = args.number_of_epochs #@param {type:"number"}
#@markdown ###Default advanced parameters
use_default_advanced_parameters = False #@param {type:"boolean"}
#@markdown <font size = 3>If not, please change:
batch_size = args.batch_size #@param {type:"number"}
patch_size = (32,32,8) #@param {type:"number"} # in pixels
training_shape = patch_size + (1,)
image_pre_processing = 'randomly crop to patch_size' #@param ["randomly crop to patch_size", "resize to patch_size"]
validation_split_in_percent = 50 #@param{type:"number"}
downscaling_in_xy = 1#@param {type:"number"} # in pixels
binary_target = True #@param {type:"boolean"}
loss_function = args.loss_function #@param ["weighted_binary_crossentropy", "binary_crossentropy", "categorical_crossentropy", "sparse_categorical_crossentropy", "mean_squared_error", "mean_absolute_error"]
metrics = 'dice' #@param ["dice", "accuracy"]
optimizer = args.optimizer #@param ["adam", "sgd", "rmsprop"]
learning_rate = args.learning_rate #@param{type:"number"}
if image_pre_processing == "randomly crop to patch_size":
random_crop = True
else:
random_crop = False
if use_default_advanced_parameters:
print("Default advanced parameters enabled")
batch_size = 3
training_shape = (256,256,8,1)
validation_split_in_percent = 50
downscaling_in_xy = 1
random_crop = True
binary_target = True
loss_function = 'weighted_binary_crossentropy'
metrics = 'dice'
optimizer = 'adam'
learning_rate = 0.001
#@markdown ###Checkpointing parameters
#checkpointing_period = 1 #@param {type:"number"}
checkpointing_period = "epoch"
#@markdown <font size = 3>If chosen, only the best checkpoint is saved. Otherwise a checkpoint is saved every epoch:
save_best_only = True #@param {type:"boolean"}
#@markdown ###Resume training
resume_training = False #@param {type:"boolean"}
pretrained_model_choice = "Model_from_file" #@param ["Model_from_file", "bioimageio_model"]
checkpoint_path = "" #@param {type:"string"}
model_id = "" #@param {type:"string"}
# --------------------- Load the model from a bioimageio model (can be path on drive or url / doi) ---
if pretrained_model_choice == "bioimageio_model":
from bioimageio.core import load_raw_resource_description
from zipfile import ZipFile
import requests
model_spec = load_raw_resource_description(model_id)
if "keras_hdf5" not in model_spec.weights:
print("Invalid bioimageio model")
else:
url = model_spec.weights["keras_hdf5"].source
r = requests.get(url, allow_redirects=True)
open("keras_model.h5", 'wb').write(r.content)
checkpoint_path = "keras_model.h5"
if resume_training and checkpoint_path != "":
print('If resume_training is True while checkpoint_path is specified, resume_training will be set to False!')
resume_training = False
# Retrieve last checkpoint
if resume_training:
try:
ckpt_dir_list = glob(full_model_path + '/ckpt/*')
ckpt_dir_list.sort()
last_ckpt_path = ckpt_dir_list[-1]
print('Training will resume from checkpoint:', os.path.basename(last_ckpt_path))
except IndexError:
last_ckpt_path=None
print('CheckpointError: No previous checkpoints were found, training from scratch.')
elif not resume_training and checkpoint_path != "":
last_ckpt_path = checkpoint_path
assert os.path.isfile(last_ckpt_path), 'checkpoint_path does not exist!'
else:
last_ckpt_path=None
# Instantiate Unet3D
model = Unet3D(shape=training_shape)
#here we check that no model with the same name already exist
if not resume_training and os.path.exists(full_model_path):
print(bcolors.WARNING+'The model folder already exists and will be overwritten.'+bcolors.NORMAL)
# print('!! WARNING: Folder already exists and will be overwritten !!')
# shutil.rmtree(full_model_path)
# Show sample image
if os.path.isdir(training_source):
training_source_sample = sorted(glob(os.path.join(training_source, '*')))[0]
training_target_sample = sorted(glob(os.path.join(training_target, '*')))[0]
else:
training_source_sample = training_source
training_target_sample = training_target
print(training_source)
print(training_source_sample)
src_sample = tifffile.imread(training_source_sample)
src_sample = model._min_max_scaling(src_sample)
if binary_target:
tgt_sample = tifffile.imread(training_target_sample).astype(bool)
else:
tgt_sample = tifffile.imread(training_target_sample)
src_down = transform.downscale_local_mean(src_sample[0], (downscaling_in_xy, downscaling_in_xy))
tgt_down = transform.downscale_local_mean(tgt_sample[0], (downscaling_in_xy, downscaling_in_xy))
if random_crop:
true_patch_size = None
if src_down.shape[0] == training_shape[0]:
x_rand = 0
if src_down.shape[1] == training_shape[1]:
y_rand = 0
if src_down.shape[0] > training_shape[0]:
x_rand = np.random.randint(src_down.shape[0] - training_shape[0])
if src_down.shape[1] > training_shape[1]:
y_rand = np.random.randint(src_down.shape[1] - training_shape[1])
if src_down.shape[0] < training_shape[0] or src_down.shape[1] < training_shape[1]:
raise ValueError('Patch shape larger than (downscaled) source shape')
else:
true_patch_size = src_down.shape
def scroll_in_z(z):
src_down = transform.downscale_local_mean(src_sample[z-1], (downscaling_in_xy,downscaling_in_xy))
tgt_down = transform.downscale_local_mean(tgt_sample[z-1], (downscaling_in_xy,downscaling_in_xy))
if random_crop:
src_slice = src_down[x_rand:training_shape[0]+x_rand, y_rand:training_shape[1]+y_rand]
tgt_slice = tgt_down[x_rand:training_shape[0]+x_rand, y_rand:training_shape[1]+y_rand]
else:
src_slice = transform.resize(src_down, (training_shape[0], training_shape[1]), mode='constant', preserve_range=True)
tgt_slice = transform.resize(tgt_down, (training_shape[0], training_shape[1]), mode='constant', preserve_range=True)
# Save model parameters
params = {'training_source': training_source,
'training_target': training_target,
'model_name': model_name,
'model_path': model_path,
'number_of_epochs': number_of_epochs,
'batch_size': batch_size,
'training_shape': training_shape,
'downscaling': downscaling_in_xy,
'true_patch_size': true_patch_size,
'val_split': validation_split_in_percent/100,
'random_crop': random_crop,
'learning_rate':learning_rate,
'patch_size':patch_size,
'loss_function':loss_function,
'optimizer':optimizer,
'metrics':metrics}
neptune_run['parameters'] = params
params_df = pd.DataFrame.from_dict(params, orient='index')
#@markdown ##**Augmentation options**
#@markdown ###Data augmentation
apply_data_augmentation = True #@param {type:"boolean"}
# List of augmentations
augmentations = []
#@markdown ###Gaussian blur
add_gaussian_blur = True #@param {type:"boolean"}
gaussian_sigma = 0.7 #@param {type:"number"}
gaussian_frequency = 0.5 #@param {type:"number"}
if add_gaussian_blur:
augmentations.append(iaa.Sometimes(gaussian_frequency, iaa.GaussianBlur(sigma=(0, gaussian_sigma))))
#@markdown ###Linear contrast
add_linear_contrast = True #@param {type:"boolean"}
contrast_min = 0.4 #@param {type:"number"}
contrast_max = 1.6#@param {type:"number"}
contrast_frequency = 0.5 #@param {type:"number"}
if add_linear_contrast:
augmentations.append(iaa.Sometimes(contrast_frequency, iaa.LinearContrast((contrast_min, contrast_max))))
#@markdown ###Additive Gaussian noise
add_additive_gaussian_noise = True #@param {type:"boolean"}
scale_min = 0 #@param {type:"number"}
scale_max = 0.05 #@param {type:"number"}
noise_frequency = 0.5 #@param {type:"number"}
if add_additive_gaussian_noise:
augmentations.append(iaa.Sometimes(noise_frequency, iaa.AdditiveGaussianNoise(scale=(scale_min, scale_max))))
#@markdown ###Add custom augmenters
add_custom_augmenters = False #@param {type:"boolean"}
augmenters = "" #@param {type:"string"}
if add_custom_augmenters:
augmenter_params = "" #@param {type:"string"}
augmenter_frequency = "" #@param {type:"string"}
aug_lst = augmenters.split(';')
aug_params_lst = augmenter_params.split(';')
aug_freq_lst = augmenter_frequency.split(';')
assert len(aug_lst) == len(aug_params_lst) and len(aug_lst) == len(aug_freq_lst), 'The number of arguments in augmenters, augmenter_params and augmenter_frequency are not the same!'
for __, (aug, param, freq) in enumerate(zip(aug_lst, aug_params_lst, aug_freq_lst)):
aug, param, freq = aug.strip(), param.strip(), freq.strip()
aug_func = iaa.Sometimes(eval(freq), getattr(iaa, aug)(eval(param)))
augmentations.append(aug_func)
#@markdown ###Elastic deformations
add_elastic_deform = True #@param {type:"boolean"}
sigma = 2#@param {type:"number"}
points = 2#@param {type:"number"}
order = 1#@param {type:"number"}
if add_elastic_deform:
deform_params = (sigma, points, order)
else:
deform_params = None
train_generator = MultiPageTiffGenerator(training_source,
training_target,
batch_size=batch_size,
shape=training_shape,
augment=apply_data_augmentation,
augmentations=augmentations,