I want to put a derivable patch (e.g. 60×60) into a size 500×500 image, then put it into a 608×608 input network (YOLOv4).
I want to get patch.grad, how can I get it?
I realize using transforms to do resize is seemly failed.
You could create the initial patch
with requires_grad=True
and copy it into the bigger tensors using your desired indices (or apply F.interpolate
etc. on it):
patch = torch.randn(1, 1, 60, 60, requires_grad=True)
x = torch.zeros(1, 1, 500, 500)
x[:, :, :60, :60] = patch
y = torch.zeros(1, 1, 608, 608)
y[:, :, :500, :500] = x
conv = nn.Conv2d(1, 1, 3, 1, 1)
out = conv(y)
out.mean().backward()
print(patch.grad)