克服Graphdef在Tensorflow中不能大于2GB

发布于 2021-01-29 18:38:03

我正在使用tensorflow的imageNet训练模型来提取最后一个合并层的特征作为新图像数据集的表示向量。

该模型在新图像上的预测如下:

python classify_image.py --image_file new_image.jpeg

我编辑了main函数,以便可以提取图像文件夹并一次返回所有图像的预测,并将特征向量写入csv文件中。这是我的方法:

def main(_):
  maybe_download_and_extract()
  #image = (FLAGS.image_file if FLAGS.image_file else
  #         os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
  #edit to take a directory of image files instead of a one file
  if FLAGS.data_folder:
    images_folder=FLAGS.data_folder
    list_of_images = os.listdir(images_folder)
  else: 
    raise ValueError("Please specify image folder")

  with open("feature_data.csv", "wb") as f:
    feature_writer = csv.writer(f, delimiter='|')

    for image in list_of_images:
      print(image) 
      current_features = run_inference_on_image(images_folder+"/"+image)
      feature_writer.writerow([image]+current_features)

对于大约21张图像,它工作正常,但是由于以下错误而崩溃:

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1912, in as_graph_def
    raise ValueError("GraphDef cannot be larger than 2GB.")
ValueError: GraphDef cannot be larger than 2GB.

我认为通过调用该方法run_inference_on_image(images_folder+"/"+image),以前的图像数据将被覆盖,仅考虑新的图像数据,似乎并非如此。如何解决这个问题?

关注者
0
被浏览
45
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这里的问题是,每次的调用都会run_inference_on_image() 节点 添加
    到同一图中,最终会超过最大大小。至少有两种方法可以解决此问题:

    1. 一种 简单但缓慢的 方法是,对以下每个调用使用不同的默认图run_inference_on_image()

      for image in list_of_images:
      

      # …
      with tf.Graph().as_default():
      current_features = run_inference_on_image(images_folder+”/”+image)
      # …

    2. 更多参与,但更有效 的方法是修改run_inference_on_image()对多个图像运行。重新定位for循环以围绕sess.run()调用,您将不再需要在每次调用时重建整个模型,这将使​​处理每个图像的速度大大提高。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看