-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
206 lines (165 loc) · 6.65 KB
/
app.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
# Dependecies
from __future__ import division, print_function
import os
import argparse
import numpy as np
# Image
import cv2
from sklearn.preprocessing import normalize
# Tensorflow-Keras
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.models import load_model, Model
from tensorflow.keras.callbacks import Callback, ModelCheckpoint, TensorBoard
import tensorflow.keras.backend as K
# Flask utils
from gevent.pywsgi import WSGIServer
from werkzeug.utils import secure_filename
from flask import Flask, redirect, url_for, request, render_template
# Config
parser = argparse.ArgumentParser()
parser.add_argument("-w1", "--width", help="Target Image Width", type=int, default=256)
parser.add_argument("-h1", "--height", help="Target Image Height", type=int, default=256)
parser.add_argument("-c1", "--channel", help="Target Image Channel", type=int, default=1)
parser.add_argument("-p", "--path", help="Best Model Location Path", type=str, default="models/heysaw_fold_1.h5")
parser.add_argument("-s", "--save", help="Save Uploaded Image", type=bool, default=False)
parser.add_argument("--port", help="WSGIServer Port ID", type=int, default=5000)
args = parser.parse_args()
SHAPE = (args.width, args.height, args.channel)
MODEL_SAVE_PATH = args.path
SAVE_LOADED_IMAGES = args.save
# Metrics
def recall(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def f1(y_true, y_pred):
precisionx = precision(y_true, y_pred)
recallx = recall(y_true, y_pred)
return 2*((precisionx*recallx)/(precisionx+recallx+K.epsilon()))
# SE block
def squeeze_excite_block(input, ratio=16):
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = init.shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
def create_model():
dropRate = 0.3
init = Input(SHAPE)
x = Conv2D(32, (3, 3), activation=None, padding='same')(init)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(32, (3, 3), activation=None, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x1 = MaxPooling2D((2,2))(x)
x = Conv2D(64, (3, 3), activation=None, padding='same')(x1)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = squeeze_excite_block(x)
x = Conv2D(64, (5, 5), activation=None, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = squeeze_excite_block(x)
x = Conv2D(64, (3, 3), activation=None, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x2 = MaxPooling2D((2,2))(x)
x = Conv2D(128, (3, 3), activation=None, padding='same')(x2)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = squeeze_excite_block(x)
x = Conv2D(128, (2, 2), activation=None, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = squeeze_excite_block(x)
x = Conv2D(128, (3, 3), activation=None, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x3 = MaxPooling2D((2,2))(x)
ginp1 = UpSampling2D(size=(2, 2), interpolation='bilinear')(x1)
ginp2 = UpSampling2D(size=(4, 4), interpolation='bilinear')(x2)
ginp3 = UpSampling2D(size=(8, 8), interpolation='bilinear')(x3)
concat = Concatenate()([ginp1, ginp2, ginp3])
gap = GlobalAveragePooling2D()(concat)
x = Dense(256, activation=None)(gap)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(dropRate)(x)
x = Dense(256, activation=None)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dense(4, activation='softmax')(x)
model = Model(init, x)
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=1e-3, momentum=0.9), metrics=['acc', precision, recall, f1])
return model
model = create_model()
print(model.summary())
model.load_weights(MODEL_SAVE_PATH)
print('Model loaded. Check http://localhost:{}/'.format(args.port))
def model_predict(img_path, model):
img = np.array(cv2.imread(img_path))
img = cv2.resize(img, SHAPE[:2])
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = normalize(img)
img = np.expand_dims(img, axis=2)
prediction = model.predict(np.expand_dims(img, axis=0), batch_size=1)
return prediction
# Threshold predictions
def threshold_arr(array):
new_arr = []
for ix, val in enumerate(array):
loc = np.array(val).argmax(axis=0)
k = list(np.zeros((len(val)), dtype=np.float16))
k[loc]=1
new_arr.append(k)
return np.array(new_arr, dtype=np.float16)
os.chdir("deploy/")
# Define a flask app
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# Get the file from post request
f = request.files['file']
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(f.filename))
f.save(file_path)
# Make prediction
preds = model_predict(file_path, model)
pred_class = threshold_arr(preds)[0]
if pred_class[0] == 1:
result = "Choroidal Neovascularization (" + str(preds[0][0]) + ")"
elif pred_class[1] == 1:
result = "Diabetic Macular Edema (" + str(preds[0][1]) + ")"
elif pred_class[2] == 1:
result = "DRUSEN (" + str(preds[0][2]) + ")"
elif pred_class[3] == 1:
result = "NORMAL (" + str(preds[0][3]) + ")"
if not SAVE_LOADED_IMAGES:
os.remove(file_path)
return result
return None
if __name__ == '__main__':
http_server = WSGIServer(('', args.port), app)
http_server.serve_forever()