How do I calculate the mean and standard deviation from training images

I have training images with different size. I will resize if needed in the dataloader. However, how do I calculate it for dataloader?

Hello,

You will have to make a script that passes every image in your dataset beforehand. You can use torch.mean(img, dim=(1, 2)) and torch.std(img, dim=(1, 2)) to compute the mean and standard deviation for one image across the 3 channels.

    x = torch.rand(size=(3, 64, 64), dtype=torch.float32)
    mean = torch.mean(x, dim=(1, 2))
    std = torch.std(x, dim=(1, 2))
    print(mean)
    # tensor([0.4923, 0.5065, 0.5018])
    print(std)
    # tensor([0.2874, 0.2872, 0.2864])

Add the mean and std of every image from your dataset, then divide by the total number of images and save the final mean and std values for future uses!

Hope this helps!

1 Like