gpt4 book ai didi

python - 循环运行 label_image.py

转载 作者:太空宇宙 更新时间:2023-11-04 02:39:25 25 4
gpt4 key购买 nike

我的目标是持续对来自视频流的 .jpg 图像进行分类。

为此,我刚刚修改了 label_image.py example .

我正在加载图表并预先打开 session 。然后我只在循环中运行以下代码:

t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)


input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);

results = sess2.run(output_operation.outputs[0],
{input_operation.outputs[0]: t}
)

results = np.squeeze(results)

top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)

它在几分钟内运行良好,但问题是每个周期分类都会逐渐变慢。一分钟内从半秒到几秒。我的内存使用量也在缓慢上升,大约每 3 秒增加 1 MB。

如果我多次对单个图像进行分类,而忽略了“read_tensor_from_image_file”,我就不会遇到此错误。

所以图像加载代码中的某些东西每次都必须占用更多空间,而不是正确清理:

def read_tensor_from_image_file(file_name, input_height=192, input_width=192,
input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels = 3,
name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])

result = sess1.run(normalized)


return result

非常感谢每一个建议,我完全坚持这个。

我在带有 raspbian jessie 的树莓派上使用 python 3.4.2 和 tensorflow 1.1.0。

非常感谢!

最佳答案

每次调用 read_tensor_from_image_file 时,都会在 TensorFlow 图中创建大量新节点。正如您所说,此函数在您的代码中循环调用,因此它会在每次迭代中动态创建许多新的图形节点。这可能是内存使用量增加和缓慢的原因。

更好的方法是创建一次图形,然后在循环中运行图形。例如,您可以按如下方式修改您的 read_tensor_from_image_file:

def read_tensor_from_image_file(input_height=192, input_width=192, input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"

# [NEW] make file_name as a placeholder.
file_name = tf.placeholder("string", name="fname")

file_reader = tf.read_file(file_name, input_name)
...
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])

# [NEW] don't call sess1 when building graph.
# result = sess1.run(normalized)
# return result
return normalized

在您的服务器中,您只调用一次 read_tensor_from_image_file,并将其保存为 read_tensor_from_image_file_op =read_tensor_from_image_file(...) 某处。

在你的循环中,你可以简单地调用:

t = sess2.run(read_tensor_from_image_file_op, feed_dict={"fname:0": file_name})

input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
results = sess2.run(output_operation.outputs[0],
{input_operation.outputs[0]: t}
)
results = np.squeeze(results)

top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)

希望对您有所帮助。

关于python - 循环运行 label_image.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977868/

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