用Keras模型进行预测,在多线程的情况下,特别是对于采用GPU时,会出现诸如类似以下的错误。
ValueError: Tensor Tensor("Sigmoid_2:0", shape=(?, 17), dtype=float32) is not an element of this graph.
解决方法如下:
self.model.load_weights(weight_file_path)
self.model._make_predict_function()
self.graph = tf.get_default_graph()
在使用预测时 :
with self.graph.as_default():
predicted_class = np.argmax(self.model.predict(np.array([feature]))[0])