Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在循环中运行label_image.py_Python_Python 3.x_Tensorflow - Fatal编程技术网

Python 在循环中运行label_image.py

Python 在循环中运行label_image.py,python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,我的目标是对来自视频流的.jpg图像进行连续分类 为此,我刚刚修改了 我正在加载图表并提前打开会话。然后我只在循环中运行以下代码: t = read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width,

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

为此,我刚刚修改了

我正在加载图表并提前打开会话。然后我只在循环中运行以下代码:

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

如果我对一个图像进行多次分类,而忽略了“从图像文件中读取张量”,我就不会发现这个错误

因此,图像加载代码中的某些内容每次都必须占用更多空间,不能正确清除:

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
非常感谢你的每一个建议,我完全坚持这一点

我正在使用Python3.4.2和tensorflow 1.1.0,使用raspberry pi和raspbian jessie


非常感谢

每次从\u image\u文件调用read\u tensor\u时,都会在TensorFlow图中创建许多新节点。正如您所说的,这个函数在代码的循环中被调用,因此它将在每次迭代中动态创建许多新的图节点。这可能是内存使用量增加和缓慢的原因

更好的方法是创建一次图,然后在循环中运行该图。例如,您可以修改
从\u image\u文件中读取\u tensor\u,如下所示:

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\u tensor\u from\u image\u file
,并将其另存为
read\u tensor\u from\u image\u file\u op=read\u tensor\u from\u image\u 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)

希望有帮助。

非常感谢,它很有效!你真的救了我一天。如果有人试图复制它,您只需将feed_dict={“fname”:file_name}更改为feed_dict={“fname:0”:file_name}