Pytorch torchvision: Efficient way of calculating the mean and stds for images in the train set from datasets.ImageFolder

Let us say I have the loading images from my local files using the pytorch torchvision datasets.ImageFolder as follows:

train_data = datasets.ImageFolder(
    os.path.join(out_dir, Training),
    transform=transforms.Compose([
        transforms.Resize([224, 224]), # alenet image size
        transforms.ToTensor() # so that we will be able to calculate mean and std
    ])
)

How can I efficiently calculate the means and stds for each color channel I know when loading dataset from torchvision.dataset I can do it as follows:

train_data = datasets.CIFAR10('.',
                              train=True,
                              download=True
                              )
means = train_data.data.mean(axis = (0, 1, 2))/255
stds = train_data.data.std(axis=(0, 1, 2))/255

My question is how can I calculate the means from the datasets.ImageFolder.

Any help input will be appreciated.

Topic torchvision pytorch torch python

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.