英文:
Efficient algorithm for online Variance over image batches
问题
我有大量的图像,并想计算它们的方差(每个通道的方差)。
我在寻找一个有效的算法/设置时遇到问题。
我阅读了Welford的在线算法之一,但它太慢了,因为它无法在单个图像或一批图像上进行向量化。
所以我在思考如何通过使用向量化或利用内置的方差算法来提高速度。
英文:
I have a large amount of images and want to calculate the variance (of each channel) across all of them.
I'm having problem finding an efficient algorithm / setup for that.
I read on of the Welford's online algorithm but it is way to slow as it is does not vectorize across a single image or a batch of images.
So I'm wondering how to improve the speed of it to by using vectorization or making use of inbuilt variance algorithms.
答案1
得分: 0
这是更新/合并两个批次的均值和方差所需的两个函数。这两个函数可以用于向量(3个颜色通道),均值和方差可以从内置方法中获取,例如batch.var()
。
方程式来自:https://notmatthancock.github.io/2017/03/23/simple-batch-stat-updates.html
# m:所有先前批次中的样本数(或像素数)
# n:新进批次中的样本数
# mu1:先前的均值
# mu2:当前批次的均值
# v1:先前的方差
# v2:当前批次的方差
def combine_means(mu1, mu2, m, n):
"""
从m个样本的旧均值mu1和n个样本的新均值mu2中更新旧均值。
返回m+n个样本的均值。
"""
return (m / (m+n)) * mu1 + (n/(m+n)) * mu2
def combine_vars(v1, v2, mu1, mu2, m, n):
"""
从m个样本的旧方差v1和n个样本的新方差v2中更新旧方差。
返回m+n个样本的方差。
"""
return (m/(m+n)) * v1 + n/(m+n) * v2 + m*n/(m+n)**2 * (mu1 - mu2)**2
英文:
These are the two functions needed to update/combine the mean and variances of two batches. Both functions can be used with vectors (the 3 color channels) and the mean and variance can be acquired from inbuilt methods like batch.var()
.
Equations taken from: https://notmatthancock.github.io/2017/03/23/simple-batch-stat-updates.html
# m amount of samples (or pixels) over all previous badges
# n amount of samples in new incoming batch
# mu1 previous mean
# mu2 mean of current batch
# v1 previous variance
# v2 variance of current batch
def combine_means(mu1, mu2, m, n):
"""
Updates old mean mu1 from m samples with mean mu2 of n samples.
Returns the mean of the m+n samples.
"""
return (m / (m+n)) * mu1 + (n/(m+n))*mu2
def combine_vars(v1, v2, mu1, mu2, m, n):
"""
Updates old variance v1 from m samples with variance v2 of n samples.
Returns the variance of the m+n samples.
"""
return (m/(m+n)) *v1 + n/(m+n) *v2 + m*n/(m+n)**2 * (mu1 - mu2)**2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论