Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] db_module_loss,negative number encountered in sqrt #1640

Conversation

KevinNuNu
Copy link
Contributor

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

训练自定义数据集时遇到RuntimeWarning如下
image
调试定位到出错原因如下:
由于pt1, pt2都是float32,计算a_square, b_square, c_square存在数值精度问题
导致后续根据余弦公式计算neg_cos_c时出现了小于-1或者大于1的情况
从而使得计算distance时sqrt下a_square * b_square * square_sin 出现负数

# suppose a triangle with three edge abc with c=point_1 point_2
# a^2
a_square = np.square(xs - pt1[0]) + np.square(ys - pt1[1])
# b^2
b_square = np.square(xs - pt2[0]) + np.square(ys - pt2[1])
# c^2
c_square = np.square(pt1[0] - pt2[0]) + np.square(pt1[1] - pt2[1])
# -cosC=(c^2-a^2-b^2)/2(ab)
neg_cos_c = (
(c_square - a_square - b_square) /
(np.finfo(np.float32).eps + 2 * np.sqrt(a_square * b_square)))
# sinC^2=1-cosC^2
square_sin = 1 - np.square(neg_cos_c)
square_sin = np.nan_to_num(square_sin)
# distance=a*b*sinC/c=a*h/c=2*area/c
result = np.sqrt(a_square * b_square * square_sin /
(np.finfo(np.float32).eps + c_square))

Modification

解决方法:
将计算得到的neg_cos_c值进一步限制在[-1,1]间

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

可以复现该问题的case
xs = [[0,1,2,...,35, 36], [0,1,2,...,35, 36],..., [0,1,2,...,35, 36], [0,1,2,...,35, 36],],shape=(22,37)
ys = [[0,0,...,0,0], [1,1,...,1,1],..., [20,20,...,20,20], [21,21,...,21,21]],shape=(22,37)
pt1 = array([4.8011017, 18.335022], dtype=float32)
pt2 = array([3.2233124, 7.532318], dtype=float32)

Checklist

Before PR:

  • [] I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • [] Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • [] Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • [] New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • [] The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects.
  • CLA has been signed and all committers have signed the CLA in this PR.

Copy link
Collaborator

@gaotongxiao gaotongxiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It's a good catch. Since it's essentially a precision error, we can also manually convert these arrays' types to np.float64 at the beginning of this function, which should handle some error-prone cases. Clipping can still be here as the last line of defense.

@gaotongxiao gaotongxiao merged commit d2a6845 into open-mmlab:dev-1.x Dec 27, 2022
@KevinNuNu KevinNuNu deleted the kevinnunu/fix_db_module_loss_dist_points2line_func branch December 29, 2022 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants