-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_online.py
54 lines (44 loc) · 6.69 KB
/
serve_online.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
import argparse
import numpy as np
import tensorflow as tf
from flask import Flask, request
from tensorflow.core.framework.graph_pb2 import GraphDef
from convolutional import SERVE_INPUT, SERVE_OUTPUT
webapp = Flask(__name__)
sess, input, output = None, None, None
input_shape = None
# curl -X POST http://localhost:8080/predict -d '-0.2568628,-0.3745098,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3588235,-0.3745098,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.4843137,-0.1196078,0.009803922,-0.009803922,-0.1117647,-0.3352941,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.2058824,0.5,0.5,0.5,0.5,0.4960784,0.1980392,-0.2411765,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3705882,0.2764706,0.3745098,0.3862745,0.4803922,0.5,0.5,0.5,0.06470589,-0.4607843,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.4764706,-0.4686275,-0.3666667,-0.01764706,0.4019608,0.5,0.5,0.05294118,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.4019608,0.2921569,0.5,0.3039216,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,0.1352941,0.5,0.3,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.2960784,0.4490196,0.5,0.1078431,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.1470588,0.4411765,0.5,0.4764706,-0.3470588,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.4411765,0.07254902,0.5,0.5,0.4882353,-0.1470588,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.2529412,0.3784314,0.5,0.5,0.3509804,-0.2647059,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.1235294,0.4764706,0.5,0.5,0.1509804,-0.4333333,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.4372549,0.1509804,0.5,0.5,0.4490196,-0.1862745,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.2333333,0.3470588,0.5,0.5,0.3078431,-0.3352941,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.4764706,0.08431373,0.5,0.5,0.4764706,0.04901961,-0.4568627,-0.4607843,-0.3156863,-0.1941177,-0.05294118,0.0372549,0.04117647,0.04117647,0.0372549,0.04117647,0.04117647,0.0372549,0.04901961,0.08039216,0.1196078,0.1235294,0.127451,-0.04117647,-0.3588235,-0.5,-0.5,-0.4843137,0.2215686,0.5,0.5,0.5,0.4647059,0.2529412,0.3352941,0.445098,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,-0.3588235,-0.5,-0.5,-0.4647059,0.4411765,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.4176471,0.3431373,0.327451,0.3392157,0.3313726,0.3352941,0.3352941,0.3392157,0.327451,0.2960784,0.2764706,0.2686275,0.2843137,0.127451,-0.3588235,-0.5,-0.5,-0.5,-0.2647059,0.09607843,0.1039216,0.0882353,0.0882353,0.0882353,0.03333334,-0.1,-0.2176471,-0.3431373,-0.4411765,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3588235,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.3627451,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5' -H 'Content-type: text/plain'
# Result should be like '2.52395e-08,8.37243e-07,0.999999,3.34691e-08,4.00975e-12,2.02342e-12,5.76613e-12,1.11706e-09,6.23306e-07,2.575e-10'
@webapp.route('/predict', methods=['POST'])
def predict():
global sess, input, output, input_shape
content = request.get_data().decode()
print('Request:', content)
floats = [float(item) for item in content.split(',')]
tensor = np.array(floats).reshape(input_shape)
result = sess.run(output, feed_dict={input:tensor})
response = ','.join([str(prob) for prob in result[0]])
print('Response:', response)
return response
def launch(args):
global sess, input, output, input_shape
ckpt = tf.train.get_checkpoint_state(args.model_dir)
if ckpt and ckpt.model_checkpoint_path:
saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path + '.meta' )
sess = tf.Session()
saver.restore(sess, ckpt.model_checkpoint_path)
graph = tf.get_default_graph()
input = graph.get_tensor_by_name(SERVE_INPUT + ':0')
output = graph.get_tensor_by_name(SERVE_OUTPUT + ':0')
input_shape = [dim.value for dim in input.shape]
else:
raise FileNotFoundError('Cannot find valid checkpoint from %s' % args.model_dir)
webapp.run(port=args.server_port) # Start service
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', type=str, default='model',
help='TensorFlow checkpoint directory to load.')
parser.add_argument('--server_port', type=int, default=8080,
help='Port of the web service.')
args, unknown = parser.parse_known_args()
launch(args)