TensorFlow REST前端,但不TensorFlow服务 [英] TensorFlow REST Frontend but not TensorFlow Serving

查看:194
本文介绍了TensorFlow REST前端,但不TensorFlow服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部署一个简单的TensorFlow模型,并运行在像Flask这样的REST服务中。
在github或这里没有找到好的例子。



我还没有准备好使用TF Serving,谷歌,但我的任务与gRPC,bazel,C ++编码,protobuf矫枉过正...

解决方案

有不同的方法来做这个。纯粹的,使用张量不是很灵活,但是相对简单。这种方法的不足之处在于,您必须重新生成图形并在恢复模型的代码中初始化变量。在 tensorflow skflow / contrib learn 中显示的方式更多优雅,但是这似乎并没有在目前功能和文件已过时。

我在github 这里上放了一个简短的例子,将GET或POST参数命名为瓶型REST部署的tensorflow模型。



然后,主要的代码在一个基于POST / GET数据的字典中:

<$ p
$ parse_postget $ b $ def apply_model($)$ b $($)$ app $ b tf.reset_default_graph()
with tf.Session()as session:
n = 1
x = tf.placeholder(tf.float32,[n],name ='x')
y = tf.placeholder(tf.float32,[n],name ='y')
m = tf.Variable([1.0],name ='m')
b = tf.Variable([ 1.0],name ='b')
y = tf.add(tf.mul(m,x),b)#fit y_i = m * x_i + b
y_act = tf.placeholder(tf。 (0.05).minimize(float32,[n],name ='y_')
error = tf.sqrt((y - y_act)*(y - y_act))
train_step = tf.train.AdamOptimizer (error)

feed_dict = {x:np.array([float(d ['x_in'])]),y_act:np.array([float(d ['y_star'])] )}
saver = tf.train.Saver()
saver.restore(session ,'linear.chk')
y_i,_,_ = session.run([y,m,b],feed_dict)
返回jsonify(输出= float(y_i))


I want to deploy a simple TensorFlow model and run it in REST service like Flask. Did not find so far good example on github or here.

I am not ready to use TF Serving as suggested in other posts, it is perfect solution for Google but it overkill for my tasks with gRPC, bazel, C++ coding, protobuf...

解决方案

There are different ways to do this. Purely, using tensorflow is not very flexible, however relatively straightforward. The downside of this approach is that you have to rebuild the graph and initialize variables in the code where you restore the model. There is a way shown in tensorflow skflow/contrib learn which is more elegant, however this doesn't seem to be functional at the moment and the documentation is out of date.

I put a short example together on github here that shows how you would named GET or POST parameters to a flask REST-deployed tensorflow model.

The main code is then in a function that takes a dictionary based on the POST/GET data:

@app.route('/model', methods=['GET', 'POST'])
@parse_postget
def apply_model(d):
    tf.reset_default_graph()
    with tf.Session() as session:
        n = 1
        x = tf.placeholder(tf.float32, [n], name='x')
        y = tf.placeholder(tf.float32, [n], name='y')
        m = tf.Variable([1.0], name='m')
        b = tf.Variable([1.0], name='b')
        y = tf.add(tf.mul(m, x), b) # fit y_i = m * x_i + b
        y_act = tf.placeholder(tf.float32, [n], name='y_')
        error = tf.sqrt((y - y_act) * (y - y_act))
        train_step = tf.train.AdamOptimizer(0.05).minimize(error)

        feed_dict = {x: np.array([float(d['x_in'])]), y_act: np.array([float(d['y_star'])])}
        saver = tf.train.Saver()
        saver.restore(session, 'linear.chk')
        y_i, _, _ = session.run([y, m, b], feed_dict)
    return jsonify(output=float(y_i))

这篇关于TensorFlow REST前端,但不TensorFlow服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆