python - 有效地从磁盘加载大量图像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),为什么会发生这种情况以及如何更有效地加载图像?
解决方案
我正在为有类似问题的人发布答案,问题是 jpeg 图像在存储在磁盘中时会被压缩,所以如果 30000 个尺寸为 256x256 的图像占用让我们说磁盘中的 512mb 将它们加载到 ram 上将使用 30000x256x256x3 ~= 6GB
幸运的是,有更好的方法来加载图像,您可以在下面的文章中找到 有效的图像加载
推荐阅读
- java - 我正在尝试从 Make My trip 网站中的出发日历中选择当前日期,但无法通过 selenium 和 java 找到 Web 元素
- mysql - MySQL Left Join 抛出我的计数
- sql-server - SQL Server 事务表可以有一个代理键作为它的主键,它有一个子表吗?
- swift - 未调用 HomeKit 附件DidUpdateServices
- acumatica - 在 MySQL 中未正确解析的 Search4 选择器
- winapi - 在 Win32 上检测范围内的 BLE 设备
- css - 在 Shiny 中,如何更改范围滑块标签的格式?
- r - 使用 R 展平 JSON 文件中的列表
- r - How to prevent infinite loop in dataframe lookup where elements are bi-directional
- scala - SBT publish failing due Maven2 path restrictions