Image normalisation after applying augmentation techniques

username: Yanhong_Zhao
cooked: <p>data_transform = transforms.Compose([<br>
transforms.Resize((229,229)),<br>
transforms.RandomResizedCrop((229,229)),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize(mean=[unknown],<br>
std=[unknown])<br>
])</p>
<p>In order to use Data Transforms like this on my own training set, I first need to calculate the mean and standard deviation of my images in the training set which is [87.92133030884222, 89.66749718221185, 81.1012737560522] and [53.17980884, 51.26849681, 51.69558279].</p>
<p>Now I am wondering after applying random sized crop and random horizontal flip which essentially is generating more training examples in the data loader object so surely now my standard deviation and mean of the images will change a little bit.</p>
<p>What is the workaround of this or rather people tend to normalise images first upfront to the [0,1] for all three channels and apply a vanilla mean and variance like [0.5,0.5,0.5] and [0.5,0.5,0.5]?</p>
<p>Thank you!</p>

username: KFrank
cooked: <p>Hello Yanhong!</p>
<aside class="quote no-group" data-username="Yanhong_Zhao" data-post="1" data-topic="68242" data-full="true">
<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/yanhong_zhao/48/14534_2.png" class="avatar"> Yanhong_Zhao:</div>
<blockquote>
<p>Now I am wondering after applying random sized crop and random horizontal flip which essentially is generating more training examples in the data loader object so surely now my standard deviation and mean of the images will change a little bit.</p>
</blockquote>
</aside>
<p>It really doesn’t matter.  There is nothing magic about normalizing your<br>
data to get exactly some mean and standard deviation – you just want<br>
to get the general scale of your data to be sensible.</p>
<p>Unless there is something perniciously weird about your data, the<br>
augmentation you are using will have very little effect on the statistics<br>
of your data set.  You’ll be fine calculating the mean and standard<br>
deviation of your original training set and using those values to<br>
normalize your augmented set.</p>
<p>Good luck!</p>
<p>K. Frank</p>

username: Yanhong_Zhao
cooked: <p>Do you know a quick way of calculating the mean and standard deviation of pixel value for the three respective channels of the training set?</p>

username: KFrank
cooked: <p>Hi Yanhong!</p>
<aside class="quote no-group" data-username="Yanhong_Zhao" data-post="3" data-topic="68242" data-full="true">
<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/yanhong_zhao/48/14534_2.png" class="avatar"> Yanhong_Zhao:</div>
<blockquote>
<p>Do you know a quick way of calculating the mean and standard deviation of pixel value for the three respective channels of the training set?</p>
</blockquote>
</aside>
<p>If your training set is packaged as a giant tensor, <code>train</code>,  (of shape<br>
<code>(nSample, width, height, nChannel)</code>), then you can use<br>
<code>torch.mean()</code> and <code>torch.std()</code> with the <code>dim</code> argument:<br>
<code>torch.mean (train, dim = (0, 1, 2)</code>.  This will return a tensor<br>
of shape <code>(nChannel, )</code> containing the means for each of the channels.</p>
<p>Best.</p>
<p>K. Frank</p>