From 1cad786b0b5e32f30696c177b2905e1c5f5c1030 Mon Sep 17 00:00:00 2001 From: Sumanth Ratna Date: Sat, 25 Apr 2020 22:33:41 -0400 Subject: [PATCH 1/5] Fix non_parametric and use time.perf_counter --- pathflow_mixmatch/cli.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/pathflow_mixmatch/cli.py b/pathflow_mixmatch/cli.py index 5fb30c2..f24ba43 100644 --- a/pathflow_mixmatch/cli.py +++ b/pathflow_mixmatch/cli.py @@ -87,7 +87,7 @@ def displace_image(img, displacement, gpu_device, dtype=th.float32): def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similarity', gpu_device=0, opt_cm=True, sigma=[[11,11],[11,11],[3,3]], order=2, pyramid=[[4,4],[2,2]], loss_fn='mse', use_mask=False, interpolation='bicubic'): assert use_mask==False, "Masking not implemented" assert transform_type in ['similarity', 'affine', 'rigid', 'non_parametric','bspline','wendland'] - start = time.time() + start = time.perf_counter() # set the used data type dtype = th.float32 @@ -142,8 +142,12 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari for level, (mov_im_level, fix_im_level) in enumerate(zip(moving_image_pyramid, fixed_image_pyramid)): # choose the affine transformation model - if transform_type in ['non_parametric','bspline','wendland']: + if transform_type == 'non_parametric': + transform_args[0]=mov_im_level[0].size + elif transform_type in ['bspline','wendland']: transform_args[0]=mov_im_level.size + # transform_args[0]=tuple(mov_im_level.size) + # transform_opts['sigma'] = (11, 11, 3) transformation = transforms[transform_type](*transform_args,**transform_opts) @@ -191,13 +195,14 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari displacement = transformation.get_displacement() warped_image = al.transformation.utils.warp_image(moving_image, displacement) - end = time.time() + end = time.perf_counter() print("=================================================================") print("Registration done in:", end - start, "s") - print("Result parameters:") - transformation.print() + if transform_type != 'non_parametric': + print("Result parameters:") + transformation.print() # only works with rigid? # plot the results plt.subplot(131) @@ -211,7 +216,18 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari plt.subplot(133) plt.imshow(warped_image.numpy(), cmap='gray') plt.title('Warped Moving Image') - return displacement, warped_image, transformation._phi_z, registration.loss.data.item() + + if transform_type == 'similarity' or transform_type == 'affine' or transform_type == 'rigid': + # TODO: check if transformation has parent class + # airlab.transformation.pairwise.RigidTransformation + transformation_param = transformation._phi_z + elif transform_type == 'non_parametric': + transformation_param = transformation.trans_parameters + elif transform_type == 'bspline' or transform_type == 'wendland': + transformation_param = transformation._kernel + else: + pass + return displacement, warped_image, transformation_param, registration.loss.data.item() def get_loss(im1,im2,gpu_device): From 649ee034a6b9682b41acbb57c77dd2051fb9f956 Mon Sep 17 00:00:00 2001 From: Sumanth Ratna Date: Sun, 26 Apr 2020 10:00:33 -0400 Subject: [PATCH 2/5] Fix wendland --- pathflow_mixmatch/cli.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pathflow_mixmatch/cli.py b/pathflow_mixmatch/cli.py index f24ba43..aba12cd 100644 --- a/pathflow_mixmatch/cli.py +++ b/pathflow_mixmatch/cli.py @@ -145,9 +145,7 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari if transform_type == 'non_parametric': transform_args[0]=mov_im_level[0].size elif transform_type in ['bspline','wendland']: - transform_args[0]=mov_im_level.size - # transform_args[0]=tuple(mov_im_level.size) - # transform_opts['sigma'] = (11, 11, 3) + transform_opts['sigma'] = (11, 11) transformation = transforms[transform_type](*transform_args,**transform_opts) @@ -200,9 +198,9 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari print("=================================================================") print("Registration done in:", end - start, "s") - if transform_type != 'non_parametric': + if transform_type in ['similarity', 'affine', 'rigid']: print("Result parameters:") - transformation.print() # only works with rigid? + transformation.print() # plot the results plt.subplot(131) @@ -217,9 +215,7 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari plt.imshow(warped_image.numpy(), cmap='gray') plt.title('Warped Moving Image') - if transform_type == 'similarity' or transform_type == 'affine' or transform_type == 'rigid': - # TODO: check if transformation has parent class - # airlab.transformation.pairwise.RigidTransformation + if transform_type in ['similarity', 'affine', 'rigid']: transformation_param = transformation._phi_z elif transform_type == 'non_parametric': transformation_param = transformation.trans_parameters From b8ad9c59bf97a89eb529f20720150e0e57a14574 Mon Sep 17 00:00:00 2001 From: Sumanth Ratna Date: Sun, 26 Apr 2020 17:06:34 -0400 Subject: [PATCH 3/5] Slightly reduce loss of microarchitectural details in wendland --- pathflow_mixmatch/cli.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pathflow_mixmatch/cli.py b/pathflow_mixmatch/cli.py index aba12cd..3e9114a 100644 --- a/pathflow_mixmatch/cli.py +++ b/pathflow_mixmatch/cli.py @@ -145,7 +145,10 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari if transform_type == 'non_parametric': transform_args[0]=mov_im_level[0].size elif transform_type in ['bspline','wendland']: - transform_opts['sigma'] = (11, 11) + # for wendland, sigma must be positive tuple of ints + # for wendland, smaller sigma tuple means less loss of + # microarchitectural details + transform_opts['sigma'] = (1, 1) transformation = transforms[transform_type](*transform_args,**transform_opts) From 82312f54ce1774cf350c3664f983f7786ff1b423 Mon Sep 17 00:00:00 2001 From: Sumanth Ratna Date: Fri, 1 May 2020 17:37:19 -0400 Subject: [PATCH 4/5] Comment out bspline constant_flow Constant flow raises an error about the interpolation method. --- pathflow_mixmatch/cli.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pathflow_mixmatch/cli.py b/pathflow_mixmatch/cli.py index 3e9114a..a508511 100644 --- a/pathflow_mixmatch/cli.py +++ b/pathflow_mixmatch/cli.py @@ -143,20 +143,22 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari # choose the affine transformation model if transform_type == 'non_parametric': - transform_args[0]=mov_im_level[0].size + transform_args[0]=mov_im_level[level].size elif transform_type in ['bspline','wendland']: - # for wendland, sigma must be positive tuple of ints - # for wendland, smaller sigma tuple means less loss of + # for bspline, sigma must be positive tuple of ints + # for bspline, smaller sigma tuple means less loss of # microarchitectural details + + # transform_opts['sigma'] = sigma[level] transform_opts['sigma'] = (1, 1) transformation = transforms[transform_type](*transform_args,**transform_opts) - if level > 0 and transform_type=='bspline': - constant_flow = al.transformation.utils.upsample_displacement(constant_flow, - mov_im_level.size, - interpolation=interpolation) - transformation.set_constant_flow(constant_flow) + # if level > 0 and transform_type=='bspline': + # constant_flow = al.transformation.utils.upsample_displacement(constant_flow, + # mov_im_level.size, + # interpolation=interpolation) + # transformation.set_constant_flow(constant_flow) if transform_type in ['similarity', 'affine', 'rigid']: # initialize the translation with the center of mass of the fixed image From 312a973109827998ae5d7b65d0f0aee995109609 Mon Sep 17 00:00:00 2001 From: Sumanth Ratna Date: Fri, 8 May 2020 17:58:06 -0400 Subject: [PATCH 5/5] Comment out B-spline constant flow for each iteration --- pathflow_mixmatch/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pathflow_mixmatch/cli.py b/pathflow_mixmatch/cli.py index a508511..7cddeb9 100644 --- a/pathflow_mixmatch/cli.py +++ b/pathflow_mixmatch/cli.py @@ -187,8 +187,8 @@ def affine_register(im1, im2, iterations=1000, lr=0.01, transform_type='similari # start the registration registration.start() - if transform_type == 'bspline': - constant_flow = transformation.get_flow() + # if transform_type == 'bspline': + # constant_flow = transformation.get_flow() # set the intensities back to the original for the visualisation fixed_image.image = 1 - fixed_image.image