克服tensorflow中Graphdef不能大于2GB
overcome Graphdef cannot be larger than 2GB in tensorflow
我正在使用 tensorflow 的 imageNet trained model 提取最后一个池化层的特征作为新图像数据集的表示向量。
模型在新图像上的预测如下:
python classify_image.py --image_file new_image.jpeg
我编辑了 main 函数,这样我就可以获取一个图像文件夹和 return 一次对所有图像进行预测,并将特征向量写入 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)
之前的图像数据会被覆盖,只考虑新的图像数据,似乎不是这样。如何解决这个问题?
这里的问题是每次调用 run_inference_on_image()
都会将 个节点添加到同一个图中,最终超过最大尺寸。至少有两种方法可以解决这个问题:
简单但缓慢 的方法是为每次调用 run_inference_on_image()
:
使用不同的默认图表
for image in list_of_images:
# ...
with tf.Graph().as_default():
current_features = run_inference_on_image(images_folder+"/"+image)
# ...
更复杂但更有效的方法是在多个图像上将run_inference_on_image()
修改为运行。将 for
循环重新定位到 this sess.run()
call 周围,您将不再需要在每次调用时重建整个模型,这将使处理每张图像的速度更快。
您可以将 create_graph()
移动到某个地方 之前 此循环 for image in list_of_images:
(循环遍历文件)。
它所做的是对同一个图进行多次推理。
最简单的方法是把create_graph()
放在main函数的第一个。
然后,它只创建图表
很好地解释了为什么会提到这样的错误 here,我在使用 tf 数据集时遇到了同样的错误 api 并且开始理解在会话中迭代数据时,它得到附加在现有图表上。所以我所做的是在数据集迭代器之前使用 tf.reset_default_graph()
以确保清除之前的图形。
希望这对这种情况有所帮助。
我正在使用 tensorflow 的 imageNet trained model 提取最后一个池化层的特征作为新图像数据集的表示向量。
模型在新图像上的预测如下:
python classify_image.py --image_file new_image.jpeg
我编辑了 main 函数,这样我就可以获取一个图像文件夹和 return 一次对所有图像进行预测,并将特征向量写入 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)
之前的图像数据会被覆盖,只考虑新的图像数据,似乎不是这样。如何解决这个问题?
这里的问题是每次调用 run_inference_on_image()
都会将 个节点添加到同一个图中,最终超过最大尺寸。至少有两种方法可以解决这个问题:
简单但缓慢 的方法是为每次调用
使用不同的默认图表run_inference_on_image()
:for image in list_of_images: # ... with tf.Graph().as_default(): current_features = run_inference_on_image(images_folder+"/"+image) # ...
更复杂但更有效的方法是在多个图像上将
run_inference_on_image()
修改为运行。将for
循环重新定位到 thissess.run()
call 周围,您将不再需要在每次调用时重建整个模型,这将使处理每张图像的速度更快。
您可以将 create_graph()
移动到某个地方 之前 此循环 for image in list_of_images:
(循环遍历文件)。
它所做的是对同一个图进行多次推理。
最简单的方法是把create_graph()
放在main函数的第一个。
然后,它只创建图表
很好地解释了为什么会提到这样的错误 here,我在使用 tf 数据集时遇到了同样的错误 api 并且开始理解在会话中迭代数据时,它得到附加在现有图表上。所以我所做的是在数据集迭代器之前使用 tf.reset_default_graph()
以确保清除之前的图形。
希望这对这种情况有所帮助。