Я генерирую файл .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))
Я использую tenorflow-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))
То, что я здесь заметил, неважно, что я назначаю процессору или процессору, время, затрачиваемое всегда, одно и то же. Модель представляет собой модель VGG16 с трансферным обучением. Я делаю что-то не так в коде? Мой графический процессор Quadro 8GB