{
  "id": 100437,
  "title": "Imagenet Denormalization",
  "url": "/competitions/generative-dog-images/discussion/100437",
  "author_name": "Nirjhar Roy",
  "post_date": "2019-07-18T14:44:28.208000",
  "votes": 0,
  "comment_count": 7,
  "views": 0,
  "content": "<p>I need help .</p>\n\n<p>I am doing a normalization like below  : \n<code>transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])</code></p>\n\n<p>I am doing a denormalization like below  : \n```\ngen_z = torch.randn(32, nz, 1, 1, device=device)\ngen_images = netG(gen_z).to(\"cpu\").clone().detach() \ngen_images = gen_images.numpy().transpose(0, 2, 3, 1)</p>\n\n<p>fig = plt.figure(figsize=(25, 16))\nfor ii, img in enumerate(gen_images):\n    ax = fig.add_subplot(4, 8, ii + 1, xticks=[], yticks=[])\n    plt.imshow(img[:,:,:]*([ 0.485, 0.456, 0.406])+([ 0.229, 0.224, 0.225 ]))</p>\n\n<p>```\nBut images output after dnorm are little dark .  What am I doing wrong ?</p>",
  "messages": [
    {
      "id": 580073,
      "postDate": "2019-07-19T16:34:09.537Z",
      "content": "<p>By the way, why do you use Imagenet normalization parameter? I think you can not use pretrained model parameter.</p>",
      "rawMarkdown": "By the way, why do you use Imagenet normalization parameter? I think you can not use pretrained model parameter."
    },
    {
      "id": 579205,
      "postDate": "2019-07-18T15:51:32.797Z",
      "content": "<p>Swap std and mean in denorm part</p>",
      "rawMarkdown": "Swap std and mean in denorm part",
      "replies": [
        {
          "id": 579218,
          "postDate": "2019-07-18T16:09:06.257Z",
          "content": "<p>Like this ?\n<code>plt.imshow(img[:,:,:]*([ 0.229, 0.224, 0.225 ]) + ([ 0.485, 0.456, 0.406]))</code></p>",
          "rawMarkdown": "Like this ?\n`plt.imshow(img[:,:,:]*([ 0.229, 0.224, 0.225 ]) + ([ 0.485, 0.456, 0.406]))`"
        },
        {
          "id": 579250,
          "postDate": "2019-07-18T16:43:53.917Z",
          "rawMarkdown": "",
          "isDeleted": true
        },
        {
          "id": 579254,
          "postDate": "2019-07-18T16:47:31.993Z",
          "content": "<p>oh, no... That looks completely wrong :)</p>\n\n<p><code>\nplt.imshow((img[:,:,:] + ([ 0.485, 0.456, 0.406])) /([ 0.229, 0.224, 0.225 ])\n</code></p>\n\n<p>This should work, given mean and std are pytorch tensors\n<code>\ndef img_denorm(img, mean, std):\n    denormalize = transforms.Normalize((-1 * mean / std), (1.0 / std))\n    res = img.squeeze(0)\n    res = denormalize(res)\n    res = torch.clamp(res, 0, 1)\n    return (res)\n</code></p>",
          "rawMarkdown": "oh, no... That looks completely wrong :)\n\n```\nplt.imshow((img[:,:,:] + ([ 0.485, 0.456, 0.406])) /([ 0.229, 0.224, 0.225 ])\n```\n\nThis should work, given mean and std are pytorch tensors\n```\ndef img_denorm(img, mean, std):\n    denormalize = transforms.Normalize((-1 * mean / std), (1.0 / std))\n    res = img.squeeze(0)\n    res = denormalize(res)\n    res = torch.clamp(res, 0, 1)\n    return (res)\n```"
        },
        {
          "id": 580063,
          "postDate": "2019-07-19T16:09:43.500Z",
          "content": "<p>Hey Dmitry,\nThank you for the snippet.</p>\n\n<p>Though, for me, it didnt improve my score on RaLSGAN</p>",
          "rawMarkdown": "Hey Dmitry,\nThank you for the snippet.\n\nThough, for me, it didnt improve my score on RaLSGAN",
          "votes": 1
        },
        {
          "id": 580089,
          "postDate": "2019-07-19T16:50:30.420Z",
          "content": "<p>I used the snippet , but still the image output was little weird .. And yes score didnot improved..</p>",
          "rawMarkdown": "I used the snippet , but still the image output was little weird .. And yes score didnot improved.."
        }
      ]
    },
    {
      "id": 579158,
      "postDate": "2019-07-18T14:44:28.207Z",
      "content": "<p>I need help .</p>\n\n<p>I am doing a normalization like below  : \n<code>transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])</code></p>\n\n<p>I am doing a denormalization like below  : \n```\ngen_z = torch.randn(32, nz, 1, 1, device=device)\ngen_images = netG(gen_z).to(\"cpu\").clone().detach() \ngen_images = gen_images.numpy().transpose(0, 2, 3, 1)</p>\n\n<p>fig = plt.figure(figsize=(25, 16))\nfor ii, img in enumerate(gen_images):\n    ax = fig.add_subplot(4, 8, ii + 1, xticks=[], yticks=[])\n    plt.imshow(img[:,:,:]*([ 0.485, 0.456, 0.406])+([ 0.229, 0.224, 0.225 ]))</p>\n\n<p>```\nBut images output after dnorm are little dark .  What am I doing wrong ?</p>",
      "rawMarkdown": "I need help .\n\nI am doing a normalization like below  : \n`transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])`\n\nI am doing a denormalization like below  : \n```\ngen_z = torch.randn(32, nz, 1, 1, device=device)\ngen_images = netG(gen_z).to(\"cpu\").clone().detach() \ngen_images = gen_images.numpy().transpose(0, 2, 3, 1)\n\nfig = plt.figure(figsize=(25, 16))\nfor ii, img in enumerate(gen_images):\n    ax = fig.add_subplot(4, 8, ii + 1, xticks=[], yticks=[])\n    plt.imshow(img[:,:,:]*([ 0.485, 0.456, 0.406])+([ 0.229, 0.224, 0.225 ]))\n\n```\nBut images output after dnorm are little dark .  What am I doing wrong ?"
    }
  ],
  "comments": [
    {
      "id": 580073,
      "author_name": "hirune924",
      "author_url": "",
      "post_date": "2019-07-19T16:34:09.537000",
      "content": "<p>By the way, why do you use Imagenet normalization parameter? I think you can not use pretrained model parameter.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 579205,
      "author_name": "Dmitry Vorobiev",
      "author_url": "",
      "post_date": "2019-07-18T15:51:32.797000",
      "content": "<p>Swap std and mean in denorm part</p>",
      "votes": 0,
      "replies": [
        {
          "id": 579218,
          "author_name": "Nirjhar Roy",
          "author_url": "",
          "post_date": "2019-07-18T16:09:06.257000",
          "content": "<p>Like this ?\n<code>plt.imshow(img[:,:,:]*([ 0.229, 0.224, 0.225 ]) + ([ 0.485, 0.456, 0.406]))</code></p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 579250,
          "author_name": "",
          "author_url": "",
          "post_date": "2019-07-18T16:43:53.917000",
          "content": "",
          "votes": 0,
          "replies": []
        },
        {
          "id": 579254,
          "author_name": "Dmitry Vorobiev",
          "author_url": "",
          "post_date": "2019-07-18T16:47:31.993000",
          "content": "<p>oh, no... That looks completely wrong :)</p>\n\n<p><code>\nplt.imshow((img[:,:,:] + ([ 0.485, 0.456, 0.406])) /([ 0.229, 0.224, 0.225 ])\n</code></p>\n\n<p>This should work, given mean and std are pytorch tensors\n<code>\ndef img_denorm(img, mean, std):\n    denormalize = transforms.Normalize((-1 * mean / std), (1.0 / std))\n    res = img.squeeze(0)\n    res = denormalize(res)\n    res = torch.clamp(res, 0, 1)\n    return (res)\n</code></p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 580063,
          "author_name": "Dhruuv Agarwal",
          "author_url": "",
          "post_date": "2019-07-19T16:09:43.500000",
          "content": "<p>Hey Dmitry,\nThank you for the snippet.</p>\n\n<p>Though, for me, it didnt improve my score on RaLSGAN</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 580089,
          "author_name": "Nirjhar Roy",
          "author_url": "",
          "post_date": "2019-07-19T16:50:30.420000",
          "content": "<p>I used the snippet , but still the image output was little weird .. And yes score didnot improved..</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "580073": "By the way, why do you use Imagenet normalization parameter? I think you can not use pretrained model parameter.",
    "579205": "Swap std and mean in denorm part",
    "579158": "I need help .\n\nI am doing a normalization like below  : \n`transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])`\n\nI am doing a denormalization like below  : \n```\ngen_z = torch.randn(32, nz, 1, 1, device=device)\ngen_images = netG(gen_z).to(\"cpu\").clone().detach() \ngen_images = gen_images.numpy().transpose(0, 2, 3, 1)\n\nfig = plt.figure(figsize=(25, 16))\nfor ii, img in enumerate(gen_images):\n    ax = fig.add_subplot(4, 8, ii + 1, xticks=[], yticks=[])\n    plt.imshow(img[:,:,:]*([ 0.485, 0.456, 0.406])+([ 0.229, 0.224, 0.225 ]))\n\n```\nBut images output after dnorm are little dark .  What am I doing wrong ?"
  }
}