Image normalization after loading dataset

Yun_Lee: <p>I want to ask you how to normalize batch-images again.</p>
<p>After loading cifar10 dataset, I did custom transformation on image,<br>
and I want to normalize image again before passing to the network.</p>
<p>I followed this code (<a href="https://forums.fast.ai/t/image-normalization-in-pytorch/7534/7" class="inline-onebox" rel="noopener nofollow ugc">Image normalization in PyTorch - Deep Learning - Deep Learning Course Forums</a>)<br>
and could get mean and std from each channel of image</p>
<p>and I want to normalize [128,3,32,32] transformed image again, and pass to the image<br>
and I don’t know how to apply normalization<br>
(do I have to use for-loop…? )</p>
<pre><code class="lang-python">for batch_idx in range(128):
    for channel_idx in range(3):
           image = (image-mean[channel_idx])/std[channel_idx]
</code></pre>
<p>I wanted to use transforms.Normalize(mean, std) but<br>
I don’t know how to change shape of mean and standard<br>
mean,std shapes are [ , , ] [ , , ] for three channels<br>
and if I pass</p>
<pre><code class="lang-auto">transforms.Normalize(mean,std)(data)
</code></pre>
<p>then I get error message,</p>
<pre><code class="lang-auto">valueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([128, 3, 32, 32]).
</code></pre>
<p>in what shape I can pass Normalize function?<br>
or do I have to use other iterative method?</p>
<p>thank you for reading my question.</p>

user_123454321: <p>I think you should pass a single image for the normalize transform. Usually the transform is called inside the ‘<strong>getitem</strong>’ function of the dataset class. You are I think passing a whole batch of 128 images.</p>

Yun_Lee: <p>thank you for answering to my question!<br>
I followed your suggestion and<br>
Normalize function works<br>
as I passed each [3,32,32] image (not batch together [128,3,32,32])</p>
<pre><code class="lang-auto">for I in range(data.shape[0]):
    data[i]  = transforms.Normalize(mean,std)(data[i])
</code></pre>

Xiaoyu_Song: <p>Please define a customed <strong>dataset</strong> following the official tutoral.<br>
Inside the <strong>CustomedDataset</strong> class</p>
<pre><code class="lang-auto">def __getitem__(self, idx):
</code></pre>
<p>define:</p>
<pre><code class="lang-auto"> compose = transforms.Compose([transforms.ToPILImage(), 
                                         transforms.Resize((128,128),interpolation=Image.NEAREST),
                                         transforms.ToTensor(),
                                         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
</code></pre>
<p>then use:</p>
<pre><code class="lang-auto">img_tensor = compose(img_tensor)
</code></pre>

Yun_Lee: <p>Thank you for answering to my question!<br>
I am sorry but I didn’t get it.<br>
you mean I have to create another customDataset<br>
after loading cifar10 dataset?</p>
<p>I already loaded cifar10 dataset and did my-custom transformation on cifar10 tensor images<br>
and want to normalize transformed-cifar10-tensors again before passing to the network.</p>

Xiaoyu_Song: <p>why you want to normalize twice ?</p>

Yun_Lee: <p>because I hope &lt;image transformation &amp; the network&gt; performs better.<br>
after loading cifar10 dataset, I performed some transformations on the image.<br>
Then, mean,stds are not same as before.<br>
So, I wanted to make images normalized again before passing to the network.<br>
and in this case, what you suggested does not work? right?</p>

Xiaoyu_Song: <p>ok, I misunderstood.<br>
In this case, just try to get image one by one and do the normalization.</p>
<p>In your error, it said you get the <strong>batched data</strong> (128 batch size) instead of C,H,W which is one data.</p>

Yun_Lee: <p>Thank you for your answer!</p>

user_123454321: <p>You can vectorize this for faster processing using</p>
<pre><code class="lang-auto">mean = torch.tensor(mean)[None, :, None, None] # assuming mean is 3 values for 3 channels, 
# convert it into shape (1, 3, 1, 1) for broadcasting, similarly
std = torch.tensor(std)[None, :, None, None]
# and then you can do normalization using
data = (data - mean) / (std + 1e-11)
</code></pre>

Yun_Lee: <p>I was finding the fast way to normalize images rather than iterative method, and you gave the right solution!<br>
thank you so much <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>