1.django多次加载keras_model导致错误
将加载过程放到{path_to}\__init__.py(自己创建的专门处理keras相关事务的包)中,在web启动时加载model
print('>>>loading model ...')
with open('model.json', 'r')as f:
json_string = json.load(f)
model = model_from_json(json_string)
model.load_weights('model.h5')
print('>>>load done')
model.summary()
2.多次执行predict报错,Tensor Tensor(“activation_5/Softmax:0”, shape=(?, 4), dtype=float32) is not an element of this graph
由于django的线程机制,导致多次predict时tf错误,解决graph = tf.get_default_graph()
参考:https://github.com/keras-team/keras/issues/2397
参考:https://qiita.com/itisyuu/items/7c9d7ff43b3936704918
#加载model之后
graph = tf.get_default_graph()
def category_class(x):
global model
global graph
with graph.as_default():
prediction = np.argmax(
model.predict(x)
)
return prediction
3.print()时报错,OSError: raw write() returned invalid length 134 (should have been between 0 and 67)
这个错误不时出现,出现的原因可能和win10系统有关
import win_unicode_console
#for raw write()
win_unicode_console.enable()
参考:https://www.cnblogs.com/yanjj/p/8275995.html