Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Faster cached piecewise affine, Cython varient demoted #425
PWA was written a long time ago in Menpo's development. We wanted something that was fast for the case in AAMs where source points remain the same as only target points change, but we couldn't compromise on the central API for transforms - that this sequence of operations should be completety supported by a transform:
pwa = PiecewiseAffine(source, target) pwa.apply(some_points) pwa.apply(some_other_points) pwa.set_target(some_new_target) pwa.apply(some_points)
in AAM's the pattern looks like this:
pwa = PiecewiseAffine(source, target) pwa.apply(points) pwa.set_target(some_new_target) pwa.apply(points) pwa.set_target(another_new_target) pwa.apply(points) ...
if you are expecting this pattern you can go a lot faster by caching the calculation of the barycentric coordinates in the source domain once, and then re-using them.
Our solution 1 year ago was to add a C hashmap variant of PWA. This maintained a hashmap in C of all the points that had been provided to this transform. When a new point was brought in, it was checked against the hash. If we already had calculated the barycentric coordinates the cached value was returned - if not, we recalculated. This approach was nice for a number of reasons:
but it has some negatives:
As we start to think about extracting a clean 'core' toolset from Menpo having a Cython data structure in there is a little messy, so I started to wonder if it's avoidable.
As an alternative, I simplified the PWA package (pulling out much of the functionality as simple functions). I removed the unused
A simple subclass of
This is not that surprising as
Note that this PR does not remove the CythonPWA, it just demotes it from being the default choice.
Demonstration and timings: