-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCombining Segmentation and Classification Model.py
109 lines (91 loc) · 3.9 KB
/
Combining Segmentation and Classification Model.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
def prediction(test, model, model_seg):
'''
Predcition function which takes dataframe containing ImageID as Input and perform 2 type of prediction on the image
Initially, image is passed through the classification network which predicts whether the image has defect or not, if the model
is 99% sure that the image has no defect, then the image is labeled as no-defect, if the model is not sure, it passes the image to the
segmentation network, it again checks if the image has defect or not, if it has defect, then the type and location of defect is found
'''
# empty list to store results
mask, image_id, has_mask = [], [], []
#itetrating through each image in test data
for i in test.image_path:
img = io.imread(i)
#normalizing
img = img *1./255.
#reshaping
img = cv2.resize(img, (256,256))
# converting img into array
img = np.array(img, dtype=np.float64)
#reshaping the image from 256,256,3 to 1,256,256,3
img = np.reshape(img, (1,256,256,3))
#making prediction for tumor in image
is_defect = model.predict(img)
#if tumour is not present we append the details of the image to the list
if np.argmax(is_defect)==0:
image_id.append(i)
has_mask.append(0)
mask.append('No mask :)')
continue
#Creating a empty array of shape 1,256,256,1
X = np.empty((1,256,256,3))
# read the image
img = io.imread(i)
#resizing the image and coverting them to array of type float64
img = cv2.resize(img, (256,256))
img = np.array(img, dtype=np.float64)
# standardising the image
img -= img.mean()
img /= img.std()
#converting the shape of image from 256,256,3 to 1,256,256,3
X[0,] = img
#make prediction of mask
predict = model_seg.predict(X)
# if sum of predicted mask is 0 then there is not tumour
if predict.round().astype(int).sum()==0:
image_id.append(i)
has_mask.append(0)
mask.append('No mask :)')
else:
#if the sum of pixel values are more than 0, then there is tumour
image_id.append(i)
has_mask.append(1)
mask.append(predict)
return pd.DataFrame({'image_path': image_id,'predicted_mask': mask,'has_mask': has_mask})
# making prediction
df_pred = prediction(test, model, seg_model)
df_pred
# merging original and prediction df
df_pred = test.merge(df_pred, on='image_path')
df_pred.head(10)
#visualizing prediction
count = 0
fig, axs = plt.subplots(15,5, figsize=(30,70))
for i in range(len(df_pred)):
if df_pred.has_mask[i]==1 and count<15:
#read mri images
img = io.imread(df_pred.image_path[i])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
axs[count][0].imshow(img)
axs[count][0].title.set_text('Brain MRI')
#read original mask
mask = io.imread(df_pred.mask_path[i])
axs[count][1].imshow(mask)
axs[count][1].title.set_text('Original Mask')
#read predicted mask
pred = np.array(df_pred.predicted_mask[i]).squeeze().round()
axs[count][2].imshow(pred)
axs[count][2].title.set_text('AI predicted mask')
#overlay original mask with MRI
img[mask==255] = (255,0,0)
axs[count][3].imshow(img)
axs[count][3].title.set_text('Brain MRI with original mask (Ground Truth)')
#overlay predicted mask and MRI
img_ = io.imread(df_pred.image_path[i])
img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
img_[pred==1] = (0,255,150)
axs[count][4].imshow(img_)
axs[count][4].title.set_text('MRI with AI PREDICTED MASK')
count +=1
if (count==15):
break
fig.tight_layout()