You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import torch
  3. def images_to_levels(target, num_levels):
  4. """Convert targets by image to targets by feature level.
  5. [target_img0, target_img1] -> [target_level0, target_level1, ...]
  6. """
  7. target = torch.stack(target, 0)
  8. level_targets = []
  9. start = 0
  10. for n in num_levels:
  11. end = start + n
  12. # level_targets.append(target[:, start:end].squeeze(0))
  13. level_targets.append(target[:, start:end])
  14. start = end
  15. return level_targets
  16. def anchor_inside_flags(flat_anchors,
  17. valid_flags,
  18. img_shape,
  19. allowed_border=0):
  20. """Check whether the anchors are inside the border.
  21. Args:
  22. flat_anchors (torch.Tensor): Flatten anchors, shape (n, 4).
  23. valid_flags (torch.Tensor): An existing valid flags of anchors.
  24. img_shape (tuple(int)): Shape of current image.
  25. allowed_border (int, optional): The border to allow the valid anchor.
  26. Defaults to 0.
  27. Returns:
  28. torch.Tensor: Flags indicating whether the anchors are inside a \
  29. valid range.
  30. """
  31. img_h, img_w = img_shape[:2]
  32. if allowed_border >= 0:
  33. inside_flags = valid_flags & \
  34. (flat_anchors[:, 0] >= -allowed_border) & \
  35. (flat_anchors[:, 1] >= -allowed_border) & \
  36. (flat_anchors[:, 2] < img_w + allowed_border) & \
  37. (flat_anchors[:, 3] < img_h + allowed_border)
  38. else:
  39. inside_flags = valid_flags
  40. return inside_flags
  41. def calc_region(bbox, ratio, featmap_size=None):
  42. """Calculate a proportional bbox region.
  43. The bbox center are fixed and the new h' and w' is h * ratio and w * ratio.
  44. Args:
  45. bbox (Tensor): Bboxes to calculate regions, shape (n, 4).
  46. ratio (float): Ratio of the output region.
  47. featmap_size (tuple): Feature map size used for clipping the boundary.
  48. Returns:
  49. tuple: x1, y1, x2, y2
  50. """
  51. x1 = torch.round((1 - ratio) * bbox[0] + ratio * bbox[2]).long()
  52. y1 = torch.round((1 - ratio) * bbox[1] + ratio * bbox[3]).long()
  53. x2 = torch.round(ratio * bbox[0] + (1 - ratio) * bbox[2]).long()
  54. y2 = torch.round(ratio * bbox[1] + (1 - ratio) * bbox[3]).long()
  55. if featmap_size is not None:
  56. x1 = x1.clamp(min=0, max=featmap_size[1])
  57. y1 = y1.clamp(min=0, max=featmap_size[0])
  58. x2 = x2.clamp(min=0, max=featmap_size[1])
  59. y2 = y2.clamp(min=0, max=featmap_size[0])
  60. return (x1, y1, x2, y2)

No Description

Contributors (3)