-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
148 lines (106 loc) · 4.63 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
from keras import backend
import tensorflow as tf
session = tf.Session(config=tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1,
allow_soft_placement=True
))
backend.set_session(session)
import pdb
from time import time
from flask import Flask, render_template, request
from keras.applications import vgg16, vgg19, resnet50, inception_v3, mobilenet, xception, densenet, inception_resnet_v2, nasnet
from keras.preprocessing import image
import numpy as np
import os
# ERROR Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# file processing
from werkzeug.utils import secure_filename
# import global variables (model_mobilenet, model_resnet50, model_inception_v3)
import mymodels
# create Flask app
app = Flask(__name__)
def upload_image(request):
# Get the file from post request
file = request.files['file']
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
image_path = os.path.join(basepath, 'uploads', secure_filename(file.filename))
file.save(image_path)
return image_path
@app.route('/', methods = ['GET'])
def show_index():
print('*** entered homepage')
return render_template('index.html')
def preprocess_image(image_path, image_size, preprocess_function):
# load image from path
image_input = image.load_img(image_path, target_size=(image_size, image_size))
# read image input
img = image.img_to_array(image_input)
# reshape data for the model
img = np.expand_dims(img, axis=0)
# prepare the image for the models
img = preprocess_function(img)
print("image preprocessed")
return img
def predict_image(image_path, model_label, model_dict) -> str:
tic = time()
print("*** entered predict_image() for " + model_label)
# Get model-specific metadata and functions from model dictionary
image_size = model_dict['image_size']
top1_acc = model_dict['top1_acc']
top5_acc = model_dict['top5_acc']
model_preprocess_input = model_dict['model_preprocess_input']
model_decode_predictions = model_dict['model_decode_predictions']
# Get model instance and model name
model = model_dict['model_instance']
model_name = model.name
# preprocess image
img = preprocess_image(image_path, image_size, model_preprocess_input)
# predict the probability across all output classes
predictions = model.predict(img)
print("image predicted")
# convert the probabilities to class labels
label = model_decode_predictions(predictions, top=1)
print("predictions decoded")
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# get classification confidence (%)
confidence = '%.2f' % (label[2] * 100)
toc = time()
processing_time = '%.2f' % (toc-tic)
print("Image classified by %s in >> %s" % (model_name, processing_time))
classification = (label[1], model_label, confidence, processing_time, top1_acc, top5_acc)
print("image classified >> " + str(classification))
return classification
@app.route('/predict', methods = ['GET', 'POST'])
def generate_classifications_output():
print('*entered generate_classifications_output()')
if request.method == 'POST':
tic = time()
image_path = upload_image(request)
# Create html table for all classifications
output = '<table style="width:70%">'
output += '<caption>Top10 Machine Learning Models classify <br><em>%s</em> </caption>' % (os.path.basename(image_path))
output += '<tr><th>Classification</th><th>Model</th><th>Confidence</th><th>Time</th><th>Top1-Acc</th><th>Top5-Acc</th></tr>'
# feed predict_image(model_name, top1_acc, top5_acc, ptime, confidence, label) directly into html
html_content_list = [
'<tr><td>%s </td><td>%s</td><td>%s%%</td><td> %ss </td><td> %s%% </td><td> %s%% </td></tr>'
% predict_image(image_path, key, values)
for key, values in mymodels.model_data.items() ]
output += '\n'.join(html_content_list)
output += '</table>'
toc = time()
print("Total time for all predictions >> %.2f s" % (toc-tic) )
return output
return None
if (__name__ == '__main__'):
print('*** Starting WSGI Server...')
print('****************************************************')
print('*** Server is available at http://127.0.0.1:5000')
print('****************************************************')
# Get wsgi server to replace flask app.run()
from gevent.pywsgi import WSGIServer
web_server = WSGIServer(('', 5000), app)
web_server.serve_forever()