如何为保存的模型运行预测(使用图像作为输入)? [英] How to run prediction (using image as input) for a saved model?

查看:30
本文介绍了如何为保存的模型运行预测(使用图像作为输入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我对 Tensorflow 很陌生.我的具体问题是我应该在 sess.run(fetches, feed_dict) 函数中放入哪些特定参数.例如,如何找出参数的值?

I am very new to Tensorflow. My specific question is what particular arguments should I put inside sess.run(fetches, feed_dict) function. For instance, how could find out what the values of the arguments?

步骤:

这是我看了其他帖子后对步骤的理解.

Here is my understanding of the steps after looking at other posts.

  1. 保存 tranied tensorflow 模型,它应该由 4 个文件组成,以下是我的输出:

  • 检查站
  • Inception_resnet_v2.ckpt.data-00000-of-00001
  • Inception_resnet_v2.ckpt.index
  • Inception_resnet_v2.ckpt.meta
    1. 将输入图像调整为神经网络所需的任何格式.

    1. Resize the input image to whatever format required by the neural network.

    开始 tensorflow 会话.

    Start tensorflow session.

    检索图形和相关参数、张量...

    Retrive the Graph and associated parameters, tensors...

    预测输入图像.

    代码:

    训练代码:

    https://github.com/taki0112/SENet-Tensorflow/blob/master/SE_Inception_resnet_v2.py

    [已解决] 测试代码:

    import tensorflow as tf
    import numpy as np
    import cv2
    
    labels = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]
    
    # Load graph and parameters, etc.
    sess=tf.Session()
    saver = tf.train.import_meta_graph('./model/Inception_resnet_v2.ckpt.meta')
    saver.restore(sess, tf.train.latest_checkpoint("./model/"))
    graph = tf.get_default_graph()
    
    # Get tensor names
    x = graph.get_tensor_by_name("Placeholder:0")
    training_flag = graph.get_tensor_by_name("Placeholder_2:0")
    op_to_restore = graph.get_tensor_by_name("final_fully_connected/dense/BiasAdd:0")
    
    # Preprocess imgae imput
    src = cv2.imread("./input/car3.jpg")
    dst = cv2.resize(src, (32, 32), interpolation=cv2.INTER_CUBIC)
    b,g,r = cv2.split(dst)
    b = (b - np.mean(b)) / np.std(b) * .1
    g = (g - np.mean(g)) / np.std(g) * .1
    r = (r - np.mean(r)) / np.std(r) * .1
    src = cv2.merge((b,g,r))
    
    picture = dst.reshape(1, 32, 32, 3)
    feed_dict ={x: picture, training_flag:False}
    
    result_index = sess.run(op_to_restore,feed_dict)
    print(result_index)
    print (labels[np.argmax(result_index)])
    

    推荐答案

    参数实际上取决于您在做什么,但主要是第一个参数是权重和占位符.每当您使用 Tensorflow 时,您都会定义一个图表,其中包含示例(训练数据)和一些超参数,例如学习率、全局步长等.使用占位符提供所有训练数据和超参数是标准做法.当您使用占位符构建网络并保存时,会保存网络,但不会保存占位符的值.

    the arguments actually depend on what you're doing, but mostly the first argument is the weights and placeholders. Whenever you are working with Tensorflow, you define a graph which is fed examples(training data) and some hyperparameters like learning rate, global step etc. It’s a standard practice to feed all the training data and hyperparameters using placeholders. when you build a network using placeholders and save it the network is saved, however, values of the placeholders are not saved.

    让我们看一个玩具示例:

    Let's see a toy example:

    import tensorflow as tf
    
    #Prepare to feed input, i.e. feed_dict and placeholders
    w1 = tf.placeholder("float", name="w1")
    w2 = tf.placeholder("float", name="w2")
    b1= tf.Variable(2.0,name="bias")
    feed_dict ={w1:4,w2:8}
    
    #Define a test operation that we will restore
    w3 = tf.add(w1,w2)
    w4 = tf.multiply(w3,b1,name="op_to_restore")
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    
    #Create a saver object which will save all the variables
    saver = tf.train.Saver()
    
    #Run the operation by feeding input
    print sess.run(w4,feed_dict)
    #Prints 24 which is sum of (w1+w2)*b1 
    
    #Now, save the graph
    saver.save(sess, 'my_test_model',global_step=1000)
    

    现在,当我们想要恢复它时,我们不仅要恢复图和权重,还要准备一个新的 feed_dict,它将新的训练数据提供给网络.我们可以通过 graph.get_tensor_by_name() 方法获取对这些保存的操作和占位符变量的引用.所以如果你想用更多的新数据训练同一个模型,那么你就必须利用这些权重,但是如果你只想从你训练的模型中获得预测,你可以利用 op_to_restorefeed_dict 作为新数据.像这样,如果你按照上面的例子:

    Now, when we want to restore it, we not only have to restore the graph and weights, but also prepare a new feed_dict that will feed the new training data to the network. We can get reference to these saved operations and placeholder variables via graph.get_tensor_by_name() method. So if you want to train the same model with further new data, then you would have to utilize those weigtages, if however you just want to get the prediction from the model you trained, you could utilize the op_to_restore and the feed_dict as new data. Something like this, if you follow the above example:

    import tensorflow as tf
    
    sess=tf.Session()    
    #First let's load meta graph and restore weights
    saver = tf.train.import_meta_graph('my_test_model-1000.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))
    
    
    # Now, let's access and create placeholders variables and
    # create feed-dict to feed new data
    
    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name("w1:0")
    w2 = graph.get_tensor_by_name("w2:0")
    feed_dict ={w1:13.0,w2:17.0}
    
    #Now, access the op that you want to run. 
    op_to_restore = graph.get_tensor_by_name("op_to_restore:0")
    
    print sess.run(op_to_restore,feed_dict)
    #This will print 60 which is calculated 
    #using new values of w1 and w2 and saved value of b1. 
    

    所以,这就是它的工作方式,在您的情况下,由于您正在尝试加载 Inception 模型,如果您能告诉我们,您的 op_to_restore 应该取决于您尝试恢复的内容你想要做什么,那么只有提出建议才是可能的.然而,在另一个参数 feed_dict 中,它只是图像像素的 numpy 数组,您正在尝试分类/预测或您正在做的任何事情.

    So, this is how it works, in your case, since you're trying to load the Inception model, your op_to_restore should depend on what you're trying to restore if you could tell us what you're trying to do, then only it's possible to suggest something. However in the other parameter feed_dict , it's just the numpy array of image pixel, of you, you're trying to classify/predict or whatever you're doing.

    我从以下文章中获取了代码.这也会对你有所帮助.http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

    I took the code from the following article. This will help you as well. http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

    更新:对于您的特定情况,您可能想尝试以下代码来预测新图像中的类别.

    Update: For your particular case, you may like to try the following code to predict the classes in the new images.

    import tensorflow as tf
    slim = tf.contrib.slim
    from inception_resnet_v2 import *
    
    #Well, since you're using resnet_v2, this may be equivalent to you. 
    checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'
    sample_images = ['dog.jpg', 'panda.jpg']
    #Load the model
    sess = tf.Session()
    arg_scope = inception_resnet_v2_arg_scope()
    with slim.arg_scope(arg_scope):
      logits, end_points = inception_resnet_v2(input_tensor, is_training=False)
    
    #With this, you could consider the op_variable with the following
    predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})
    
    #Here im is the normalized numpy array of the image pixels.
    

    此外,以下资源可以为您提供更多帮助:在 Tensorflow 中使用预训练的 inception_resnet_v2

    Furthermore, the following resources may help you even more: Using pre-trained inception_resnet_v2 with Tensorflow

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

    这篇关于如何为保存的模型运行预测(使用图像作为输入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆