【问题标题】:Keras with tensorflow error when prediction预测时出现张量流错误的 Keras
【发布时间】:2019-04-28 15:01:02
【问题描述】:

我正在使用 sjango,我正在尝试使用带有 tensorflow 后端的 Keras 来预测图像,但我遇到了这个错误:

line 3669, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_6/Softmax:0", shape=(?, 50), dtype=float32) is not an element of this graph.
[28/Apr/2019 16:54:53] "POST /facture/upload/ HTTP/1.1" 500 133945

这是我的代码:

#Loading the model 
pwd = os.path.dirname(__file__)
with open(pwd+'/ModelML/model_architecture19.json', 'r') as f:
    model = model_from_json(f.read())
# Load weights into the new model
model.load_weights(pwd+'/ModelML/model_weights19.h5')



roi = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
            ret2, roi = cv2.threshold(roi, 127, 255, cv2.THRESH_BINARY_INV)    
            roi = cv2.resize(roi, (IMG_SIZE, IMG_SIZE)) # Resize the image
            roi = roi.reshape(1,IMG_SIZE, IMG_SIZE,1)
            #normalize image
            roi = roi /255 


            graph = tf.get_default_graph()
            with graph.as_default():
                pred =model.predict(roi)

错误来自最后一行“模型预测”

提前感谢您的帮助

【问题讨论】:

  • 我建议检查权重是否与架构相符。
  • 是的,是一样的

标签: django python-3.x tensorflow machine-learning keras


【解决方案1】:

graph = tf.get_default_graph() 是在函数还是方法中?

把它移到model.load_weights(pwd+'/ModelML/model_weights19.h5') 之后怎么样?

这些行

pwd = os.path.dirname(__file__)
with open(pwd+'/ModelML/model_architecture19.json', 'r') as f:
    model = model_from_json(f.read())
# Load weights into the new model
model.load_weights(pwd+'/ModelML/model_weights19.h5')
graph = tf.get_default_graph()

应该在任何类或函数之外。

【讨论】:

  • 谢谢我有同样的错误:raise ValueError("Tensor %s is not an element of this graph." % obj) ValueError: Tensor Tensor("dense_4/Softmax:0", shape=( ?, 50), dtype=float32) 不是该图的元素。 [28/Apr/2019 21:43:42]“POST /facture/upload/HTTP/1.1”500 132809
  • graph = tf.get_default_graph() 功能失效
  • 在错误为 ValueError: Tensor Tensor("dense_6/Softmax:0".. 现在我有 ValueError: Tensor Tensor("dense_4/Softmax:0", shape=(? , 50)
  • 已将 densr_4 更改为 dense_6
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 2017-11-22
  • 2016-10-31
  • 2017-04-28
相关资源
最近更新 更多