Answers.Body: <p>These are the precalculated values over the train split of the CIFAR10 dataset.</p>
  <p>See this link, <a href="https://github.com/mingkai-zheng/ReSSL/issues/5" rel="nofollow noreferrer">for example</a>. You can try calculating these values for yourself to see if they match up.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-05T09:56:34.750Z
Answers.Text: thank you. the discussion you linked doesn't give steps of how to get the values.
Answers.UserDisplayName: JJJohn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-05T01:12:40.313Z
Answers.LastActivityDate: 2023-10-05T01:12:40.313Z
Answers.UserDisplayName: Alexander Wan
Answers.Body: <p>They should be the means and standard deviations over the RGB channels of images in the training dataset</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-05T01:10:56.187Z
Answers.LastActivityDate: 2023-10-05T01:10:56.187Z
Answers.UserDisplayName: Karl
Body: <p>i'm studying a resnet50 <a href="https://www.mindspore.cn/tutorials/application/en/r2.1/cv/resnet50.html" rel="nofollow noreferrer">tutorial</a>, which contains the following piece of code</p>
  <pre><code>def create_dataset_cifar10(dataset_dir, usage, resize, batch_size, workers):
  
      data_set = ds.Cifar10Dataset(dataset_dir=dataset_dir,
                                   usage=usage,
                                   num_parallel_workers=workers,
                                   shuffle=True)
  
      trans = []
      if usage == &quot;train&quot;:
          trans += [
              vision.RandomCrop((32, 32), (4, 4, 4, 4)),
              vision.RandomHorizontalFlip(prob=0.5)
          ]
  
      trans += [
          vision.Resize(resize),
          vision.Rescale(1.0 / 255.0, 0.0),
          vision.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
          vision.HWC2CHW()
      ]
      ...
  </code></pre>
  <p>I'm aware that normalization is an important step of data pre-processing. I'd just like to know where do the values such as 0.2023 come from.</p>
  <p>Some friends guessed the values referred to the means and standard deviations of the dataset. The following code is to verify the assumption.</p>
  <pre><code>(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
  train_images, test_images = train_images / 255.0, test_images / 255.0
  all_images = np.vstack((test_images, train_images))
  
  reshaped_images = np.copy(all_images)
  reshaped_images = reshaped_images.reshape((3, 60000, 32, 32))
  reshaped_images = reshaped_images.reshape((3, 60000 * 32 * 32))
  print(np.mean(reshaped_images, axis=1))
  print(np.std(reshaped_images, axis=1))
  </code></pre>
  <p>gives</p>
  <pre><code>[0.47562782 0.47245647 0.47361567]
  [0.25186847 0.25178283 0.25087802]
  </code></pre>
  <p>while</p>
  <pre><code>reshaped_images = np.copy(train_images)
  reshaped_images = reshaped_images.reshape((3, 50000, 32, 32))
  reshaped_images = reshaped_images.reshape((3, 50000 * 32 * 32))
  print(np.mean(reshaped_images, axis=1))
  print(np.std(reshaped_images, axis=1))
  </code></pre>
  <p>gives</p>
  <pre><code>[0.47410759, 0.4726623 , 0.47331911]
  [0.2520572 , 0.25201249, 0.25063239]
  </code></pre>
  <p>neither is consistent with <code>0.2023</code></p>
  
Tags: <computer-vision>
Title: Where does 0.2023 for normalization on cifar10 come from?
UserDisplayName: JJJohn
ThreadText: # Where does 0.2023 for normalization on cifar10 come from?
  
  (Asked by: username_0 on 2023-10-05)
  
  i'm studying a resnet50 [tutorial](https://www.mindspore.cn/tutorials/application/en/r2.1/cv/resnet50.html), which contains the following piece of code
  
  ```
  def create_dataset_cifar10(dataset_dir, usage, resize, batch_size, workers):
  
      data_set = ds.Cifar10Dataset(dataset_dir=dataset_dir,
                                   usage=usage,
                                   num_parallel_workers=workers,
                                   shuffle=True)
  
      trans = []
      if usage == "train":
          trans += [
              vision.RandomCrop((32, 32), (4, 4, 4, 4)),
              vision.RandomHorizontalFlip(prob=0.5)
          ]
  
      trans += [
          vision.Resize(resize),
          vision.Rescale(1.0 / 255.0, 0.0),
          vision.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
          vision.HWC2CHW()
      ]
      ...
  
  ```
  
  I'm aware that normalization is an important step of data pre-processing. I'd just like to know where do the values such as 0.2023 come from.
  
  Some friends guessed the values referred to the means and standard deviations of the dataset. The following code is to verify the assumption.
  
  ```
  (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
  train_images, test_images = train_images / 255.0, test_images / 255.0
  all_images = np.vstack((test_images, train_images))
  
  reshaped_images = np.copy(all_images)
  reshaped_images = reshaped_images.reshape((3, 60000, 32, 32))
  reshaped_images = reshaped_images.reshape((3, 60000 * 32 * 32))
  print(np.mean(reshaped_images, axis=1))
  print(np.std(reshaped_images, axis=1))
  
  ```
  
  gives
  
  ```
  [0.47562782 0.47245647 0.47361567]
  [0.25186847 0.25178283 0.25087802]
  
  ```
  
  while
  
  ```
  reshaped_images = np.copy(train_images)
  reshaped_images = reshaped_images.reshape((3, 50000, 32, 32))
  reshaped_images = reshaped_images.reshape((3, 50000 * 32 * 32))
  print(np.mean(reshaped_images, axis=1))
  print(np.std(reshaped_images, axis=1))
  
  ```
  
  gives
  
  ```
  [0.47410759, 0.4726623 , 0.47331911]
  [0.2520572 , 0.25201249, 0.25063239]
  
  ```
  
  neither is consistent with `0.2023`
  
  **Tags:** computer-vision
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-10-05. Score: 1
  
  These are the precalculated values over the train split of the CIFAR10 dataset.
  
  See this link, [for example](https://github.com/mingkai-zheng/ReSSL/issues/5). You can try calculating these values for yourself to see if they match up.
  
  > **Comments:**
  > **username_0**: thank you. the discussion you linked doesn't give steps of how to get the values.  
  
  ---
  
  ### Answer by username_2 on 2023-10-05. Score: 0
  
  They should be the means and standard deviations over the RGB channels of images in the training dataset
  
  
  ---
  
  