运行TensorFlow Inception v3时内存是否未释放?

时间:2016-06-20 11:35:36

标签: python c++ tensorflow

我正尝试使用Tensorflow Inception v3对一组照片进行分类(< 10000)。

我正在Macbook Air上使用TensorFlow Docker installation CPU Binary image plus source code和4gb DDR3。

我编写了一个python脚本来生成一个子进程来对每个图像进行分类:

"""
classifier_spawner.py

Runs custom_classify_image.py in subprocess
"""
if __name__ == "__main__":
  import sqlite3
  import subprocess as sub
  from ast import literal_eval

  conn = sqlite3.connect("database_name.db")
  c = conn.cursor()
  c.execute("SELECT * FROM images")
  images = c.fetchall()
  for image in images:
      p = sub.Popen(["python", "custom_classify_image.py", "--image_file=image_dir/" + str(image[0]) + ".jpg"], stdout=sub.PIPE, stderr=sub.PIPE)
      output, errors = p.communicate()
      categories = literal_eval(output)
      for cat in categories:
          c.execute("""INSERT OR IGNORE INTO classification (image_id, class_id, probability) VALUES(?,?,?)""", (str(image[0]), str(cat[0]), float(cat[1])))
          conn.commit()
  print("Classification complete, exiting.")
  conn.close()

我修改了classify_image.py as provided with TensorFlow中的run_inference_on_image(image)来打印前5个分类的元组的python列表,如图所示。

"""My custom run_inference_on_image(image) """

def run_inference_on_image(image):
  """Runs inference on an image.

  Args:
    image: Image file name.

  Returns:
Nothing
  """
  if not tf.gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = tf.gfile.FastGFile(image, 'rb').read()

  # Creates graph from saved GraphDef.
  """Creates a graph from saved GraphDef file and returns a saver."""
  # Creates graph from saved graph_def.pb.
  with tf.gfile.FastGFile(os.path.join(
      FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')
      with tf.Session() as sess:
        # Some useful tensors:
        # 'softmax:0': A tensor containing the normalized prediction across
        #   1000 labels.
        # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
        #   float description of the image.
        # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
        #   encoding of the image.
        # Runs the softmax tensor by feeding the image_data as input to the graph.
        softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
        predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})
        predictions = np.squeeze(predictions)

        # Creates node ID --> English string lookup.
        node_lookup = NodeLookup()

        top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
        top_k_tup_list = []
        for node_id in top_k:
          human_string = node_lookup.id_to_string(node_id)
          score = predictions[node_id]
          top_k_tup_list.append((human_string, score))
        sess.close()
        print(top_k_tup_list)

我的问题是,当我运行classifier_spawner.py时,在子进程完成后,它所使用的内存不会被释放,在~10分类之后我的磁盘已满(在写入~15gb到磁盘之后)。然后我必须删除我的虚拟机。

我不明白为什么会发生这种情况,在(1)TensorFlow会话结束和(2)进程退出后,是否应该释放子进程使用的虚拟内存?

提前致谢,如果您需要任何澄清,请告诉我。

0 个答案:

没有答案