-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreastCancer.py
More file actions
139 lines (113 loc) · 3.93 KB
/
BreastCancer.py
File metadata and controls
139 lines (113 loc) · 3.93 KB
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
import numpy as np
from keras.preprocessing import image
from keras.models import load_model
from PIL import Image
import keras.backend as K
K.set_image_data_format('channels_last')
import os
modelSavePath = 'D:\LIVE WIRE - DS\Project\Cancer-detection-20200429T095004Z-001\Cancer-detection\\my_model3.h5'
numOfTestPoints = 2
batchSize = 16
numOfEpoches = 10
classes = []
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
def getCropImgs(img, needRotations=False):
z = np.asarray(img, dtype=np.int8)
c = []
for i in range(3):
for j in range(4):
crop = z[512 * i:512 * (i + 1), 512 * j:512 * (j + 1), :]
c.append(crop)
if needRotations:
c.append(np.rot90(np.rot90(crop)))
return c
def getAsSoftmax(fname):
if (fname == 'b'):
return [1, 0, 0, 0]
elif (fname == 'is'):
return [0, 1, 0, 0]
elif (fname == 'iv'):
return [0, 0, 1, 0]
else:
return [0, 0, 0, 1]
def get_imgs_frm_folder(path):
x = []
y = []
cnt = 0
for foldname in os.listdir(path):
for filename in os.listdir(os.path.join(path, foldname)):
img = Image.open(os.path.join(os.path.join(path, foldname), filename))
# img.show()
crpImgs = getCropImgs(img)
cnt += 1
if cnt % 10 == 0:
print(str(cnt) + " Images loaded")
for im in crpImgs:
x.append(np.divide(np.asarray(im, np.float16), 255.))
y.append(getAsSoftmax(foldname))
print("Images cropped")
print("Loading as array")
return x, y, cnt
def predict(img, savedModelPath, showImg=True):
model = load_model(savedModelPath)
# if showImg:
# Image.fromarray(np.array(img, np.float16), 'RGB').show()
x = img
if showImg:
Image.fromarray(np.array(img, np.float16), 'RGB').show()
x = np.expand_dims(x, axis=0)
softMaxPred = model.predict(x)
print("prediction from Algo: " + str(softMaxPred) + "\n")
probs = softmaxToProbs(softMaxPred)
# plot_model(model, to_file='Model.png')
# SVG(model_to_dot(model).create(prog='dot', format='svg'))
maxprob = 0
maxI = 0
for j in range(len(probs)):
# print(str(j) + " : " + str(round(probs[j], 4)))
if probs[j] > maxprob:
maxprob = probs[j]
maxI = j
# print(softMaxPred)
print("prediction index: " + str(maxI))
return maxI, probs
def softmaxToProbs(soft):
z_exp = [np.math.exp(i) for i in soft[0]]
sum_z_exp = sum(z_exp)
return [(i / sum_z_exp) * 100 for i in z_exp]
def predictImage(img_path, arrayImg=None, printData=True):
crops = []
if arrayImg == None:
img = image.load_img(img_path)
crops = np.array(getCropImgs(img, needRotations=False), np.float16)
crops = np.divide(crops, 255.)
Image.fromarray(np.array(crops[0]), "RGB").show()
classes = []
classes.append("Benign")
classes.append("InSitu")
classes.append("Invasive")
classes.append("Normal")
compProbs = []
compProbs.append(0)
compProbs.append(0)
compProbs.append(0)
compProbs.append(0)
for i in range(len(crops)):
if printData:
print("\n\nCrop " + str(i + 1) + " prediction:\n")
___, probs = predict(crops[i], modelSavePath, showImg=False)
for j in range(len(classes)):
if printData:
print(str(classes[j]) + " : " + str(round(probs[j], 4)) + "%")
compProbs[j] += probs[j]
if printData:
print("\n\nAverage from all crops\n")
for j in range(len(classes)):
if printData:
print(str(classes[j]) + " : " + str(round(compProbs[j] / 12, 4)) + "%")
import tkinter.filedialog as f
import tkinter
win=tkinter.Tk()
predictImage(f.askopenfilename(filetypes=()))
win.destroy()