我试图迭代100,000多个图像,捕获一些图像特性,并将产生的dataFrame存储在磁盘上,作为一个泡菜文件。
不幸的是,由于RAM的限制,我不得不将图像分割成20,000块,并在将结果保存到磁盘之前对它们执行操作。
下面编写的代码应该在开始循环之前保存20,000个图像的结果数据,以处理接下来的20,000幅图像。
然而,这似乎并没有解决我的问题,因为内存在第一个for循环结束时没有从RAM中释放出来。
所以在处理第5万条记录的某个地方,程序会因为内存不足而崩溃。
在将对象保存到磁盘并调用垃圾收集器之后,我尝试删除这些对象,但是RAM的使用似乎并没有下降。
我遗漏了什么?
#file_list_1 contains 100,000 images
file_list_chunks = list(divide_chunks(file_list_1,20000))
for count,f in enumerate(file_list_chunks):
# make the Pool of workers
pool = ThreadPool(64)
results = pool.map(get_image_features,f)
# close the pool and wait for the work to finish
list_a, list_b = zip(*results)
df = pd.DataFrame({'filename':list_a,'image_features':list_b})
df.to_pickle("PATH_TO_FILE"+str(count)+".pickle")
del list_a
del list_b
del df
gc.collect()
pool.close()
pool.join()
print("pool closed")
发布于 2019-05-18 08:03:15
备注:这不是一个答案,而是一个快速列出的问题和建议
ThreadPool()
from multiprocessing.pool
吗?这并不是很好的文档(在python3
中),我宁愿使用ThreadPoolExecutor (也参见这里)。sys.getsizeof()
返回所有声明的globals()
的列表,以及它们的内存占用。del results
(虽然我猜这不应该太大)https://stackoverflow.com/questions/56126062
复制相似问题