# 阿里云机器学习PAI EAS部署TensorFlow Model

#### Step By Step

1、TensorFlow模型训练Code Sample

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
import tensorflow as tf

if __name__ == '__main__':
x = tf.placeholder(tf.float32, [None,784], name="x")
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b, name="y")
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_:mnist.test.labels}))
saver = tf.train.Saver()
tf.saved_model.simple_save(
sess,
"./savedmodel/",
inputs={"image": x},   ## x是模型的输入变量
outputs={"scores": y}  ## y是模型的输出
)

2、模型导出保存打包

3、EAS控制台导入模型

4、获取模型信息

curl http://18482178.cn-shanghai.pai-eas.aliyuncs.com/api/predict/* -H 'Authorization:' | python -mjson.tool

5、Python SDK调用

eas-prediction 包安装

Code Sample

#!/usr/bin/env python
from eas_prediction import PredictClient, TFRequest

import cv2
import numpy as np

with open('2.jpg', 'rb') as infile:
# 使用numpy将字节流转换成array
x = np.fromstring(buf, dtype='uint8')
# 将读取到的array进行图片解码获得28 × 28的矩阵
# 由于预测服务API需要长度为784的一维向量将矩阵reshape成784
img = np.reshape(img, 784)

if __name__ == '__main__':

# http://1848217816******.cn-shanghai.pai-eas.aliyuncs.com/api/predict/tarotensor
client = PredictClient('1848******.cn-shanghai.pai-eas.aliyuncs.com', 'tarotensor')
#  注意上面的client = PredictClient(）内填入的信息，是通过对调用信息窗口（下图）中获取的访问地址的拆分
client.set_token('NjlmZDFjYzR*******')
#  Token信息在“EAS控制台—服务列表—服务—调用信息—公网地址调用—Token”中获取
client.init()

req = TFRequest('serving_default') # signature_name 参数

resp = client.predict(req)
print(resp)

Result

outputs {
key: "scores"
value {
dtype: DT_FLOAT
array_shape {
dim: 1
dim: 10
}
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
}
}

+ 订阅