-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring.py
64 lines (47 loc) · 1.69 KB
/
scoring.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
"""
scoring.py
By: Sebastian D. Goodfellow, Ph.D., 2019
"""
# 3rd party imports
import os
import json
import base64
import numpy as np
from PIL import Image
from io import BytesIO
import tensorflow as tf
from azureml.core.model import Model
def init():
"""Initialization function for model deployment."""
# Set global variables
global images, predictions, sess
# Get model path
model_path = Model.get_model_path(model_name='mnist_tf_model', version=6)
# Start session
tf.reset_default_graph()
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
# Import meta graph
saver = tf.train.import_meta_graph(os.path.join(model_path, 'graphs',
'inference_graph_{}.meta'.format('array')))
# Get graph
graph = tf.get_default_graph()
# Get input tensor
images = graph.get_tensor_by_name(name='images:0')
# Get output tensor
predictions = graph.get_tensor_by_name(name='prediction:0')
# Initialize global variables
sess.run(tf.global_variables_initializer())
# Restore graph variables from checkpoint
saver.restore(sess=sess, save_path=os.path.join(model_path, 'checkpoints', 'model'))
def run(raw_data):
"""Run model inference."""
# Image shape
image_shape = (28, 28, 1)
# Load raw data
data = json.loads(raw_data)
# Get image arrays
inputs = np.array([np.array(Image.open(BytesIO(base64.b64decode(row['image']))),
dtype=np.uint8).reshape(image_shape) for row in data])
# Run model inverse with input data
output = predictions.eval(session=sess, feed_dict={images: inputs})
return json.dumps(output.tolist())