{
  "id": 134064,
  "title": "How to normalize training images?",
  "url": "/competitions/bengaliai-cv19/discussion/134064",
  "author_name": "",
  "post_date": "2020-03-05T18:13:18.342811200Z",
  "votes": 2,
  "comment_count": 5,
  "views": 0,
  "content": "<p>Hey guys,</p>\n\n<p>I have a dumb question to ask: in a lot of the notebook shared by fellow Kagglers, the image is normalized by \n<code>image = (image - mean) / std</code>\nbefore putting into the model, and mostly the mean value is around 0.5, and std is around 0.225.</p>\n\n<p>The dumb question is, how is this image normalization value calculated? Take the mean value as an example, since most of the pixel will be very close to 0 (most of the pixel in an image is white), it seems unlikely the mean of all the pixel is near 0.5 (if white pixel 0 and black pixel is 1).</p>\n\n<p>So can someone help me understand what is the method to calculate the mean and std of your training data set (I assume this step is important after the CV fold split).</p>\n\n<p>Another question is, if you are expanding the grey scale image to RGB, do you simply do <code>torch.Tensor.repeat()</code>, or is there a better way to do that to get more accuracy?</p>\n\n<p>Thanks!</p>",
  "messages": [
    {
      "id": "764678",
      "postDate": "03/05/2020 18:13:18",
      "content": "<p>Hey guys,</p>\n\n<p>I have a dumb question to ask: in a lot of the notebook shared by fellow Kagglers, the image is normalized by \n<code>image = (image - mean) / std</code>\nbefore putting into the model, and mostly the mean value is around 0.5, and std is around 0.225.</p>\n\n<p>The dumb question is, how is this image normalization value calculated? Take the mean value as an example, since most of the pixel will be very close to 0 (most of the pixel in an image is white), it seems unlikely the mean of all the pixel is near 0.5 (if white pixel 0 and black pixel is 1).</p>\n\n<p>So can someone help me understand what is the method to calculate the mean and std of your training data set (I assume this step is important after the CV fold split).</p>\n\n<p>Another question is, if you are expanding the grey scale image to RGB, do you simply do <code>torch.Tensor.repeat()</code>, or is there a better way to do that to get more accuracy?</p>\n\n<p>Thanks!</p>",
      "rawMarkdown": "Hey guys,\n\nI have a dumb question to ask: in a lot of the notebook shared by fellow Kagglers, the image is normalized by \n`image = (image - mean) / std`\nbefore putting into the model, and mostly the mean value is around 0.5, and std is around 0.225.\n\nThe dumb question is, how is this image normalization value calculated? Take the mean value as an example, since most of the pixel will be very close to 0 (most of the pixel in an image is white), it seems unlikely the mean of all the pixel is near 0.5 (if white pixel 0 and black pixel is 1).\n\nSo can someone help me understand what is the method to calculate the mean and std of your training data set (I assume this step is important after the CV fold split).\n\nAnother question is, if you are expanding the grey scale image to RGB, do you simply do `torch.Tensor.repeat()`, or is there a better way to do that to get more accuracy?\n\nThanks!",
      "votes": null
    },
    {
      "id": "764715",
      "postDate": "03/05/2020 19:44:56",
      "content": "<p>For calculating mean and std: <a href=\"https://www.kaggle.com/iafoss/image-preprocessing-128x128\">https://www.kaggle.com/iafoss/image-preprocessing-128x128</a></p>",
      "rawMarkdown": "For calculating mean and std: https://www.kaggle.com/iafoss/image-preprocessing-128x128",
      "votes": null
    },
    {
      "id": "765769",
      "postDate": "03/07/2020 04:13:09",
      "content": "<p>Thanks! But the mean value in the notebook above is around 0.07, but in quite a few notebooks that are shared in the forum, the mean is at around 0.5. Wonder how they got this mean value.</p>",
      "rawMarkdown": "Thanks! But the mean value in the notebook above is around 0.07, but in quite a few notebooks that are shared in the forum, the mean is at around 0.5. Wonder how they got this mean value.",
      "votes": null
    },
    {
      "id": "765840",
      "postDate": "03/07/2020 07:17:36",
      "content": "<p>These are the imagenet values for mean and std, the dataset the models are usually pretrained on. Values can be found here, <a href=\"https://pytorch.org/docs/master/torchvision/models.html\">https://pytorch.org/docs/master/torchvision/models.html</a></p>",
      "rawMarkdown": "These are the imagenet values for mean and std, the dataset the models are usually pretrained on. Values can be found here, https://pytorch.org/docs/master/torchvision/models.html",
      "votes": null
    },
    {
      "id": "765851",
      "postDate": "03/07/2020 07:57:23",
      "content": "<p>The mean and std are the mean and std of the ImageNet dataset. We are using pretrained backbones such as ResNet50 etc. When these CNNs were trained with the entire ImageNet dataset, they subtracted the mean and std of the entire ImageNet dataset. </p>\n\n<p>Therefore technically we should normalize our images by subtracting the same mean that they used and the same standard deviation that they used. You can either look up the values <code>transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])</code> or use the <code>preprocess()</code> function that comes with the CNN you download.</p>\n\n<p>Since we are finetuning these models and thus updating the weights more, it isn't that important to get this right because the network will adjust to whatever new standardization technique we use. But if you don't update the CNN, but use the pretrained ImageNet weights to make features for another model, you should normalize properly.</p>",
      "rawMarkdown": "The mean and std are the mean and std of the ImageNet dataset. We are using pretrained backbones such as ResNet50 etc. When these CNNs were trained with the entire ImageNet dataset, they subtracted the mean and std of the entire ImageNet dataset. \n\nTherefore technically we should normalize our images by subtracting the same mean that they used and the same standard deviation that they used. You can either look up the values `transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])` or use the `preprocess()` function that comes with the CNN you download.\n\nSince we are finetuning these models and thus updating the weights more, it isn't that important to get this right because the network will adjust to whatever new standardization technique we use. But if you don't update the CNN, but use the pretrained ImageNet weights to make features for another model, you should normalize properly.",
      "votes": null
    },
    {
      "id": "766163",
      "postDate": "03/07/2020 19:10:49",
      "content": "<p>Ah, that makes sense. Thanks <a href=\"/cdeotte\">@cdeotte</a> <a href=\"/maxjon\">@maxjon</a>  for the explanation!</p>",
      "rawMarkdown": "Ah, that makes sense. Thanks @cdeotte @maxjon  for the explanation!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 764715,
      "author_name": "greatgamedota",
      "author_url": "",
      "post_date": "03/05/2020 19:44:56",
      "content": "<p>For calculating mean and std: <a href=\"https://www.kaggle.com/iafoss/image-preprocessing-128x128\">https://www.kaggle.com/iafoss/image-preprocessing-128x128</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 765769,
          "author_name": "axiostpc",
          "author_url": "",
          "post_date": "03/07/2020 04:13:09",
          "content": "<p>Thanks! But the mean value in the notebook above is around 0.07, but in quite a few notebooks that are shared in the forum, the mean is at around 0.5. Wonder how they got this mean value.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 765840,
          "author_name": "maxjon",
          "author_url": "",
          "post_date": "03/07/2020 07:17:36",
          "content": "<p>These are the imagenet values for mean and std, the dataset the models are usually pretrained on. Values can be found here, <a href=\"https://pytorch.org/docs/master/torchvision/models.html\">https://pytorch.org/docs/master/torchvision/models.html</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 765851,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "03/07/2020 07:57:23",
      "content": "<p>The mean and std are the mean and std of the ImageNet dataset. We are using pretrained backbones such as ResNet50 etc. When these CNNs were trained with the entire ImageNet dataset, they subtracted the mean and std of the entire ImageNet dataset. </p>\n\n<p>Therefore technically we should normalize our images by subtracting the same mean that they used and the same standard deviation that they used. You can either look up the values <code>transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])</code> or use the <code>preprocess()</code> function that comes with the CNN you download.</p>\n\n<p>Since we are finetuning these models and thus updating the weights more, it isn't that important to get this right because the network will adjust to whatever new standardization technique we use. But if you don't update the CNN, but use the pretrained ImageNet weights to make features for another model, you should normalize properly.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 766163,
      "author_name": "axiostpc",
      "author_url": "",
      "post_date": "03/07/2020 19:10:49",
      "content": "<p>Ah, that makes sense. Thanks <a href=\"/cdeotte\">@cdeotte</a> <a href=\"/maxjon\">@maxjon</a>  for the explanation!</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "764678": "Hey guys,\n\nI have a dumb question to ask: in a lot of the notebook shared by fellow Kagglers, the image is normalized by \n`image = (image - mean) / std`\nbefore putting into the model, and mostly the mean value is around 0.5, and std is around 0.225.\n\nThe dumb question is, how is this image normalization value calculated? Take the mean value as an example, since most of the pixel will be very close to 0 (most of the pixel in an image is white), it seems unlikely the mean of all the pixel is near 0.5 (if white pixel 0 and black pixel is 1).\n\nSo can someone help me understand what is the method to calculate the mean and std of your training data set (I assume this step is important after the CV fold split).\n\nAnother question is, if you are expanding the grey scale image to RGB, do you simply do `torch.Tensor.repeat()`, or is there a better way to do that to get more accuracy?\n\nThanks!",
    "764715": "For calculating mean and std: https://www.kaggle.com/iafoss/image-preprocessing-128x128",
    "765769": "Thanks! But the mean value in the notebook above is around 0.07, but in quite a few notebooks that are shared in the forum, the mean is at around 0.5. Wonder how they got this mean value.",
    "765840": "These are the imagenet values for mean and std, the dataset the models are usually pretrained on. Values can be found here, https://pytorch.org/docs/master/torchvision/models.html",
    "765851": "The mean and std are the mean and std of the ImageNet dataset. We are using pretrained backbones such as ResNet50 etc. When these CNNs were trained with the entire ImageNet dataset, they subtracted the mean and std of the entire ImageNet dataset. \n\nTherefore technically we should normalize our images by subtracting the same mean that they used and the same standard deviation that they used. You can either look up the values `transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])` or use the `preprocess()` function that comes with the CNN you download.\n\nSince we are finetuning these models and thus updating the weights more, it isn't that important to get this right because the network will adjust to whatever new standardization technique we use. But if you don't update the CNN, but use the pretrained ImageNet weights to make features for another model, you should normalize properly.",
    "766163": "Ah, that makes sense. Thanks @cdeotte @maxjon  for the explanation!"
  },
  "source": "meta"
}