0%

keras+python web 遇到的问题

1. ensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.

​ 最近在使用flask部署keras服务的时候。遇到改错误,看错误,感觉是变量没有初始化,百度各种资料,成功找到解决办法。

​  creating a reference to the session that is used for loading the models and then to set it to be used by keras in each request

​ 在加载模型之前,创建session。并在模型调用predict的时候,设置同样的session。保证整个项目使用模型使用同一个session。成功解决问题。

1
2
3
4
5
6
7
8
9
10
11
from tensorflow.python.keras.backend import set_session
from tensorflow.python.keras.models import load_model

tf_config = some_custom_config
sess = tf.Session(config=tf_config)
graph = tf.get_default_graph()

# IMPORTANT: models have to be loaded AFTER SETTING THE SESSION for keras!
# Otherwise, their weights will be unavailable in the threads after the session there has been set
set_session(sess)
model = load_model(...)

在调用的时候,如下设置。

1
2
3
4
5
global sess
global graph
with graph.as_default():
set_session(sess)
model.predict(...)

参考:

https://github.com/tensorflow/tensorflow/issues/28287

https://www.datalearner.com/blog/1051572578207468

2. XLA_GPU

参考:https://stackoverflow.com/questions/53696654/why-keras-does-not-see-my-gpu-while-tensorflow-does?r=SearchResults

解决办法:

sudo chmod -R a+r /usr/local/cuda*