Django调用keras的模型出现问题的解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JohinieLi/article/details/82083362

笔者小白在用Django写一个表格单据图片的识别应用的时候,遇到了调用基于Tensorflow的keras模型出错的问题。
出现的错误信息类似于以下:

ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32)

通过查询相关的资料,对解决的方式做一个记录。

方法1、通过导入 import Keras
然后在构建模型前面加一句 keras.backend.clear_session()

方法2、通过提前predict一个全0的数据。

from keras.models import load_model
import numpy as np
print('load model...')
model = load_model('static\\CnnBankUp.h5', compile=False)
print('load done.')

#一定要添加这段代码,先测试一下,可以避免ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) 
#is not an element of this graph.的错误
print('test model...')
#根据自己传入图片格式定义np.zeros()
print(model.predict(np.zeros((2, 200,200,1))))
print('test done.')

参考文献:
1、https://blog.csdn.net/lhs960124/article/details/79028691 2018.8.26
2、https://www.cnblogs.com/yanjj/p/8242595.html 2018.8.26
3、https://blog.csdn.net/qq_37879432/article/details/79032664 2018.8.26

猜你喜欢

转载自blog.csdn.net/JohinieLi/article/details/82083362
今日推荐