有效地对大型图像数据矩阵进行求和

huangapple go评论60阅读模式
英文:

Summing large matrices of image data efficiently

问题

如何最高效地对大型 (100, 1024, 1024) .npz 文件进行求和?是否有更好的文件格式来存储给定维度的Python数据?

我目前使用以下方法来对矩阵求和:

summed_matrix = np.sum([np.load(file) for file in files_list])

这超出了我的可用内存。

英文:

How do I sum large (100, 1024, 1024) .npz files most efficiently? Is there a better file format to store python data of the given dimensions?

I currently use this to sum the matrices:

summed_matrix = np.sum([np.load(file) for file in files_list])

This exceeds my available memory.

答案1

得分: 7

目前,您一次加载所有文件,然后才开始求和。如果内存是一个问题,更好的方法是逐个读取并相加:

summed_matrix = np.zeros((100, 1024, 1024))

for file in files_list:
    summed_matrix += np.load(file)
英文:

Currently, you're loading all files at once and only then begin summing. If memory is a concern, a better approach would be to read and add one at a time:

summed_matrix = np.zeros((100, 1024, 1024))

for file in files_list:
    summed_matrix += np.load(file)

答案2

得分: 5

你可以尝试使用 functools.reducenumpy.add 的生成器:

from functools import reduce

summed_matrix = reduce(np.add, (np.load(file) for file in files_list))

# 或者函数式方式
summed_matrix = reduce(np.add, map(np.load, files_list))
英文:

You could try a generator with functools.reduce and numpy.add:

from functools import reduce

summed_matrix = reduce(np.add, (np.load(file) for file in files_list))

# or functional-style
summed_matrix = reduce(np.add, map(np.load, files_list))

huangapple
  • 本文由 发表于 2023年7月20日 20:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定