cython_bbox icon indicating copy to clipboard operation
cython_bbox copied to clipboard

First attempt at sparse implementation

Open redhog opened this issue 2 years ago • 0 comments

Hi!

When working with large numbers of bboxes (at least outside of the neural network domain), it is useful to use sparse matrixes (as most bboxes won't overlap).

I have mashed together a some very simple support for that here, mostly as a discussion starter. Would you be ok with including something along these lines?

If so, I'd be happy to discuss how to make a better PR (for now, I basically included two copies of the code, one sparse and one dense, which is not optimal, but at least does not slow down either implementation).

Usage example:

>>> ov = cython_bbox.bbox_overlaps(shapes.bounds.values, shapes.bounds.values, True)
>>> print(type(ov), ov.size, (ov > 0).sum(), ov.shape[0]*ov.shape[1])
<class 'scipy.sparse._arrays.dok_array'> 27906 27906 6574096

>>> ov = cython_bbox.bbox_overlaps(shapes.bounds.values, shapes.bounds.values)
>>> print(type(ov), ov.size, (ov > 0).sum(), ov.shape[0]*ov.shape[1])
<class 'numpy.ndarray'> 6574096 27906 6574096

Best regards, Egil

redhog avatar Nov 16 '22 15:11 redhog