-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_benign_normal_malignant.py
108 lines (82 loc) · 2.7 KB
/
predict_benign_normal_malignant.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
from __future__ import print_function
import os
# import glob
from glob import glob
import cv2 as cv
import numpy as np
from datetime import datetime
import keras
from keras.models import Sequential
from keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D, MaxPooling2D, AveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.core import Activation, Flatten, Dense, Dropout
from keras.callbacks import ModelCheckpoint
# from keras import backend as Kback
def build_model(hight, weight, num_classes):
model = Sequential()
# Layer 1
model.add(Conv2D(4, (3,3), padding="same", input_shape = (hight, weight, 1)))
model.add(LeakyReLU(alpha=0.03))
# Layer 2
model.add(Conv2D(4, (3,3), padding="same"))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D())
# Layer 3
model.add(Conv2D(4, (3,3), padding="same"))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D())
# Layer 4
model.add(Conv2D(4, (3,3), padding="same"))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D())
# Layer 5
model.add(Conv2D(4, (3,3), padding="same"))
model.add(LeakyReLU(alpha=0.03))
# Layer 6
model.add(Conv2D(4, (3,3), padding="same"))
model.add(LeakyReLU(alpha=0.03))
model.add(AveragePooling2D())
# Fully Connected Layer
# Layer 1 fully connected
model.add(Flatten())
model.add(Dense(100))
model.add(Activation('relu'))
# Layer 2 fully connected
model.add(Dense(100))
model.add(Activation('relu'))
# Layer 3 fully connected
model.add(Dense(num_classes))
model.add(Activation('softmax'))
return model
# Hyperparameters
imageShape = (224, 224)
def test(weights_file):
start = datetime.now()
ipath = glob(os.path.join("test", "*.pgm"))
arrList = []
for path in ipath:
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
img = cv.resize(img, imageShape).reshape((imageShape[0], imageShape[1], 1))
arrList.append(img)
x_test = np.array(arrList)
x_test = x_test.astype('float32')
x_test /= 255
print(x_test.shape)
model = build_model(imageShape[0], imageShape[1], 3)
model.load_weights(weights_file)
output = model.predict_classes(x_test, verbose=1)
print(output)
# for out in output:
# if out == 0:
for out, name in zip(output, ipath):
name = name.split("/")[1]
if out == 0:
# print(name, "Normal")
print("{0:5} Normal".format(name))
elif out == 1:
print("{0:5} Benign".format(name))
else:
print("{0:5} Malignant".format(name))
# train()
test('weights-img-0.99.hdf5')