Tensorflow模型即服务产生错误

时间:2018-08-03 05:32:18

标签: python tensorflow flask

我想使用冻结的pb模型,但是有这样的问题:

ValueError:提取参数不能解释为张量。 (操作名称:“ init_1” op:“ NoOp”  不是该图的元素。)

import base64
import json, argparse, time
import sys
import tensorflow as tf

from flask import Flask, request
from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app)

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name="prefix")
    return graph


@app.route("/predict", methods=['GET'])
def predict():
    start = time.time()

    persistent_sess.run(tf.global_variables_initializer())

    image_b64 = base64.urlsafe_b64encode(open('1.jpg', "rb").read())
    y_out = persistent_sess.run(y, feed_dict={ x:image_b64 })

    return 'Success'

if __name__ == "__main__":
    try:
        port = int(sys.argv[1])
    except Exception as e:
        port = 80

    print('Loading the model')
    graph = load_graph('output/open_nsfw.pb')
    x = graph.get_tensor_by_name('prefix/input:0')
    y = graph.get_tensor_by_name('prefix/predictions:0')

    persistent_sess = tf.Session(graph=graph)

    print('Starting the API')
    app.run(host='0.0.0.0', port=port, debug=True)

Stacktrace:

    追溯(最近一次通话):       在__call__文件1997行中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”         返回self.wsgi_app(环境,start_response)       wsgi_app中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,第1985行         响应= self.handle_exception(e)       包裹在函数中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask_cors/extension.py”,第161行         返回cors_after_request(app.make_response(f(* args,** kwargs)))       在handle_exception中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,第1540行         加价(exc_type,exc_value,tb)       重新列出文件“ /home/anaconda3/lib/python3.6/site-packages/flask/_compat.py”,第33行         提高价值       wsgi_app中的第1982行的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”         响应= self.full_dispatch_request()       在full_dispatch_request中,文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,行1614         rv = self.handle_user_exception(e)       包裹在函数中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask_cors/extension.py”,第161行         返回cors_after_request(app.make_response(f(* args,** kwargs)))       在handle_user_exception中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,第1517行         加价(exc_type,exc_value,tb)       重新列出文件“ /home/anaconda3/lib/python3.6/site-packages/flask/_compat.py”,第33行         提高价值       在full_dispatch_request中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,行1612         rv = self.dispatch_request()       在dispatch_request中的文件“ /home/anaconda3/lib/python3.6/site-packages/flask/app.py”,行1598         返回self.view_functions [rule.endpoint](** req.view_args)       预测文件“ /home/notebook/tensorflow-open_nsfw-master/tf.py”,第32行         persistent_sess.run(tf.global_variables_initializer())       运行中的文件“ /home/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,行778         run_metadata_ptr)       _run中的文件“ /home/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,行969         fetch_handler = _FetchHandler(self._graph,获取,feed_dict_string)       __init__中的文件“ /home/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,第408行         self._fetch_mapper = _FetchMapper.for_fetch(获取)       for_fetch中的文件“ /home/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,第238行         返回_ElementFetchMapper(fetches,contraction_fn)       __init__中的文件“ /home/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”,第274行         '张量。 (%s)'%(获取,str(e)))     ValueError:无法将获取参数解释为张量。 (操作名称:“ init_5”     op:“ NoOp”      不是此图的元素。)

0 个答案:

没有答案