NVlabs / SegFormer

Official PyTorch implementation of SegFormer
https://arxiv.org/abs/2105.15203
Other
2.5k stars 349 forks source link

How can Fig. 6 be reproduced?? (Effective Receptive Field) #123

Open Loparch opened 1 year ago

bbbxyz commented 1 year ago

According to https://arxiv.org/pdf/1701.04128.pdf:

we place a gradient signal of 1 at the center of the output plane and 0 everywhere else, and then back-propagate this gradient through the network to get input gradients.

Something like this should work (not sure about dimension order):

def erf(image, model):
  b, h, w, c = image.shape
  image = image.requires_grad(True)
  output = model(image)
  gradient = torch.zeros_like(output)
  gradient[0, h//2, w//2, :] = 1
  gradient.requires_grad = True
  output.backwards(gradient = gradient)

  return image.grad.detach()