-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_kernel.py
605 lines (483 loc) · 19.5 KB
/
test_kernel.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
import os
import sys
import random
import math
import numpy as np
import cv2
import matplotlib.pyplot as plt
import json
import pydicom
from imgaug import augmenters as iaa
from tqdm import tqdm
import pandas as pd
import glob
#from sklearn.model_selection import KFold
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
#config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))
os.environ['CUDA_VISIBLE_DEVICES']="0"
#DATA_DIR = '/kaggle/input'
DATA_DIR = '/data/krf/dataset'
# Directory to save logs and trained model
#ROOT_DIR = '/kaggle/working'
ROOT_DIR = '/data/krf/model/rsna'
# Import Mask RCNN
sys.path.append(os.path.join(ROOT_DIR, 'Mask_RCNN')) # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log
train_dicom_dir = os.path.join(DATA_DIR, 'stage_1_train_images')
test_dicom_dir = os.path.join(DATA_DIR, 'stage_1_test_images')
COCO_WEIGHTS_PATH = "mask_rcnn_coco.h5"
def get_dicom_fps(dicom_dir):
dicom_fps = glob.glob(dicom_dir+'/'+'*.dcm')
return list(set(dicom_fps))
def parse_dataset(dicom_dir, anns):
image_fps = get_dicom_fps(dicom_dir)
image_annotations = {fp: [] for fp in image_fps}
for index, row in anns.iterrows():
fp = os.path.join(dicom_dir, row['patientId']+'.dcm')
image_annotations[fp].append(row)
return image_fps, image_annotations
# The following parameters have been selected to reduce running time for demonstration purposes
# These are not optimal
class DetectorConfig(Config):
"""Configuration for training pneumonia detection on the RSNA pneumonia dataset.
Overrides values in the base Config class.
"""
# Give the configuration a recognizable name
NAME = 'pneumonia'
# Train on 1 GPU and 8 images per GPU. We can put multiple images on each
# GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
GPU_COUNT = 1
IMAGES_PER_GPU = 8
BACKBONE = 'resnet50'
NUM_CLASSES = 2 # background + 1 pneumonia classes
IMAGE_MIN_DIM = 256
IMAGE_MAX_DIM = 256
RPN_ANCHOR_SCALES = (16, 32, 64, 128)
TRAIN_ROIS_PER_IMAGE = 32
MAX_GT_INSTANCES = 3
DETECTION_MAX_INSTANCES = 3
DETECTION_MIN_CONFIDENCE = 0.7 ## match target distribution
DETECTION_NMS_THRESHOLD = 0.1
STEPS_PER_EPOCH = 200
config = DetectorConfig()
config.display()
class DetectorDataset(utils.Dataset):
"""Dataset class for training pneumonia detection on the RSNA pneumonia dataset.
"""
def __init__(self, image_fps, image_annotations, orig_height, orig_width):
super().__init__(self)
# Add classes
self.add_class('pneumonia', 1, 'Lung Opacity')
# add images
for i, fp in enumerate(image_fps):
annotations = image_annotations[fp]
self.add_image('pneumonia', image_id=i, path=fp,
annotations=annotations, orig_height=orig_height, orig_width=orig_width)
def image_reference(self, image_id):
info = self.image_info[image_id]
return info['path']
def load_image(self, image_id):
info = self.image_info[image_id]
fp = info['path']
ds = pydicom.read_file(fp)
image = ds.pixel_array
# If grayscale. Convert to RGB for consistency.
if len(image.shape) != 3 or image.shape[2] != 3:
image = np.stack((image,) * 3, -1)
return image
def load_mask(self, image_id):
info = self.image_info[image_id]
annotations = info['annotations']
count = len(annotations)
if count == 0:
mask = np.zeros((info['orig_height'], info['orig_width'], 1), dtype=np.uint8)
class_ids = np.zeros((1,), dtype=np.int32)
else:
mask = np.zeros((info['orig_height'], info['orig_width'], count), dtype=np.uint8)
class_ids = np.zeros((count,), dtype=np.int32)
for i, a in enumerate(annotations):
if a['Target'] == 1:
x = int(a['x'])
y = int(a['y'])
w = int(a['width'])
h = int(a['height'])
mask_instance = mask[:, :, i].copy()
cv2.rectangle(mask_instance, (x, y), (x+w, y+h), 255, -1)
mask[:, :, i] = mask_instance
class_ids[i] = 1
return mask.astype(np.bool), class_ids.astype(np.int32)
# training dataset
anns = pd.read_csv(os.path.join(DATA_DIR, 'stage_1_train_labels.csv'))
anns.head()
image_fps, image_annotations = parse_dataset(train_dicom_dir, anns=anns)
ds = pydicom.read_file(image_fps[0]) # read dicom image from filepath
image = ds.pixel_array # get image array
# Original DICOM image size: 1024 x 1024
ORIG_SIZE = 1024
image_fps_list = list(image_fps)
random.seed(42)
random.shuffle(image_fps_list)
val_size = 1500
image_fps_val = image_fps_list[:val_size]
image_fps_train = image_fps_list[val_size:]
print(len(image_fps_train), len(image_fps_val))
# print(image_fps_val[:6])
# prepare the training dataset
dataset_train = DetectorDataset(image_fps_train, image_annotations, ORIG_SIZE, ORIG_SIZE)
dataset_train.prepare()
# Show annotation(s) for a DICOM image
test_fp = random.choice(image_fps_train)
image_annotations[test_fp]
# prepare the validation dataset
dataset_val = DetectorDataset(image_fps_val, image_annotations, ORIG_SIZE, ORIG_SIZE)
dataset_val.prepare()
# Load and display random sample and their bounding boxes
class_ids = [0]
while class_ids[0] == 0: ## look for a mask
image_id = random.choice(dataset_train.image_ids)
image_fp = dataset_train.image_reference(image_id)
image = dataset_train.load_image(image_id)
mask, class_ids = dataset_train.load_mask(image_id)
print(image.shape)
plt.figure(figsize=(10, 10))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.axis('off')
plt.subplot(1, 2, 2)
masked = np.zeros(image.shape[:2])
for i in range(mask.shape[2]):
masked += image[:, :, 0] * mask[:, :, i]
plt.imshow(masked, cmap='gray')
plt.axis('off')
print(image_fp)
print(class_ids)
# Image augmentation (light but constant)
augmentation = iaa.Sequential([
iaa.OneOf([ ## geometric transform
iaa.Affine(
scale={"x": (0.98, 1.02), "y": (0.98, 1.04)},
translate_percent={"x": (-0.02, 0.02), "y": (-0.04, 0.04)},
rotate=(-2, 2),
shear=(-1, 1),
),
iaa.PiecewiseAffine(scale=(0.001, 0.025)),
]),
iaa.OneOf([ ## brightness or contrast
iaa.Multiply((0.9, 1.1)),
iaa.ContrastNormalization((0.9, 1.1)),
]),
iaa.OneOf([ ## blur or sharpen
iaa.GaussianBlur(sigma=(0.0, 0.1)),
iaa.Sharpen(alpha=(0.0, 0.1)),
]),
])
# test on the same image as above
imggrid = augmentation.draw_grid(image[:, :, 0], cols=5, rows=2)
plt.figure(figsize=(30, 12))
_ = plt.imshow(imggrid[:, :, 0], cmap='gray')
model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR)
# # Exclude the last layers because they require a matching
# # number of classes
# model.load_weights(COCO_WEIGHTS_PATH, by_name=True, exclude=[
# "mrcnn_class_logits", "mrcnn_bbox_fc",
# "mrcnn_bbox", "mrcnn_mask"])
# LEARNING_RATE = 0.002
# # Train Mask-RCNN Model
# import warnings
# warnings.filterwarnings("ignore")
# ## train heads with higher lr to speedup the learning
# model.train(dataset_train, dataset_val,
# learning_rate=LEARNING_RATE*2,
# epochs=2,
# layers='heads',
# augmentation=None) ## no need to augment yet
# history = model.keras_model.history.history
# model.train(dataset_train, dataset_val,
# learning_rate=LEARNING_RATE,
# epochs=6,
# layers='all',
# augmentation=augmentation)
# new_history = model.keras_model.history.history
# for k in new_history: history[k] = history[k] + new_history[k]
# model.train(dataset_train, dataset_val,
# learning_rate=LEARNING_RATE/5,
# epochs=20,
# layers='all',
# augmentation=augmentation)
# new_history = model.keras_model.history.history
# for k in new_history: history[k] = history[k] + new_history[k]
# model.train(dataset_train, dataset_val,
# learning_rate = 0.0001,
# epochs=40,
# layers='all',
# augmentation=augmentation)
# new_history = model.keras_model.history.history
# for k in new_history: history[k] = history[k] + new_history[k]
# model.train(dataset_train, dataset_val,
# learning_rate = 0.00001,
# epochs=60,
# layers='all',
# augmentation=augmentation)
# new_history = model.keras_model.history.history
# for k in new_history: history[k] = history[k] + new_history[k]
# epochs = range(1,len(next(iter(history.values())))+1)
# pd.DataFrame(history, index=epochs)
# plt.figure(figsize=(17,5))
# plt.subplot(131)
# plt.plot(epochs, history["loss"], label="Train loss")
# plt.plot(epochs, history["val_loss"], label="Valid loss")
# plt.legend()
# plt.subplot(132)
# plt.plot(epochs, history["mrcnn_class_loss"], label="Train class ce")
# plt.plot(epochs, history["val_mrcnn_class_loss"], label="Valid class ce")
# plt.legend()
# plt.subplot(133)
# plt.plot(epochs, history["mrcnn_bbox_loss"], label="Train box loss")
# plt.plot(epochs, history["val_mrcnn_bbox_loss"], label="Valid box loss")
# plt.legend()
# plt.show()
# plt.savefig(ROOT_DIR+"/log.png")
# best_epoch = np.argmin(history["val_loss"])
# print("Best Epoch:", best_epoch + 1)
best_epoch = 25
## select trained model
dir_names = next(os.walk(model.model_dir))[1]
key = config.NAME.lower()
dir_names = filter(lambda f: f.startswith(key), dir_names)
dir_names = sorted(dir_names)
if not dir_names:
import errno
raise FileNotFoundError(
errno.ENOENT,
"Could not find model directory under {}".format(self.model_dir))
fps = []
# Pick last directory
#for d in dir_names:
d = dir_names[-1]
dir_name = os.path.join(model.model_dir, d)
# Find the last checkpoint
checkpoints = next(os.walk(dir_name))[2]
#print(checkpoints)
checkpoints = filter(lambda f: f.startswith("mask_rcnn"), checkpoints)
checkpoints = sorted(checkpoints)
if not checkpoints:
print('No weight files in {}'.format(dir_name))
else:
print(checkpoints)
checkpoint = os.path.join(dir_name, checkpoints[best_epoch])
fps.append(checkpoint)
model_path = sorted(fps)[-1]
print('Found model {}'.format(model_path))
class InferenceConfig(DetectorConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1
inference_config = InferenceConfig()
# Recreate the model in inference mode
model = modellib.MaskRCNN(mode='inference',
config=inference_config,
model_dir=ROOT_DIR)
# Load trained weights (fill in path to trained weights here)
assert model_path != "", "Provide path to trained weights"
print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)
# set color for class
def get_colors_for_class_ids(class_ids):
colors = []
for class_id in class_ids:
if class_id == 1:
colors.append((.941, .204, .204))
return colors
# Show few example of ground truth vs. predictions on the validation dataset
dataset = dataset_val
fig = plt.figure(figsize=(10, 30))
for i in range(6):
image_id = random.choice(dataset.image_ids)
original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\
modellib.load_image_gt(dataset_val, inference_config,
image_id, use_mini_mask=False)
print(original_image.shape)
plt.subplot(6, 2, 2*i + 1)
visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id,
dataset.class_names,
colors=get_colors_for_class_ids(gt_class_id), ax=fig.axes[-1])
plt.subplot(6, 2, 2*i + 2)
results = model.detect([original_image]) #, verbose=1)
r = results[0]
visualize.display_instances(original_image, r['rois'], r['masks'], r['class_ids'],
dataset.class_names, r['scores'],
colors=get_colors_for_class_ids(r['class_ids']), ax=fig.axes[-1])
# Get filenames of test dataset DICOM images
test_image_fps = get_dicom_fps(test_dicom_dir)
# Make predictions on test images, write out sample submission
def predict(image_fps, filepath='submission.csv', min_conf=0.9):
# assume square image
resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]
#resize_factor = ORIG_SIZE
with open(filepath, 'w') as file:
file.write("patientId,PredictionString\n")
for image_id in tqdm(image_fps):
ds = pydicom.read_file(image_id)
image = ds.pixel_array
# If grayscale. Convert to RGB for consistency.
if len(image.shape) != 3 or image.shape[2] != 3:
image = np.stack((image,) * 3, -1)
image, window, scale, padding, crop = utils.resize_image(
image,
min_dim=config.IMAGE_MIN_DIM,
min_scale=config.IMAGE_MIN_SCALE,
max_dim=config.IMAGE_MAX_DIM,
mode=config.IMAGE_RESIZE_MODE)
patient_id = os.path.splitext(os.path.basename(image_id))[0]
results = model.detect([image])
r = results[0]
out_str = ""
out_str += patient_id
out_str += ","
assert( len(r['rois']) == len(r['class_ids']) == len(r['scores']) )
if len(r['rois']) == 0:
pass
else:
num_instances = len(r['rois'])
for i in range(num_instances):
if r['scores'][i] > min_conf:
out_str += ' '
out_str += str(round(r['scores'][i], 2))
out_str += ' '
# x1, y1, width, height
x1 = r['rois'][i][1]
y1 = r['rois'][i][0]
width = r['rois'][i][3] - x1
height = r['rois'][i][2] - y1
bboxes_str = "{} {} {} {}".format(x1*resize_factor, y1*resize_factor, \
width*resize_factor, height*resize_factor)
out_str += bboxes_str
file.write(out_str+"\n")
submission_fp = os.path.join(ROOT_DIR, 'submission_1024testkernel2.csv')
predict(test_image_fps, filepath=submission_fp)
print(submission_fp)
output = pd.read_csv(submission_fp)
output.head(60)
# show a few test image detection example
def visualize():
image_id = random.choice(test_image_fps)
ds = pydicom.read_file(image_id)
# original image
image = ds.pixel_array
# assume square image
resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]
# If grayscale. Convert to RGB for consistency.
if len(image.shape) != 3 or image.shape[2] != 3:
image = np.stack((image,) * 3, -1)
resized_image, window, scale, padding, crop = utils.resize_image(
image,
min_dim=config.IMAGE_MIN_DIM,
min_scale=config.IMAGE_MIN_SCALE,
max_dim=config.IMAGE_MAX_DIM,
mode=config.IMAGE_RESIZE_MODE)
patient_id = os.path.splitext(os.path.basename(image_id))[0]
print(patient_id)
results = model.detect([resized_image])
r = results[0]
for bbox in r['rois']:
print(bbox)
x1 = int(bbox[1] * resize_factor)
y1 = int(bbox[0] * resize_factor)
x2 = int(bbox[3] * resize_factor)
y2 = int(bbox[2] * resize_factor)
cv2.rectangle(image, (x1,y1), (x2,y2), (77, 255, 9), 3, 1)
width = x2 - x1
height = y2 - y1
print("x {} y {} h {} w {}".format(x1, y1, width, height))
plt.figure()
plt.imshow(image, cmap=plt.cm.gist_gray)
plt.savefig(ROOT_DIR+"/"+patient_id+".png")
visualize()
visualize()
visualize()
visualize()
# helper function to calculate IoU
def iou(box1, box2):
x11, y11, w1, h1 = box1
x21, y21, w2, h2 = box2
#assert w1 * h1 > 0
#assert w2 * h2 > 0
x12, y12 = x11 + w1, y11 + h1
x22, y22 = x21 + w2, y21 + h2
area1, area2 = w1 * h1, w2 * h2
xi1, yi1, xi2, yi2 = max([x11, x21]), max([y11, y21]), min([x12, x22]), min([y12, y22])
if xi2 <= xi1 or yi2 <= yi1:
return 0
else:
intersect = (xi2-xi1) * (yi2-yi1)
union = area1 + area2 - intersect
return intersect / union
def map_iou(boxes_true, boxes_pred, scores, thresholds = [0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75],min_conf=0.9):
"""
Mean average precision at differnet intersection over union (IoU) threshold
input:
boxes_true: Mx4 numpy array of ground true bounding boxes of one image.
bbox format: (x1, y1, w, h)
boxes_pred: Nx4 numpy array of predicted bounding boxes of one image.
bbox format: (x1, y1, w, h)
scores: length N numpy array of scores associated with predicted bboxes
thresholds: IoU shresholds to evaluate mean average precision on
output:
map: mean average precision of the image
"""
# According to the introduction, images with no ground truth bboxes will not be
# included in the map score unless there is a false positive detection (?)
# return None if both are empty, don't count the image in final evaluation (?)
if len(boxes_true) == 0 and len(boxes_pred) == 0:
return 0
assert boxes_true.shape[1] == 4 or boxes_pred.shape[1] == 4, "boxes should be 2D arrays with shape[1]=4"
if len(boxes_pred):
assert len(scores) == len(boxes_pred), "boxes_pred and scores should be same length"
# sort boxes_pred by scores in decreasing order
scores = np.array(scores)
boxes_pred = boxes_pred[scores>=min_conf]
scores = scores[scores>=min_conf]
boxes_pred = boxes_pred[np.argsort(scores)[::-1], :]
if len(boxes_true) == 0 and len(boxes_pred) == 0:
return 0
map_total = 0
# loop over thresholds
for t in thresholds:
matched_bt = set()
tp, fn = 0, 0
for i, bt in enumerate(boxes_true):
matched = False
for j, bp in enumerate(boxes_pred):
miou = iou(bt, bp)
if miou >= t and not matched and j not in matched_bt:
matched = True
tp += 1 # bt is matched for the first time, count as TP
matched_bt.add(j)
if not matched:
fn += 1 # bt has no match, count as FN
fp = len(boxes_pred) - len(matched_bt) # FP is the bp that not matched to any bt
m = tp / (tp + fn + fp)
map_total += m
return map_total / len(thresholds)
# detection on val_data
def evaluate(dataset_eval):
summ = 0.0
# assume square image
resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]
#resize_factor = ORIG_SIZE
for image_id in tqdm(dataset_eval.image_ids):
original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\
modellib.load_image_gt(dataset_eval, inference_config,
image_id, use_mini_mask=False)
results = model.detect([original_image]) #, verbose=1)
r = results[0]
summ += map_iou(gt_bbox,r['rois'],r['scores'])
return summ/len(dataset_eval.image_ids)
print(evaluate(dataset_val))