seam-carving icon indicating copy to clipboard operation
seam-carving copied to clipboard

perf: support for numba

Open keurcien opened this issue 3 years ago • 1 comments

See this PR only as a POC. Maybe you don't want to change the API, but I thought it'd be interesting to share it.

It was actually hard to speed up things since most of the functions are already super fast. But I have found that _get_forward_seam could greatly benefit from numba, since it can be called a large number of times. The good news is that it does not require that much change to get some speedup #1.

Most edits serve the purpose of making _get_forward_seam numba-compatible. The tricky part was that numpy.min(..., axis=0) and numpy.argmin(..., axis=0) are still not supported by numba, and writing such functions, even using numba would have been inefficient.

But, the choices array has a small fixed number of rows, so it was possible to implement a function min_argmin_along_axis_0 that calculates both the minimum and the corresponding indices. In my tests, it is normally faster that numpy.min and numpy.argmin when compiled just-in-time. Replacing numpy.min(..., axis=0) and numpy.argmin(..., axis=0) with min_argmin_along_axis_0 was the key part to make _get_forward_seam compatible with numba.

In my tests (e.g: adding 300 seams for a 900x1200 image), it was actually 3 times faster. The more seams are added, the greater the benefit.

N.B: it is only tested for the forward energy, as I only use this.

keurcien avatar Dec 12 '21 16:12 keurcien

That's cool. I need some time to review and test it. Glad that you're interested in this repo!

li-plus avatar Dec 23 '21 17:12 li-plus

Hi, I finally got time to review this POC and based on it I have implemented an accelerated version supporting both forward and backward energy (#4). Numba jit gives a 4x end-to-end speedup for forward energy and 2x for backward energy. I really appreciate your efforts and contribution to this package. You may try out the new version seam-carving==1.1.0 for further speedup.

Closing this PR. Feel free to re-open it if you got any issue.

li-plus avatar Nov 12 '22 09:11 li-plus