如何计算图像数据集中 RGB 值的 3x3 协方差矩阵?

2024-03-04

我需要计算图像数据集中 RGB 值的协方差矩阵,然后将 Cholesky 分解应用于最终结果。

RGB 值的协方差矩阵是 3x3 矩阵 M,其中 M_(i, i) 是通道 i 的方差,M_(i, j) 是通道 i 和 j 之间的协方差。

最终结果应该是这样的:

([[0.26, 0.09, 0.02],
[0.27, 0.00, -0.05],
[0.27, -0.09, 0.03]])

尽管 Numpy 有 Cov 函数,但我还是更愿意坚持使用 PyTorch 函数。

我尝试根据其他 cov 实现和克隆在 PyTorch 中重新创建 numpy Cov 函数:

def pytorch_cov(tensor, tensor2=None, rowvar=True):
    if tensor2 is not None:
        tensor = torch.cat((tensor, tensor2), dim=0)
    tensor = tensor.view(1, -1) if tensor.dim() < 2 else tensor
    tensor = tensor.t() if not rowvar and tensor.size(0) != 1 else tensor
    tensor = tensor - torch.mean(tensor, dim=1, keepdim=True)
    return 1 / (tensor.size(1) - 1) * tensor.mm(tensor.t())

def cov_vec(x):
    c = x.size(0)
    m1 = x - torch.sum(x, dim=[1],keepdims=True)/ c
    out = torch.einsum('ijk,ilk->ijl',m1,m1)  / (c - 1)
    return out

数据集加载将如下所示:

dataset = torchvision.datasets.ImageFolder(data_path)
loader = torch.utils.data.DataLoader(dataset)

for images, _ in loader:
    batch_size = images.size(0) 
    ...

目前我只是尝试使用创建的图像torch.randn(batch_size, 3, height, width).

Edit:

我正在尝试从 Tensorflow 的 Lucid 复制矩阵here https://github.com/tensorflow/lucid/blob/master/lucid/optvis/param/color.py#L24,并在 distill.pub 上做了一些解释here https://distill.pub/2017/feature-visualization/#d-footnote-8-listing.

第二次编辑:

为了使输出类似于示例,您必须这样做而不是使用 Cholesky:

rgb_cov_tensor = rgb_cov_tensor / len(loader.dataset)
U,S,V = torch.svd(rgb_cov_tensor)
epsilon = 1e-10
svd_sqrt = U @ torch.diag(torch.sqrt(S + epsilon))

然后,生成的矩阵可用于执行颜色去相关,这对于可视化特征 (DeepDream) 很有用。我已经在我的项目中实现了here https://github.com/ProGamerGov/dream-creator/blob/master/data_tools/calc_cm.py.


这是一个用于计算 3 通道图像上的(无偏)样本协方差矩阵的函数,名为rgb_cov。 Cholesky 分解很简单torch.cholesky:

import torch
def rgb_cov(im):
    '''
    Assuming im a torch.Tensor of shape (H,W,3):
    '''
    im_re = im.reshape(-1, 3)
    im_re -= im_re.mean(0, keepdim=True)
    return 1/(im_re.shape[0]-1) * im_re.T @ im_re

#Test:
im = torch.randn(50,50,3)
cov = rgb_cov(im)
L_cholesky = torch.cholesky(cov)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何计算图像数据集中 RGB 值的 3x3 协方差矩阵? 的相关文章

随机推荐