backwarded way:
x0 = 0; y0 = 0
t_out = net(target_img) #t_out.shape [1x10]
t_out[0,0].backward()
print(target_img.grad[x0, y0]) 
# out is 0.0007

brute force
x0 = 0; y0 = 0
h = 1e-3
with torch.no_grad():
    img0 = img.clone()
    img0[0, 0, x0, y0] += h
    imgcat = torch.cat((img0, img), dim=0)
    out = forward_target(net, imgcat, target)
    return (out[0] - out[1]) / h
# out is 4122