din14970 / pyMatchSeries

a python wrapper for matchSeries
GNU General Public License v3.0
13 stars 9 forks source link

Python implementation refactor, numba kernel optimizations, some GPU implementations #8

Open din14970 opened 1 year ago

din14970 commented 1 year ago

After more than a year I finally found a bit of spare time to work on this project a bit more. I have taken your implementation @berkels and made some improvements in terms of:

What still needs to happen:

@berkels I would really appreciate if you could skim over this if/when you find time and comment on:

berkels commented 1 year ago

The bias correction step indeed still seems to be missing. Let $$N[\psi]:=\frac12\sum{i=1}^{n}\int\Omega\lVert\phi_i(\psi(x))-x\rVert^2\mathrm{d}x.$$ To compute the minimizing $\phi$, we do a gradient flow of $N$ with respect to $\psi$. This needs the first variation $$\langle N'[\psi,\phi_1,\ldots,\phin],\zeta\rangle=\sum{i=1}^{n}\int_\Omega (D\phi_i)(\psi(x))^T(\phi_i(\psi(x))-x)\cdot\zeta\mathrm{d}x.$$ This is very similar to how the registration itself is done. This just doesn't have a regularizer and includes all deformations, not just one.

berkels commented 1 year ago

Note $N$ can be seen as sum of data terms typical for registration. Let $x_1$ and $x_2$ denote the components of $x$, i.e. $x=(x_1,x_2)$. Furthermore, denote the components of $\phi_i(x)$ with $\phii(x)=(\phi{i,1}(x),\phi{i,2}(x))$. Then, $$N[\psi]=\frac12\sum{i=1}^{n}\sum{j=1}^2\int\Omega(\phi_{i,j}(\psi(x))-x_j)^2\mathrm{d}x.$$ and $$\langle N'[\psi,\phi_1,\ldots,\phin],\zeta\rangle=\sum{i=1}^{n}\sum{j=1}^2\int\Omega(\phi_{i,j}(\psi(x))-xj)\nabla\phi{i,j}(\psi(x)) \cdot\zeta.$$ Using this, you should be able to implement this using the data term and its gradient you implemented for the registration. There the images would be $\phi_{i,j}$ and $x_j$. The C++ code uses the interpretation above, but reusing your existing registration should work as well and take less effort.

berkels commented 1 year ago

BTW: I'm very happy to see that you made a lot of progress on this!

berkels commented 1 year ago

FYI, the C++ code handles the bias correction in SeriesMatching::reduceDeformations in projects/electronMicroscopy/matchSeries.h. I don't think that though this is very helpful as reference.