Skip to content

Instantly share code, notes, and snippets.

@imneonizer
Created July 8, 2022 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imneonizer/2c12e2235219ebad1f2de2839f9f18d9 to your computer and use it in GitHub Desktop.
Save imneonizer/2c12e2235219ebad1f2de2839f9f18d9 to your computer and use it in GitHub Desktop.
import torch
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
data_path = './Archive_jpg/'
transform_img = transforms.Compose([
transforms.Resize((480, 640)),
# transforms.CenterCrop(256),
transforms.ToTensor(),
])
image_data = torchvision.datasets.ImageFolder(
root=data_path, transform=transform_img
)
batch_size = 32
loader = DataLoader(
image_data,
batch_size = batch_size
)
def batch_mean_and_sd(loader):
cnt = 0
fst_moment = torch.empty(3)
snd_moment = torch.empty(3)
for images, _ in loader:
b, c, h, w = images.shape
nb_pixels = b * h * w
sum_ = torch.sum(images, dim=[0, 2, 3])
sum_of_square = torch.sum(images ** 2,
dim=[0, 2, 3])
fst_moment = (cnt * fst_moment + sum_) / (cnt + nb_pixels)
snd_moment = (cnt * snd_moment + sum_of_square) / (cnt + nb_pixels)
cnt += nb_pixels
mean, std = fst_moment, torch.sqrt(snd_moment - fst_moment ** 2)
return mean,std
mean, std = batch_mean_and_sd(loader)
print("mean and std: \n", mean, std)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment