-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.py
1439 lines (1119 loc) · 51.3 KB
/
util.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
import os
import math
import numpy as np
import random
import h5py
import zarr
import matplotlib
import matplotlib.pyplot as plt
import scipy.ndimage
import copy
from PIL import Image
from tqdm import tqdm
from skimage.io import imsave, imread
from skimage import measure
from collections import namedtuple
from biapy.engine.metrics import jaccard_index_numpy, voc_calculation
def create_plots(results, metrics, job_id, chartOutDir):
"""Create loss and main metric plots with the given results.
Parameters
----------
results : Keras History object
Record of training loss values and metrics values at successive epochs. History object is returned by Keras
`fit() <https://keras.io/api/models/model_training_apis/#fit-method>`_ method.
metrics : List of str
Metrics used.
job_id : str
Jod identifier.
chartOutDir : str
Path where the charts will be stored into.
Examples
--------
+-----------------------------------------------+-----------------------------------------------+
| .. figure:: ../../img/chart_loss.png | .. figure:: ../../img/chart_jaccard_index.png |
| :width: 80% | :width: 80% |
| :align: center | :align: center |
| | |
| Loss values on each epoch | Jaccard index values on each epoch |
+-----------------------------------------------+-----------------------------------------------+
"""
print("Creating training plots . . .")
os.makedirs(chartOutDir, exist_ok=True)
# For matplotlib errors in display
os.environ['QT_QPA_PLATFORM']='offscreen'
# Loss
plt.plot(results['loss'])
if 'val_loss' in results:
plt.plot(results['val_loss'])
plt.title('Model JOBID=' + job_id + ' loss')
plt.ylabel('Value')
plt.xlabel('Epoch')
if 'val_loss' in results:
plt.legend(['Train loss', 'Val. loss'], loc='upper left')
else:
plt.legend(['Train loss'], loc='upper left')
plt.savefig(os.path.join(chartOutDir, job_id + '_loss.png'))
plt.clf()
# Metric
for i in range(len(metrics)):
plt.plot(results[metrics[i]])
plt.plot(results['val_' + metrics[i]])
plt.title('Model JOBID=' + job_id + " " + metrics[i])
plt.ylabel('Value')
plt.xlabel('Epoch')
plt.legend([f'Train {metrics[i]}', f'Val. {metrics[i]}'], loc='upper left')
plt.savefig(os.path.join(chartOutDir, job_id + '_' + metrics[i] +'.png'))
plt.clf()
def threshold_plots(preds_test, Y_test, n_dig, job_id, job_file, char_dir, r_val=0.5):
"""Create a plot with the different metric values binarizing the prediction with different thresholds, from ``0.1``
to ``0.9``.
Parameters
----------
preds_test : 4D Numpy array
Predictions made by the model. E.g. ``(num_of_images, y, x, channels)``.
Y_test : 4D Numpy array
Ground truth of the data. E.g. ``(num_of_images, y, x, channels)``.
n_dig : int
The number of digits used for encoding temporal indices (e.g. ``3``). Used by the DET calculation binary.
job_id : str
Id of the job.
job_file : str
Id and run number of the job.
char_dir : str
Path to store the charts generated.
r_val : float, optional
Threshold values to return.
Returns
-------
t_jac : float
Value of the Jaccard index when the threshold is ``r_val``.
t_voc : float
Value of VOC when the threshold is ``r_val``.
Examples
--------
::
jac, voc = threshold_plots(
preds_test, Y_test, det_eval_ge_path, det_eval_path, det_bin,
n_dig, args.job_id, '278_3', char_dir)
Will generate 3 charts, one per each metric: IoU and VOC. In the x axis represents the 9 different
thresholds applied, that is: ``0.1, 0.2, 0.3, ..., 0.9``. The y axis is the value of the metric in each chart. For
instance, the Jaccard/IoU chart will look like this:
.. image:: ../../img/278_3_threshold_Jaccard.png
:width: 60%
:align: center
In this example, the best value, ``0.868``, is obtained with a threshold of ``0.4``.
"""
char_dir = os.path.join(char_dir, "t_" + job_file)
t_jac = np.zeros(9)
t_voc = np.zeros(9)
objects = []
r_val_pos = 0
for i, t in enumerate(np.arange(0.1,1.0,0.1)):
if t == r_val:
r_val_pos = i
objects.append(str('%.2f' % float(t)))
# Threshold images
bin_preds_test = (preds_test > t).astype(np.uint8)
# Metrics (Jaccard + VOC)
print("Calculate metrics . . .")
t_jac[i] = jaccard_index_numpy(Y_test, bin_preds_test)
t_voc[i] = voc_calculation(Y_test, bin_preds_test, t_jac[i])
print("t_jac[{}]: {}".format(i, t_jac[i]))
print("t_voc[{}]: {}".format(i, t_voc[i]))
# For matplotlib errors in display
os.environ['QT_QPA_PLATFORM']='offscreen'
os.makedirs(char_dir, exist_ok=True)
# Plot Jaccard values
plt.clf()
plt.plot(objects, t_jac)
plt.title('Model JOBID=' + job_file + ' Jaccard', y=1.08)
plt.ylabel('Value')
plt.xlabel('Threshold')
for k, point in enumerate(zip(objects, t_jac)):
plt.text(point[0], point[1], '%.3f' % float(t_jac[k]))
plt.savefig(os.path.join(char_dir, job_file + '_threshold_Jaccard.png'))
plt.clf()
# Plot VOC values
plt.plot(objects, t_voc)
plt.title('Model JOBID=' + job_file + ' VOC', y=1.08)
plt.ylabel('Value')
plt.xlabel('Threshold')
for k, point in enumerate(zip(objects, t_voc)):
plt.text(point[0], point[1], '%.3f' % float(t_voc[k]))
plt.savefig(os.path.join(char_dir, job_file + '_threshold_VOC.png'))
plt.clf()
return t_jac[r_val_pos], t_voc[r_val_pos]
def save_tif(X, data_dir=None, filenames=None, verbose=True):
"""Save images in the given directory.
Parameters
----------
X : 4D/5D numpy array
Data to save as images. The first dimension must be the number of images. E.g.
``(num_of_images, y, x, channels)`` or ``(num_of_images, z, y, x, channels)``.
data_dir : str, optional
Path to store X images.
filenames : List, optional
Filenames that should be used when saving each image.
verbose : bool, optional
To print saving information.
"""
if verbose:
s = X.shape if not isinstance(X, list) else X[0].shape
print("Saving {} data as .tif in folder: {}".format(s, data_dir))
os.makedirs(data_dir, exist_ok=True)
if filenames is not None:
if len(filenames) != len(X):
raise ValueError("Filenames array and length of X have different shapes: {} vs {}".format(len(filenames),len(X)))
if not isinstance(X, list):
_dtype = X.dtype if X.dtype in [np.uint8, np.uint16, np.float32] else np.float32
ndims = X.ndim
else:
_dtype = X[0].dtype if X[0].dtype in [np.uint8, np.uint16, np.float32] else np.float32
ndims = X[0].ndim
d = len(str(len(X)))
for i in tqdm(range(len(X)), leave=False):
if filenames is None:
f = os.path.join(data_dir, str(i).zfill(d)+'.tif')
else:
f = os.path.join(data_dir, os.path.splitext(filenames[i])[0]+'.tif')
if ndims == 4:
if not isinstance(X, list):
aux = np.expand_dims(np.expand_dims(X[i],0).transpose((0,3,1,2)), -1).astype(_dtype)
else:
aux = np.expand_dims(np.expand_dims(X[i][0],0).transpose((0,3,1,2)), -1).astype(_dtype)
else:
if not isinstance(X, list):
aux = np.expand_dims(X[i].transpose((0,3,1,2)), -1).astype(_dtype)
else:
aux = np.expand_dims(X[i][0].transpose((0,3,1,2)), -1).astype(_dtype)
try:
imsave(f, np.expand_dims(aux, 0), imagej=True, metadata={'axes': 'TZCYXS'}, check_contrast=False, compression=('zlib', 1))
except:
imsave(f, np.expand_dims(aux, 0), imagej=True, metadata={'axes': 'TZCYXS'}, check_contrast=False)
def save_tif_pair_discard(X, Y, data_dir=None, suffix="", filenames=None, discard=True, verbose=True):
"""Save images in the given directory.
Parameters
----------
X : 4D/5D numpy array
Data to save as images. The first dimension must be the number of images. E.g.
``(num_of_images, y, x, channels)`` or ``(num_of_images, z, y, x, channels)``.
Y : 4D/5D numpy array
Data mask to save. The first dimension must be the number of images. E.g.
``(num_of_images, y, x, channels)`` or ``(num_of_images, z, y, x, channels)``.
data_dir : str, optional
Path to store X images.
suffix : str, optional
Suffix to apply on output directory.
filenames : List, optional
Filenames that should be used when saving each image.
discard : bool, optional
Whether to discard image/mask pairs if the mask has no label information.
verbose : bool, optional
To print saving information.
"""
if verbose:
s = X.shape if not isinstance(X, list) else X[0].shape
print("Saving {} data as .tif in folder: {}".format(s, data_dir))
os.makedirs(os.path.join(data_dir, 'x'+suffix), exist_ok=True)
os.makedirs(os.path.join(data_dir, 'y'+suffix), exist_ok=True)
if filenames is not None:
if len(filenames) != len(X):
raise ValueError("Filenames array and length of X have different shapes: {} vs {}".format(len(filenames),len(X)))
_dtype = X.dtype if X.dtype in [np.uint8, np.uint16, np.float32] else np.float32
d = len(str(len(X)))
for i in tqdm(range(X.shape[0]), leave=False):
if len(np.unique(Y[i])) >= 2 or not discard:
if filenames is None:
f1 = os.path.join(data_dir, 'x'+suffix, str(i).zfill(d)+'.tif')
f2 = os.path.join(data_dir, 'y'+suffix, str(i).zfill(d)+'.tif')
else:
f1 = os.path.join(data_dir, 'x'+suffix, os.path.splitext(filenames[i])[0]+'.tif')
f2 = os.path.join(data_dir, 'y'+suffix, os.path.splitext(filenames[i])[0]+'.tif')
if X.ndim == 4:
aux = np.expand_dims(np.expand_dims(X[i],0).transpose((0,3,1,2)), -1).astype(_dtype)
else:
aux = np.expand_dims(X[i].transpose((0,3,1,2)), -1).astype(_dtype)
imsave(f1, np.expand_dims(aux, 0), imagej=True, metadata={'axes': 'TZCYXS'}, check_contrast=False, compression=('zlib', 1))
if Y.ndim == 4:
aux = np.expand_dims(np.expand_dims(Y[i],0).transpose((0,3,1,2)), -1).astype(_dtype)
else:
aux = np.expand_dims(Y[i].transpose((0,3,1,2)), -1).astype(_dtype)
imsave(f2, np.expand_dims(aux, 0), imagej=True, metadata={'axes': 'TZCYXS'}, check_contrast=False, compression=('zlib', 1))
def save_img(X=None, data_dir=None, Y=None, mask_dir=None, scale_mask=True,
prefix="", extension=".png", filenames=None):
"""Save images in the given directory.
Parameters
----------
X : 4D numpy array, optional
Data to save as images. The first dimension must be the number of images. E.g. ``(num_of_images, y, x, channels)``.
data_dir : str, optional
Path to store X images.
Y : 4D numpy array, optional
Masks to save as images. The first dimension must be the number of images. E.g. ``(num_of_images, y, x, channels)``.
scale_mask : bool, optional
To allow mask be multiplied by 255.
mask_dir : str, optional
Path to store Y images.
prefix : str, optional
Path to store generated charts.
filenames : list, optional
Filenames that should be used when saving each image. If any provided each image should be named as:
``prefix + "_x_" + image_number + extension`` when ``X.ndim < 4`` and ``prefix + "_x_" + image_number +
"_" + slice_numger + extension`` otherwise. E.g. ``prefix_x_000.png`` when ``X.ndim < 4`` or
``prefix_x_000_000.png`` when ``X.ndim >= 4``. The same applies to ``Y``.
"""
if prefix == "":
p_x = "x_"
p_y = "y_"
else:
p_x = prefix + "_x_"
p_y = prefix + "_y_"
if X is not None:
if data_dir is not None:
os.makedirs(data_dir, exist_ok=True)
else:
print("Not data_dir provided so no image will be saved!")
return
print("Saving images in {}".format(data_dir))
v = 1 if np.max(X) > 2 else 255
if X.ndim > 4:
d = len(str(X.shape[0]*X.shape[3]))
for i in tqdm(range(X.shape[0])):
for j in range(X.shape[3]):
if X.shape[-1] == 1:
im = Image.fromarray((X[i,:,:,j,0]*v).astype(np.uint8))
im = im.convert('L')
else:
im = Image.fromarray((X[i,:,:,j]*v).astype(np.uint8), 'RGB')
if filenames is None:
f = os.path.join(data_dir, p_x + str(i).zfill(d) + "_" + str(j).zfill(d) + extension)
else:
f = os.path.join(data_dir, filenames[(i*j)+j] + extension)
im.save(f)
else:
d = len(str(X.shape[0]))
for i in tqdm(range(X.shape[0])):
if X.shape[-1] == 1:
im = Image.fromarray((X[i,:,:,0]*v).astype(np.uint8))
im = im.convert('L')
else:
im = Image.fromarray((X[i]*v).astype(np.uint8), 'RGB')
if filenames is None:
f = os.path.join(data_dir, p_x + str(i).zfill(d) + extension)
else:
f = os.path.join(data_dir, filenames[i] + extension)
im.save(f)
if Y is not None:
if mask_dir is not None:
os.makedirs(mask_dir, exist_ok=True)
else:
print("Not mask_dir provided so no image will be saved!")
return
print("Saving images in {}".format(mask_dir))
v = 1 if np.max(Y) > 2 or not scale_mask else 255
if Y.ndim > 4:
d = len(str(Y.shape[0]*Y.shape[3]))
for i in tqdm(range(Y.shape[0])):
for j in range(Y.shape[3]):
for k in range(Y.shape[-1]):
im = Image.fromarray((Y[i,:,:,j,k]*v).astype(np.uint8))
im = im.convert('L')
if filenames is None:
c = "" if Y.shape[-1] == 1 else "_c"+str(j)
f = os.path.join(mask_dir, p_y + str(i).zfill(d) + "_" + str(j).zfill(d)+c+extension)
else:
f = os.path.join(data_dir, filenames[(i*j)+j] + extension)
im.save(f)
else:
d = len(str(Y.shape[0]))
for i in tqdm(range(0, Y.shape[0])):
for j in range(Y.shape[-1]):
im = Image.fromarray((Y[i,:,:,j]*v).astype(np.uint8))
im = im.convert('L')
if filenames is None:
c = "" if Y.shape[-1] == 1 else "_c"+str(j)
f = os.path.join(mask_dir, p_y+str(i).zfill(d)+c+extension)
else:
f = os.path.join(mask_dir, filenames[i] + extension)
im.save(f)
def make_weight_map(label, binary = True, w0 = 10, sigma = 5):
"""Generates a weight map in order to make the U-Net learn better the borders of cells and distinguish individual
cells that are tightly packed. These weight maps follow the methodology of the original U-Net paper.
Based on `unet/py_files/helpers.py <https://github.com/deepimagej/python4deepimagej/blob/499955a264e1b66c4ed2c014cb139289be0e98a4/unet/py_files/helpers.py>`_.
Parameters
----------
label : 3D numpy array
Corresponds to a label image. E.g. ``(y, x, channels)``.
binary : bool, optional
Corresponds to whether or not the labels are binary.
w0 : float, optional
Controls for the importance of separating tightly associated entities.
sigma : int, optional
Represents the standard deviation of the Gaussian used for the weight map.
Examples
--------
Notice that weight has been defined where the objects are almost touching
each other.
.. image:: ../../img/weight_map.png
:width: 650
:align: center
"""
# Initialization.
lab = np.array(label)
lab_multi = lab
if len(lab.shape) == 3:
lab = lab[:, :, 0]
# Get shape of label.
rows, cols = lab.shape
if binary:
# Converts the label into a binary image with background = 0
# and cells = 1.
lab[lab == 255] = 1
# Builds w_c which is the class balancing map. In our case, we want
# cells to have weight 2 as they are more important than background
# which is assigned weight 1.
w_c = np.array(lab, dtype=float)
w_c[w_c == 1] = 1
w_c[w_c == 0] = 0.5
# Converts the labels to have one class per object (cell).
lab_multi = measure.label(lab, neighbors = 8, background = 0)
else:
# Converts the label into a binary image with background = 0.
# and cells = 1.
lab[lab > 0] = 1
# Builds w_c which is the class balancing map. In our case, we want
# cells to have weight 2 as they are more important than background
# which is assigned weight 1.
w_c = np.array(lab, dtype=float)
w_c[w_c == 1] = 1
w_c[w_c == 0] = 0.5
components = np.unique(lab_multi)
n_comp = len(components)-1
maps = np.zeros((n_comp, rows, cols))
map_weight = np.zeros((rows, cols))
if n_comp >= 2:
for i in range(n_comp):
# Only keeps current object.
tmp = (lab_multi == components[i+1])
# Invert tmp so that it can have the correct distance.
# transform
tmp = ~tmp
# For each pixel, computes the distance transform to
# each object.
maps[i][:][:] = scipy.ndimage.distance_transform_edt(tmp)
maps = np.sort(maps, axis=0)
# Get distance to the closest object (d1) and the distance to the second
# object (d2).
d1 = maps[0][:][:]
d2 = maps[1][:][:]
map_weight = w0*np.exp(-((d1+d2)**2)/(2*(sigma**2)) ) * (lab==0).astype(int);
map_weight += w_c
return map_weight
def do_save_wm(labels, path, binary = True, w0 = 10, sigma = 5):
"""Retrieves the label images, applies the weight-map algorithm and save the weight maps in a folder. Uses
internally :meth:`util.make_weight_map`.
Based on `deepimagejunet/py_files/helpers.py <https://github.com/deepimagej/python4deepimagej/blob/499955a264e1b66c4ed2c014cb139289be0e98a4/unet/py_files/helpers.py>`_.
Parameters
----------
labels : 4D numpy array
Corresponds to given label images. E.g. ``(num_of_images, y, x, channels)``.
path : str
Refers to the path where the weight maps should be saved.
binary : bool, optional
Corresponds to whether or not the labels are binary.
w0 : float, optional
Controls for the importance of separating tightly associated entities.
sigma : int, optional
Represents the standard deviation of the Gaussian used for the weight
map.
"""
# Copy labels.
labels_ = copy.deepcopy(labels)
# Perform weight maps.
for i in range(len(labels_)):
labels_[i] = make_weight_map(labels[i].copy(), binary, w0, sigma)
maps = np.array(labels_)
n, rows, cols = maps.shape
# Resize correctly the maps so that it can be used in the model.
maps = maps.reshape((n, rows, cols, 1))
# Count number of digits in n. This is important for the number
# of leading zeros in the name of the maps.
n_digits = len(str(n))
# Save path with correct leading zeros.
path_to_save = path + "weight/{b:0" + str(n_digits) + "d}.npy"
# Saving files as .npy files.
for i in range(len(labels_)):
np.save(path_to_save.format(b=i), labels_[i])
return None
def foreground_percentage(mask, class_tag):
"""Percentage of pixels that corresponds to the class in the given image.
Parameters
----------
mask : 2D Numpy array
Image mask to analize.
class_tag : int
Class to find in the image.
Returns
-------
x : float
Percentage of pixels that corresponds to the class. Value between ``0``
and ``1``.
"""
c = 0
for i in range(0, mask.shape[0]):
for j in range(0, mask.shape[1]):
if mask[i, j, 0] == class_tag:
c = c + 1
return c/(mask.shape[0]*mask.shape[1])
def divide_images_on_classes(data, data_mask, out_dir, num_classes=2, th=0.8):
"""Create a folder for each class where the images that have more pixels labeled as the class (in percentage) than
the given threshold will be stored.
Parameters
----------
data : 4D numpy array
Data to save as images. The first dimension must be the number of images. E. g.``(num_of_images, y, x, channels)``.
data_mask : 4D numpy array
Data mask to save as images. The first dimension must be the number of images. E. g. ``(num_of_images, y, x, channels)``.
out_dir : str
Path to save the images.
num_classes : int, optional
Number of classes.
th : float, optional
Percentage of the pixels that must be labeled as a class to save it inside that class folder.
"""
# Create the directories
for i in range(num_classes):
os.makedirs(os.path.join(out_dir, "x", "class"+str(i)), exist_ok=True)
os.makedirs(os.path.join(out_dir, "y", "class"+str(i)), exist_ok=True)
print("Dividing provided data into {} classes . . .".format(num_classes))
d = len(str(data.shape[0]))
for i in tqdm(range(data.shape[0])):
# Assign the image to a class if it has, in percentage, more pixels of
# that class than the given threshold
for j in range(num_classes):
t = foreground_percentage(data_mask[i], j)
if t > th:
im = Image.fromarray(data[i,:,:,0])
im = im.convert('L')
im.save(os.path.join(os.path.join(out_dir, "x", "class"+str(j)), "im_" + str(i).zfill(d) + ".png"))
im = Image.fromarray(data_mask[i,:,:,0]*255)
im = im.convert('L')
im.save(os.path.join(os.path.join(out_dir, "y", "class"+str(j)), "mask_" + str(i).zfill(d) + ".png"))
def save_filters_of_convlayer(model, out_dir, l_num=None, name=None, prefix="", img_per_row=8):
"""Create an image of the filters learned by a convolutional layer. One can identify the layer with ``l_num`` or
``name`` args. If both are passed ``name`` will be prioritized.
Inspired by https://machinelearningmastery.com/how-to-visualize-filters-and-feature-maps-in-convolutional-neural-networks
Parameters
----------
model : Keras Model
Model where the layers are stored.
out_dir : str
Path where the image will be stored.
l_num : int, optional
Number of the layer to extract filters from.
name : str, optional
Name of the layer to extract filters from.
prefix : str, optional
Prefix to add to the output image name.
img_per_row : int, optional
Filters per row on the image.
Raises
------
ValueError
if ``l_num`` and ``name`` not provided.
Examples
--------
To save the filters learned by the layer called ``conv1`` one can call
the function as follows ::
save_filters_of_convlayer(model, char_dir, name="conv1", prefix="model")
That will save in ``out_dir`` an image like this:
.. image:: ../../img/save_filters.png
:width: 60%
:align: center
"""
if l_num is None and name is None:
raise ValueError("One between 'l_num' or 'name' must be provided")
# For matplotlib errors in display
os.environ['QT_QPA_PLATFORM']='offscreen'
# Find layer number of the layer named by 'name' variable
if name is not None:
pos = 0
for layer in model.layers:
if name == layer.name:
break
pos += 1
l_num = pos
filters, biases = model.layers[l_num].get_weights()
# normalize filter values to 0-1 so we can visualize them
f_min, f_max = filters.min(), filters.max()
filters = (filters - f_min) / (f_max - f_min)
rows = int(math.floor(filters.shape[3]/img_per_row))
i = 0
for r in range(rows):
for c in range(img_per_row):
ax = plt.subplot(rows, img_per_row, i+1)
ax.set_xticks([])
ax.set_yticks([])
f = filters[:,:,0,i]
plt.imshow(filters[:,:,0,i], cmap='gray')
i += 1
prefix += "_" if prefix != "" else prefix
plt.savefig(os.path.join(out_dir, prefix + 'f_layer' + str(l_num) + '.png'))
plt.clf()
def check_masks(path, n_classes=2):
"""Check Whether the data masks have the correct labels inspection a few random images of the given path. If the
function gives no error one should assume that the masks are correct.
Parameters
----------
path : str
Path to the data mask.
n_classes : int, optional
Maximum classes that the masks must contain.
"""
print("Checking ground truth classes in {} . . .".format(path))
ids = sorted(next(os.walk(path))[2])
# Check only 4 random images or less if there are not as many
num_sample = [4, len(ids)]
numbers = random.sample(range(0, len(ids)), min(num_sample))
for i in numbers:
img = imread(os.path.join(path, ids[i]))
values, _ = np.unique(img, return_counts=True)
if len(values) > n_classes :
raise ValueError("Error: given mask ({}) has more classes than specified in 'MODEL.N_CLASSES'."
"That variable value need to be set without counting with background class. "
" E.g. if mask has [0,1,2] 'MODEL.N_CLASSES' should be 2.\n"
"Values found: {}".format(os.path.join(path, ids[i]), values))
if not (values == range(len(values))).all() and len(values) > 2:
raise ValueError("Mask values need to be consecutive. E.g. [0,1,2,3...]. Provided: {}"
.format(values))
def img_to_onehot_encoding(img, num_classes=2):
"""Converts image given into one-hot encode format.
The opposite function is :func:`~onehot_encoding_to_img`.
Parameters
----------
img : Numpy 3D/4D array
Image. E.g. ``(y, x, channels)`` or ``(z, y, x, channels)``.
num_classes : int, optional
Number of classes to distinguish.
Returns
-------
one_hot_labels : Numpy 3D/4D array
Data one-hot encoded. E.g. ``(y, x, num_classes)`` or ``(z, y, x, num_classes)``.
"""
if img.ndim == 4:
shape = img.shape[:3]+(num_classes,)
else:
shape = img.shape[:2]+(num_classes,)
encoded_image = np.zeros(shape, dtype=np.int8)
for i in range(num_classes):
if img.ndim == 4:
encoded_image[:,:,:,i] = np.all(img.reshape((-1,1)) == i, axis=1).reshape(shape[:3])
else:
encoded_image[:,:,i] = np.all(img.reshape((-1,1)) == i, axis=1).reshape(shape[:2])
return encoded_image
def onehot_encoding_to_img(encoded_image):
"""Converts one-hot encode image into an image with jus tone channel and all the classes represented by an integer.
The opposite function is :func:`~img_to_onehot_encoding`.
Parameters
----------
encoded_image : Numpy 3D/4D array
Image. E.g. ``(y, x, channels)`` or ``(z, y, x, channels)``.
Returns
-------
img : Numpy 3D/4D array
Data one-hot encoded. E.g. ``(z, y, x, num_classes)``.
"""
if encoded_image.ndim == 4:
shape = encoded_image.shape[:3]+(1,)
else:
shape = encoded_image.shape[:2]+(1,)
img = np.zeros(shape, dtype=np.int8)
for i in range(img.shape[-1]):
img[encoded_image[...,i] == 1] = i
return img
def load_data_from_dir(data_dir, crop=False, crop_shape=None, overlap=(0,0), padding=(0,0), return_filenames=False,
reflect_to_complete_shape=False, check_channel=True, convert_to_rgb=False, check_drange=True):
"""
Load data from a directory. If ``crop=False`` all the data is suposed to have the same shape.
Parameters
----------
data_dir : str
Path to read the data from.
crop : bool, optional
Crop each image into desired shape pointed by ``crop_shape``.
crop_shape : Tuple of 3 ints, optional
Shape of the crop to be made. E.g. ``(y, x, channels)``.
overlap : Tuple of 2 floats, optional
Amount of minimum overlap on x and y dimensions. The values must be on range ``[0, 1)``, that is, ``0%`` or
``99%`` of overlap. E. g. ``(y, x)``.
padding : Tuple of 2 ints, optional
Size of padding to be added on each axis ``(y, x)``. E.g. ``(24, 24)``.
return_filenames : bool, optional
Return a list with the loaded filenames. Useful when you need to save them afterwards with the same names as
the original ones.
reflect_to_complete_shape : bool, optional
Whether to increase the shape of the dimension that have less size than selected patch size padding it with
'reflect'.
check_channel : bool, optional
Whether to check if the crop_shape channel matches with the loaded images' one.
convert_to_rgb : bool, optional
In case RGB images are expected, e.g. if ``crop_shape`` channel is 3, those images that are grayscale are
converted into RGB.
check_drange : bool, optional
Whether to check if the data loaded is in the same range.
Returns
-------
data : 4D Numpy array or list of 3D Numpy arrays
Data loaded. E.g. ``(num_of_images, y, x, channels)`` if all files have same shape, otherwise a list of
``(y, x, channels)`` arrays will be returned.
data_shape : List of tuples
Shapes of all 3D images readed. Useful to reconstruct the original images together with ``crop_shape``.
crop_shape : List of tuples
Shape of the loaded 3D images after cropping. Useful to reconstruct the original images together with
``data_shape``.
filenames : List of str, optional
Loaded filenames.
Examples
--------
::
# EXAMPLE 1
# Case where we need to load 165 images of shape (1024, 768)
data_path = "data/train/x"
load_data_from_dir(data_path)
# The function will print the shape of the created array. In this example:
# *** Loaded data shape is (165, 768, 1024, 1)
# Notice height and width swap because of Numpy ndarray terminology
# EXAMPLE 2
# Case where we need to load 165 images of shape (1024, 768) but
# cropping them into (256, 256, 1) patches
data_path = "data/train/x"
crop_shape = (256, 256, 1)
load_data_from_dir(data_path, crop=True, crop_shape=crop_shape)
# The function will print the shape of the created array. In this example:
# *** Loaded data shape is (1980, 256, 256, 1)
"""
if crop:
from biapy.data.data_2D_manipulation import crop_data_with_overlap
print("Loading data from {}".format(data_dir))
ids = sorted(next(os.walk(data_dir))[2])
data = []
data_shape = []
c_shape = []
filenames = []
if len(ids) == 0:
raise ValueError("No images found in dir {}".format(data_dir))
for n, id_ in tqdm(enumerate(ids), total=len(ids)):
if id_.endswith('.npy'):
img = np.load(os.path.join(data_dir, id_))
elif id_.endswith('.hdf5') or id_.endswith('.h5'):
img = h5py.File(os.path.join(data_dir, id_),'r')
img = np.array(img[list(img)[0]])
else:
img = imread(os.path.join(data_dir, id_))
img = np.squeeze(img)
if img.ndim > 3:
raise ValueError("Read image seems to be 3D: {}. Path: {}".format(img.shape, os.path.join(data_dir, id_)))
filenames.append(id_)
if img.ndim == 2:
img = np.expand_dims(img, -1)
else:
if img.shape[0] <= 3: img = img.transpose((1,2,0))
if reflect_to_complete_shape: img = pad_and_reflect(img, crop_shape, verbose=False)
if crop_shape is not None and check_channel:
if crop_shape[-1] != img.shape[-1]:
if crop_shape[-1] == 3 and convert_to_rgb:
img = np.repeat(img, 3, axis=-1)
else:
raise ValueError("Channel of the patch size given {} does not correspond with the loaded image {}. "
"Please, check the channels of the images!".format(crop_shape[-1], img.shape[-1]))
data_shape.append(img.shape)
img = np.expand_dims(img, axis=0)
if crop and img[0].shape != crop_shape[:2]+(img.shape[-1],):
img = crop_data_with_overlap(img, crop_shape[:2]+(img.shape[-1],), overlap=overlap, padding=padding,
verbose=False)
c_shape.append(img.shape)
data.append(img)
same_shape = True
s = data[0].shape
dtype = data[0].dtype
drange = data_range(data[0])
for i in range(1,len(data)):
if check_drange and drange != data_range(data[i]):
raise ValueError("Input images ({} vs {}) seem to have different data ranges ({} and {} found) Please check it "
"and ensure all images have same data type"
.format(filenames[0], filenames[i], drange, data_range(data[i])))
if s != data[i].shape:
same_shape = False
if crop or same_shape:
data = np.concatenate(data)
print("*** Loaded data shape is {}".format(data.shape))
else:
print("Not all samples seem to have the same shape. Number of samples: {}".format(len(data)))
print("*** First sample shape is {}".format(data[0].shape[1:]))
if return_filenames:
return data, data_shape, c_shape, filenames
else:
return data, data_shape, c_shape
def load_ct_data_from_dir(data_dir, shape=None):
"""Load CT data from a directory.
Parameters
----------
data_dir : str
Path to read the data from.
shape : 3D int tuple, optional
Shape of the data to load. If is not provided the shape is calculated automatically looping over all data
files and it will be the maximum value found per axis. So, given the value the process should be faster.
E.g. ``(y, x, channels)``.