跨图像通道 PyTorch 查找均值和标准差

Finding mean and standard deviation across image channels PyTorch

假设我有一批尺寸为 (B x C x W x H) 的张量图像,其中 B 是批量大小,C 是图像中的通道数,W 和 H 是分别是图像的宽度和高度。我希望使用 transforms.Normalize() 函数根据 C 图像通道 中数据集 的均值和标准差对我的图像进行归一化,这意味着我想要一个结果1 x C 形式的张量。有直接的方法吗?

我尝试了 torch.view(C, -1).mean(1)torch.view(C, -1).std(1) 但我收到错误:

view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

编辑

在研究了 view() 在 PyTorch 中的工作原理后,我知道为什么我的方法不起作用;但是,我仍然不知道如何获得每个通道的平均值和标准偏差。

您只需要以正确的方式重新排列批量张量:从 [B, C, W, H][B, C, W * H] 通过:

batch = batch.view(batch.size(0), batch.size(1), -1)

这是随机数据的完整用法示例:

代码:

import torch
from torch.utils.data import TensorDataset, DataLoader

data = torch.randn(64, 3, 28, 28)
labels = torch.zeros(64, 1)
dataset = TensorDataset(data, labels)
loader = DataLoader(dataset, batch_size=8)

nimages = 0
mean = 0.
std = 0.
for batch, _ in loader:
    # Rearrange batch to be the shape of [B, C, W * H]
    batch = batch.view(batch.size(0), batch.size(1), -1)
    # Update total number of images
    nimages += batch.size(0)
    # Compute mean and std here
    mean += batch.mean(2).sum(0) 
    std += batch.std(2).sum(0)

# Final step
mean /= nimages
std /= nimages

print(mean)
print(std)

输出:

tensor([-0.0029, -0.0022, -0.0036])
tensor([0.9942, 0.9939, 0.9923])

请注意,增加的是方差,而不是标准差。请在此处查看详细说明:https://apcentral.collegeboard.org/courses/ap-statistics/classroom-resources/why-variances-add-and-why-it-matters

修改后的代码如下:

nimages = 0
mean = 0.0
var = 0.0
for i_batch, batch_target in enumerate(trainloader):
    batch = batch_target[0]
    # Rearrange batch to be the shape of [B, C, W * H]
    batch = batch.view(batch.size(0), batch.size(1), -1)
    # Update total number of images
    nimages += batch.size(0)
    # Compute mean and std here
    mean += batch.mean(2).sum(0) 
    var += batch.var(2).sum(0)

mean /= nimages
var /= nimages
std = torch.sqrt(var)

print(mean)
print(std)