Skip to content

Commit

Permalink
Fix: rasanc max_samples_by_conf to not return negative (#2897)
Browse files Browse the repository at this point in the history
* added failing test

* added the bugfix of RANSAC.max_samples_by_conf

* fixing broken links

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
vicsyl and pre-commit-ci[bot] committed May 7, 2024
1 parent 05f580e commit f0ed53e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Everyone is welcome to get involved with the project. There are different ways t
1. using the GitHub discussion at Kornia repo: [GH Discussions](https://github.com/kornia/kornia/discussions)
2. our slack workspace to keep in touch with our core contributors and the community:
[Join Here](https://join.slack.com/t/kornia/shared_invite/zt-csobk21g-2AQRi~X9Uu6PLMuUZdvfjA)
3. using the `#kornia` tag in [PyTorch Discuss](https://discuss.pytorch.org>)
3. using the `#kornia` tag in [PyTorch Discuss](https://discuss.pytorch.org)
- Please, don't use GitHub issues for Q&A.
- In case you are a developer and want to learn more about the PyTorch ecosystem, we suggest you join the PyTorch
slack. You can apply using this form: [https://bit.ly/ptslack](https://bit.ly/ptslack>)
slack. You can apply using this form: [https://bit.ly/ptslack](https://bit.ly/ptslack)

2. Report bugs through [GitHub issues](https://github.com/kornia/kornia/issues>):
2. Report bugs through [GitHub issues](https://github.com/kornia/kornia/issues):
- Do a quick search first to see whether others reported a similar issue.
- In case you find an unreported bug, please open a new ticket.
- Try to provide as much information as possible. Report using one of the available templates. Some tips:
Expand Down
2 changes: 1 addition & 1 deletion kornia/geometry/ransac.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def max_samples_by_conf(n_inl: int, num_tc: int, sample_size: int, conf: float)
return 1.0
if n_inl == num_tc:
return 1.0
return math.log(1.0 - conf) / max(eps, math.log(max(eps, 1.0 - math.pow(n_inl / num_tc, sample_size))))
return math.log(1.0 - conf) / min(-eps, math.log(max(eps, 1.0 - math.pow(n_inl / num_tc, sample_size))))

def estimate_model_from_minsample(self, kp1: Tensor, kp2: Tensor) -> Tensor:
batch_size, sample_size = kp1.shape[:2]
Expand Down
16 changes: 16 additions & 0 deletions tests/geometry/test_ransac.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

import pytest
import torch

Expand Down Expand Up @@ -241,3 +243,17 @@ def gradfun(p1, p2):
return model(p1, p2)[0]

self.gradcheck(gradfun, (points1, points2), fast_mode=False, requires_grad=(True, False, False))


class TestRansacMethods:
def test_max_samples_by_conf(self):
conf = 0.99
eps = 1e-9

x = RANSAC.max_samples_by_conf(n_inl=1, num_tc=1000, sample_size=7, conf=conf)
assert x > 0.0
assert x == math.log(1.0 - conf) / -eps

x = RANSAC.max_samples_by_conf(n_inl=999999999, num_tc=1000000000, sample_size=1, conf=conf)
assert x > 0.0
assert x == math.log(1.0 - conf) / math.log(eps)

0 comments on commit f0ed53e

Please sign in to comment.