简体   繁体   English

如何在张量流会话中运行Keras.model()进行预测?

[英]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.

相关问题 在 keras.Model 中降低维度 - Reduce dimensionality in keras.Model 如何在 Keras.Model 中获得 l2 正则化损失值? - How to get l2 regularize loss value in Keras.Model? TensorFlow2 / Keras:当子类化 keras.Model 时 input_shape 似乎没有效果 - TensorFlow2 / Keras: input_shape seems to not have an effect when subclassing keras.Model 如何使用 keras 和 tensorflow 改进预测 - How to improve prediction with keras and tensorflow 使用model.fit时如何将'training'参数传递给tf,keras.Model - how to pass in 'training' argument to tf,keras.Model when using model.fit TensorFlow 联邦学习后的 Keras 模型预测 - Keras model prediction after tensorflow federated learning keras 和 tensorflow lite model 之间的预测差异 - Prediction differences between keras and tensorflow lite model 使用自定义训练的 Keras model 和 Sagemaker 端点导致“会话未在运行()之前使用图形创建”错误,而预测 - Using custom trained Keras model with Sagemaker endpoint results in "Session was not created with a graph before Run()" error while prediction 使用来自 keras 模型的预测作为另一个 keras 模型内的层 - Using prediction from keras model as a layer inside another keras model 修复 Keras 类中的 TypeError - 如何将 Keras.Sequential 转换为 Layer 类,使其成为 Keras.Model 实例的一部分 - Fix TypeError in Keras class - how to cast Keras.Sequential to Layer class so it can be part of a Keras.Model instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM