{
  "id": 99403,
  "title": "Dog Starter Code Question .",
  "url": "/competitions/generative-dog-images/discussion/99403",
  "author_name": "",
  "post_date": "2019-07-11T03:07:25.894587900Z",
  "votes": 1,
  "comment_count": 5,
  "views": 0,
  "content": "<p>Hi ,</p>\n\n<p>I have a very silly question . I am new to torch . In the starter code and quite a few public kernel I see :</p>\n\n<p>\"gen_images = netG(gen_z)\nimages = gen_images.to(\"cpu\").clone().detach()\nimages = images.numpy().transpose(0, 2, 3, 1)\nfor i_image in range(gen_images.size(0)):\n    save_image(gen_images[i_image, :, :, :], os.path.join('../output_images', f'image_{i_batch+i_image:05d}.png'))\" <br>\nWhen i read this part of code , i can see that there is some cloning and copying of tensor happens from gen_images to images . after that \"images\" variable is converted to numpy and then the channels are shifted to last (probably ) . However , we never used that variable and went ahead saving the original from gen_images . Why is it so ? What am i missing ?</p>\n\n<p>Can anyone  please help out  ?</p>",
  "messages": [
    {
      "id": "572521",
      "postDate": "07/11/2019 03:07:25",
      "content": "<p>Hi ,</p>\n\n<p>I have a very silly question . I am new to torch . In the starter code and quite a few public kernel I see :</p>\n\n<p>\"gen_images = netG(gen_z)\nimages = gen_images.to(\"cpu\").clone().detach()\nimages = images.numpy().transpose(0, 2, 3, 1)\nfor i_image in range(gen_images.size(0)):\n    save_image(gen_images[i_image, :, :, :], os.path.join('../output_images', f'image_{i_batch+i_image:05d}.png'))\" <br>\nWhen i read this part of code , i can see that there is some cloning and copying of tensor happens from gen_images to images . after that \"images\" variable is converted to numpy and then the channels are shifted to last (probably ) . However , we never used that variable and went ahead saving the original from gen_images . Why is it so ? What am i missing ?</p>\n\n<p>Can anyone  please help out  ?</p>",
      "rawMarkdown": "Hi ,\n\nI have a very silly question . I am new to torch . In the starter code and quite a few public kernel I see :\n\n\"gen_images = netG(gen_z)\nimages = gen_images.to(\"cpu\").clone().detach()\nimages = images.numpy().transpose(0, 2, 3, 1)\nfor i_image in range(gen_images.size(0)):\n    save_image(gen_images[i_image, :, :, :], os.path.join('../output_images', f'image_{i_batch+i_image:05d}.png'))\"  \nWhen i read this part of code , i can see that there is some cloning and copying of tensor happens from gen_images to images . after that \"images\" variable is converted to numpy and then the channels are shifted to last (probably ) . However , we never used that variable and went ahead saving the original from gen_images . Why is it so ? What am i missing ?\n\nCan anyone  please help out  ?",
      "votes": null
    },
    {
      "id": "572523",
      "postDate": "07/11/2019 03:14:44",
      "content": "<p>I have noticed this too, we never used variable images; function saveimage seems only accept tensors; maybe variable images just created for showing but never used in this code. </p>",
      "rawMarkdown": "I have noticed this too, we never used variable images; function saveimage seems only accept tensors; maybe variable images just created for showing but never used in this code.",
      "votes": null
    },
    {
      "id": "572540",
      "postDate": "07/11/2019 03:57:25",
      "content": "<p>Newbies like me should be careful.  I was so engrossed in the architecture and loss function that I didn't notice that I was applying channel switch and denorm on a variable that was not probably  saving the modification . Other few public kernel also have used it as is . Example RaLSGAN kernel .</p>",
      "rawMarkdown": "Newbies like me should be careful.  I was so engrossed in the architecture and loss function that I didn't notice that I was applying channel switch and denorm on a variable that was not probably  saving the modification . Other few public kernel also have used it as is . Example RaLSGAN kernel .",
      "votes": null
    },
    {
      "id": "572562",
      "postDate": "07/11/2019 05:25:08",
      "content": "<p>The RaLSGAN kernel looks like only using the dcgan not RaLAGAN, I didn't see the least square loss, lol</p>",
      "rawMarkdown": "The RaLSGAN kernel looks like only using the dcgan not RaLAGAN, I didn't see the least square loss, lol",
      "votes": null
    },
    {
      "id": "572589",
      "postDate": "07/11/2019 06:29:27",
      "content": "<p>Yeah :) . I noticed that .. </p>",
      "rawMarkdown": "Yeah :) . I noticed that ..",
      "votes": null
    },
    {
      "id": "572907",
      "postDate": "07/11/2019 15:01:29",
      "content": "<p>This kernel uses PyTorch. PyTorch stores arrays differently than NumPy in memory. If you simply convert a PyTorch array (tensor) to NumPy array and then call <code>plt.imshow()</code>, you will not get a correct image. Because all the rows and columns will be different. This isn't a problem with TensorFlow tensors. If you convert TensorFlow to NumPy, the rows and columns will be ready for <code>plt.imshow()</code>.</p>\n\n<p>The lines of code you reference are not used in the kernel but they are there to show you how to convert to NumPy array in case you need that.</p>",
      "rawMarkdown": "This kernel uses PyTorch. PyTorch stores arrays differently than NumPy in memory. If you simply convert a PyTorch array (tensor) to NumPy array and then call `plt.imshow()`, you will not get a correct image. Because all the rows and columns will be different. This isn't a problem with TensorFlow tensors. If you convert TensorFlow to NumPy, the rows and columns will be ready for `plt.imshow()`.\n\nThe lines of code you reference are not used in the kernel but they are there to show you how to convert to NumPy array in case you need that.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 572523,
      "author_name": "jionie",
      "author_url": "",
      "post_date": "07/11/2019 03:14:44",
      "content": "<p>I have noticed this too, we never used variable images; function saveimage seems only accept tensors; maybe variable images just created for showing but never used in this code. </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 572540,
      "author_name": "phoenix9032",
      "author_url": "",
      "post_date": "07/11/2019 03:57:25",
      "content": "<p>Newbies like me should be careful.  I was so engrossed in the architecture and loss function that I didn't notice that I was applying channel switch and denorm on a variable that was not probably  saving the modification . Other few public kernel also have used it as is . Example RaLSGAN kernel .</p>",
      "votes": null,
      "replies": [
        {
          "id": 572562,
          "author_name": "jionie",
          "author_url": "",
          "post_date": "07/11/2019 05:25:08",
          "content": "<p>The RaLSGAN kernel looks like only using the dcgan not RaLAGAN, I didn't see the least square loss, lol</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 572589,
          "author_name": "phoenix9032",
          "author_url": "",
          "post_date": "07/11/2019 06:29:27",
          "content": "<p>Yeah :) . I noticed that .. </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 572907,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "07/11/2019 15:01:29",
      "content": "<p>This kernel uses PyTorch. PyTorch stores arrays differently than NumPy in memory. If you simply convert a PyTorch array (tensor) to NumPy array and then call <code>plt.imshow()</code>, you will not get a correct image. Because all the rows and columns will be different. This isn't a problem with TensorFlow tensors. If you convert TensorFlow to NumPy, the rows and columns will be ready for <code>plt.imshow()</code>.</p>\n\n<p>The lines of code you reference are not used in the kernel but they are there to show you how to convert to NumPy array in case you need that.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "572521": "Hi ,\n\nI have a very silly question . I am new to torch . In the starter code and quite a few public kernel I see :\n\n\"gen_images = netG(gen_z)\nimages = gen_images.to(\"cpu\").clone().detach()\nimages = images.numpy().transpose(0, 2, 3, 1)\nfor i_image in range(gen_images.size(0)):\n    save_image(gen_images[i_image, :, :, :], os.path.join('../output_images', f'image_{i_batch+i_image:05d}.png'))\"  \nWhen i read this part of code , i can see that there is some cloning and copying of tensor happens from gen_images to images . after that \"images\" variable is converted to numpy and then the channels are shifted to last (probably ) . However , we never used that variable and went ahead saving the original from gen_images . Why is it so ? What am i missing ?\n\nCan anyone  please help out  ?",
    "572523": "I have noticed this too, we never used variable images; function saveimage seems only accept tensors; maybe variable images just created for showing but never used in this code.",
    "572540": "Newbies like me should be careful.  I was so engrossed in the architecture and loss function that I didn't notice that I was applying channel switch and denorm on a variable that was not probably  saving the modification . Other few public kernel also have used it as is . Example RaLSGAN kernel .",
    "572562": "The RaLSGAN kernel looks like only using the dcgan not RaLAGAN, I didn't see the least square loss, lol",
    "572589": "Yeah :) . I noticed that ..",
    "572907": "This kernel uses PyTorch. PyTorch stores arrays differently than NumPy in memory. If you simply convert a PyTorch array (tensor) to NumPy array and then call `plt.imshow()`, you will not get a correct image. Because all the rows and columns will be different. This isn't a problem with TensorFlow tensors. If you convert TensorFlow to NumPy, the rows and columns will be ready for `plt.imshow()`.\n\nThe lines of code you reference are not used in the kernel but they are there to show you how to convert to NumPy array in case you need that."
  },
  "source": "meta"
}