Why can't we use mean and std used for normalization of image data per image and per channel?

pp18: <p>I have seen the same value use of mean and std for nomalizing image data.</p>
<p>For example in <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#afterword-torchvision" rel="nofollow noopener">dataloader</a> tutorial following lines:</p>
<pre><code class="lang-auto">from torchvision import transforms

data_transform = transforms.Compose([
        transforms.RandomSizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
</code></pre>
<p>and</p>
<p>from PyTorch Hub <a href="https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/" rel="nofollow noopener">ResNet</a> model:</p>
<pre><code class="lang-auto"># sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], 
                         std=[0.229, 0.224, 0.225]),])
</code></pre>
<p>I learned from the forum that these are used because it has been computed on imagenet database. My doubt is why can’t we use mean and std per image for nomalization?</p>

JuanFMontesinos: <p>You can really use what you want. Normalizing wrt dataset makes all the samples to belong to a distribution with zero mean and unitary variance. The other way around your input follows several distributions.</p>

pp18: <p>Thanks for the answer. I wan’t thinking like that. I though an images pixels distribution is contingent to the source from where it is obtained and thus uniquely could have high contrast, low saturation etc. Therefore, it is better to normalize individually than all together. The main purpose of normalization was to have faster learning and better convergence. For classification task objective would be learn distribution over images to discriminate  between classes.</p>

JuanFMontesinos: <p>It’s a long story.</p>
<p>Check this plot (which is totally off topic, from an mnist example)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/e/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7_2_690x279.jpeg" alt="image" data-base62-sha1="wdV0WmIALaurmp5KGlSVW5MblGv" width="690" height="279" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/e/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7_2_690x279.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/e/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7_2_1035x418.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/e/e1d8605cc887d195ec62056d24ef5d7ed2b8e2a7.jpeg 2x" data-dominant-color="ECECE9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1132×458 181 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
If you normilize your pixels wrt images, you are losing perspective of differences between images. You map everything into the same normal distribution like fig 2.<br>
If you normalize wrt dataset, each image still has unique distribution with its own mean and std.</p>
<p>You can probably yet classify learning distributions over images but there is no gain on loosing information about differences between images.</p>
<p>In addition we are simplifying ideal classification that would be having per pixel estimators rather than per-channel estimators.</p>
<p>Anyway normalization was dataset-wise because ideally we are supposed to iterate over the whole dataset instead of using mini-batch strategy. There are arguments about using batch-wise normalization instead of dataset.</p>

pp18: <p>Thank you again for your answer. I understand that while I will be still able to learn the classifying distribution but I will be loosing the perspective of differences between images which is further emphasized by the plots. However, I do get the intuition why would we loose the value of difference between images?</p>
<p>I currently do the following</p>
<pre><code class="lang-auto"># img_tensor is of shape C x H x W
mean_vals= img_tensor.view(img_tensor.shape[0], -1).mean(dim=1)
std_vals = img_tensor.view(img_tensor.shape[0], -1).std(dim=1)
		
normalizer = transforms.Normalize(mean=mean_vals, std=std_vals) 
img_tensor = normalizer(img_tensor)    -- (1)
</code></pre>
<p>instead of</p>
<pre><code class="lang-auto"># Consider the images are from ImageNet
mean_vals = [0.485, 0.456, 0.406]
std_vals = [0.229, 0.224, 0.225]
		
normalizer = transforms.Normalize(mean=mean_vals, std=std_vals) 
img_tensor = normalizer(img_tensor)    -- (2)
</code></pre>
<p>You are saying the <code>(1)</code> would give me middle plot and <code>(2)</code> would give me the first or third plot.</p>

JuanFMontesinos: <p>In the end, when you normalize you are getting zero mean and unitary variance.<br>
This is roughly a circle in a plot.</p>
<p>Suppose you have an image big (and variate) enough. If you sample pixels from that image and plot them, you would end up filling the whole circle, namely, having points for all the possible situations.</p>
<p>As you image-wise normalized , each image follows same distribution, 0 mean unitary variance. If you sample pixels from different images and you plot them together you would get 2nd plot. All the points fall in same range.</p>
<p>On contrary, if you dataset-wise normalize, each image would have a different distribution with a unique mean and unique variance. If you sample pixels from each image, you would get different distributions like plot 3.</p>
<p>Explaining it the other way around.<br>
If you apply per-image normalization and then get a single image. You would observe mean of its pixels is zero, and std 1.</p>
<p>If you apply dataset normalization and then you get a single image you would observe mean of its pixels is not zero and std is not one.</p>

pp18: <p>Thanks Juan. You make it more clear now. Now, I have to think about when in past I have normalized 2D data per subject and still got results comparable to SOTA. Maybe I will rerun the experiments someday.</p>

JuanFMontesinos: <p>Hi, Normalization does not get a super awesome boosting. As far as I remember, if  you normalize between 0-1 just dividing by 255 results are yet nice.</p>
<p>You will scratch some decimals by doing one or other normalization.</p>

Intel_Novel: <aside class="quote no-group" data-username="pp18" data-post="1" data-topic="47727">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/e0b2c6/48.png" class="avatar"> pp18:</div>
<blockquote>
<p>transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])</p>
</blockquote>
</aside>
<p>How did we get these numbers. Any ideas? Maybe if we glue all imagenet images into a single big image and then calculate the meant and std? Is this the way?</p>

JuanFMontesinos: <p>The mathematical way is just summing up everything channel-wise for the whole dataset and dividing by amount of pixels.</p>
<p>As imagenet is huge they may have chosen a subset of images or maybe mean per-image means (if images are same size it’s equivalent to the first operation).</p>

pp18: <p><a class="mention" href="/u/intel_novel">@Intel_Novel</a> That is correct. These values are computed on ImageNet database.</p>

Intel_Novel: <p>Would be great if someone can point to the archive paper on why it is used, and how it is calculated exactly. Good for the reference. Also, I would like to say there is an ideal set of transforms for the whole dataset, that would help the machine learning, but this may not be true until some other constraints are set, for instance it may be paired with ideal learning rates, or some other hp. Still, the lr may change over time so looks complicated.</p>

kkpytorch: <p>In tensorflow official resnet model, they use per image normalization.<br>
The 2D example is right only for low dimension data.<br>
I think for high dimension data, normalization does not mix different classes because they have different directions.</p>

Kira_879: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> When you say image-wise normalized , Mean is calculated by Summing up all images and dividing the sum with total number of images ??</p>
<p>From the above answer I’m confused on how image-wise normalization  and dataset normalization is different wrt to calculations .</p>

JuanFMontesinos: <p>You can normalize your image wrt itself. This would be taking an image and for each channel: summing up all its pixels, compute mean and std and normalizing .</p>
<p>Yoy can normalize an image wrt the dataset it comes from. This would be taking the whole dataset and  for each channel of each image: summing up all its pixels. Once you have the total sum for the whole dataset, just divide by the amount of pixels (of the whole dataset). Obviously this may be very slow or not feasible for a very big dataset in which case you can take a representative subset or to use some maths</p>

zeynepdeniz: <p>which one is more useful and more common to use? normalization per image or dataset normalization?</p>

JuanFMontesinos: <p>I’d say per dataset but in practice (and with the expresiveness of the current networks) It does’t matter much. The gain could be 0.5% accuracy or so</p>