出现问题:ValueError: Tensor Tensor(“...”, shape=(?, 6), dtype=float32) is not an element of this graph. 即报错tensor的元素不是此图的
问题背景:启动web项目[django/flask]时,使用检测模型执行检测任务时
出现原因:web项目启动时开启一个线程,此时预编译引入tensorflow生成一张graph图;当
以request请求执行检测函数时,开启另一个线程,检测任务在第二个线程调用graph时无法使用第一个线程的graph,因此报错。
解决方法:将python文件上方的tensorflow引入改为函数内引入,即tensorflow会在接受request请求后才会启动生成graph和检测任务在同一线程。
#detect.py
def detect():
try:
from keras.models impoer load_model
load_model('xxxx.h5')
except Exception as e:
print(e)
个人理解,有不对欢迎指正