-
Notifications
You must be signed in to change notification settings - Fork 129
/
dm_image.py
1418 lines (1303 loc) · 61.7 KB
/
dm_image.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 numpy as np
from numpy.random import RandomState
from os import path
import os
from keras.preprocessing.image import (
ImageDataGenerator,
Iterator,
# NumpyArrayIterator
)
from keras.utils.np_utils import to_categorical
import keras.backend as K
import cv2
try:
import dicom
except ImportError:
import pydicom as dicom
from dm_preprocess import DMImagePreprocessor as prep
data_format = K.image_data_format()
def crop_img(img, bbox):
'''Crop an image using bounding box
'''
x,y,w,h = bbox
return img[y:y+h, x:x+w]
def add_img_margins(img, margin_size):
'''Add all zero margins to an image
'''
enlarged_img = np.zeros((img.shape[0]+margin_size*2,
img.shape[1]+margin_size*2))
enlarged_img[margin_size:margin_size+img.shape[0],
margin_size:margin_size+img.shape[1]] = img
return enlarged_img
def to_sparse(y):
'''Convert labels to sparse format if they are onehot encoded
'''
if y.ndim == 1:
sparse_y = y
elif y.ndim == 2:
sparse_y = []
for r in y:
label = r.nonzero()[0]
if len(label) != 1 or r[label[0]] != 1:
raise ValueError('Expect one-hot encoding for y. '
'Got sample:', r)
sparse_y.append(label)
sparse_y = np.concatenate(sparse_y)
else:
raise ValueError('Labels should use either sparse '
'or onehot encoding format. Found '
'shape to be: %s' % (y.shape))
return sparse_y
def index_balancer(index_array, classes, ratio, rng):
'''Balance an index array according to desired neg:pos ratio
'''
current_batch_size = len(index_array)
pos_weight = len(classes) / (np.sum(classes==1) + 1e-7)
neg_weight = len(classes) / (np.sum(classes!=1) + 1e-7)
neg_weight *= ratio
probs = np.zeros(current_batch_size)
probs[classes==1] = pos_weight
probs[classes!=1] = neg_weight
probs /= probs.sum()
index_array = rng.choice(index_array, current_batch_size, p=probs)
index_array.sort() # can avoid repeated img reading from disks.
return index_array
def read_resize_img(fname, target_size=None, target_height=None,
target_scale=None, gs_255=False, rescale_factor=None):
'''Read an image (.png, .jpg, .dcm) and resize it to target size.
'''
if target_size is None and target_height is None:
raise Exception('One of [target_size, target_height] must not be None')
if path.splitext(fname)[1] == '.dcm':
img = dicom.read_file(fname).pixel_array
else:
if gs_255:
img = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
else:
img = cv2.imread(fname, cv2.IMREAD_UNCHANGED)
if target_height is not None:
target_width = int(float(target_height)/img.shape[0]*img.shape[1])
else:
target_height, target_width = target_size
if (target_height, target_width) != img.shape:
img = cv2.resize(
img, dsize=(target_width, target_height),
interpolation=cv2.INTER_CUBIC)
img = img.astype('float32')
if target_scale is not None:
img_max = img.max() if img.max() != 0 else target_scale
img *= target_scale/img_max
if rescale_factor is not None:
img *= rescale_factor
return img
def read_img_for_pred(fname, equalize_hist=False, data_format='channels_last',
dup_3_channels=True,
transformer=None, standardizer=None, **kwargs):
'''Read an image and prepare it for prediction through a network
'''
img = read_resize_img(fname, **kwargs)
if equalize_hist:
img = cv2.equalizeHist(img.astype('uint8'))
nb_channel = 3 if dup_3_channels else 1
if data_format == 'channels_first':
x = np.zeros((nb_channel,) + img.shape, dtype='float32')
x[0,:,:] = img
if dup_3_channels:
x[1,:,:] = img
x[2,:,:] = img
else:
x = np.zeros(img.shape + (nb_channel,), dtype='float32')
x[:,:,0] = img
if dup_3_channels:
x[:,:,1] = img
x[:,:,2] = img
x = transformer(x) if transformer is not None else x
x = standardizer(x) if standardizer is not None else x
return x
def get_roi_patches(img, key_pts, roi_size=(256, 256)):
'''Extract image patches according to a key points list
'''
def clip(v, minv, maxv):
'''Clip a coordinate value to be within an image's bounds
'''
v = minv if v < minv else v
v = maxv if v > maxv else v
return v
patches = np.zeros((len(key_pts),) + roi_size, dtype='float32')
for i, kp in enumerate(key_pts):
if isinstance(kp, np.ndarray):
xc, yc = kp
else:
xc, yc = kp.pt
x = int(xc - roi_size[1]/2)
x = clip(x, 0, img.shape[1])
y = int(yc - roi_size[0]/2)
y = clip(y, 0, img.shape[0])
roi = img[y:y+roi_size[0], x:x+roi_size[1]]
patch = np.zeros(roi_size)
patch[0:roi.shape[0], 0:roi.shape[1]] = roi
patches[i] = patch
return patches
def clust_kpts(key_pts, nb_clust, seed=12345):
'''Cluster key points and return cluster centroids
'''
from sklearn.cluster import KMeans
xy_coord = [ [kp.pt[0], kp.pt[1]] for kp in key_pts ]
xy_coord = np.array(xy_coord)
# K-means.
clt = KMeans(nb_clust, init='k-means++', n_init=10, max_iter=30,
random_state=seed)
clt.fit(xy_coord)
return clt.cluster_centers_
def sweep_img_patches(img, patch_size, stride, target_scale=None,
equalize_hist=False):
nb_row = round(float(img.shape[0] - patch_size)/stride + .49)
nb_col = round(float(img.shape[1] - patch_size)/stride + .49)
nb_row = int(nb_row)
nb_col = int(nb_col)
sweep_hei = patch_size + (nb_row - 1)*stride
sweep_wid = patch_size + (nb_col - 1)*stride
y_gap = int((img.shape[0] - sweep_hei)/2)
x_gap = int((img.shape[1] - sweep_wid)/2)
patch_list = []
for y in xrange(y_gap, y_gap + nb_row*stride, stride):
for x in xrange(x_gap, x_gap + nb_col*stride, stride):
patch = img[y:y+patch_size, x:x+patch_size].copy()
if target_scale is not None:
patch_max = patch.max() if patch.max() != 0 else target_scale
patch *= target_scale/patch_max
if equalize_hist:
patch = cv2.equalizeHist(patch.astype('uint8'))
patch_list.append(patch.astype('float32'))
return np.stack(patch_list), nb_row, nb_col
def get_prob_heatmap(img_list, target_height, target_scale, patch_size, stride,
model, batch_size,
featurewise_center=False, featurewise_mean=91.6,
preprocess=None, parallelized=False,
equalize_hist=False):
'''Sweep image data with a trained model to produce prob heatmaps
Args:
img_list (str or list of str): can be either an image file name or a
list of image file names.
'''
if img_list is None:
return [None]
elif isinstance(img_list, str):
img_list = [img_list]
is_single = True
else:
is_single = False
heatmap_list = []
for img_fn in img_list:
img = read_resize_img(img_fn, target_height=target_height)
img,_ = prep.segment_breast(img)
img = add_img_margins(img, patch_size/2)
patch_dat, nb_row, nb_col = sweep_img_patches(
img, patch_size, stride, target_scale=target_scale,
equalize_hist=equalize_hist)
# Make even patches if necessary.
if parallelized and len(patch_dat) % 2 == 1:
last_patch = patch_dat[-1:,:,:]
patch_dat = np.append(patch_dat, last_patch, axis=0)
appended = True
else:
appended = False
if data_format == 'channels_first':
patch_X = np.zeros((patch_dat.shape[0], 3,
patch_dat.shape[1],
patch_dat.shape[2]),
dtype='float32')
patch_X[:,0,:,:] = patch_dat
patch_X[:,1,:,:] = patch_dat
patch_X[:,2,:,:] = patch_dat
else:
patch_X = np.zeros((patch_dat.shape[0],
patch_dat.shape[1],
patch_dat.shape[2], 3),
dtype='float32')
patch_X[:,:,:,0] = patch_dat
patch_X[:,:,:,1] = patch_dat
patch_X[:,:,:,2] = patch_dat
if featurewise_center:
patch_X -= featurewise_mean
elif preprocess is not None:
patch_X = preprocess(patch_X)
pred = model.predict(patch_X, batch_size=batch_size)
if appended: # remove the appended prediction.
pred = pred[:-1]
heatmap = pred.reshape((nb_row, nb_col, pred.shape[1]))
heatmap_list.append(heatmap)
if is_single:
return heatmap_list[0]
return heatmap_list
class DMImgListIterator(Iterator):
'''An iterator for a flatten image list
'''
def __init__(self, img_list, lab_list, image_data_generator,
target_size=(1152, 896), target_scale=4095, gs_255=False,
data_format='default',
class_mode='binary', validation_mode=False,
balance_classes=False, all_neg_skip=0.,
batch_size=32, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg', verbose=True):
'''DM image iterator
Args:
target_size ([tuple of int]): (height, width).
balance_classes ([bool or float]): Control class balance. When False
or .0, no balancing is performed. When a float, it gives the
ratio of negatives vs. positives. E.g., when balance_classes=2.0,
the image iterator will generate two times more negatives than
positives.
'''
if data_format == 'default':
data_format = K.image_data_format()
self.image_data_generator = image_data_generator
self.target_size = tuple(target_size)
self.target_scale = target_scale
self.gs_255 = gs_255
self.data_format = data_format
# Always gray-scale.
if self.data_format == 'channels_last':
self.image_shape = self.target_size + (1,)
else:
self.image_shape = (1,) + self.target_size
if class_mode not in {'categorical', 'binary', 'sparse', None}:
raise ValueError('Invalid class_mode:', class_mode,
'; expected one of "categorical", '
'"binary", "sparse", or None.')
self.class_mode = class_mode
self.validation_mode = validation_mode
if validation_mode:
balance_classes = False
all_neg_skip = 0.
shuffle = False
self.balance_classes = balance_classes
self.all_neg_skip = all_neg_skip
self.seed = seed
self.save_to_dir = save_to_dir
self.save_prefix = save_prefix
self.save_format = save_format
self.verbose = verbose
# Convert flattened image list.
self.nb_sample = len(img_list)
self.nb_class = 2
self.filenames = list(img_list)
self.classes = np.array(lab_list)
nb_pos = np.sum(self.classes == 1)
nb_neg = np.sum(self.classes == 0)
if verbose:
print('There are %d cancer cases and %d normal cases.' % (nb_pos, nb_neg))
super(DMImgListIterator, self).__init__(
self.nb_sample, batch_size, shuffle, seed)
def next(self):
with self.lock:
index_array, current_index, current_batch_size = next(self.index_generator)
classes_ = self.classes[index_array]
# Obtain the random state for the current batch.
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
while self.all_neg_skip > rng.uniform() and np.all(classes_ == 0):
index_array, current_index, current_batch_size = next(self.index_generator)
classes_ = self.classes[index_array]
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
if self.balance_classes:
ratio = float(self.balance_classes) # neg vs. pos.
index_array = index_balancer(index_array, classes_, ratio, rng)
# The transformation of images is not under thread lock so it can be done in parallel
batch_x = np.zeros((current_batch_size,) + self.image_shape, dtype='float32')
# build batch of image data, read images first.
last_fname = None
for bi, ii in enumerate(index_array): # bi: batch idx; ii: img idx.
fname = self.filenames[ii]
if fname == last_fname:
batch_x[bi] = batch_x[bi-1] # avoid repeated readings.
else:
last_fname = fname
img = read_resize_img(
fname, self.target_size, target_scale=self.target_scale,
gs_255=self.gs_255)
# Always have one channel.
if self.data_format == 'channels_first':
x = img.reshape((1, img.shape[0], img.shape[1]))
else:
x = img.reshape((img.shape[0], img.shape[1], 1))
batch_x[bi] = x
# transform and standardize.
for i, x in enumerate(batch_x):
if not self.validation_mode:
x = self.image_data_generator.random_transform(x)
x = self.image_data_generator.standardize(x)
batch_x[i] = x
# optionally save augmented images to disk for debugging purposes
if self.save_to_dir:
for i in xrange(current_batch_size):
fname = '{prefix}_{index}_{hash}.{format}'.\
format(prefix=self.save_prefix, index=current_index + i,
hash=rng.randint(1e4), format=self.save_format)
img = batch_x[i]
if self.data_format == 'channels_first':
img = img.reshape((img.shape[1], img.shape[2]))
else:
img = img.reshape((img.shape[0], img.shape[1]))
# it seems only 8-bit images are supported.
cv2.imwrite(path.join(self.save_to_dir, fname), img)
# build batch of labels
if self.class_mode == 'sparse':
batch_y = self.classes[index_array]
elif self.class_mode == 'binary':
batch_y = self.classes[index_array].astype('float32')
elif self.class_mode == 'categorical':
batch_y = to_categorical(self.classes[index_array], self.nb_class)
else:
return batch_x
return batch_x, batch_y
class DMExamListIterator(Iterator):
'''An iterator for a flatten exam list
'''
def __init__(self, exam_list, image_data_generator,
target_size=(1152, 896), target_scale=4095, gs_255=False,
data_format='default',
class_mode='binary', validation_mode=False, prediction_mode=False,
balance_classes=False, all_neg_skip=0.,
batch_size=16, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg', verbose=True):
if data_format == 'default':
data_format = K.image_data_format()
self.image_data_generator = image_data_generator
self.target_size = tuple(target_size)
self.target_scale = target_scale
self.gs_255 = gs_255
self.data_format = data_format
# Always gray-scale. Two inputs: CC and MLO.
if self.data_format == 'channels_last':
self.image_shape = self.target_size + (1,)
else:
self.image_shape = (1,) + self.target_size
if class_mode not in {'categorical', 'binary', 'sparse', None}:
raise ValueError('Invalid class_mode:', class_mode,
'; expected one of "categorical", '
'"binary", "sparse", or None.')
self.class_mode = class_mode
self.validation_mode = validation_mode
self.prediction_mode = prediction_mode
if validation_mode or prediction_mode:
shuffle = False
balance_classes = False
all_neg_skip = 0.
self.balance_classes = balance_classes
self.all_neg_skip = all_neg_skip
self.seed = seed
self.save_to_dir = save_to_dir
self.save_prefix = save_prefix
self.save_format = save_format
self.verbose = verbose
# Convert exam list.
self.exam_list = exam_list
self.nb_exam = len(exam_list)
self.nb_class = 2
self.err_counter = 0
# For each exam: 0 => subj id, 1 => exam idx, 2 => exam dat.
self.classes = [ (e[2]['L']['cancer'], e[2]['R']['cancer'])
for e in exam_list ]
self.classes = np.array(self.classes) # (exams, breasts)
if verbose:
print('For left breasts, normal=%d, cancer=%d, unimaged/masked=%d.' %
(np.sum(self.classes[:, 0] == 0),
np.sum(self.classes[:, 0] == 1),
np.sum(np.isnan(self.classes[:, 0])))
)
print('For right breasts, normal=%d, cancer=%d, unimaged/masked=%d.' %
(np.sum(self.classes[:, 1] == 0),
np.sum(self.classes[:, 1] == 1),
np.sum(np.isnan(self.classes[:, 1])))
)
super(DMExamListIterator, self).__init__(
self.nb_exam, batch_size, shuffle, seed)
def next(self):
with self.lock:
index_array, current_index, current_batch_size = next(self.index_generator)
classes_ = np.array([ 1 if p[0] or p[1] else 0 for
p in self.classes[index_array, :] ])
# Obtain the random state for the current batch.
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
while self.all_neg_skip > rng.uniform() and np.all(classes_ == 0):
index_array, current_index, current_batch_size = next(self.index_generator)
classes_ = np.array([ 1 if p[0] or p[1] else 0 for
p in self.classes[index_array, :] ])
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
if self.balance_classes:
ratio = float(self.balance_classes) # neg vs. pos.
index_array = index_balancer(index_array, classes_, ratio, rng)
# batch size measures the number of exams, an exam has two breasts.
current_batch_size = current_batch_size*2
# The transformation of images is not under thread lock so it can be done in parallel
if self.prediction_mode:
batch_x_cc = [] # a list (of breasts) of lists of image arrays.
batch_x_mlo = []
batch_subj = []
batch_exam = []
else:
batch_x_cc = np.zeros( (current_batch_size,) + self.image_shape, dtype='float32' )
batch_x_mlo = np.zeros( (current_batch_size,) + self.image_shape, dtype='float32' )
def draw_img(img_df, exam=None):
'''Read image(s) based on different modes
Returns: a single image array or a list of image arrays
'''
try:
if self.prediction_mode:
img = []
for fname in img_df['filename']:
img.append(read_resize_img(
fname, self.target_size,
target_scale=self.target_scale,
gs_255=self.gs_255))
if len(img) == 0:
raise ValueError('empty image dataframe')
else:
if self.validation_mode:
fname = img_df['filename'].iloc[0] # read the canonical view.
else: # training mode.
fname = img_df['filename'].sample(1, random_state=rng).iloc[0]
img = read_resize_img(
fname, self.target_size, target_scale=self.target_scale,
gs_255=self.gs_255)
except ValueError:
if self.err_counter < 10:
print "Error encountered reading an image dataframe:",
print img_df, "Use a blank image instead."
print "Exam caused trouble:", exam
self.err_counter += 1
img = np.zeros(self.target_size, dtype='float32')
return img
def read_breast_imgs(breast_dat, **kwargs):
'''Read the images for both views for a breast
'''
#!!! if a view is missing, use a zero-filled 2D array.
#!!! this may need to be changed depending on the deep learning design.
if breast_dat['CC'] is None:
img_cc = np.zeros(self.target_size, dtype='float32')
else:
img_cc = draw_img(breast_dat['CC'], **kwargs)
if breast_dat['MLO'] is None:
img_mlo = np.zeros(self.target_size, dtype='float32')
else:
img_mlo = draw_img(breast_dat['MLO'], **kwargs)
# Convert all to lists of image arrays.
if not isinstance(img_cc, list):
img_cc = [img_cc]
if not isinstance(img_mlo, list):
img_mlo = [img_mlo]
# Reshape each image array in the image lists.
for i, img_cc_ in enumerate(img_cc):
# Always have one channel.
if self.data_format == 'channels_first':
img_cc[i] = img_cc_.reshape((1, img_cc_.shape[0], img_cc_.shape[1]))
else:
img_cc[i] = img_cc_.reshape((img_cc_.shape[0], img_cc_.shape[1], 1))
for i, img_mlo_ in enumerate(img_mlo):
if self.data_format == 'channels_first':
img_mlo[i] = img_mlo_.reshape((1, img_mlo_.shape[0], img_mlo_.shape[1]))
else:
img_mlo[i] = img_mlo_.reshape((img_mlo_.shape[0], img_mlo_.shape[1], 1))
# Only predictin mode needs lists of image arrays.
if not self.prediction_mode:
img_cc = img_cc[0]
img_mlo = img_mlo[0]
return (img_cc, img_mlo)
# build batch of image data
adv = 0
# last_eidx = None
for eidx in index_array:
# last_eidx = eidx # no copying because sampling a diff img is expected.
subj_id = self.exam_list[eidx][0]
exam_idx = self.exam_list[eidx][1]
exam_dat = self.exam_list[eidx][2]
img_cc, img_mlo = read_breast_imgs(exam_dat['L'], exam=self.exam_list[eidx])
if not self.prediction_mode:
batch_x_cc[adv] = img_cc
batch_x_mlo[adv] = img_mlo
adv += 1
else:
# left_cc = img_cc
# left_mlo = img_mlo
batch_x_cc.append(img_cc)
batch_x_mlo.append(img_mlo)
img_cc, img_mlo = read_breast_imgs(exam_dat['R'], exam=self.exam_list[eidx])
if not self.prediction_mode:
batch_x_cc[adv] = img_cc
batch_x_mlo[adv] = img_mlo
adv += 1
else:
# right_cc = img_cc
# right_mlo = img_mlo
batch_x_cc.append(img_cc)
batch_x_mlo.append(img_mlo)
batch_subj.append(subj_id)
batch_exam.append(exam_idx)
# transform and standardize.
for i in xrange(current_batch_size):
if self.prediction_mode:
for ii, img_cc_ in enumerate(batch_x_cc[i]):
if not np.all(img_cc_ == 0):
batch_x_cc[i][ii] = \
self.image_data_generator.standardize(img_cc_)
for ii, img_mlo_ in enumerate(batch_x_mlo[i]):
if not np.all(img_mlo_ == 0):
batch_x_mlo[i][ii] = \
self.image_data_generator.standardize(img_mlo_)
else:
if not np.all(batch_x_cc[i] == 0):
if not self.validation_mode:
batch_x_cc[i] = self.image_data_generator.\
random_transform(batch_x_cc[i])
batch_x_cc[i] = self.image_data_generator.\
standardize(batch_x_cc[i])
if not np.all(batch_x_mlo[i] == 0):
if not self.validation_mode:
batch_x_mlo[i] = self.image_data_generator.\
random_transform(batch_x_mlo[i])
batch_x_mlo[i] = self.image_data_generator.\
standardize(batch_x_mlo[i])
# optionally save augmented images to disk for debugging purposes
if self.save_to_dir:
def save_aug_img(img, bi, view, ii=None):
'''Save an augmented image
Args:
img (array): image array.
bi (int): breast index.
view (str): view name.
ii ([int]): (within breast) image index.
'''
if not self.prediction_mode:
fname_base = '{prefix}_{index}_{view}_{hash}'.\
format(prefix=self.save_prefix, index=bi, view=view,
hash=rng.randint(1e4))
else:
fname_base = '{prefix}_{bi}_{view}_{ii}_{hash}'.\
format(prefix=self.save_prefix, bi=bi, view=view, ii=ii,
hash=rng.randint(1e4))
fname = fname_base + '.' + self.save_format
if self.data_format == 'channels_first':
img = img.reshape((img.shape[1], img.shape[2]))
else:
img = img.reshape((img.shape[0], img.shape[1]))
# it seems only 8-bit images are supported.
cv2.imwrite(path.join(self.save_to_dir, fname), img)
for i in xrange(current_batch_size):
if not self.prediction_mode:
img_cc = batch_x_cc[i]
img_mlo = batch_x_mlo[i]
save_aug_img(img_cc, current_index*2 + i, 'cc')
save_aug_img(img_mlo, current_index*2 + i, 'mlo')
else:
for ii, img_cc_ in enumerate(batch_x_cc[i]):
save_aug_img(img_cc_, current_index*2 + i, 'cc', ii)
for ii, img_mlo_ in enumerate(batch_x_mlo[i]):
save_aug_img(img_mlo_, current_index*2 + i, 'mlo', ii)
# build batch of labels
flat_classes = self.classes[index_array, :].ravel() # [L, R, L, R, ...]
# flat_classes = flat_classes[np.logical_not(np.isnan(flat_classes))]
flat_classes[np.isnan(flat_classes)] = 0 # fill in non-cancerous labels.
if self.class_mode == 'sparse':
batch_y = flat_classes
elif self.class_mode == 'binary':
batch_y = flat_classes.astype('float32')
elif self.class_mode == 'categorical':
batch_y = to_categorical(flat_classes, self.nb_class)
else: # class_mode is None.
if self.prediction_mode:
return [batch_subj, batch_exam, batch_x_cc, batch_x_mlo]
else:
return [batch_x_cc, batch_x_mlo]
if self.prediction_mode:
return [batch_subj, batch_exam, batch_x_cc, batch_x_mlo], batch_y
else:
return [batch_x_cc, batch_x_mlo], batch_y
#### An illustration of what is returned in prediction mode: ####
# let exam_blob = next(pred_datgen_exam)
#
# then exam_blob[0][1][0][0]
# / | \ \
# / | \ \
# / | \ \--- 1st img
# img cc 1st
# tuple view breast
#
# if class_mode is None, then the first index is not needed.
class DMCandidROIIterator(Iterator):
'''An iterator for candidate ROIs from mammograms
'''
def __init__(self, image_data_generator, img_list, lab_list=None,
target_height=1024, target_scale=4095, gs_255=False,
data_format='default',
class_mode='categorical', validation_mode=False,
img_per_batch=2, roi_per_img=32, roi_size=(256, 256),
one_patch_mode=False,
low_int_threshold=.05, blob_min_area=3,
blob_min_int=.5, blob_max_int=.85, blob_th_step=10,
tf_graph=None, roi_clf=None, clf_bs=32, cutpoint=.5,
amp_factor=1., return_sample_weight=True,
auto_batch_balance=True,
all_neg_skip=0., shuffle=True, seed=None,
return_raw_img=False,
save_to_dir=None, save_prefix='', save_format='jpeg',
verbose=True):
'''DM candidate roi iterator
'''
if data_format == 'default':
data_format = K.image_data_format()
self.image_data_generator = image_data_generator
self.target_height = target_height
self.target_scale = target_scale
self.gs_255 = gs_255
self.data_format = data_format
self.roi_per_img = roi_per_img
self.roi_size = roi_size
self.one_patch_mode = one_patch_mode
if one_patch_mode:
amp_factor = 1.
self.tf_graph = tf_graph
self.roi_clf = roi_clf
self.clf_bs = clf_bs
self.cutpoint = cutpoint
if amp_factor < 1.:
raise Exception('amp_factor must not be less than 1.0')
self.amp_factor = amp_factor
self.return_sample_weight = return_sample_weight
self.auto_batch_balance = auto_batch_balance
self.low_int_threshold = low_int_threshold
# Always gray-scale.
if self.data_format == 'channels_last':
self.image_shape = self.roi_size + (1,)
else:
self.image_shape = (1,) + self.roi_size
if class_mode not in {'categorical', 'binary', 'sparse', None}:
raise ValueError('Invalid class_mode:', class_mode,
'; expected one of "categorical", '
'"binary", "sparse", or None.')
self.class_mode = class_mode
self.validation_mode = validation_mode
if validation_mode:
all_neg_skip = 0.
shuffle = False
self.all_neg_skip = all_neg_skip
self.seed = seed
self.return_raw_img = return_raw_img
self.save_to_dir = save_to_dir
self.save_prefix = save_prefix
self.save_format = save_format
self.verbose = verbose
# Convert flattened image list.
self.nb_sample = len(img_list)
self.nb_class = 3 # bkg, pos and neg.
self.filenames = list(img_list)
if lab_list is not None:
self.classes = np.array(lab_list)
nb_pos = np.sum(self.classes == 1)
nb_neg = np.sum(self.classes == 0)
if verbose:
print('There are %d cancer cases and %d normal cases.' % \
(nb_pos, nb_neg))
else:
self.classes = None
# Build a blob detector.
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = blob_min_area
params.maxArea = roi_size[0]*roi_size[1]
params.filterByCircularity = False
params.filterByColor = False
params.filterByConvexity = False
params.filterByInertia = False
# blob detection only works with "uint8" images.
params.minThreshold = int(blob_min_int*255)
params.maxThreshold = int(blob_max_int*255)
params.thresholdStep = blob_th_step
# import pdb; pdb.set_trace()
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
self.blob_detector = cv2.SimpleBlobDetector(params)
else:
self.blob_detector = cv2.SimpleBlobDetector_create(params)
super(DMCandidROIIterator, self).__init__(
self.nb_sample, img_per_batch, shuffle, seed)
def next(self):
with self.lock:
index_array, current_index, current_batch_size = \
next(self.index_generator)
# Obtain the random state for the current batch.
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
if self.classes is not None:
batch_cls = self.classes[index_array]
while self.all_neg_skip > rng.uniform() and np.all(batch_cls == 0):
index_array, current_index, current_batch_size = \
next(self.index_generator)
batch_cls = self.classes[index_array]
rng = RandomState() if self.seed is None else \
RandomState(int(self.seed) + self.total_batches_seen)
else:
batch_cls = None
# The transformation of images is not under thread lock so it can
# be done in parallel
# Create a margin between pos and neg patches on cancer mammograms.
if self.one_patch_mode:
margin_creation = False
else:
margin_creation = self.amp_factor > 1. \
and batch_cls is not None \
and self.roi_clf is not None
if margin_creation:
nb_img_roi = int(self.roi_per_img*self.amp_factor)
else:
nb_img_roi = self.roi_per_img
nb_tot_roi = current_batch_size*nb_img_roi
batch_x = np.zeros((nb_tot_roi,) + self.image_shape, dtype='float32')
# build batch of image data, read images first.
batch_idx = 0
for ii, fi in enumerate(index_array): # ii: image idx; fi: fname idx.
img = read_resize_img(
self.filenames[fi],
target_height=self.target_height,
target_scale=self.target_scale,
gs_255=self.gs_255)
# breast segmentation.
img,_ = prep.segment_breast(
img, low_int_threshold=self.low_int_threshold)
# blob detection.
key_pts = self.blob_detector.detect((img/img.max()*255).astype('uint8'))
if int(self.verbose) > 1:
print "%s: blob detection found %d key points." % \
(self.filenames[fi], len(key_pts))
if len(key_pts) > nb_img_roi:
# key_pts = rng.choice(key_pts, self.roi_per_img, replace=False)
key_pts = clust_kpts(key_pts, nb_img_roi, self.seed)
elif len(key_pts) > 3: # 3 is arbitrary choice.
key_pts = rng.choice(key_pts, nb_img_roi, replace=True)
else:
# blob detection failed, randomly draw points from the image.
# import pdb; pdb.set_trace()
pt_x = rng.randint(0, img.shape[1], nb_img_roi)
pt_y = rng.randint(0, img.shape[0], nb_img_roi)
key_pts = np.stack((pt_x, pt_y), axis=1)
# get roi image patches.
roi_patches = get_roi_patches(img, key_pts, self.roi_size)
# Always have one channel.
if self.data_format == 'channels_first':
xs = roi_patches.reshape(
(roi_patches.shape[0], 1, roi_patches.shape[1],
roi_patches.shape[2]))
else:
xs = roi_patches.reshape(
(roi_patches.shape[0], roi_patches.shape[1],
roi_patches.shape[2], 1))
# import pdb; pdb.set_trace()
batch_x[batch_idx:batch_idx+nb_img_roi] = xs
batch_idx += nb_img_roi
if self.return_raw_img:
batch_x_r = batch_x.copy()
# transform and standardize.
for i, x in enumerate(batch_x):
if not self.validation_mode:
x = self.image_data_generator.random_transform(x)
x = self.image_data_generator.standardize(x)
batch_x[i] = x
# optionally save augmented images to disk for debugging purposes
if self.save_to_dir:
for i in xrange(nb_tot_roi):
fname = '{prefix}_{index}_{hash}.{format}'.\
format(prefix=self.save_prefix,
index=current_index*nb_img_roi + i,
hash=rng.randint(1e4), format=self.save_format)
img = batch_x[i]
if self.data_format == 'channels_first':
img = img.reshape((img.shape[1], img.shape[2]))
else:
img = img.reshape((img.shape[0], img.shape[1]))
# it seems only 8-bit images are supported.
cv2.imwrite(path.join(self.save_to_dir, fname), img)
# score patches using the ROI classifier.
if self.roi_clf is None:
batch_s = np.ones((len(batch_x),))
elif self.tf_graph is not None:
with self.tf_graph.as_default():
batch_s = self.roi_clf.predict(batch_x,
batch_size=self.clf_bs)
else:
batch_s = self.roi_clf.predict(batch_x,
batch_size=self.clf_bs)
batch_s = batch_s.ravel()
# use ROI prob scores to mask patches.
if margin_creation:
batch_mask = np.ones_like(batch_s, dtype='bool')
batch_idx = 0
for cls_ in batch_cls:
img_w = batch_s[batch_idx:batch_idx+nb_img_roi]
w_sorted_idx = np.argsort(img_w)
img_m = np.ones_like(img_w, dtype='bool') # per img mask.
# filter out low score patches.
img_m[img_w < self.cutpoint] = False
# add back the max scored patch (in case no one passes cutpoint).
if cls_ == 1: # only for cancer cases.
img_m[w_sorted_idx[-1]] = True
# filter out low ranked patches (if too many pass cutpoint).
img_m[w_sorted_idx[:-self.roi_per_img]] = False
# finally, add background patches.
nb_bkg_patches = int(self.roi_per_img - img_m.sum())
nb_bkg_patches = 0 if nb_bkg_patches < 0 else nb_bkg_patches
img_m[w_sorted_idx[:nb_bkg_patches]] = True
batch_mask[batch_idx:batch_idx+nb_img_roi] = img_m
batch_idx += nb_img_roi
batch_x = batch_x[batch_mask]
batch_s = batch_s[batch_mask]
if self.return_raw_img:
batch_x_r = batch_x_r[batch_mask]
elif self.one_patch_mode:
wei_mat = batch_s.reshape((-1, self.roi_per_img))
max_wei_idx = np.argmax(wei_mat, axis=1)
max_wei_idx += np.arange(wei_mat.shape[0])*self.roi_per_img
batch_mask = np.zeros_like(batch_s, dtype='bool')
batch_mask[max_wei_idx] = True
batch_x = batch_x[batch_mask]
batch_s = batch_s[batch_mask]
if self.return_raw_img:
batch_x_r = batch_x_r[batch_mask]
else:
pass
# Create background, positive and negative classes.
if self.classes is not None:
img_y = self.classes[index_array]
if self.one_patch_mode:
batch_y = img_y
else:
batch_y = np.array([ [y]*self.roi_per_img
for y in img_y ]).ravel()
# Set low score patches to background.
batch_y[batch_s < self.cutpoint] = 0
# Add back the max scored patch (in case no one passes cutpoint).
for ii,y in enumerate(img_y):
if y == 1:
img_idx = ii*self.roi_per_img
img_w = batch_s[img_idx:img_idx+self.roi_per_img]
max_w_idx = np.argmax(img_w)
batch_y[img_idx + max_w_idx] = 1
# Assign negative patch labels.
batch_y[np.logical_and(batch_y==0,
batch_s>=self.cutpoint)] = 2
# In-batch balancing using sample weights.
batch_w = np.ones_like(batch_y, dtype='float32')
if self.auto_batch_balance:
for uy in np.unique(batch_y):
batch_w[batch_y==uy] /= (batch_y==uy).mean()
# build batch of labels
if self.classes is None or self.class_mode is None:
if self.return_sample_weight:
if self.return_raw_img:
return batch_x_r, batch_w
return batch_x, batch_w
elif self.return_raw_img:
return batch_x_r
else:
return batch_x
else:
if self.class_mode == 'sparse':
batch_y = batch_y
elif self.class_mode == 'binary':
batch_y = batch_y.astype('float32')
elif self.class_mode == 'categorical':
batch_y = to_categorical(batch_y, self.nb_class)
else: # class_mode == None
raise Exception # this shall never happen.
if self.return_sample_weight:
if self.return_raw_img:
return batch_x_r, batch_y, batch_w
# import pdb; pdb.set_trace()
return batch_x, batch_y, batch_w
elif self.return_raw_img:
return batch_x_r, batch_y
else:
return batch_x, batch_y