as doc of
clone function shows
This function is differentiable, so gradients will flow back from the result of this operation to
input. To create a tensor without an autograd relationship to
src = torch.rand(3, 5, requires_grad =True) dst = src.clone()
The gradients that related to
dst tensor will also flow back to
src tensor. As so far I guess I’m right.
Here is the case where I donot understand why clone is needed.
def forward(self, image_size, feats, boxes): """ image_size: image size, height, width feats: boxes: """ B = boxes.size(0) print("boxes.size", boxes.size()) # convert box to affine matrix self.affine_regressor.set_size(image_size) theta = self.affine_regressor.forward(boxes, image_size) # generate grids grid = self.grid_generator.forward(theta).to(feats.device) print("grid.size", grid.size()) # expand inputs for bilinear sampling print("feats.size", feats.size()) feats = feats.expand(B, feats.size(0), feats.size(1), feats.size(2)) # ^^^^^^^^^^ why is clone needed here??^^^^^^^^^^^^^^^^^^^^ # bilinear sampling box_features = self.roi_sampler.forward(feats, grid) return box_features
The function convert detected boxes
Bx2x3 normalized matrixes, then using these matrixes to generate grids for bilinear sampling, as shown at the last line. The definition of roi_sampler can be located BilinearSamplerBHWD.cu.
After bilinear sampling, I visualized the result, as the following image shows,
clone()after expanding the input
feats (3, H, W)to
(B, 3, H, W). I get the correcte results, say in each box, the cat is interpolated with given expanded feats w.r.t. the generated grids (feats are outputs of backbone model, here VGG16), as depicted in the following figure.
After expanding, the feas sizes with or without clone are the same
6x3xHxW. What kind of role is played by the clone function. The feats are already expanded in the correct dims.
With clone(), the gradients will flow back to the expanded tensor
(B, 3, H, W), which are originally based on
(3, H, W). Do the gradients flow back further to this base tensor. In my case, I need the gradients of the base tensor.