在Flask使用TensorFlow的几个常见错误

在Flask使用TensorFlow的几个常见错误

1. ValueError: Tensor Tensor(“dense_1/Sigmoid:0”, shape=(?, 1), dtype=float32) is not an element of this graph.

  在Flask中使用tensorflow的model,一在界面中调用 model.predict() 就报下面这个错误,不过在单独的 .py 文件中使用却不报错。

ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.

  这个bug真的很是纠心,网上一般说是添加如下代码

import tensorflow as tf
graph = tf.get_default_graph()
model = models.load_model(…………)

# 使用处添加:
global graph
global model
with graph.as_default():
    model.predict()
    # 执行预测函数

  但是我当时测试时又报了另一个bug,但是这个bug也不好解决,试了很多方法也没解决,当然最终还是可以解决的,具体解决方式参考第三点。

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.
	 [[{{node dense_1/BiasAdd/ReadVariableOp}}]]

  后来经过N遍测试后找到了以下两种解决方式,仅供参考:

方法一:在调用前加载model和graph,但是这样会导致程序每次调用都需要重新加载model,然后运行速度就会很慢,不过这种修改方式是最简单的。

    graph = tf.get_default_graph()
    model = models.load_model('./static/my_model2.h5')
    with graph.as_default():
        result = model.predict(tokens_pad)

方法二:在创建model后,先使用一遍 model.predict(),参数的大小和真实大小一致,这个是真正解决之道,同时不影响使用速率。

# 使用前:
model = models.load_model('./static/my_model2.h5')
# a 矩阵大小和 tokens_pad 一致
a = np.ones((1, 220))
model.predict(a)

# 使用时:
global model
result = model.predict(tokens_pad)

  但是在使用后又遇到了 The Session graph is empty…… 的错误即第二点,不过估摸着这个是个例,应该是程序问题。

2. RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

  在相关代码使用前添加graph即可。

    graph = tf.get_default_graph()
    with graph.as_default():
        # 相关代码
        # 本次测试中是需要把调用包含model.predict()方法的方法的代码放到这里

3. tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_1/bias/class tensorflow::Var does not exist.[[{{node dense_1/BiasAdd/ReadVariableOp}}]]

  这个错误呢,也是TensorFlow和Flask结合使用时的常见错误,解决方式如下:

from tensorflow.python.keras.backend import set_session
# 程序开始时声明
sess = tf.Session()
graph = tf.get_default_graph()

# 在model加载前添加set_session
set_session(sess)
model = models.load_model(…………)

# 每次使用有关TensorFlow的请求时
# in each request (i.e. in each thread):
global sess
global graph
with graph.as_default():
    set_session(sess)
    model.predict(...)

参看文章:【1】At Runtime : “Error while reading resource variable softmax/kernel from Container: localhost” #28287
     【2】构建图 Graph
     【3】Tensor Tensor(“dense_2/Softmax:0”, shape=(?, 192), dtype=float32) is not an element of this graph
     【4】flask项目中调用keras神经网络模型的一个坑
     【5】机器学习 keras+flask(或者Django)问题
     【6】Keras + Flask 提供接口服务的坑~~~

  • 27
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值