infer_utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import torch
  2. def decode_infer(output, stride):
  3. # logging.info(torch.tensor(output.shape[0]))
  4. # logging.info(output.shape)
  5. # # bz is batch-size
  6. # bz = tuple(torch.tensor(output.shape[0]))
  7. # gridsize = tuple(torch.tensor(output.shape[-1]))
  8. # logging.info(gridsize)
  9. sh = torch.tensor(output.shape)
  10. bz = sh[0]
  11. gridsize = sh[-1]
  12. output = output.permute(0, 2, 3, 1)
  13. output = output.view(bz, gridsize, gridsize, self.gt_per_grid, 5+self.numclass)
  14. x1y1, x2y2, conf, prob = torch.split(
  15. output, [2, 2, 1, self.numclass], dim=4)
  16. shiftx = torch.arange(0, gridsize, dtype=torch.float32)
  17. shifty = torch.arange(0, gridsize, dtype=torch.float32)
  18. shifty, shiftx = torch.meshgrid([shiftx, shifty] )
  19. shiftx = shiftx.unsqueeze(-1).repeat(bz, 1, 1, self.gt_per_grid)
  20. shifty = shifty.unsqueeze(-1).repeat(bz, 1, 1, self.gt_per_grid)
  21. xy_grid = torch.stack([shiftx, shifty], dim=4).cuda()
  22. x1y1 = (xy_grid+0.5-torch.exp(x1y1))*stride
  23. x2y2 = (xy_grid+0.5+torch.exp(x2y2))*stride
  24. xyxy = torch.cat((x1y1, x2y2), dim=4)
  25. conf = torch.sigmoid(conf)
  26. prob = torch.sigmoid(prob)
  27. output = torch.cat((xyxy, conf, prob), 4)
  28. output = output.view(bz, -1, 5+self.numclass)
  29. return output