首页 文章

Tensorflow slim inception resnet v2推断

提问于
浏览
0

https://github.com/tensorflow/models/blob/master/research/slim/nets/inception_resnet_v2.py

你如何正确推断这个模型?我有兴趣进行此设置,以便用户可以对他在命令行中逐个输入的单个图像进行推断 . 为了使其快速,必须加载模型ONCE并且输入图像必须是热插拔的,因为用户将它们输入命令行 .

如果对此模型的评估代码使用类似的结构,则可以执行非热插拔推理:https://github.com/tensorflow/models/blob/master/research/slim/eval_image_classifier.py

您可以稍微修改上述文件以打印您的logits并进行推断 . 但是,此解决方案每次都会重建图表,这非常慢 .

我尝试构建图形并将feed_dict传入fifo_queue_Dequeue:0 tensor,表示批处理输入 . 但是,会话将挂起并且永远不会计算 . 我相信这是因为图表被“冻结” - 张量不能接受新的输入 . 但现在我对如何获得我想要的行为感到难过 .

1 回答

  • 1

    推理步骤如下:

    创建Inception-resnet-v2图

    import sys
    # import from tensorflow models
    sys.path.append('/home/vijay/workspace/learning/tensorflow/')
    
    #Load the definitions of Inception-Resnet-v2 architecture
    import tensorflow.contrib.slim as slim
    from models.research.slim.nets.inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope
    
    
    #The pretrained model accepts size of 299x299 images
    HEIGHT = 299
    WIDTH = 299
    CHANNELS = 3
    
    # Create Graph
    
    graph = tf.Graph()
    with graph.as_default():
    
       # Create a placeholder to pass the input image
       img_tensor = tf.placeholder(tf.float32, shape=(None, HEIGHT, WIDTH, CHANNELS))
    
       # Scale the image inputs to {+1, -1} from 0 to 255
       img_scaled = tf.scalar_mul((1.0/255), img_tensor)
       img_scaled = tf.subtract(img_scaled, 0.5)
       img_scaled = tf.multiply(img_scaled, 2.0)
    
       # load Graph definitions
       with slim.arg_scope(inception_resnet_v2_arg_scope()):
          logits, end_points = inception_resnet_v2(img_scaled, is_training=False)
    
       # predict the class
       predictions = end_points['Predictions']
    

    加载测试图像(示例来自此处):

    #Loading a test image 
    img = cv2.imread('/home/vijay/datasets/image/misc/Bernese-Mountain- Dog.jpg')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (WIDTH, HEIGHT))
    
    # make the input size [BATCH, WIDTH, HEIGHT, CHANNELS] for the network
    img = np.expand_dims(img, axis=0)
    

    加载权重并运行图表

    #for labels of imagenet 
    sys.path.append('/home/vijay/workspace/learning/tensorflow/models/research/slim')
    from datasets import imagenet
    
    # Inception resnet v2 model 
    checkpoint_file='/home/vijay/datasets/pre_trained_models/inception_resnet_v2_2016_08_30.ckpt'
    
    with tf.Session(graph=train_graph) as sess:
    
       saver = tf.train.Saver()
       saver.restore(sess, checkpoint_file)
    
       pred_prob= sess.run(predictions, feed_dict={img_tensor:img})
    
       # Getting the top 5 classes of the imagenet database
       probabilities = pred_prob[0, 0:]
       sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
    
       names = imagenet.create_readable_names_for_imagenet_labels()
       for i in range(5):
           index = sorted_inds[i]
           print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))
    

    输出

    Probability 0.84% => [Bernese mountain dog]
    Probability 0.04% => [Appenzeller]
    Probability 0.03% => [EntleBucher]
    Probability 0.01% => [Greater Swiss Mountain dog]
    Probability 0.00% => [Border collie]
    

相关问题