Find out mean and std of dataset

Hello,

I’m trying to find out the mean and std value of DIV2K dataset by myself but I’m facing an issue that object() takes no parameters. any help will be appreciated.

This is my code:

from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset
from torchvision import transforms

def mean__std(data_loader):
    cnt = 0
    mean = torch.empty(3)
    std = torch.empty(3)

    for data in data_loader:

        b, c, h, w = data.shape
        nb_pixels = b * h * w
        sum_ = torch.sum(data, dim=[0, 2, 3])
        sum_of_square = torch.sum(data ** 2, dim=[0, 2, 3])
        fst_moment = (cnt * mean + sum_) / (cnt + nb_pixels)
        snd_moment = (cnt * std + sum_of_square) / (cnt + nb_pixels)

        cnt += nb_pixels

    return mean, torch.sqrt(std - mean ** 2)

train_data = torchvision.datasets.ImageFolder('img/', transform=transforms.Compose([transforms.ToTensor]))
data_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=False, num_workers=1)

mean, std = mean__std(data_loader)
print(mean, std)

This is the error:

Traceback (most recent call last):
  File "/home/Desktop/mean.py", line 36, in <module>
    mean, std = mean__std(data_loader)
  File "/home/Desktop/mean.py", line 20, in mean__std
    for data in data_loader:
  File "/home/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 637, in __next__
    return self._process_next_batch(batch)
  File "/home/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
TypeError: Traceback (most recent call last):
  File "/home/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py", line 103, in __getitem__
    sample = self.transform(sample)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py", line 49, in __call__
    img = t(img)
TypeError: object() takes no parameters

You have to change your transforms to transforms.Compose([transforms.ToTensor()]) since currently you only have a class object, but not an instance and calling a class object will result in creating an instance, but you already need a callable instance here.

1 Like