首页 > 解决方案 > 有效地从磁盘加载大量图像python

问题描述

我正在尝试使用以下函数从 pascal voc 2012 数据集(总大小~2G)中读取图像

def image2np(name_list,read = True, save=True, path="./VOC2012/JPEGImages/"):
    if read and os.path.isfile("images.npy"):
        print("reading images .... ")
        images = np.load("images.npy")

    else:
        images = []
        for name in name_list:
            img = get_image(name)
            if img is not None:
                images.append(img)
        print("total number of images read :{}".format(len(images)))
        images = np.array(images)
        if save and read == False:
            np.save("images.npy",images)

    return images

但问题是它吃掉了所有可用的内存(12G),为什么会发生这种情况以及如何更有效地加载图像?

标签: pythonnumpy

解决方案


我正在为有类似问题的人发布答案,问题是 jpeg 图像在存储在磁盘中时会被压缩,所以如果 30000 个尺寸为 256x256 的图像占用让我们说磁盘中的 512mb 将它们加载到 ram 上将使用 30000x256x256x3 ~= 6GB

幸运的是,有更好的方法来加载图像,您可以在下面的文章中找到 有效的图像加载


推荐阅读