tensorflow pb文件推理对于图像需要超过3秒

问题描述 投票:0回答:1

我正在从下面给出的代码生成一个.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))

我使用anaconda python 3.5使用tensorflow-gpu 1.12和Windows 7我也尝试将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模型。我在代码中做错了吗?我的GPU是Quadro 8GB

python tensorflow computer-vision conv-neural-network
1个回答
0
投票

经过大量的反复试验后,我注意到如果我从推理中删除了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]} )

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

© www.soinside.com 2019 - 2024. All rights reserved.