Hi,
I am applying 0 Mean & Std Dev-1 normalisation to my train and dev set but it is giving me poor metrics in comparison to without normalization after applying them.
Am I calculating the mean & Std dev correctly?
Strictly speaking, you are not supposed to aggregate the standard deviation that way.
Is there a reason why the RGB channels have vastly different stds? This seems surprising, but of course, only you know your data.
var = 0.0
mean = 0.0
nimages = 0
for i,(images,_) in enumerate(tqdm(train_loader, 0)):
batch_samples = images.size(0)
images = images.view(batch_samples, images.size(1), -1)
nimages+= images.size(0)
mean += images.mean(2).sum(0)
var += images.var(2).sum(0)
mean /= nimages
var /= nimages
std = torch.sqrt(var)
Can you say something about the images and your pipeline to load them?
The common thing is to use PIL + ToTensor and then you’d get 0…1 images, which the statistics say you don’t have.
I mentioned that before, but strictly speaking you cannot aggregate variance like that but should use M2 = (images ** 2).mean(dim=(0, 2, 3)) or so and then subtract mean**2 at the end. (see Algorithms for calculating variance - Wikipedia for more sophistication)
But then something is fishy with the averages you get - to_tensor should give you tensors with values 0…1, so you cannot possibly get negative means or std/var > 1.
Be extra sure that your normalization gets the same images (maybe up to random) as your training.
Yesss, you were right. I was calculating the normalisation values twice.
Below are my new mean and std_dev values, do you think these are correct, Also how you are able to see that values are not correct, just by looking at them?
I think these the new values are plausible. The red is quite a bit stronger than green and blue, which would be funny for “natural images”, but of course the medical images will look differently. Do the images look reddish when you open them in an image viewer?
Yes, those are fundus images which are red in colour. I’ll also fix the std dev value as you suggested above, I still quite didn’t understand it but I’ll figure it out.
Thanks.