bfscore_python icon indicating copy to clipboard operation
bfscore_python copied to clipboard

Fast version

Open bpfliegel opened this issue 5 years ago • 8 comments

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 avatar Oct 21 '19 13:10 bpfliegel

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!

bpfliegel avatar Oct 21 '19 20:10 bpfliegel

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

minar09 avatar Oct 22 '19 01:10 minar09

Thanks to you for your work! :)

bpfliegel avatar Oct 22 '19 07:10 bpfliegel

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

minar09 avatar Apr 18 '20 03:04 minar09

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

bpfliegel avatar Apr 19 '20 19:04 bpfliegel

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

minar09 avatar Apr 20 '20 07:04 minar09

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

theodumont avatar Nov 17 '20 14:11 theodumont

Thank you @theodumont for your contributions.

minar09 avatar Nov 17 '20 14:11 minar09