this returns a runtime error : One of the differentiated Variables appears to not have been used in the graph.
I did
img_tensor = transforms.Compose(...)(numpy_img.transpose(1,2,0))
out = densenet(Variable(img_tensor.unsqueeze(0).cuda(),requires_grad=True))
out = out.view(-1)
grads = grad(out[1],Variable(img_tensor.data,requires_grad=True))

where out is a 7 dim variable which is the output of the network, and img_tensor_input is a (1,3,224,224) tensor