Optimizer state_dict

username: Bruno_Oliveira
cooked: <p>I am working on a GAN for super-resolution at the moment (<a href="https://arxiv.org/abs/1609.04802" rel="noopener nofollow ugc">SRGAN</a>). This GAN’s generator is initialized as a previously trained CNN. In summary, one should train the generator first, and then train the GAN with a decent generator initialization. I have trained the CNN and its results are as expected. Now, when training the GAN, I am not getting the expected results and the generated images introduced some artifacts like this:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3f417382dec8428534136eb21db65e746f8a2f89.png" alt="Figure_1" data-base62-sha1="91AjaE3rSjRtskP41crrJZ3zUmt" width="320" height="240"><br>
High-resolution image<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/2/62120dae97d89e63de03d3a4e63c9956e0ad3392.png" alt="Figure_CNN" data-base62-sha1="dZztebvaMSKXAYi0wB7lnFNd4YO" width="320" height="240"><br>
Super-resolution image (CNN)<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b901befcb862a83ae8c98f46a3a0ae1d1d6637bf.png" alt="Figure_GAN" data-base62-sha1="qoE4whyyQONVoc1brGDfTc8DxxR" width="320" height="240"><br>
Super-resolution image (GAN)</p>
<p>The training setup is, in summary:</p>
<ol>
<li>Train the generator (optimized for MSELoss);</li>
<li>Train the GAN (optimized for Perceptual Loss, as described in the paper), initializing the generator with the results from the first step. The optimizer here is the same from 1. (Adam, LR=0.001).</li>
</ol>
<p>Among other things, I am thinking it might be a problem with the generator optimizer. After training the CNN separately, I didn’t save the optimizer’s state_dict and started training the GAN with a new optimizer, but with the same parameters. I thought this was right because the generator should be optimized with a different loss function, but I’m not sure. Does someone have an input on why this problem is happening? Do you think it can be related to the optimizer or anything else?</p>

username: Balamurali_M
cooked: <p>During training gan ? Are you using only adversarial loss ? Ideally you should train mse + adversarial + vgg together. And what kind of discriminator are you using ?</p>

username: Bruno_Oliveira
cooked: <p>I am using VGG + Adversarial for the GAN. I don’t use MSE for the GAN because the paper states a choice between MSE and VGG loss, which I followed. Also, the discriminator is exactly the same as described in the paper:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/128417a547a70e57fa978e41c0a259c135328cb7.png" data-download-href="https://discuss.pytorch.org/uploads/default/128417a547a70e57fa978e41c0a259c135328cb7" title="srgan"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/128417a547a70e57fa978e41c0a259c135328cb7_2_690x392.png" alt="srgan" data-base62-sha1="2DNBgszJrKBpKtylfun32MnF9H1" width="690" height="392" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/128417a547a70e57fa978e41c0a259c135328cb7_2_690x392.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/128417a547a70e57fa978e41c0a259c135328cb7_2_1035x588.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/128417a547a70e57fa978e41c0a259c135328cb7_2_1380x784.png 2x" data-dominant-color="CAD1CE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">srgan</span><span class="informations">2458×1398 1.08 MB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I’m trying to be as close as possible to what the paper says.</p>

username: Balamurali_M
cooked: <p>This artifacts might be generated if you are using only adversarial loss to update both generator and discriminator. Can you use adversarial, mse and perceptual together to update the weights once. Check pix2pix, dc-gan update rules and follow them.</p>

username: Bruno_Oliveira
cooked: <p>I was using Content Loss (features) and Adversarial Loss to train the GAN. In the paper, the author calculates the Content Loss either by comparing the generated image (SR image) with the original image (HR image), using MSE Loss, or by comparing the feature maps generated when feeding the low-resolution image (LR image) and HR image through the VGG, also using MSE Loss.</p>
<p>On the other hand, it is not clear if the Content Loss is exclusive. I mean, one may ask if Content Loss for the GAN is calculated only using the VGG feature maps (Case 1) or if it uses the VGG features maps AND the MSELoss(SRimage, HRimage) (Case 2).</p>
<p>I have tested briefly using both for the Content Loss. Training with the described Case 1 generates the bad images I originally mentioned (those with the artifacts and low PSNR). The Case 2 on the other hand looks promising, as it holds to an PSNR level similar to the reported in the paper, but I didn’t finish training because it seems to me this was not originally done by the author, because of the following statement in the paper:</p>
<blockquote>
<p><strong>Instead</strong> of relying on pixel-wise losses we build on the ideas of Gatys et al. [19], Bruna et al. [5] and Johnson et al. [33] and use a loss function that is closer to perceptual similarity. We define the VGG loss based on the ReLU activation layers of the pre-trained 19 layer VGG network described in Simonyan and Zisserman [49].</p>
</blockquote>
<p>This statement makes me think that Case 1 is the right one, even though I’m not reaching the expected results just yet.</p>
<p>Also, maybe I’m being too strict and the instead just means that it will not rely only on pixel-wise losses, but pixel-wise and VGG loss, which would make the Case 2 the right one. Let me know what you think, any input is good, this is a very important work for me.</p>

username: Balamurali_M
cooked: <p>Case 1 and case 2 behaviour is expected. Please send the paper title. Will have a look.</p>

username: Bruno_Oliveira
cooked: <p>I posted it originally. Quoting it:</p>
<blockquote>
<p>I am working on a GAN for super-resolution at the moment (<a href="https://arxiv.org/abs/1609.04802" rel="noopener nofollow ugc">SRGAN</a>).</p>
</blockquote>
<p>Thanks for helping out.</p>

username: Balamurali_M
cooked: <p>Went through the paper. Yeah, they seem to propose vgg loss as an alternative to mse loss. Since mse is directly related to psnr, it would be ideal to train it with mse. But in this case, since we are worried more about the perceptual quality they are using vgg perceptual loss. So, that’s the reason they have used mean opinion score measures. Now, coming to making it work, it will be better to have a <strong>smaller patch (96x96)</strong> as mentioned in the paper. Train for <strong>higher number of epochs</strong> compared to the one you do with mse, because there is no direct correlation.  I have also seen similar works which tries to bring in a indirect relation to pixel-level mapping, but i had hard time making it work.</p>

username: Bruno_Oliveira
cooked: <p>Yes, that’s pretty much what I’m doing. Recently I have changed the image patch size to 256 instead of 96. I did this because of PyTorch’s pretrained VGG19, as it asks for a input of size higher than 224, but it did not improve much my results, so I guess the problem is in my code. I’ll be reviewing the code as a whole tomorrow, in hope of finding a mistake and fixing it.</p>
<p>The good part is that I can reach the reported metrics of SRResNet-MSE, which makes me suspect that the problem is related to my VGG Loss implementation.</p>

username: Balamurali_M
cooked: <p>Cool. Which framework are you using ? Pytorch ?</p>

username: Bruno_Oliveira
cooked: <p>Yes, I’m using PyTorch.</p>

username: Balamurali_M
cooked: <p>Share the code. I will see if i can help.</p>

username: Bruno_Oliveira
cooked: <aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/BrunoVox/SRGAN" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/31042118?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/BrunoVox/SRGAN" target="_blank" rel="nofollow noopener">BrunoVox/SRGAN</a></h3>

<p>PyTorch implementation of SRGAN. Contribute to BrunoVox/SRGAN development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Sorry for the delay, I was basically rewriting the code. It seems a decent version to be shared for now. I will be writing a README tomorrow, which will help you training the model/testing it. I will also upload a pretrained SRResNet tomorrow. You can control the training parameters by editing “config/train.json” and you will need to manually change the folder output of testing in the test.py. I’m training it on aproximately 400k images from Imagenet. You will need to download the eval datasets too, but I’ll upload them tomorrow, when I’ll have some free time. The eval datasets I’m using at the moment are: Set5, Set14, BSD100. Urban100 and Manga109. Share with me what you think. The SRResNet-MSE seems just fine, and SRResNet-VGG22 looks better, but there’s still room to improve. I haven’t tested SRGAN yet. If you have any feedback, mainly regarding SRResNet-VGG22, please share with me.</p>
<p>Also, the PyTorch pretrained models of VGG requires an input of, at least, 3x224x224. I didn’t think about it, it occurred to me a few minutes ago, but I need to forward 96x96 images through this VGG model. What would you do? I’m thinking about rescaling the image to 224, forwarding in the VGG, and then downscale the feature map, but it seems odd to me, I will be losing information. Best case scenario would be training VGG from scratch I think, with the 3x96x96 inputs I need.</p>