【问题标题】:tensorflow pb file inferencing takes more than 3seconds for an imagetensorflow pb 文件推理需要超过 3 秒的图像
【发布时间】:2019-03-07 12:55:59
【问题描述】:

我正在从下面给出的代码生成一个 .pb 文件

import tensorflow as tf
with tf.Session() as sess:
    gom = tf.train.import_meta_graph('C:\\chhaya\\CLITP\\Tvs_graphs\\job.ckpt-20.meta')
    gom.restore(sess,tf.train.latest_checkpoint('C:\\chhaya\\CLITP\\Tvs_graphs'))
    graph = tf.get_default_graph()
    input_graph = graph.as_graph_def()
    output_node_name = "predictions"
    output_graph = tf.graph_util.convert_variables_to_constants(sess,input_graph,output_node_name.split(','))
    res_file = 'C:\\chhaya\\CLITP\\Tvs_graphs\\Savedmodel.pb'
    with tf.gfile.GFile(res_file,'wb') as f:
        f.write(output_graph.SerializeToString())

但在从 pb 文件推断时,第一张图像需要 5 秒,之后的其他图像需要 3 秒。推理代码如下。

import tensorflow as tf
import os
from tensorflow.python.platform import gfile
from PIL import Image
import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt
import cv2
import time
from aug_tool import data_aug
frozen_graph = 'C:\\chhaya\\CLITP\\Tvs_graphs\\Savedmodel1.pb'
with tf.gfile.GFile(frozen_graph,'rb') as f:
    reco = tf.GraphDef()
    reco.ParseFromString(f.read())

with tf.Graph().as_default() as gre:
    tf.import_graph_def(reco,input_map=None,return_elements=None,name='')

    l_input = gre.get_tensor_by_name('input_image:0') # Input Tensor
    l_output = gre.get_tensor_by_name('predictions:0')
    files=os.listdir('C:\\chhaya\CLITP\\Tvs Mysore\\NQCOVERFRONTLR\\')
    imageslst=np.zeros((len(files),224,224,3))
    i=0
    for file in files:
        image = scipy.misc.imread('C:\\chhaya\CLITP\\Tvs Mysore\\NQCOVERFRONTLR\\'+file)
        image = image.astype(np.uint8)
        Input_image_shape=(224,224,3)
        resized_img=cv2.resize(image,(224,224))
        channels = image.shape[2]
        #print(channels)
        if channels == 4:
            resized_img = cv2.cvtColor(resized_img,cv2.COLOR_RGBA2RGB)
 #image = np.expand_dims(resized_img,axis=2)
    #print() 
    image=np.expand_dims(resized_img,axis=0)
    height,width,channels = Input_image_shape
    imageslst[i,:,:,:]=image
    i=i+1

    init = tf.global_variables_initializer()
    with tf.Session(graph=gre) as sess:
        sess.run(init)
        for i  in range(4):
            t1=time.time()
            Session_out = sess.run((tf.nn.sigmoid(l_output)) , feed_dict = {l_input : imageslst[:1]} )
            print(Session_out.shape)
            t2=time.time()
            print('time:'+str(t2-t1))

我正在使用 tensorflow-gpu 1.12 和 windows 7 使用 anaconda python 3.5 我还尝试将 gpu 和 cpu 分配给类似这样的预测

with tf.device('/gpu:0'):
    for i  in range(4):
        t1=time.time()
        Session_out = sess.run((tf.nn.sigmoid(l_output)) , feed_dict = {l_input : imageslst[:1]} )
        print(Session_out.shape)
        t2=time.time()
        print('time:'+str(t2-t1))

我在这里注意的是,无论我分配什么 cpu 或 gpu,所花费的时间总是相同的。该模型是一个迁移学习 vgg16 模型。我在代码中做错了吗?我的显卡是 Quadro 8GB

【问题讨论】:

    标签: python tensorflow computer-vision conv-neural-network


    【解决方案1】:

    经过大量试验和错误后,我注意到如果我在推理时删除 tf.nn.sigmoid,那么所花费的时间不到 10 毫秒,这正是模型应该如何执行的。 在我们使用以下行来获得导致推理时间长的预测之前:-

    Session_out = sess.run((tf.nn.sigmoid(l_output)) , feed_dict = {l_input : imageslst[:1]} )
    

    所以现在我们使用以下代码来代替上面的行:-

    Session_out = sess.run(l_output , feed_dict = {l_input : imageslst[:1]} )
    

    这证明给了我们准确的结果。虽然我很不确定为什么会这样。

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多