siavashk / pycpd

Pure Numpy Implementation of the Coherent Point Drift Algorithm
MIT License
510 stars 115 forks source link

Numba decorators #49

Closed agporto closed 3 years ago

agporto commented 3 years ago

I was thinking of trying to implement pycpd using numba decorators, and I was wondering if you had tried it yourself and whether you think it might speed up calculations significantly. Since pycpd is straight numpy, it sounds to me like it would, but I wanted to make sure I wouldn't waste time on something you had tried. Any thoughts?

gattia commented 3 years ago

I implemented some parts of the pycpd algorithm in cython to speed up those parts which are iterative and published it as cycpd: https://github.com/gattia/cycpd

I also added some of the other original methods from the CPD paper (low rank non-rigid/deformable transform) into cycpd. I have since incorporated them back into pycpd as well - though only on development branch for now. See pull request/merge here: https://github.com/siavashk/pycpd/pull/37.

From what I played around with it seemed the next thing to do to speed things up was to add the Fast Gauss Transform (FGT) from the original CPD paper. I have written about this on the readme/intro page of the cycpd repo. I haven't looked into it much, but it seems like FGT should speed up all of the methods (rigid, affine, deformable).

agporto commented 3 years ago

Hi Anthony. Thanks for the link. It looks awesome. I will definitely try it out and compare. Just out of curiosity, have you tried numba decorators out? If so, how do they compare with your cython compilation?

gattia commented 3 years ago

Arthur, I dont have any experience with Numba. Though, it could be interesting due to the supposedly easy leveraging of GPUs. The below blog makes a few comparisons between Cython/Numba. I used the Cython version as a chance to learn some more Cython code as well as to speed up the python (cython) cpd version.

https://lewiscoleblog.com/cython-numba

Sounds like testing Numba is worth a try. The only thing I've read is that Numba might be more finicky to distribute than Cython.... but that might be an old problem that is solved now.

As a different note, @siavashk originally intended this to be a numpy version for teaching purposes. Though, Numba doesn't seem like it will change the code much, so maybe he's open to adding Numba, or maybe a Numba branch to keep things a little separate. Alternatively, you could create a new repo based that builds off of pycpd like I did with cycpd.

siavashk commented 3 years ago

Sorry I am kind of swamped with work. I haven't tried numba but I have heard good things about it. If you fork the project and make changes, I would be happy to help you. It would be a learning experience for me as well. I believe that the code would still be numpy readable even with numba, so people can still use it for educational purposes.

It would be really nice if you could make an example with numba decorators and show that it is faster. Something like run 1000 registrations, one with numba and one without numba and see if it is faster. This would make a good case for the integration.

agporto commented 3 years ago

@siavashk @gattia thank you both very much for the feedback. It seems that this might be an experiment worth doing. I will fork the repo and progressively work on it over the coming months. At first glance, it seems (from the numba docs) that it would be an easy conversion. I will keep you both posted. Thanks again.