gpt4 book ai didi

python - 随着时间的推移,TensorFlow 中的图像转换速度变慢

转载 作者:太空狗 更新时间:2023-10-30 02:16:03 25 4
gpt4 key购买 nike

我有这种方法可以拍摄图像并将其转换为张量。我在一个循环中调用它,转换的执行时间开始很短并不断增长。

def read_tensor_from_image_file(file_name, input_height=299, input_width=299, input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)

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])
sess = tf.Session()
result = sess.run(normalized)

return result

我该如何优化它?

最佳答案

问题几乎可以肯定是由于在对 read_tensor_from_image_file() 函数的多次调用中使用了相同的默认 tf.Graph。解决此问题的最简单方法是在函数体周围添加一个 with tf.Graph().as_default(): block ,如下所示:

def read_tensor_from_image_file(file_name, input_height=299, input_width=299, input_mean=0, input_std=255):
with tf.Graph().as_default():
input_name = "file_reader"
output_name = "normalized"
file_reader = tf.read_file(file_name, input_name)
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])
sess = tf.Session()
result = sess.run(normalized)
return result

通过此更改,每次调用该函数都会创建一个新图,而不是向默认图添加节点(默认图会随着时间增长,内存泄漏,并且每次使用它时需要更长时间才能启动)。

一个更高效的版本将使用 tf.placeholder() 作为文件名,构建单个图,并将 for 循环移动到 TensorFlow session 内部。像下面这样的东西会起作用:

def read_tensors_from_image_files(file_names, input_height=299, input_width=299, input_mean=0, input_std=255):
with tf.Graph().as_default():
input_name = "file_reader"
output_name = "normalized"
file_name_placeholder = tf.placeholder(tf.string, shape=[])
file_reader = tf.read_file(file_name_placeholder, input_name)
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])

with tf.Session() as sess:
for file_name in file_names:
yield sess.run(normalized, {file_name_placeholder: file_name})

关于python - 随着时间的推移,TensorFlow 中的图像转换速度变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398784/

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