-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation_exam.py
587 lines (418 loc) · 18.5 KB
/
segmentation_exam.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
from tensorflow import keras
import tensorflow as tf
from keras.models import Sequential
from keras.applications.vgg16 import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import (
Dense,
Conv2D,
MaxPool2D,
Flatten,
Dropout,
BatchNormalization,
)
from keras.preprocessing.image import ImageDataGenerator
import cupy as cp
import time
import os
import PIL.Image, PIL.ImageFont, PIL.ImageDraw
import numpy as np
from matplotlib import pyplot as plt
import tensorflow_datasets as tfds
from sklearn.model_selection import train_test_split
tf.random.set_seed(42)
np.random.seed(42)
def read_image_and_annotation(image, annotation):
'''
Casts the image and annotation to their expected data type and
normalizes the input image so that each pixel is in the range [-1, 1]
Args:
image (numpy array) -- input image
annotation (numpy array) -- ground truth label map
Returns:
preprocessed image-annotation pair
'''
image = tf.cast(image, dtype=tf.float32)
image = tf.reshape(image, (image.shape[0], image.shape[1], 1,))
annotation = tf.cast(annotation, dtype=tf.int32)
image = image / 127.5
image -= 1
return image, annotation
def get_training_dataset(images, annos):
'''
Prepares shuffled batches of the training set.
Args:
images (list of strings) -- paths to each image file in the train set
annos (list of strings) -- paths to each label map in the train set
Returns:
tf Dataset containing the preprocessed train set
'''
training_dataset = tf.data.Dataset.from_tensor_slices((images, annos))
training_dataset = training_dataset.map(read_image_and_annotation)
training_dataset = training_dataset.shuffle(512, reshuffle_each_iteration=True)
training_dataset = training_dataset.batch(BATCH_SIZE)
training_dataset = training_dataset.repeat()
training_dataset = training_dataset.prefetch(-1)
return training_dataset
def get_validation_dataset(images, annos):
'''
Prepares batches of the validation set.
Args:
images (list of strings) -- paths to each image file in the val set
annos (list of strings) -- paths to each label map in the val set
Returns:
tf Dataset containing the preprocessed validation set
'''
validation_dataset = tf.data.Dataset.from_tensor_slices((images, annos))
validation_dataset = validation_dataset.map(read_image_and_annotation)
validation_dataset = validation_dataset.batch(BATCH_SIZE)
validation_dataset = validation_dataset.repeat()
return validation_dataset
def get_test_dataset(images, annos):
'''
Prepares batches of the test set.
Args:
images (list of strings) -- paths to each image file in the test set
annos (list of strings) -- paths to each label map in the test set
Returns:
tf Dataset containing the preprocessed validation set
'''
test_dataset = tf.data.Dataset.from_tensor_slices((images, annos))
test_dataset = test_dataset.map(read_image_and_annotation)
test_dataset = test_dataset.batch(BATCH_SIZE, drop_remainder=True)
return test_dataset
def load_images_and_segments():
'''
Loads the images and segments as numpy arrays from npy files
and makes splits for training, validation and test datasets.
Returns:
3 tuples containing the train, val, and test splits
'''
#Loads images and segmentation masks.
images = np.load('/media/gkasap/1TB_HD/bigDatasets/coursera/computerVision/combined.npy')
segments = np.load('/media/gkasap/1TB_HD/bigDatasets/coursera/computerVision/segmented.npy')
#Makes training, validation, test splits from loaded images and segmentation masks.
train_images, val_images, train_annos, val_annos = train_test_split(images, segments, test_size=0.2, shuffle=True)
val_images, test_images, val_annos, test_annos = train_test_split(val_images, val_annos, test_size=0.2, shuffle=True)
return (train_images, train_annos), (val_images, val_annos), (test_images, test_annos)
BATCH_SIZE = 32
# Load Dataset
train_slices, val_slices, test_slices = load_images_and_segments()
# Visualization Utilities
# there are 11 classes in the dataset: one class for each digit (0 to 9) plus the background class
n_classes = 11
# assign a random color for each class
colors = [tuple(np.random.randint(256, size=3) / 255.0) for i in range(n_classes)]
def fuse_with_pil(images):
'''
Creates a blank image and pastes input images
Args:
images (list of numpy arrays) - numpy array representations of the images to paste
Returns:
PIL Image object containing the images
'''
widths = (image.shape[1] for image in images)
heights = (image.shape[0] for image in images)
total_width = sum(widths)
max_height = max(heights)
new_im = PIL.Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
pil_image = PIL.Image.fromarray(np.uint8(im))
new_im.paste(pil_image, (x_offset,0))
x_offset += im.shape[1]
return new_im
def give_color_to_annotation(annotation):
'''
Converts a 2-D annotation to a numpy array with shape (height, width, 3) where
the third axis represents the color channel. The label values are multiplied by
255 and placed in this axis to give color to the annotation
Args:
annotation (numpy array) - label map array
Returns:
the annotation array with an additional color channel/axis
'''
seg_img = np.zeros( (annotation.shape[0],annotation.shape[1], 3) ).astype('float')
for c in range(n_classes):
segc = (annotation == c)
seg_img[:,:,0] += segc*( colors[c][0] * 255.0)
seg_img[:,:,1] += segc*( colors[c][1] * 255.0)
seg_img[:,:,2] += segc*( colors[c][2] * 255.0)
return seg_img
def show_annotation_and_prediction(image, annotation, prediction, iou_list, dice_score_list):
'''
Displays the images with the ground truth and predicted label maps. Also overlays the metrics.
Args:
image (numpy array) -- the input image
annotation (numpy array) -- the ground truth label map
prediction (numpy array) -- the predicted label map
iou_list (list of floats) -- the IOU values for each class
dice_score_list (list of floats) -- the Dice Score for each class
'''
new_ann = np.argmax(annotation, axis=2)
true_img = give_color_to_annotation(new_ann)
pred_img = give_color_to_annotation(prediction)
image = image + 1
image = image * 127.5
image = np.reshape(image, (image.shape[0], image.shape[1],))
image = np.uint8(image)
images = [image, np.uint8(pred_img), np.uint8(true_img)]
metrics_by_id = [(idx, iou, dice_score) for idx, (iou, dice_score) in enumerate(zip(iou_list, dice_score_list)) if iou > 0.0 and idx < 10]
metrics_by_id.sort(key=lambda tup: tup[1], reverse=True) # sorts in place
display_string_list = ["{}: IOU: {} Dice Score: {}".format(idx, iou, dice_score) for idx, iou, dice_score in metrics_by_id]
display_string = "\n".join(display_string_list)
plt.figure(figsize=(15, 4))
for idx, im in enumerate(images):
plt.subplot(1, 3, idx+1)
if idx == 1:
plt.xlabel(display_string)
plt.xticks([])
plt.yticks([])
plt.imshow(im)
def show_annotation_and_image(image, annotation):
'''
Displays the image and its annotation side by side
Args:
image (numpy array) -- the input image
annotation (numpy array) -- the label map
'''
new_ann = np.argmax(annotation, axis=2)
seg_img = give_color_to_annotation(new_ann)
image = image + 1
image = image * 127.5
image = np.reshape(image, (image.shape[0], image.shape[1],))
image = np.uint8(image)
images = [image, seg_img]
images = [image, seg_img]
fused_img = fuse_with_pil(images)
plt.imshow(fused_img)
def list_show_annotation(dataset, num_images):
ds = dataset.unbatch()
plt.figure(figsize=(20, 15))
plt.title("Images And Annotations")
plt.subplots_adjust(bottom=0.1, top=0.9, hspace=0.05)
for idx, (image, annotation) in enumerate(ds.take(num_images)):
plt.subplot(5, 5, idx + 1)
plt.yticks([])
plt.xticks([])
show_annotation_and_image(image.numpy(), annotation.numpy())
plt.show()
# Create training, validation, test datasets.
training_dataset = get_training_dataset(train_slices[0], train_slices[1])
validation_dataset = get_validation_dataset(val_slices[0], val_slices[1])
test_dataset = get_test_dataset(test_slices[0], test_slices[1])
# get 10 images from the training set
#list_show_annotation(training_dataset, 10)
# get 10 images from the validation set
#list_show_annotation(validation_dataset, 10)
# parameter describing where the channel dimension is found in our dataset
IMAGE_ORDERING = 'channels_last'
def conv_block(input, filters, kernel_size, pooling_size, pool_strides):
'''
Args:
input (tensor) -- batch of images or features
filters (int) -- number of filters of the Conv2D layers
kernel_size (int) -- kernel_size setting of the Conv2D layers
pooling_size (int) -- pooling size of the MaxPooling2D layers
pool_strides (int) -- strides setting of the MaxPooling2D layers
Returns:
(tensor) max pooled and batch-normalized features of the input
'''
### START CODE HERE ###
# use the functional syntax to stack the layers as shown in the diagram above
x = tf.keras.layers.Conv2D(filters, kernel_size, padding='same', data_format=IMAGE_ORDERING, kernel_initializer = 'he_normal')(input)#karpathy trick
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.LeakyReLU(alpha=0.01)(x)
x = tf.keras.layers.Conv2D(filters, kernel_size, padding='same', data_format=IMAGE_ORDERING, kernel_initializer = 'he_normal')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.LeakyReLU(alpha=0.01)(x)
x = tf.keras.layers.MaxPooling2D(pool_size=pooling_size, strides=pool_strides, padding='same', data_format=IMAGE_ORDERING)(x)
### END CODE HERE ###
return x
# TEST CODE:
test_input = tf.keras.layers.Input(shape=(64,84, 1))
test_output = conv_block(test_input, 32, 3, 2, 2)
test_model = tf.keras.Model(inputs=test_input, outputs=test_output)
print(test_model.summary())
# free up test resources
del test_input, test_output, test_model
def FCN8(input_height=64, input_width=84):
'''
Defines the downsampling path of the image segmentation model.
Args:
input_height (int) -- height of the images
width (int) -- width of the images
Returns:
(tuple of tensors, tensor)
tuple of tensors -- features extracted at blocks 3 to 5
tensor -- copy of the input
'''
img_input = tf.keras.layers.Input(shape=(input_height,input_width, 1))
### START CODE HERE ###
# pad the input image width to 96 pixels
x = tf.keras.layers.ZeroPadding2D(((0, 0), (0, 96-input_width)))(img_input)
# Block 1
x = conv_block(x, filters = 32, kernel_size = 3, pooling_size = 2, pool_strides = 2)
# Block 2
x = conv_block(x, filters = 64, kernel_size = 3, pooling_size = 2, pool_strides = 2)
# Block 3
x = conv_block(x, filters = 128, kernel_size = 3, pooling_size = 2, pool_strides = 2)
# save the feature map at this stage
f3 = x
# Block 4
x = conv_block(x, filters = 256, kernel_size = 3, pooling_size = 2, pool_strides = 2)
# save the feature map at this stage
f4 = x
# Block 5
x = conv_block(x, filters = 256, kernel_size = 3, pooling_size = 2, pool_strides = 2)
# save the feature map at this stage
f5 = x
### END CODE HERE ###
return (f3, f4, f5), img_input
# TEST CODE:
test_convs, test_img_input = FCN8()
test_model = tf.keras.Model(inputs=test_img_input, outputs=[test_convs, test_img_input])
print(test_model.summary())
del test_convs, test_img_input, test_model
def fcn8_decoder(convs, n_classes):
# features from the encoder stage
f3, f4, f5 = convs
# number of filters
n = 512
o = tf.keras.layers.ZeroPadding2D(((0, 7 - f5.shape[1]), (0, 7 - f5.shape[2])))(f5)#(top, bottom), (left, right)
# add convolutional layers on top of the CNN extractor.
o = tf.keras.layers.Conv2D(n , (7 , 7) , activation=tf.keras.layers.LeakyReLU(alpha=0.01) , padding='same', name="conv6"
, kernel_initializer = 'he_normal', data_format=IMAGE_ORDERING)(o)
o = tf.keras.layers.Dropout(0.5)(o)
o = tf.keras.layers.Conv2D(n , (1 , 1) , activation=tf.keras.layers.LeakyReLU(alpha=0.01) , padding='same', name="conv7",
kernel_initializer = 'he_normal', data_format=IMAGE_ORDERING)(o)
o = tf.keras.layers.Dropout(0.5)(o)
o = tf.keras.layers.Conv2D(n_classes, (1, 1), activation=tf.keras.layers.LeakyReLU(alpha=0.01) , padding='same',
kernel_initializer = 'he_normal', data_format=IMAGE_ORDERING)(o)
### START CODE HERE ###
# Upsample `o` above and crop any extra pixels introduced
o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(4,4) , strides=(2,2) ,
use_bias=False,
data_format=IMAGE_ORDERING,
padding='valid')(f5)
o = tf.keras.layers.Cropping2D(cropping=(1,1))(o)
# load the pool 4 prediction and do a 1x1 convolution to reshape it to the same shape of `o` above
o2 = f4
o2 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation=tf.keras.layers.LeakyReLU(alpha=0.01) , padding='same',
kernel_initializer = 'he_normal', data_format=IMAGE_ORDERING)(o2)
# add the results of the upsampling and pool 4 prediction
o = tf.keras.layers.concatenate([o, o2])
"""_summary_
Returns:
_type_: _description_
""" # upsample the resulting tensor of the operation you just did
o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(4,4) , strides=(2,2) ,
use_bias=False,
data_format=IMAGE_ORDERING,
padding='valid')(o)
o = tf.keras.layers.Cropping2D(cropping=(1,1))(o)
# load the pool 3 prediction and do a 1x1 convolution to reshape it to the same shape of `o` above
o2 = f3
o2 = tf.keras.layers.Conv2D(n_classes , ( 1 , 1 ) , activation=tf.keras.layers.LeakyReLU(alpha=0.01) , padding='same',
kernel_initializer = 'he_normal', data_format=IMAGE_ORDERING)(o2)
# add the results of the upsampling and pool 3 prediction
o = tf.keras.layers.concatenate([o, o2])
# upsample up to the size of the original image
o = tf.keras.layers.Conv2DTranspose(n_classes , kernel_size=(8,8) , strides=(8,8) ,
use_bias=False,
data_format=IMAGE_ORDERING,
padding='same')(o)
o = tf.keras.layers.Cropping2D(((0, 0), (0, 96-84)))(o)
# append a sigmoid activation
o = (tf.keras.layers.Activation('sigmoid'))(o)
### END CODE HERE ###
return o
print("##########################################################################")
def combined_metric(y_true, y_pred):
# Calculate accuracy
y_true_class = tf.argmax(y_true, axis=-1)
y_pred_class = tf.argmax(y_pred, axis=-1)
accuracy = tf.reduce_mean(tf.cast(tf.equal(y_true_class, y_pred_class), tf.float32))
# Calculate dice coefficient
dice_numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=[1, 2, 3]) + 1e-6
dice_denominator = tf.reduce_sum(y_true, axis=[1, 2, 3]) + tf.reduce_sum(y_pred, axis=[1, 2, 3]) + 1e-6
dice_coef = tf.reduce_mean(dice_numerator / dice_denominator)
# Combine accuracy and dice coefficient
#combined_metric_value = (accuracy + dice_coef) / 2.0
return dice_coef
# TEST CODE
test_convs, test_img_input = FCN8()
test_fcn8_decoder = fcn8_decoder(test_convs, 11)
print(test_fcn8_decoder.shape)
del test_convs, test_img_input, test_fcn8_decoder
# start the encoder using the default input size 64 x 84
convs, img_input = FCN8()
# pass the convolutions obtained in the encoder to the decoder
dec_op = fcn8_decoder(convs, n_classes)
# define the model specifying the input (batch of images) and output (decoder output)
model = tf.keras.Model(inputs = img_input, outputs = dec_op)
model.summary()
METRICS = [ combined_metric, "accuracy"]
OPTIMIZER = keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss = "categorical_crossentropy" , metrics = METRICS, optimizer = OPTIMIZER)
# OTHER THAN SETTING THE EPOCHS NUMBER, DO NOT CHANGE ANY OTHER CODE
### START CODE HERE ###
EPOCHS = 50
### END CODE HERE ###
steps_per_epoch = 4000//BATCH_SIZE
validation_steps = 800//BATCH_SIZE
test_steps = 200//BATCH_SIZE
history = model.fit(training_dataset,
steps_per_epoch=steps_per_epoch,
validation_data=validation_dataset,
validation_steps=validation_steps,
epochs=EPOCHS,
verbose = 1)
results = model.predict(test_dataset, steps=test_steps)
print(results.shape)
def class_wise_metrics(y_true, y_pred):
'''
Computes the class-wise IOU and Dice Score.
Args:
y_true (tensor) - ground truth label maps
y_pred (tensor) - predicted label maps
'''
class_wise_iou = []
class_wise_dice_score = []
smoothing_factor = 0.00001
for i in range(n_classes):
intersection = np.sum((y_pred == i) * (y_true == i))
y_true_area = np.sum((y_true == i))
y_pred_area = np.sum((y_pred == i))
combined_area = y_true_area + y_pred_area
iou = (intersection) / (combined_area - intersection + smoothing_factor)
class_wise_iou.append(iou)
dice_score = 2 * ((intersection) / (combined_area + smoothing_factor))
class_wise_dice_score.append(dice_score)
return class_wise_iou, class_wise_dice_score
# place a number here between 0 to 191 to pick an image from the test set
integer_slider = 105
ds = test_dataset.unbatch()
ds = ds.batch(200)
images = []
y_true_segments = []
for image, annotation in ds.take(2):
y_true_segments = annotation
images = image
results = np.argmax(results, axis=3)
iou, dice_score = class_wise_metrics(np.argmax(y_true_segments[integer_slider], axis=2), results[integer_slider])
#show_annotation_and_prediction(image[integer_slider], annotation[integer_slider], results[integer_slider], iou, dice_score)
cls_wise_iou, cls_wise_dice_score = class_wise_metrics(np.argmax(y_true_segments, axis=3), results)
average_iou = 0.0
for idx, (iou, dice_score) in enumerate(zip(cls_wise_iou[:-1], cls_wise_dice_score[:-1])):
print("Digit {}: IOU: {} Dice Score: {}".format(idx, iou, dice_score))
average_iou += iou
grade = average_iou * 10
print("\nGrade is " + str(grade))
PASSING_GRADE = 60
if (grade>PASSING_GRADE):
print("You passed!")
else:
print("You failed. Please check your model and re-train")
model.save("model.h5")