我正在使用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)错误来自最后一行"model predict“
提前感谢您的帮助
发布于 2019-04-29 00:54:36
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()应该在任何类或函数之外。
https://stackoverflow.com/questions/55891530
复制相似问题