fxia22 / stn.pytorch

pytorch version of spatial transformer networks
Other
587 stars 87 forks source link

cannot compute gradients when STN is called multiple times #17

Open emited opened 7 years ago

emited commented 7 years ago

Hello, I get the following error when the code below gets executed:

<ipython-input-4-7d15b88aed95> in <module>()
     19     loss += crit(input1, input1.detach()*0)
     20 
---> 21 loss.backward()

~/anaconda2/envs/python36/lib/python3.6/site-packages/torch/autograd/variable.py in backward(self, gradient, retain_variables)
    144                     'or with gradient w.r.t. the variable')
    145             gradient = self.data.new().resize_as_(self.data).fill_(1)
--> 146         self._execution_engine.run_backward((self,), (gradient,), retain_variables)
    147 
    148     def register_hook(self, hook):

RuntimeError: could not compute gradients for some functions

This happens whenever the STN module is called more then once per backward (the same bug seems to appear when using cuda). Does this code work for somebody else? Does somebody have an idea where it comes from?

import torch
import numpy as np
from modules.stn import STN
from modules.gridgen import AffineGridGenV2

s = STN()
g = AffineGridGenV2(64, 64)
crit = torch.nn.MSELoss()

inputImages = torch.rand(1, 64, 64, 3)
input = torch.autograd.Variable(torch.from_numpy(np.array([[[1, 0.5, 0], [0.5, 1, 0]]], dtype=np.float32)), requires_grad = True)
input1 = torch.autograd.Variable(inputImages)

loss = 0
for i in range(2):
    out = g(input)
    input1 = s(input1, out)
    loss += crit(input1, input1.detach()*0)

loss.backward()
xuejingl commented 6 years ago

I encounter similar problem. Do you solve it? @emited

emited commented 6 years ago

I now use pytorch's official STN implementation! https://github.com/pytorch/pytorch/blob/master/torch/nn/_functions/vision.py