PyTorch tutorial for Neural transfert of artistic style
Hi, If someones are interested, I’ve realized this PyTorch tutorial to implement the neural transfer of artistic style developed by Leon Gatys and AL: Any feedback is welcome!
alexis-jacq
apaszke
Yes, the code works and I give the whole script in the “.py”. I show the wrong code to explain the global idea, then I give the correct version just below (maybe not a good pedagogy…). But I didn’t know that just a list of Variable works, this is much simpler that what I did (I constructed a module with the variable as a parameter). Thanks !
alexis-jacq
If using torch.optim.SGD instead of Adam, the codes work. However, I guessed Adam would give better performance in some cases, and thus tried to use Adam algorithm. Would you mind give an example of using the Adam optimizer? Thank you Adam O(∩_∩)O
phenixcx
smth
apaszke
phenixcx
<SCODE> Traceback (most recent call last):
  File "Neural_Style.py", line 205, in <module>
    style_score += sl.backward()
  File "Neural_Style.py", line 105, in backward
    self.loss.backward(retain_variables=retain_variables)
  File "/usr/local/lib/python3.6/site-packages/torch/autograd/variable.py", line 146, in backward
    self._execution_engine.run_backward((self,), (gradient,), retain_variables)
  File "/usr/local/lib/python3.6/site-packages/torch/nn/_functions/conv.py", line 48, in backward
    if self.needs_input_grad[0] else None)
  File "/usr/local/lib/python3.6/site-packages/torch/nn/_functions/conv.py", line 119, in _grad_input
    return self._thnn('grad_input', input, weight, grad_output)
  File "/usr/local/lib/python3.6/site-packages/torch/nn/_functions/conv.py", line 161, in _thnn
    return impl[fn_name](self, self._bufs[0], input, weight, *args)
  File "/usr/local/lib/python3.6/site-packages/torch/nn/_functions/conv.py", line 251, in call_grad_input
    grad_input, weight, *args)
RuntimeError: Need gradOutput of dimension 4 and gradOutput.size[1] == 64 but got gradOutput to be of shape: [64 x 2401] at /Users/soumith/code/pytorch-builder/wheel/pytorch-src/torch/lib/THNN/generic/SpatialConvolutionMM.c:50<ECODE>
ecolss
Did you update to 0.1.10?
apaszke
alexis-jacq
Yes, I did. I thought the problem was that, the input is cloned and resized in GramMatrix module, and the style loss is then computed on it, and as the error occurs at the stage of style loss backward(), so would it be that, the grad of style loss over GramMatrix is a 2 dim tensor, and further the grad over the cloned and resized input is not properly computed?
ecolss
ecolss
Debug for a while, found the root cause of the error: Variable.data.resize_() -> Variable.resize().
ecolss
Thanks for having reported this issue.
alexis-jacq
mehdi-shiba
Yes, it’s better to use .view
apaszke
ecolss
apaszke
ecolss
leongatys