title: PyTorch: How to normalize a tensor when the image is cropped randomly?
body: <p>Let's say we are working with the <a href="https://pytorch.org/vision/0.8/datasets.html#cifar" rel="nofollow noreferrer">CIFAR-10 dataset</a> and we want to apply some data augmentation and additionally normalize the tensors. Here is some reproducible code for this</p>
  <pre class="lang-py prettyprint-override"><code>from torchvision import transforms, datasets
  import matplotlib.pyplot as plt
  trafo = transforms.Compose([transforms.Pad(padding = 4, fill = 0, padding_mode = &quot;constant&quot;), 
                              transforms.RandomHorizontalFlip(p=0.5),
                              transforms.RandomCrop(size = (32, 32)), 
                              transforms.ToTensor(), 
                              transforms.Normalize(mean = (0.0, 0.0, 0.0), std = (1.0, 1.0, 1.0))]
                            )
  
  cifar10_full = datasets.CIFAR10(root = &quot;CIFAR-10&quot;, train = True, transform = trafo, target_transform = None, download = True)
  </code></pre>
  <p>The normalization I chose so far would do nothing with the tensors since I put the <code>mean</code> and <code>std</code> to <code>0</code> and <code>1</code> respectively. According to the documentation of <a href="https://pytorch.org/vision/stable/transforms.html" rel="nofollow noreferrer"><code>torchvision.transforms.Normalize</code></a>, the provided means and standard deviations are for each channel of the input. However, the problem is that that I cannot calculate the mean across each channel because of some random flipping and cropping mean. Therefore, my idea was something along the following lines</p>
  <pre class="lang-py prettyprint-override"><code>trafo_1 = transforms.Compose([transforms.Pad(padding = 4, fill = 0, padding_mode = &quot;constant&quot;), 
                              transforms.RandomHorizontalFlip(p=0.5),
                              transforms.RandomCrop(size = (32, 32)), 
                              transforms.ToTensor() 
                            )
  
  cifar10_full = datasets.CIFAR10(root = &quot;CIFAR-10&quot;, train = True, transform = trafo_1, target_transform = None, download = True)
  </code></pre>
  <p>Now I could calculate the mean across each channel of the input and then I wanted to normalize the tensors again. <strong>However, I cannot simply use <code>transforms.Normalize()</code> as <code>cifar10_full</code> is not the original dataset anymore, but how I could proceed instead?</strong> (One solution would be to simply fix the seed of the random generators, i.e use <code>torch.manual_seed(0)</code>, but I would like to avoid this for now...)</p>