- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试迭代超过 100,000 张图像并捕获一些图像特征并将生成的 dataFrame 作为 pickle 文件存储在磁盘上。
不幸的是,由于 RAM 的限制,我不得不将图像分成 20,000 个 block ,并在将结果保存到磁盘之前对它们执行操作。
下面编写的代码应该在开始循环处理接下来的 20,000 张图像之前保存 20,000 张图像的结果数据帧。
但是 - 这似乎并没有解决我的问题,因为在第一个 for 循环结束时内存没有从 RAM 中释放
因此在处理第 50,000 条记录时,程序因内存不足错误而崩溃。
我尝试在将对象保存到磁盘并调用垃圾收集器后将其删除,但 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")
最佳答案
现在,可能是第 50,000 个中的某些东西非常大,这导致了 OOM,所以为了测试这个我首先尝试:
file_list_chunks = list(divide_chunks(file_list_1,20000))[30000:]
如果它在 10,000 时失败,这将确认 20k 是否太大了,或者如果它在 50,000 时再次失败,则代码有问题...
好的,进入代码...
首先,您不需要显式的 list
构造函数,在 python 中迭代比将整个列表生成到内存中要好得多。
file_list_chunks = list(divide_chunks(file_list_1,20000))
# becomes
file_list_chunks = divide_chunks(file_list_1,20000)
我认为您可能在这里滥用了线程池:
Prevents any more tasks from being submitted to the pool. Once all the tasks have been completed the worker processes will exit.
这看起来像 close
可能还有一些想法仍在运行,虽然我想这是安全的,但感觉有点不符合 Python 风格,最好使用 ThreadPool 的上下文管理器:
with ThreadPool(64) as pool:
results = pool.map(get_image_features,f)
# etc.
python 中的显式del
aren't actually guaranteed to free memory .
您应该在 之后 加入/之后收集:
with ThreadPool(..):
...
pool.join()
gc.collect()
您也可以尝试将其分成更小的部分,例如10,000 甚至更少!
有一件事,我会考虑在这里做,而不是使用 pandas DataFrames 和大列表是使用 SQL 数据库,您可以使用 sqlite3 在本地执行此操作:
import sqlite3
conn = sqlite3.connect(':memory:', check_same_thread=False) # or, use a file e.g. 'image-features.db'
并使用上下文管理器:
with conn:
conn.execute('''CREATE TABLE images
(filename text, features text)''')
with conn:
# Insert a row of data
conn.execute("INSERT INTO images VALUES ('my-image.png','feature1,feature2')")
这样,我们就不必处理大型列表对象或 DataFrame。
你可以将连接传递给每个线程......你可能需要一些奇怪的东西,比如:
results = pool.map(get_image_features, zip(itertools.repeat(conn), f))
然后,在计算完成后,您可以从数据库中选择所有格式,选择您喜欢的格式。例如。使用 read_sql .
在这里使用一个子进程,而不是在同一个 python 实例中运行它“shell out”到另一个。
由于您可以将开始和结束作为 sys.args 传递给 python,因此您可以对这些进行切片:
# main.py
# a for loop to iterate over this
subprocess.check_call(["python", "chunk.py", "0", "20000"])
# chunk.py a b
for count,f in enumerate(file_list_chunks):
if count < int(sys.argv[1]) or count > int(sys.argv[2]):
pass
# do stuff
这样,子进程将正确清理 python(不会有内存泄漏,因为进程将被终止)。
我敢打赌,Hammer 1 是可行的方法,感觉就像您在粘合大量数据,并不必要地将其读入 python 列表,而使用 sqlite3(或其他一些数据库)完全避免了这种情况。
关于python - 如何销毁 Python 对象并释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56126062/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!