Coder Social home page Coder Social logo

gattia / cycpd Goto Github PK

View Code? Open in Web Editor NEW
24.0 24.0 7.0 55.19 MB

Cython implementation of Coherent Point Drift (CPD)

License: MIT License

Python 79.95% C 0.88% Cython 17.48% Makefile 1.69%
cpd cython image-processing medical-imaging point-cloud python registration

cycpd's People

Contributors

gattia avatar walbukhanajer avatar wedesoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cycpd's Issues

Fix numpy dependency

Can't remember why specific numpy version was pinned. Should be able to fix this to be any numpy version... Currently limiting to max python 3.10 becuase 3.11 does not have the appropriate version of numpy released.

5647c9b

Update verbosity

change verbosity / print_reg_params so that verbose takes multiple levels, higher levels will print the reg_params

Issue when using relative imports

Hi,

I'm not quite sure how to go about debugging this - but I wonder if the imports within the library need to be changed. When using the library via another module (say a relatively simply wrapper like this:)

from cycpd import affine_registration

def affine_cpd(X, Y, max_iterations=100, init_only=False):
    reg = affine_registration(**{'X': X, 'Y': Y, 'max_iterations': max_iterations, 'print_reg_params': False, 'verbose': False})
    # B is the rotation matrix, t is the translation vector
    if not init_only:
        B, t = reg.register()

    return reg

...this error occurs:

File "cycpd\cython\cython_functions.pyx", line 209, in cython_functions.__pyx_fused_cpdef
TypeError: No matching signature found

Is there anything obviously wrong here? Library installed via: pip install cycpd

Tackle "ValueError: buffer source array is read-only"

Hi. Thank you for your implementation using Cython.
I am using your code for pointcloud registration where multiple pointclouds should be registered, so I use ray to parallelize my code.
I encountered a small issue but I found the solution here: https://github.com/dask/distributed/issues/1978

So I add this function above the definition of class "expectation_maximization_registration";

def _memoryview_safe(x):
    """Make array safe to run in a Cython memoryview-based kernel. These
    kernels typically break down with the error ``ValueError: buffer source
    array is read-only`` when running in dask distributed.
    """
    if not x.flags.writeable:
        if not x.flags.owndata:
            x = x.copy(order='C')
        x.setflags(write=True)
    return x

and modify the functions "register" and "expectation" in expectation_maximization_registration.py

def register(self, callback=lambda **kwargs: None):
    tic = time.time()
    self.transform_point_cloud()
    self.X = _memoryview_safe(self.X)
    self.Y = _memoryview_safe(self.Y)
    self.sigma2 = cy.initialize_sigma2(self.X, self.Y)
    ......
def expectation(self):
    self.X = _memoryview_safe(self.X)
    self.TY = _memoryview_safe(self.TY)
    self.P1, self.Pt1, self.PX, self.Np, self.E = cy.expectation_2(self.X,
                                                                   self.TY,
                                                                   self.sigma2,
                                                                   self.M,
                                                                   self.N,
                                                                   self.D,
                                                                   self.w)

Another small issue is that I have to convert my pointcloud numpy array (np.float64) into array of dtype np.double to avoid the TypeError: No matching signature found when using ray to parallelize the code, but it works well without using ray. I am not sure maybe that's an issue of ray since I am not familiar with Cython.

probability matrix P is missing in function expectation_2

Hi. I find that you do not return the probability matrix P (shape=(M,N)) when you calculate the expectation using funtion expectation_2 in your cython code.

Does it slow down the entire registration?
Also I have to analyze that matrix in my following step. Could you please help me check whether my modifcation is coorect? It seems right during my execution.

@cython.boundscheck(False)  # Deactivate bounds checking
@cython.wraparound(False)   # Deactivate negative indexing.
@cython.cdivision(True) # Do division using C?
def expectation_3(my_type[:,:] X, my_type[:,:] TY, my_type sigma2, int M, int N, int D, double w):
    # TY is MxD (same as Y - it is just transformed Y). M = rows of points Y, and D is dimensions.
    # tic = time.time()
    # cdef Py_ssize_t x_i_shape, x_j_shape, y_i_shape, y_j_shape
    if my_type is double:
        dtype = np.double

    cdef Py_ssize_t n, m, d  # i, j, k

    cdef double ksig, diff, w_tmp, den, tmp_total, Np, E # den = sp in original matlab, tmp_total = razn
    # cdef double [:] temp_x
    temp_x = np.zeros(D, dtype=np.double)
    cdef double [:] temp_x_view = temp_x

    ksig = -2.0 * sigma2
    w_tmp = (w * M * c_pow(-ksig * 3.14159265358979,0.5*D))/((1-w)*N) # c in the original manuscript

    cdef my_type[:, :] X_view = X
    cdef my_type[:, :] TY_view = TY
	
    probP = np.zeros((M, N), dtype=dtype)
    cdef my_type[:,:] probP_view = probP

     # Make P array
    # cdef my_type[:] P
    P = np.zeros(M, dtype=dtype)
    cdef my_type[:] P_view = P

    # Make P1 array
    # cdef my_type[:] P1
    P1= np.zeros(M, dtype=dtype)
    cdef my_type[:] P1_view = P1

    # Make Pt1 array
    # cdef my_type[:] Pt1
    Pt1= np.zeros(N, dtype=dtype)
    cdef my_type[:] Pt1_view = Pt1

    # Make Pt1 array
    # cdef my_type[:, :] Px
    Px= np.zeros((M, D), dtype=dtype)
    cdef my_type[:, :] Px_view = Px


    for n in range(N):
        den = 0
        for m in range(M):
            tmp_total = 0
            for d in range(D):
                diff = X_view[n, d] - TY_view[m, d]
                diff = diff * diff
                tmp_total += diff
            P_view[m] = c_exp(tmp_total/ksig)
            probP_view[m, n] = c_exp(tmp_total/ksig)
            den += P_view[m]
        den += w_tmp
        for m in range(M):
            probP_view[m, n] = probP_view[m, n] / den
        Pt1_view[n] = 1-w_tmp/den

        for d in range(D):
            temp_x_view[d] = X_view[n, d] / den

        for m in range(M):
            P1_view[m] += P_view[m] / den

            for d in range(D):
                Px_view[m, d] += temp_x_view[d] * P_view[m]
        E += -c_log(den)

    Np = 0
    for m in range(M):
        Np += P1_view[m]

    E += D * Np * c_log(sigma2)/2

    return probP, P1, Pt1, Px, Np, E

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.