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

Fast version #1

Open
bpfliegel opened this issue Oct 21, 2019 · 8 comments · Fixed by #2
Open

Fast version #1

bpfliegel opened this issue Oct 21, 2019 · 8 comments · Fixed by #2

Comments

@bpfliegel
Copy link

Dude, the prec/recall function is very slow. Use numpy if possible for batch calculations:

def calc_precision_recall(contours_a, contours_b, threshold):
        x = contours_a
        y = contours_b

        t1 = np.tile(x, (len(y),1))
        t2 = np.repeat(y, len(x), axis=0)
        t = np.concatenate( (t1,t2), axis=1)

        dx = (t[...,0] - t[...,2]) * (t[...,0] - t[...,2])
        dy = (t[...,1] - t[...,3]) * (t[...,1] - t[...,3])
        thr = (dx + dy) < (threshold * threshold)

        blocks = np.split(thr, len(y))
        block_has_hit = [np.any(x == True) for x in blocks]
        top_count = np.sum(block_has_hit)

        precision_recall = top_count/len(y)
        return precision_recall, top_count, len(y)

It should be the same, you can validate it with your own data.
Thanks, Balint

@bpfliegel
Copy link
Author

Even faster to keep the outer loop:

    def calc_precision_recall(contours_a, contours_b, threshold):
        x = contours_a
        y = contours_b

        xx = np.array(x)
        hits = []
        for yrec in y:
            d = np.square(xx[:,0] - yrec[0]) + np.square(xx[:,1] - yrec[1]) < (threshold * threshold)
            hits.append(np.any(d == True))
        top_count = np.sum(hits)       

        precision_recall = top_count/len(y)
        return precision_recall, top_count, len(y)

Thanks, all the best!

@minar09
Copy link
Owner

minar09 commented Oct 22, 2019

Thank you very much, @bpfliegel for your suggestions. Appreciate it.

@bpfliegel
Copy link
Author

Thanks to you for your work! :)

@minar09
Copy link
Owner

minar09 commented Apr 18, 2020

Hey, @bpfliegel , just wondering could you please create a pull request with your improvements? That would be really great. Thank you very much.

@bpfliegel
Copy link
Author

@minar09 too busy, sorry, but I posted you the source code above ^^^ thanks

@minar09
Copy link
Owner

minar09 commented Apr 20, 2020

@bpfliegel , same here. No problem. Thank you very much for the codes. I will try to update the repository.

@theodumont
Copy link
Contributor

Hi! Thanks for the repo.
I implemented @bpfliegel's 2nd version and did a PR, all the details are there: #2

@minar09
Copy link
Owner

minar09 commented Nov 17, 2020

Thank you @theodumont for your contributions.

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 a pull request may close this issue.

3 participants