[英]How to run Keras.model() for prediction inside a tensorflow session?
I am currently having an issue, while executing my model predict of keras inside a tensorflow session. 我目前在执行我的模型预测张量流会话中的角膜时遇到问题。
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
## want to know how to add model.predict() inside this condition
predictions = model.predict(#my_model)
#predictions output is same not appending
or any alternative method will be helpful. 或任何其他方法都会有帮助。
Any help would be appreciated. 任何帮助,将不胜感激。
from keras import backend as K
with tf.Graph().as_default():
with tf.Session() as sess:
K.set_session(sess)
model = load_model(model_path)
preds = model.predict(in_data)
from keras.models import load_model
with tf.Session(graph=K.get_session().graph) as session:
session.run(tf.global_variables_initializer())
model = load_model('model.h5')
predictions = model.predict(input)
Above code works for me. 上面的代码对我有用。 I am using keras mobilenet inside tensorflow.
我在张量流中使用keras mobilenet。
Have to declare placeholder first..then load the model 必须先声明占位符..然后加载模型
input_img = tf.placeholder(tf.float32,
(1,12,8,3), name = 'image')
CnnClassifier=tf.keras.models.load_model('model.h5',custom_objects
=None,compile = True)
output = CnnClassifier(input_img)
with tf.Session() as sess:
sess.run(tf.global_variables_intializer())
output_val = sess.run(output,
{input_img:np.expend_dims(img,0)})
If im not mistaken you could replace 如果我没记错的话,您可以更换
with tf.Session() as sess:
simply by 简单地通过
sess = K.get_session()
(K is keras.backend imported) (K是keras.backend导入的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.