tensorflow pb文件推断对于一个imag需要3秒钟以上

2024-10-01 05:05:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我使用的是TensorFlowGPU1.12和使用AnacondaPython3.5的Windows7 我也试着分配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


Tags: imageimportinputoutputtimesessiontfas
1条回答
网友
1楼 · 发布于 2024-10-01 05:05:51

经过大量的尝试和错误之后,我注意到如果我在推断时删除了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]} )

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

相关问题 更多 >