Finding the mean and std of pixel values for grayscale images in pytorch
I'm trying to normalize this grayscale xray images dataset https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia
I have a few doubts
1)I looked up some of the projects done using the same dataset and this one below has three mean values (presumably for the three channels). But since this is a grayscale image dataset how can it have mean pixel values for 3 channels? Shouldn't it just be one number between 0 and 1? (https://www.kaggle.com/brennolins/image-classification-pytorch-transfer-learning)

In an attempt to find the mean and std, I attempted to do this -
 train_loader = DataLoader(dataset = train_set, batch_size=64, 
 shuffle=True)

 def get_mean_std(loader):
     channels_sum, channels_square_sum, num_batches= 0, 0, 0

     for data, _ in loader:
         channels_sum += torch.mean(data, dim=[])
         channels_square_sum += torch.mean(data**2, dim = [0,2,3])
         num_batches += 1

     mean=channels_sum/num_batches
     std= (channels_square_sum/num_batches - mean**2)

     return mean, std

 mean, std= get_mean_std(train_loader) 
 print(mean)
 print(std)



It gives me one single value as my pixel mean. I ran this twice and I got two different mean values and a different set of std values. How can this happen?
This is the transformation Im trying to apply to my training set -
transf_train = tt.Compose([
    tt.Resize(60),
    tt.RandomCrop(54, padding=4, padding_mode='reflect'),
    tt.ToTensor(), # converts pixels [0-255] to tensors [0-1]
    tt.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])]) 

(I've taken these current values from the pytorch project done above. I wanted to know how I can find these out myself and how there are three mean channels when the images are grayscale)
thank you!

You get different numbers because the tt.RandomCrop introduces randomness into the data.  You need to go once over the training set and compute mean and std without augmentations.
