-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.py
801 lines (640 loc) · 29.5 KB
/
all.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
import keras
from keras.models import *
from keras.layers import *
from keras.callbacks import Callback
import glob
import six
import json
import cv2
import numpy as np
from tqdm import tqdm
from time import time
import itertools
import os
import random
from types import MethodType
################################# Config ####################
DATA_LOADER_SEED = 0
random.seed(DATA_LOADER_SEED)
IMAGE_ORDERING_CHANNELS_LAST = "channels_last"
IMAGE_ORDERING_CHANNELS_FIRST = "channels_first"
# Default IMAGE_ORDERING = channels_last
IMAGE_ORDERING = IMAGE_ORDERING_CHANNELS_LAST
if IMAGE_ORDERING == 'channels_first':
pretrained_url = "https://github.com/fchollet/deep-learning-models/" \
"releases/download/v0.1/" \
"vgg16_weights_th_dim_ordering_th_kernels_notop.h5"
elif IMAGE_ORDERING == 'channels_last':
pretrained_url = "https://github.com/fchollet/deep-learning-models/" \
"releases/download/v0.1/" \
"vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
if IMAGE_ORDERING == 'channels_first':
MERGE_AXIS = 1
elif IMAGE_ORDERING == 'channels_last':
MERGE_AXIS = -1
############################ Model Preparation #######################
def _unet(n_classes, encoder, l1_skip_conn=True, input_height=416,
input_width=608):
img_input, levels = encoder(
input_height=input_height, input_width=input_width)
[f1, f2, f3, f4, f5] = levels
o = f4
o = (ZeroPadding2D((1, 1), data_format=IMAGE_ORDERING))(o)
o = (Conv2D(512, (3, 3), padding='valid' , activation='relu' , data_format=IMAGE_ORDERING))(o)
o = (BatchNormalization())(o)
o = (UpSampling2D((2, 2), data_format=IMAGE_ORDERING))(o)
o = (concatenate([o, f3], axis=MERGE_AXIS))
o = (ZeroPadding2D((1, 1), data_format=IMAGE_ORDERING))(o)
o = (Conv2D(256, (3, 3), padding='valid', activation='relu' , data_format=IMAGE_ORDERING))(o)
o = (BatchNormalization())(o)
o = (UpSampling2D((2, 2), data_format=IMAGE_ORDERING))(o)
o = (concatenate([o, f2], axis=MERGE_AXIS))
o = (ZeroPadding2D((1, 1), data_format=IMAGE_ORDERING))(o)
o = (Conv2D(128, (3, 3), padding='valid' , activation='relu' , data_format=IMAGE_ORDERING))(o)
o = (BatchNormalization())(o)
o = (UpSampling2D((2, 2), data_format=IMAGE_ORDERING))(o)
if l1_skip_conn:
o = (concatenate([o, f1], axis=MERGE_AXIS))
o = (ZeroPadding2D((1, 1), data_format=IMAGE_ORDERING))(o)
o = (Conv2D(64, (3, 3), padding='valid', activation='relu', data_format=IMAGE_ORDERING))(o)
o = (BatchNormalization())(o)
o = Conv2D(n_classes, (3, 3), padding='same',
data_format=IMAGE_ORDERING)(o)
model = get_segmentation_model(img_input, o)
return model
def vgg_unet(n_classes, input_height=416, input_width=608 , encoder_level=3):
model = _unet(n_classes, get_vgg_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "vgg_unet"
return model
def get_vgg_encoder(input_height=224, input_width=224, pretrained='imagenet'):
assert input_height % 32 == 0
assert input_width % 32 == 0
if IMAGE_ORDERING == 'channels_first':
img_input = Input(shape=(3, input_height, input_width))
elif IMAGE_ORDERING == 'channels_last':
img_input = Input(shape=(input_height, input_width, 3))
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool',
data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool',
data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool',
data_format=IMAGE_ORDERING)(x)
f3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool',
data_format=IMAGE_ORDERING)(x)
f4 = x
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool',
data_format=IMAGE_ORDERING)(x)
f5 = x
if pretrained == 'imagenet':
VGG_Weights_path = keras.utils.get_file(
pretrained_url.split("/")[-1], pretrained_url)
Model(img_input, x).load_weights(VGG_Weights_path)
return img_input, [f1, f2, f3, f4, f5]
def get_segmentation_model(input, output):
img_input = input
o = output
o_shape = Model(img_input, o).output_shape
i_shape = Model(img_input, o).input_shape
if IMAGE_ORDERING == 'channels_first':
output_height = o_shape[2]
output_width = o_shape[3]
input_height = i_shape[2]
input_width = i_shape[3]
n_classes = o_shape[1]
o = (Reshape((-1, output_height*output_width)))(o)
o = (Permute((2, 1)))(o)
elif IMAGE_ORDERING == 'channels_last':
output_height = o_shape[1]
output_width = o_shape[2]
input_height = i_shape[1]
input_width = i_shape[2]
n_classes = o_shape[3]
o = (Reshape((output_height*output_width, -1)))(o)
o = (Activation('softmax'))(o)
model = Model(img_input, o)
model.output_width = output_width
model.output_height = output_height
model.n_classes = n_classes
model.input_height = input_height
model.input_width = input_width
model.model_name = ""
model.train = MethodType(train, model)
model.predict_segmentation = MethodType(predict, model)
model.predict_multiple = MethodType(predict_multiple, model)
model.evaluate_segmentation = MethodType(evaluate, model)
return model
########################################### Data loading ###################################
random.seed(DATA_LOADER_SEED)
class_colors = [(random.randint(0, 255), random.randint(
0, 255), random.randint(0, 255)) for _ in range(5000)]
class DataLoaderError(Exception):
pass
def get_image_array(image_input,
width, height,
imgNorm="sub_mean", ordering='channels_first'):
""" Load image array from input """
if type(image_input) is np.ndarray:
# It is already an array, use it as it is
img = image_input
elif isinstance(image_input, six.string_types):
if not os.path.isfile(image_input):
raise DataLoaderError("get_image_array: path {0} doesn't exist"
.format(image_input))
img = cv2.imread(image_input, 1)
else:
raise DataLoaderError("get_image_array: Can't process input type {0}"
.format(str(type(image_input))))
if imgNorm == "sub_and_divide":
img = np.float32(cv2.resize(img, (width, height))) / 127.5 - 1
elif imgNorm == "sub_mean":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img[:, :, 0] -= 103.939
img[:, :, 1] -= 116.779
img[:, :, 2] -= 123.68
img = img[:, :, ::-1]
elif imgNorm == "divide":
img = cv2.resize(img, (width, height))
img = img.astype(np.float32)
img = img/255.0
if ordering == 'channels_first':
img = np.rollaxis(img, 2, 0)
return img
def get_segmentation_array(image_input, nClasses,
width, height, no_reshape=False):
""" Load segmentation array from input """
seg_labels = np.zeros((height, width, nClasses))
if type(image_input) is np.ndarray:
# It is already an array, use it as it is
img = image_input
elif isinstance(image_input, six.string_types):
if not os.path.isfile(image_input):
raise DataLoaderError("get_segmentation_array: "
"path {0} doesn't exist".format(image_input))
img = cv2.imread(image_input, 1)
else:
raise DataLoaderError("get_segmentation_array: "
"Can't process input type {0}"
.format(str(type(image_input))))
img = cv2.resize(img, (width, height), interpolation=cv2.INTER_NEAREST)
img = img[:, :, 0]
for c in range(nClasses):
seg_labels[:, :, c] = (img == c).astype(int)
if not no_reshape:
seg_labels = np.reshape(seg_labels, (width*height, nClasses))
return seg_labels
def get_pairs_from_paths(images_path, segs_path, ignore_non_matching=False):
""" Find all the images from the images_path directory and
the segmentation images from the segs_path directory
while checking integrity of data """
ACCEPTABLE_IMAGE_FORMATS = [".jpg", ".jpeg", ".png", ".bmp"]
ACCEPTABLE_SEGMENTATION_FORMATS = [".png", ".bmp"]
image_files = []
segmentation_files = {}
for dir_entry in os.listdir(images_path):
if os.path.isfile(os.path.join(images_path, dir_entry)) and \
os.path.splitext(dir_entry)[1] in ACCEPTABLE_IMAGE_FORMATS:
file_name, file_extension = os.path.splitext(dir_entry)
image_files.append((file_name, file_extension,
os.path.join(images_path, dir_entry)))
for dir_entry in os.listdir(segs_path):
if os.path.isfile(os.path.join(segs_path, dir_entry)) and \
os.path.splitext(dir_entry)[1] in ACCEPTABLE_SEGMENTATION_FORMATS:
file_name, file_extension = os.path.splitext(dir_entry)
full_dir_entry = os.path.join(segs_path, dir_entry)
if file_name in segmentation_files:
raise DataLoaderError("Segmentation file with filename {0}"
" already exists and is ambiguous to"
" resolve with path {1}."
" Please remove or rename the latter."
.format(file_name, full_dir_entry))
segmentation_files[file_name] = (file_extension, full_dir_entry)
return_value = []
# Match the images and segmentations
for image_file, _, image_full_path in image_files:
if image_file in segmentation_files:
return_value.append((image_full_path,
segmentation_files[image_file][1]))
elif ignore_non_matching:
continue
else:
# Error out
raise DataLoaderError("No corresponding segmentation "
"found for image {0}."
.format(image_full_path))
return return_value
def image_segmentation_generator(images_path, segs_path, batch_size,
n_classes, input_height, input_width,
output_height, output_width,
do_augment=False,
augmentation_name="aug_all"):
img_seg_pairs = get_pairs_from_paths(images_path, segs_path)
random.shuffle(img_seg_pairs)
zipped = itertools.cycle(img_seg_pairs)
while True:
X = []
Y = []
for _ in range(batch_size):
im, seg = next(zipped)
im = cv2.imread(im, 1)
seg = cv2.imread(seg, 1)
if do_augment:
im, seg[:, :, 0] = augment_seg(im, seg[:, :, 0],
augmentation_name)
X.append(get_image_array(im, input_width,
input_height, ordering=IMAGE_ORDERING))
Y.append(get_segmentation_array(
seg, n_classes, output_width, output_height))
yield np.array(X), np.array(Y)
def verify_segmentation_dataset(images_path, segs_path,
n_classes, show_all_errors=False):
try:
img_seg_pairs = get_pairs_from_paths(images_path, segs_path)
if not len(img_seg_pairs):
print("Couldn't load any data from images_path: "
"{0} and segmentations path: {1}"
.format(images_path, segs_path))
return False
return_value = True
for im_fn, seg_fn in tqdm(img_seg_pairs):
img = cv2.imread(im_fn)
seg = cv2.imread(seg_fn)
# Check dimensions match
if not img.shape == seg.shape:
return_value = False
print("The size of image {0} and its segmentation {1} "
"doesn't match (possibly the files are corrupt)."
.format(im_fn, seg_fn))
if not show_all_errors:
break
else:
max_pixel_value = np.max(seg[:, :, 0])
if max_pixel_value >= n_classes:
return_value = False
print("The pixel values of the segmentation image {0} "
"violating range [0, {1}]. "
"Found maximum pixel value {2}"
.format(seg_fn, str(n_classes - 1), max_pixel_value))
if not show_all_errors:
break
if return_value:
print("Dataset verified! ")
else:
print("Dataset not verified!")
return return_value
except DataLoaderError as e:
print("Found error during data loading\n{0}".format(str(e)))
return False
############################# Training #######################
def train(model,
train_images,
train_annotations,
input_height=None,
input_width=None,
n_classes=None,
verify_dataset=True,
checkpoints_path=None,
epochs=1,
batch_size=2,
validate=False,
val_images=None,
val_annotations=None,
val_batch_size=2,
auto_resume_checkpoint=False,
load_weights=None,
steps_per_epoch=512,
val_steps_per_epoch=512,
gen_use_multiprocessing=False,
ignore_zero_class=False,
optimizer_name='adam',
do_augment=False,
augmentation_name="aug_all"):
model_from_name = {}
model_from_name["vgg_unet"] = vgg_unet
# from .models.all_models import model_from_name
# check if user gives model name instead of the model object
if isinstance(model, six.string_types):
# create the model from the name
assert (n_classes is not None), "Please provide the n_classes"
if (input_height is not None) and (input_width is not None):
model = model_from_name[model](
n_classes, input_height=input_height, input_width=input_width)
else:
model = model_from_name[model](n_classes)
n_classes = model.n_classes
input_height = model.input_height
input_width = model.input_width
output_height = model.output_height
output_width = model.output_width
if validate:
assert val_images is not None
assert val_annotations is not None
if optimizer_name is not None:
if ignore_zero_class:
loss_k = masked_categorical_crossentropy
else:
loss_k = 'categorical_crossentropy'
model.compile(loss=loss_k,
optimizer=optimizer_name,
metrics=['accuracy'])
if checkpoints_path is not None:
with open(checkpoints_path+"_config.json", "w") as f:
json.dump({
"model_class": model.model_name,
"n_classes": n_classes,
"input_height": input_height,
"input_width": input_width,
"output_height": output_height,
"output_width": output_width
}, f)
if load_weights is not None and len(load_weights) > 0:
print("Loading weights from ", load_weights)
model.load_weights(load_weights)
if auto_resume_checkpoint and (checkpoints_path is not None):
latest_checkpoint = find_latest_checkpoint(checkpoints_path)
if latest_checkpoint is not None:
print("Loading the weights from latest checkpoint ",
latest_checkpoint)
model.load_weights(latest_checkpoint)
if verify_dataset:
print("Verifying training dataset")
verified = verify_segmentation_dataset(train_images,
train_annotations,
n_classes)
assert verified
if validate:
print("Verifying validation dataset")
verified = verify_segmentation_dataset(val_images,
val_annotations,
n_classes)
assert verified
train_gen = image_segmentation_generator(
train_images, train_annotations, batch_size, n_classes,
input_height, input_width, output_height, output_width,
do_augment=do_augment, augmentation_name=augmentation_name)
if validate:
val_gen = image_segmentation_generator(
val_images, val_annotations, val_batch_size,
n_classes, input_height, input_width, output_height, output_width)
callbacks = [
CheckpointsCallback(checkpoints_path)
]
if not validate:
model.fit_generator(train_gen, steps_per_epoch,
epochs=epochs, callbacks=callbacks)
else:
model.fit_generator(train_gen,
steps_per_epoch,
validation_data=val_gen,
validation_steps=val_steps_per_epoch,
epochs=epochs, callbacks=callbacks,
use_multiprocessing=gen_use_multiprocessing)
def find_latest_checkpoint(checkpoints_path, fail_safe=True):
def get_epoch_number_from_path(path):
return path.replace(checkpoints_path, "").strip(".")
# Get all matching files
all_checkpoint_files = glob.glob(checkpoints_path + ".*")
all_checkpoint_files = [ ff.replace(".index" , "" ) for ff in all_checkpoint_files ] # to make it work for newer versions of keras
# Filter out entries where the epoc_number part is pure number
all_checkpoint_files = list(filter(lambda f: get_epoch_number_from_path(f)
.isdigit(), all_checkpoint_files))
if not len(all_checkpoint_files):
# The glob list is empty, don't have a checkpoints_path
if not fail_safe:
raise ValueError("Checkpoint path {0} invalid"
.format(checkpoints_path))
else:
return None
# Find the checkpoint file with the maximum epoch
latest_epoch_checkpoint = max(all_checkpoint_files,
key=lambda f:
int(get_epoch_number_from_path(f)))
return latest_epoch_checkpoint
def masked_categorical_crossentropy(gt, pr):
from keras.losses import categorical_crossentropy
mask = 1 - gt[:, :, 0]
return categorical_crossentropy(gt, pr) * mask
class CheckpointsCallback(Callback):
def __init__(self, checkpoints_path):
self.checkpoints_path = checkpoints_path
def on_epoch_end(self, epoch, logs=None):
if self.checkpoints_path is not None:
self.model.save_weights(self.checkpoints_path + "." + str(epoch))
print("saved ", self.checkpoints_path + "." + str(epoch))
############################## Model Prediction ########################
def predict(model=None, inp=None, out_fname=None,
checkpoints_path=None, overlay_img=False,
class_names=None, show_legends=False, colors=class_colors,
prediction_width=None, prediction_height=None):
if model is None and (checkpoints_path is not None):
model = model_from_checkpoint_path(checkpoints_path)
assert (inp is not None)
assert ((type(inp) is np.ndarray) or isinstance(inp, six.string_types)),\
"Input should be the CV image or the input file name"
if isinstance(inp, six.string_types):
inp = cv2.imread(inp)
assert len(inp.shape) == 3, "Image should be h,w,3 "
output_width = model.output_width
output_height = model.output_height
input_width = model.input_width
input_height = model.input_height
n_classes = model.n_classes
x = get_image_array(inp, input_width, input_height,
ordering=IMAGE_ORDERING)
pr = model.predict(np.array([x]))[0]
pr = pr.reshape((output_height, output_width, n_classes)).argmax(axis=2)
seg_img = visualize_segmentation(pr, inp, n_classes=n_classes,
colors=colors, overlay_img=overlay_img,
show_legends=show_legends,
class_names=class_names,
prediction_width=prediction_width,
prediction_height=prediction_height)
# cv2.imshow("predict: ", seg_img)
# key = cv2.waitKey(1) & 0xFF
if out_fname is not None:
cv2.imwrite(out_fname, seg_img)
return pr
def model_from_checkpoint_path(checkpoints_path):
model_from_name = {}
model_from_name["vgg_unet"] = vgg_unet
# from .models.all_models import model_from_name
assert (os.path.isfile(checkpoints_path+"_config.json")
), "Checkpoint not found."
model_config = json.loads(
open(checkpoints_path+"_config.json", "r").read())
latest_weights = find_latest_checkpoint(checkpoints_path)
assert (latest_weights is not None), "Checkpoint not found."
model = model_from_name[model_config['model_class']](
model_config['n_classes'], input_height=model_config['input_height'],
input_width=model_config['input_width'])
print("loaded weights ", latest_weights)
model.load_weights(latest_weights)
return model
################################# Visualization ####################
def get_colored_segmentation_image(seg_arr, n_classes, colors=class_colors):
output_height = seg_arr.shape[0]
output_width = seg_arr.shape[1]
seg_img = np.zeros((output_height, output_width, 3))
for c in range(n_classes):
seg_arr_c = seg_arr[:, :] == c
seg_img[:, :, 0] += ((seg_arr_c)*(colors[c][0])).astype('uint8')
seg_img[:, :, 1] += ((seg_arr_c)*(colors[c][1])).astype('uint8')
seg_img[:, :, 2] += ((seg_arr_c)*(colors[c][2])).astype('uint8')
return seg_img
def get_legends(class_names, colors=class_colors):
n_classes = len(class_names)
legend = np.zeros(((len(class_names) * 25) + 25, 125, 3),
dtype="uint8") + 255
class_names_colors = enumerate(zip(class_names[:n_classes],
colors[:n_classes]))
for (i, (class_name, color)) in class_names_colors:
color = [int(c) for c in color]
cv2.putText(legend, class_name, (5, (i * 25) + 17),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 1)
cv2.rectangle(legend, (100, (i * 25)), (125, (i * 25) + 25),
tuple(color), -1)
return legend
def overlay_seg_image(inp_img, seg_img):
orininal_h = inp_img.shape[0]
orininal_w = inp_img.shape[1]
seg_img = cv2.resize(seg_img, (orininal_w, orininal_h))
fused_img = (inp_img/2 + seg_img/2).astype('uint8')
return fused_img
def concat_lenends(seg_img, legend_img):
new_h = np.maximum(seg_img.shape[0], legend_img.shape[0])
new_w = seg_img.shape[1] + legend_img.shape[1]
out_img = np.zeros((new_h, new_w, 3)).astype('uint8') + legend_img[0, 0, 0]
out_img[:legend_img.shape[0], : legend_img.shape[1]] = np.copy(legend_img)
out_img[:seg_img.shape[0], legend_img.shape[1]:] = np.copy(seg_img)
return out_img
def visualize_segmentation(seg_arr, inp_img=None, n_classes=None,
colors=class_colors, class_names=None,
overlay_img=False, show_legends=False,
prediction_width=None, prediction_height=None):
if n_classes is None:
n_classes = np.max(seg_arr)
seg_img = get_colored_segmentation_image(seg_arr, n_classes, colors=colors)
# cv2.imshow("visualize_segmentation: ", seg_img)
# key = cv2.waitKey(1) & 0xFF
if inp_img is not None:
orininal_h = inp_img.shape[0]
orininal_w = inp_img.shape[1]
seg_img = cv2.resize(seg_img, (orininal_w, orininal_h))
if (prediction_height is not None) and (prediction_width is not None):
seg_img = cv2.resize(seg_img, (prediction_width, prediction_height))
if inp_img is not None:
inp_img = cv2.resize(inp_img,
(prediction_width, prediction_height))
if overlay_img:
assert inp_img is not None
seg_img = overlay_seg_image(inp_img, seg_img)
if show_legends:
assert class_names is not None
legend_img = get_legends(class_names, colors=colors)
seg_img = concat_lenends(seg_img, legend_img)
return seg_img
def predict_multiple(model=None, inps=None, inp_dir=None, out_dir=None,
checkpoints_path=None, overlay_img=False,
class_names=None, show_legends=False, colors=class_colors,
prediction_width=None, prediction_height=None):
if model is None and (checkpoints_path is not None):
model = model_from_checkpoint_path(checkpoints_path)
if inps is None and (inp_dir is not None):
inps = glob.glob(os.path.join(inp_dir, "*.jpg")) + glob.glob(
os.path.join(inp_dir, "*.png")) + \
glob.glob(os.path.join(inp_dir, "*.jpeg"))
inps = sorted(inps)
assert type(inps) is list
all_prs = []
for i, inp in enumerate(tqdm(inps)):
if out_dir is None:
out_fname = None
else:
if isinstance(inp, six.string_types):
out_fname = os.path.join(out_dir, os.path.basename(inp))
else:
out_fname = os.path.join(out_dir, str(i) + ".jpg")
pr = predict(model, inp, out_fname,
overlay_img=overlay_img, class_names=class_names,
show_legends=show_legends, colors=colors,
prediction_width=prediction_width,
prediction_height=prediction_height)
all_prs.append(pr)
return all_prs
def evaluate(model=None, inp_images=None, annotations=None,
inp_images_dir=None, annotations_dir=None, checkpoints_path=None):
if model is None:
assert (checkpoints_path is not None),\
"Please provide the model or the checkpoints_path"
model = model_from_checkpoint_path(checkpoints_path)
if inp_images is None:
assert (inp_images_dir is not None),\
"Please provide inp_images or inp_images_dir"
assert (annotations_dir is not None),\
"Please provide inp_images or inp_images_dir"
paths = get_pairs_from_paths(inp_images_dir, annotations_dir)
paths = list(zip(*paths))
inp_images = list(paths[0])
annotations = list(paths[1])
assert type(inp_images) is list
assert type(annotations) is list
tp = np.zeros(model.n_classes)
fp = np.zeros(model.n_classes)
fn = np.zeros(model.n_classes)
n_pixels = np.zeros(model.n_classes)
for inp, ann in tqdm(zip(inp_images, annotations)):
pr = predict(model, inp)
gt = get_segmentation_array(ann, model.n_classes,
model.output_width, model.output_height,
no_reshape=True)
gt = gt.argmax(-1)
pr = pr.flatten()
gt = gt.flatten()
for cl_i in range(model.n_classes):
tp[cl_i] += np.sum((pr == cl_i) * (gt == cl_i))
fp[cl_i] += np.sum((pr == cl_i) * ((gt != cl_i)))
fn[cl_i] += np.sum((pr != cl_i) * ((gt == cl_i)))
n_pixels[cl_i] += np.sum(gt == cl_i)
cl_wise_score = tp / (tp + fp + fn + 0.000000000001)
n_pixels_norm = n_pixels / np.sum(n_pixels)
frequency_weighted_IU = np.sum(cl_wise_score*n_pixels_norm)
mean_IU = np.mean(cl_wise_score)
return {
"frequency_weighted_IU": frequency_weighted_IU,
"mean_IU": mean_IU,
"class_wise_IU": cl_wise_score
}