delta_bbox_coder.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import paddle
  2. import numpy as np
  3. from ppdet.core.workspace import register
  4. from ppdet.modeling.bbox_utils import delta2bbox_v2, bbox2delta_v2
  5. __all__ = ['DeltaBBoxCoder']
  6. @register
  7. class DeltaBBoxCoder:
  8. """Encode bboxes in terms of delta/offset of a reference bbox.
  9. Args:
  10. norm_mean (list[float]): the mean to normalize delta
  11. norm_std (list[float]): the std to normalize delta
  12. wh_ratio_clip (float): to clip delta wh of decoded bboxes
  13. ctr_clip (float or None): whether to clip delta xy of decoded bboxes
  14. """
  15. def __init__(self,
  16. norm_mean=[0.0, 0.0, 0.0, 0.0],
  17. norm_std=[1., 1., 1., 1.],
  18. wh_ratio_clip=16/1000.0,
  19. ctr_clip=None):
  20. self.norm_mean = norm_mean
  21. self.norm_std = norm_std
  22. self.wh_ratio_clip = wh_ratio_clip
  23. self.ctr_clip = ctr_clip
  24. def encode(self, bboxes, tar_bboxes):
  25. return bbox2delta_v2(
  26. bboxes, tar_bboxes, means=self.norm_mean, stds=self.norm_std)
  27. def decode(self, bboxes, deltas, max_shape=None):
  28. return delta2bbox_v2(
  29. bboxes,
  30. deltas,
  31. max_shape=max_shape,
  32. wh_ratio_clip=self.wh_ratio_clip,
  33. ctr_clip=self.ctr_clip,
  34. means=self.norm_mean,
  35. stds=self.norm_std)