TensorFlow REST Frontend but not TensorFlow Serving

前端 未结 3 1587
情深已故
情深已故 2021-02-04 01:07

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

3条回答
  •  梦谈多话
    2021-02-04 02:05

    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))
    

提交回复
热议问题