东方耀AI技术分享

标题: FailedPreconditionError: Error while reading resource variable was uninitialized [打印本页]

作者: 东方耀    时间: 2020-3-13 13:32
标题: FailedPreconditionError: Error while reading resource variable was uninitialized
问题变了:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable batch_normalization_2/moving_mean from Container: localhost.
This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/batch_normalization_2/moving_mean)
         [[{{node batch_normalization_2/cond/FusedBatchNormV3/ReadVariableOp}}]]




以上报错,是在keras模型部署到python的flask服务器 遇到  估计django也会出现


从提示看:读资源变量出错  变量没有初始化等   这里意思是session已经变了


为了保持只使用一个session:


# 定义一个全局的sess变量
global sess
sess = tf.Session(graph=graph)  这个不行了
用这个 即可:sess = keras.backend.get_session()



结合前面的报错:
http://www.ai111.vip/thread-993-1-1.html


总体解决方案:
import tensorflow as tf
import keras


global graph, sess


graph = tf.get_default_graph()
sess = keras.backend.get_session()


预测处:
    with sess.as_default():
        with graph.as_default():

              y_pred = 你的model.predict(X)




注意:每个py文件中 只要出现了模型预测的地方 都需要加上面的代码

到此为止,问题全部解决,如果你还遇到问题,请联系我的微信:dfy_88888







作者: 东方耀    时间: 2020-3-13 14:03
tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.




欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4