我正在使用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)
错误来自“模型预测”的最后一行
预先感谢您的帮助
答案 0 :(得分:0)
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()
应该在任何类或函数之外。