gpt4 book ai didi

python - 如何使用 Tensorflow Slim 库执行迭代推理

转载 作者:行者123 更新时间:2023-12-01 02:51:17 25 4
gpt4 key购买 nike

有很多示例展示了如何使用 tf.contrib.slim 库对从网络下载的单个图像进行分类。事实上tensorflow github 提供了这个。然而,我正在努力理解循环执行此操作的最佳方法。任何使用 Tensorflow 进行分类的应用程序都必须对多于一批的图像进行分类。推理过程包括构建图表并从检查点加载权重。当迭代运行时,一遍又一遍地重复这些步骤似乎很浪费。事实上,当我尝试这个基本方法时,我可以看到分配给 python 的内存在每次迭代中都在持续增长。有人可以帮助建议如何修改基本示例以实现重复/迭代推理吗?这是我当前有效的方法,但显然浪费了内存资源(此代码使内存有限的机器崩溃,新图像定期转储到全局框架中): def 分类():

def classification():
global frame
global count

slim = tf.contrib.slim
image_size = inception_v4.inception_v4.default_image_size
names = imagenet.create_readable_names_for_imagenet_labels()
checkpoints_dir = '../../checkpoints'

# Don't classify the first few frames
while count < 5:
pass

while True:
start = count
with tf.Graph().as_default():
image = tf.convert_to_tensor(frame,dtype=tf.float32)
processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
# processed_images will be a 1x299x299x3 tensor of float32

# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
logits, _ = inception_v4.inception_v4(processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)

init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
slim.get_model_variables('InceptionV4'))

with tf.Session() as sess:
init_fn(sess)
np_image, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]

for i in range(5):
index = sorted_inds[i]
print('Probability %0.2f%% => [%s]' % (probabilities[index] * 100, names[index]))

end = count
print "Classification latency = %d frames" % (end-start)

最佳答案

我让它工作起来,仍然会欣赏其他人的一些智慧。我的解决方案是使用占位符作为输入来构建图表。然后可以将视频帧输入到使用 feed_dict 的 session 运行方法中。这允许我将 while 循环放在对 session 运行的调用周围。使用这种方法的延迟是我分享的原始方法的1/10,并且内存指纹稳定。这是我用于对网络摄像头的视频帧进行分类的完整代码。请注意,它有一个问题。我没有干净地退出线程的机制。 Ctrl+C 不会终止脚本。另请注意,要运行此程序,您需要克隆 github tensorflow 模型存储库,并在 ../../checkpoints 下载并解压预训练权重。

import cv2
import os
import time
import numpy as np
from threading import Thread
import tensorflow as tf
from datasets import imagenet
from nets import inception_v4
from preprocessing import inception_preprocessing
######################################################

# Global Variables Shared by threads
frame = None
count = 0

######################################################
def capture():
######################################################
global frame
global count

video_capture = cv2.VideoCapture(0)

while True:
# Capture frame-by-frame
ret, frame_bgr = video_capture.read()

# Display the resulting frame
cv2.imshow('Video', frame_bgr)

# Convert to RGB format (Inception expects RGB not BGR color channels)
frame = cv2.cvtColor(frame_bgr,cv2.COLOR_BGR2RGB)

# Increment frame counter (Used only to calculate latency)
count += 1

# Kill loop when user hits q
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
######################################################

######################################################
def classification():
######################################################
global frame
global count

slim = tf.contrib.slim
image_size = inception_v4.inception_v4.default_image_size
names = imagenet.create_readable_names_for_imagenet_labels()
checkpoints_dir = '../../checkpoints'

# Don't classify the None Object
time.sleep(5)

with tf.Graph().as_default():
image = tf.placeholder(tf.uint8,[480,640,3])
processed_image = inception_preprocessing.preprocess_image(image,
image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
# processed_images will be a 1x299x299x3 tensor of float32

# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
logits, _ = inception_v4.inception_v4(processed_images, num_classes=1001, is_training=False)
probs = tf.nn.softmax(logits)

init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
slim.get_model_variables('InceptionV4'))

with tf.Session() as sess:
init_fn(sess)

while True:
start = count
probabilities = sess.run(probs,feed_dict={image: frame})
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]

for i in range(5):
index = sorted_inds[i]
print('Probability %0.2f%% => [%s]' % (probabilities[index] * 100, names[index]))

end = count
print "Classification latency = %d frames" % (end-start)

# How to end this thread cleanly?
######################################################

# Start the threads
capture_thread = Thread(target=capture)
classify_thread = Thread(target=classification)
capture_thread.start()
classify_thread.start()

关于python - 如何使用 Tensorflow Slim 库执行迭代推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com