How can I calculate mean and std before creating loader?

Arturas_Druteika: <p>I’ve seen many tutorials where first a <strong>transform</strong> object is created and then it is passed as an argument to ImageFolder() to create dataset. Transform object can normalize data by picking mean and the standart deviation for every channel. An example:</p>
<pre><code class="lang-auto">transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((some_values between [0, 1]), (some_values between [0, 1]))])

train_dataset = ImageFolder(train_dir, transform)
</code></pre>
<p>What I don’t understand is how can we know what are mean and std values before we pass it to for example ImageFolder() class. What it looks to me is that we have to KNOW what are means and stds before we even have a chance to process dataset. Do we need to first preprocess all data, and calculate mean and std manually?  Many tutorials show how to calculate mean and std after we create loaders, but isn’t it worthless to know these numbers if we already have all loaders created? Am I missing some key idea behind the logic of this or is there a way (a smart way where you wouldn’t need to separately preprocess every image) to find what mean and std for every channel are?</p>

ptrblck: <aside class="quote no-group" data-username="Arturas_Druteika" data-post="1" data-topic="116194">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arturas_druteika/48/53210_2.png" class="avatar"> Arturas_Druteika:</div>
<blockquote>
<p>Many tutorials show how to calculate mean and std after we create loaders, but isn’t it worthless to know these numbers if we already have all loaders created?</p>
</blockquote>
</aside>
<p>I don’t think it’s worthless, as you would then be able to use these calculated stats in your <code>Normalize</code> transformation.<br>
On the other hand you could also try to use e.g. the ImageNet stats, if your data is “similar” to the ImageNet dataset and you expect the mean and stddev to be approx. the same.</p>

Arturas_Druteika: <p>Reply to first statement " I don’t think it’s worthless, as you would then be able to use these calculated stats in your <code>Normalize</code> transformation.". But doesn’t that mean that on the 1-st step I have to create a loader, then on the 2-nd step calculate mean and std and then as a 3-rd step create a new loader with <code>Normalize</code> using mean and std values calculated in a previous step?</p>

omarfoq: <p>Hello,</p>
<p>Well that’s one possibility, and other possibility is to iterate over you dataset without creating a loader. <strong>Note also that this step is very fast (in comparison to the total training duration), and it helps improving the quality of your final model.</strong> As <a class="mention" href="/u/ptrblck">@ptrblck</a> said, if you use real RGB images dataset, that is similar to ImageNet, you can use the same stats.</p>

ptrblck: <aside class="quote no-group" data-username="Arturas_Druteika" data-post="3" data-topic="116194">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arturas_druteika/48/53210_2.png" class="avatar"> Arturas_Druteika:</div>
<blockquote>
<p>But doesn’t that mean that on the 1-st step I have to create a loader, then on the 2-nd step calculate mean and std and then as a 3-rd step create a new loader with <code>Normalize</code> using mean and std values calculated in a previous step?</p>
</blockquote>
</aside>
<p>Yes, you could use either this workflow (or the <code>Dataset</code> directly as mentioned by <a class="mention" href="/u/omarfoq">@omarfoq</a>).<br>
Note, that calculating the dataset stats would only be needed once (if you have a fixed training, validation, and test split) and you could thus reuse the stats in all future experiments (as is done with the ImageNet stats).</p>