我正在使用Google的inceptionv-3模型和TensorFlow进行性别识别,使用转移学习(本教程的here's the link)在我的数据集上有135个女性脸部图像和335个男性脸部图像。在训练结束后,它显示出95%的准确率,我在几张图像上测试它的工作正常,但是分类结果需要大约3秒钟。
我正在尝试实时(实时视频)
下面是我修改它的代码,我首先加载模型需要一秒钟,然后我在无限循环中进行,所以我只是传递图像并得到预测结果。
import os, sys
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import time
# Unpersists graph from file
with tf.gfile.FastGFile("retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
# change this as you see fit
image_path = sys.argv[1]
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("retrained_labels.txt")]
while True:
start_time = time.time()
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
print("--- %s seconds ---" % (time.time() - start_time))
# Sort to show labels of first prediction in order of confidence
"""top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]"""
这是输出:
--- 2.21238899231 seconds ---
--- 2.1374540329 seconds ---
--- 2.08863019943 seconds ---
--- 2.08074688911 seconds ---
--- 2.07966399193 seconds ---
有什么方法可以减少时间,我很抱歉代码格式化。 我不确定我是否提出了正确的问题,我对ML很新。
感谢您的帮助!
sys config:AMD-A4,2.5GHz,8GB RAM
答案 0 :(得分:1)
首先,您不应该在循环中从头开始重新创建会话。创建一个会话对象,并在循环期间一遍又一遍地调用sess.run(...)。此外,你应该调用" get_tensor_by_name"只有一次(在循环之外)。除此之外,没有任何代码可以做到。你可以把执行放在gpu上以便加快速度。