简体   繁体   English

Python-在不使用所有可用内存的情况下加载大量图像

[英]Python - load lots of images without using all available ram

I have about 1.5 GB of images that I need to process. 我有大约1.5 GB的图像需要处理。 The problem is that when I try loading them as np arrays I seem to use up all of my ram (8 GB). 问题是,当我尝试将它们作为np数组加载时,我似乎用光了我所有的ram(8 GB)。

Here is my method for loading images: 这是我加载图像的方法:

def load_image( infilename ) :
    img = Image.open( infilename )
    img.load()
    data = np.asarray( img, dtype="int32" )
    img.close()
    del img
    return data

I thought closing and deleting the img would help, but it doesn't. 我以为关闭和删除img会有所帮助,但没有帮助。 Can this have something to do with garbage collection? 这可能与垃圾回收有关吗?

Code to loop through all images in a list of file names: 代码遍历文件名列表中的所有图像:

for i in range(len(files)):
    imgArray = imgs.load_image(files[i])
    images.append(imgArray)
    shapes.append(np.shape(imgArray))

Is there a better way? 有没有更好的办法?

It might be worth it to load the image files one by one using PIL to get their size tuples, collect your statistics about averages and what not, then open them again in numpy or PIL to do the actual processing. 可能值得使用PIL一张一张地加载图像文件以获取它们的大小元组,收集有关平均值的统计信息,然后收集它们的平均值,然后在numpy或PIL中再次打开它们以进行实际处理。 You might also want to consider sampling for the statistics part so you don't need to load all of them, not that it should take that long anyway, PIL is relatively efficient. 您可能还需要考虑对统计部分进行抽样,因此您不需要全部加载,也不用说要花那么长时间,因为PIL相对有效。

You may be able to use manual garbage collection to clear some of the memory between loops: 您也许可以使用手动垃圾回收来清除循环之间的某些内存:

def memclear():
    import gc   #garbage collector
    cleared = gc.collect()

    print(cleared)

call: memclear() at the end of each loop, so: 在每个循环结束时致电: memclear() ,因此:

for i in range(len(files)):
    imgArray = imgs.load_image(files[i])
    images.append(imgArray)
    shapes.append(np.shape(imgArray))
    memclear()

Hopefully this fixes it. 希望这可以解决它。 I'm assuming this was downvoted because it manually calls garbage cleaning, which is generally frowned upon, but unfortunately it seems to be necessary sometimes. 我认为这是不受欢迎的,因为它手动调用了垃圾清理,这通常是令人不悦的,但是不幸的是有时似乎有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM