def __init__( self, nRows, nCols, vol_preserve, base, nLevels, valid_outside=True, tess='tri', sigma_lm=1000 / 10, # worked pretty well (hands data) # sigma_lm = 1000/100, # Didn't work so well scale_spatial=1.0 * .1, scale_value=100, zero_v_across_bdry=[False] * 2, wlp=1e-4, scale_quiver=None): self.base = base self.nLevels = nLevels self.sigma_lm = sigma_lm self.wlp = wlp self.tw = TransformWrapper(nRows=nRows, nCols=nCols, vol_preserve=vol_preserve, nLevels=nLevels, base=base, scale_spatial=scale_spatial, scale_value=scale_value, tess=tess, valid_outside=valid_outside, zero_v_across_bdry=zero_v_across_bdry) if scale_quiver is None: raise ValueError self.scale_quiver = scale_quiver
def __init__(self,nRows=100, nCols=100, base = [2,2], nLevels=4, tess='tri', zero_v_across_bdry=[False]*2, # range_max_val = 1, # FOR NOW, KEEP IT. # nPtsDense=10000, scale_spatial=1.0 * 10, scale_value=2.0, sigma_signal=None, wlp=1e-4, ll_type=['gaussian','gaussian_on_distancetransform'][0], only_local=False, valid_outside=True): ll_type = ll_type.lower() if ll_type == 'gaussian': self.SDLL=SDLL_gaussian # elif ll_type =='gaussian_on_distancetransform': # self.SDLL=SDLL_gaussian_on_distancetransform # elif ll_type == 'vmf': # self.SDLL=SDLL_VMF else: raise ValueError(ll_type) # self.range_min_val = 0.0 # self.range_max_val = range_max_val self.base = base self.nLevels=nLevels if sigma_signal is None: raise ValueError("sigma_signal cannot be None") self.sigma_signal = sigma_signal self.wlp = wlp self.tw = TransformWrapper(nRows=nRows,nCols=nCols, nLevels=nLevels, base=base, tess=tess, # nPtsDense=nPtsDense, scale_spatial=scale_spatial, scale_value=scale_value, zero_v_across_bdry=zero_v_across_bdry, only_local=only_local, valid_outside=valid_outside )
def example(img=None,tess='I',eval_cell_idx=True,eval_v=True,show_downsampled_pts=True, valid_outside=True,base=[1,1], scale_spatial=.1, scale_value=100, permute_cell_idx_for_display=True, nLevels=3, vol_preserve=False, zero_v_across_bdry=[0,0], use_lims_when_plotting=True): show_downsampled_pts = bool(show_downsampled_pts) eval_cell_idx = bool(eval_cell_idx) eval_v = bool(eval_cell_idx) valid_outside = bool(valid_outside) permute_cell_idx_for_display = bool(permute_cell_idx_for_display) vol_preserve = bool(vol_preserve) if img is None: img = Img(get_std_test_img()) else: img=Img(img) img = img[:,:,::-1] # bgr2rgb tw = TransformWrapper(nRows=img.shape[0], nCols=img.shape[1], nLevels=nLevels, base=base, scale_spatial=scale_spatial, # controls the prior's smoothness scale_value=scale_value, # controls the prior's variance tess=tess, vol_preserve=vol_preserve, zero_v_across_bdry=zero_v_across_bdry, valid_outside=valid_outside) print tw # You probably want to do that: padding image border with zeros border_width=1 img[:border_width]=0 img[-border_width:]=0 img[:,:border_width]=0 img[:,-border_width:]=0 # The tw.calc_T_fwd (or tw.calc_T_inv) is always done in gpu. # After using it to compute new pts, # you may want to use remap (to warp an image accordingly). # If you will use tw.remap_fwd (or tw.remap_inv), which is done in gpu, # then the image type can be either float32 or float64. # But if you plan to use tw.tw.remap_fwd_opencv (or tw.remap_inv_opencv), # which is done in cpu (hence slightly lower) but supports better # interpolation methods, then the image type must be np.float32. # img_original = CpuGpuArray(img.copy().astype(np.float32)) img_original = CpuGpuArray(img.copy().astype(np.float64)) img_wrapped_fwd= CpuGpuArray.zeros_like(img_original) img_wrapped_bwd= CpuGpuArray.zeros_like(img_original) seed=0 np.random.seed(seed) ms_Avees=tw.get_zeros_PA_all_levels() ms_theta=tw.get_zeros_theta_all_levels() for level in range(tw.ms.nLevels): if level==0: tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=None)# zero mean else: tw.sample_from_the_ms_prior_coarse2fine_one_level(ms_Avees,ms_theta, level_fine=level) print('\nimg shape: {}\n'.format(img_original.shape)) # You don't have use these. You can use any 2d array # that has two columns (regardless of the number of rows). pts_src = tw.pts_src_dense # Create buffers for the output pts_fwd = CpuGpuArray.zeros_like(pts_src) pts_inv = CpuGpuArray.zeros_like(pts_src) for level in range(tw.ms.nLevels): ####################################################################### # instead of the tw.sample_from_the_ms_prior() above, # you may want to use one of the following. # 1) # tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=None)# zero mean # 2) # tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=some_user_specified_mu) # The following should be used only for level>0 : # 3) # tw.sample_normal_in_one_level_using_the_coarser_as_mean(Avees_coarse=ms_Avees[level-1], # Avees_fine=ms_Avees[level], # theta_fine=ms_theta[level], # level_fine=level) # ####################################################################### # You can also change the values this way: # cpa_space = tw.ms.L_cpa_space[level] # theta = cpa_space.get_zeros_theta() # theta[:] = some values # Avees = cpa_space.get_zeros_PA() # cpa_space.theta2Avees(theta,Avees) # cpa_space.update_pat(Avees) # This step is important and must be done # before are trying to "use" the new values of # the (vectorized) A's. tw.update_pat_from_Avees(ms_Avees[level],level) if eval_v: # Evaluating the velocity field. # You don't have to do it in unless you want to visualize v. # (when evaluting the treansformation, v will be internally # evaluated anyway -- but its result won't be stored) tw.calc_v(level=level) # optional, if you want to time it timer_gpu_T_fwd = GpuTimer() # Simply calling # tic = time.clock() # and then # tic = time.clock() # won't work. # In fact, most likely you will get that toc-tic is zero. # You need to use the GpuTimer object. When you do that, # one side effect is that suddenly the toc-tic from above will # give you a more realistic result. tic = time.clock() timer_gpu_T_fwd.tic() tw.calc_T_fwd(pts_src,pts_fwd,level=level) timer_gpu_T_fwd.toc() toc = time.clock() print 'Time, in sec, for computing T_fwd:' print timer_gpu_T_fwd.secs print toc-tic # likely to be 0, unless you also used the GpuTimer. # You can also time the inv of course. Results will be similar. tw.calc_T_inv(pts_src,pts_inv,level=level) if eval_cell_idx: # cell_idx is computed here just for display. cell_idx = CpuGpuArray.zeros(len(pts_src),dtype=np.int32) tw.calc_cell_idx(pts_src,cell_idx,level, permute_for_disp=permute_cell_idx_for_display) # If may also want ro to time the remap. # However, the remap is usually very fast (e.g, about 2 milisec). # timer_gpu_remap_fwd = GpuTimer() # tic = time.clock() # timer_gpu_remap_fwd.tic() # tw.remap_fwd(pts_inv=pts_inv,img=img_original,img_wrapped_fwd=img_wrapped_fwd) tw.remap_fwd(pts_inv=pts_inv,img=img_original,img_wrapped_fwd=img_wrapped_fwd) # timer_gpu_remap_fwd.toc() # toc = time.clock() # If the img type is np.float32, you may also use # tw.remap_fwd_opencv instead of tw.remap_fw. The differences between # the two methods are explained above tw.remap_inv(pts_fwd=pts_fwd,img=img_original,img_wrapped_inv=img_wrapped_bwd) # For display purposes, do gpu2cpu transfer print ("For display purposes, do gpu2cpu transfer") if eval_cell_idx: cell_idx.gpu2cpu() if eval_v: tw.v_dense.gpu2cpu() pts_fwd.gpu2cpu() pts_inv.gpu2cpu() img_wrapped_fwd.gpu2cpu() img_wrapped_bwd.gpu2cpu() figsize = (12,12) plt.figure(figsize=figsize) if eval_v: plt.subplot(332) tw.imshow_vx() plt.title('vx') plt.subplot(333) tw.imshow_vy() plt.title('vy') if eval_cell_idx: plt.subplot(331) cell_idx_disp = cell_idx.cpu.reshape(img.shape[0],-1) plt.imshow(cell_idx_disp) plt.title('tess (type {})'.format(tess)) if show_downsampled_pts: ds=20 pts_src_grid = pts_src.cpu.reshape(tw.nRows,-1,2) pts_src_ds=pts_src_grid[::ds,::ds].reshape(-1,2) pts_fwd_grid = pts_fwd.cpu.reshape(tw.nRows,-1,2) pts_fwd_ds=pts_fwd_grid[::ds,::ds].reshape(-1,2) pts_inv_grid = pts_inv.cpu.reshape(tw.nRows,-1,2) pts_inv_ds=pts_inv_grid[::ds,::ds].reshape(-1,2) use_lims=use_lims_when_plotting # return tw plt.subplot(334) plt.plot(pts_src_ds[:,0],pts_src_ds[:,1],'r.') plt.title('pts ds') tw.config_plt() plt.subplot(335) plt.plot(pts_fwd_ds[:,0],pts_fwd_ds[:,1],'g.') plt.title('fwd(pts)') tw.config_plt(axis_on_or_off='on',use_lims=use_lims) plt.subplot(336) plt.plot(pts_inv_ds[:,0],pts_inv_ds[:,1],'b.') plt.title('inv(pts)') tw.config_plt(axis_on_or_off='on',use_lims=use_lims) plt.subplot(337) plt.imshow(img_original.cpu.astype(np.uint8)) plt.title('img') # plt.axis('off') plt.subplot(338) plt.imshow(img_wrapped_fwd.cpu.astype(np.uint8)) # plt.axis('off') plt.title('fwd(img)') plt.subplot(339) plt.imshow(img_wrapped_bwd.cpu.astype(np.uint8)) # plt.axis('off') plt.title('inv(img)') return tw
class Register(object): def __init__(self,nRows=100, nCols=100, base = [2,2], nLevels=4, tess='tri', zero_v_across_bdry=[False]*2, # range_max_val = 1, # FOR NOW, KEEP IT. # nPtsDense=10000, scale_spatial=1.0 * 10, scale_value=2.0, sigma_signal=None, wlp=1e-4, ll_type=['gaussian','gaussian_on_distancetransform'][0], only_local=False, valid_outside=True): ll_type = ll_type.lower() if ll_type == 'gaussian': self.SDLL=SDLL_gaussian # elif ll_type =='gaussian_on_distancetransform': # self.SDLL=SDLL_gaussian_on_distancetransform # elif ll_type == 'vmf': # self.SDLL=SDLL_VMF else: raise ValueError(ll_type) # self.range_min_val = 0.0 # self.range_max_val = range_max_val self.base = base self.nLevels=nLevels if sigma_signal is None: raise ValueError("sigma_signal cannot be None") self.sigma_signal = sigma_signal self.wlp = wlp self.tw = TransformWrapper(nRows=nRows,nCols=nCols, nLevels=nLevels, base=base, tess=tess, # nPtsDense=nPtsDense, scale_spatial=scale_spatial, scale_value=scale_value, zero_v_across_bdry=zero_v_across_bdry, only_local=only_local, valid_outside=valid_outside ) # 500,500,500,50000,5000 def set_dense(self,domain_start=-10,domain_end=10): """ Remarks: 1) The range of the domain has already been determined in self.tw 2) For now, the src is always the "uniform cdf" """ self.src_dense = self.tw.pts_src_dense self.transformed_dense = self.tw.transformed_dense def set_data(self,x,signal_src,signal_dst,isbinary): """ For now, assumes dst was evaluated on evenly-space points // Is the comment above still current? """ self.isbinary=isbinary nPts = len(x) if x.ndim !=2 or x.shape[1]!=2: raise ValueError(x.shape) if signal_src.shape != signal_dst.shape: raise ValueError(gnal_src.shape , signal_dst.shape) if signal_src.ndim !=2: # Want a single channel raise ValueError(signal_src.shape) # signal_src = signal_src.reshape(nPts,1).astype(np.float64) # signal_dst = signal_dst.reshape(nPts,1).astype(np.float64) signal_src = signal_src.astype(np.float64) signal_dst = signal_dst.astype(np.float64) # if nPts != signal_src.shape[0]: # raise ValueError( nPts , signal_src.shape) # if x.shape[0] != signal_dst.shape[0]: # raise ValueError( nPts , signal_dst.shape) if nPts != signal_src.size: raise ValueError( nPts , signal_src.shape) if x.shape[0] != signal_dst.size: raise ValueError( nPts , signal_dst.shape) if x.dtype != np.float64: raise TypeError(x.dtype) if signal_src.dtype != np.float64: raise TypeError(signal_src.dtype) if signal_dst.dtype != np.float64: raise TypeError(signal_dst.dtype) if signal_src.ndim == 1: raise ValueError(signal_src.ndim) if signal_dst.ndim == 1: raise ValueError(signal_dst.ndim) if not isinstance(signal_src,CpuGpuArray): signal_src = CpuGpuArray(signal_src) if not isinstance(signal_dst,CpuGpuArray): signal_dst = CpuGpuArray(signal_dst) self.signal = Bunch() self.signal.src=signal_src self.signal.dst=signal_dst self.src = x self.transformed = CpuGpuArray.zeros_like(self.src) self.signal.transformed=CpuGpuArray.zeros_like(signal_src) def set_run_lengths(self,run_lengths): if len(run_lengths)!=self.nLevels: raise ValueError(len(run_lengths),self.nLevels) self.run_lengths = run_lengths def fit(self,use_prior=False,proposal_scale=0.001,use_local=True,dispOn=True, interp_type_for_ll=None, interp_type_during_visualization=None, scale_local_proposal=None): nLevels=self.nLevels tw = self.tw sigma_signal = self.sigma_signal self.use_local = use_local inference_record = Bunch() inference_record.tw_args = tw.args inference_record.steps = [] inference_record.use_prior = use_prior inference_record.proposal_scale= proposal_scale inference_record.sigma_signal = sigma_signal try: run_lengths = self.run_lengths except AttributeError: self.set_run_lengths([500]*self.nLevels) run_lengths = self.run_lengths # raise Exception("self.set_run_lengths was not called yet") wlp=self.wlp for i in range(nLevels): # for i in range(nLevels+5): if i<nLevels: level=i if level == 0: theta = tw.ms.L_cpa_space[level].get_zeros_theta() else: theta_fine = tw.ms.L_cpa_space[level].get_zeros_theta() tw.ms.propogate_theta_coarse2fine(theta_coarse=theta,theta_fine=theta_fine) theta = theta_fine _sigma_signal = sigma_signal else: _sigma_signal *= 0.9 print '-'*10 ,'level',level, '-'*10 cpa_space = tw.ms.L_cpa_space[level] print cpa_space data = {'src':self.src,'transformed':self.transformed, 'signal':self.signal} if use_prior: lp_func = LP(ms=tw.ms,msp=tw.msp,level=level,SDLP=SDLP, required={}) else: lp_func = None sampler = Metropolis(ll_func= LL( ms=tw.ms,level=level,SDLL=self.SDLL, data=data, required={'sigma_signal':_sigma_signal, 'params_flow_int':tw.params_flow_int_coarse, 'interp_type_for_ll':interp_type_for_ll} ), proposal=Proposal(ms=tw.ms,msp=tw.msp,level=level, scale=proposal_scale, use_local=use_local, scale_local_proposal=scale_local_proposal), # proposal=ProposalSphericalGaussian(ms=tw.ms, # level=level, # scale=0.1 / (1.2**level) # scale=0.01, # scale=0.1 / (1.1**level) # scale=0.1 # ) , lp_func=lp_func, wlp=wlp ) sampler.set_theta(theta) run_length = run_lengths[level] sampler.run(run_length) theta = sampler.theta_current # prepare for next iteration inference_record.steps.append(sampler.get_record()) if dispOn: if i >= nLevels-1 or 1: plt.figure(i+1) plt.clf() self.disp(sampler=sampler, interp_type_during_visualization=interp_type_during_visualization) inference_record.theta = theta.copy() steps = inference_record.steps nAccepted = [ step.nAccepted for step in steps] run_lengths = [ step.N for step in steps] times = [ step.runs[0].time for step in steps] total_time = sum(times) print "run_lengths:",run_lengths print "nAccepted:",nAccepted print 'times:' print_iterable(times) print 'time total:',total_time return theta.copy(),inference_record def plot_src(self,*args,**kwargs): x = self.x plt.plot(x,self.src.cpu,*args,**kwargs) # def plot_dst(self,*args,**kwargs): # x = self.x # plt.plot(x,self.dst.cpu,*args,**kwargs) def __repr__(self): s = 'Register:' s += '\n'+ repr(self.tw.ms) return s @staticmethod def plot_inference_summary(inference_record): ll = [] lp = [] wlp_plus_ll=[] for step in inference_record.steps: ll += step.ll[1:] # start from 1 and not 0: to skip the initial guess try: lp += step.lp[1:] wlp_plus_ll += list((step.wlp * np.asarray(step.lp[1:]) + np.asarray(step.ll[1:])).tolist()) except AttributeError: pass plt.title('ll',fontsize=30) plt.plot(ll,lw=2) plt.plot(lp,lw=2) plt.plot(wlp_plus_ll,lw=2) counter = 0 for i,step in enumerate(inference_record.steps): if i%2==1: facecolor = ".2" else: facecolor = ".5" plt.axvspan(counter, counter+step.nAccepted, facecolor=facecolor, alpha=0.2) counter += step.nAccepted def disp(self,sampler,interp_type_during_visualization): level=sampler.level theta=sampler.theta_current tw=self.tw # interval=self.interval # interval_dense=self.interval_dense markersize = 5 fontsize=30 cpa_space = tw.ms.L_cpa_space[level] plt.subplot(231) sampler.plot_ll() plt.title('ll',fontsize=fontsize) sampler.plot_wlp() sampler.plot_wlp_plus_ll() if sampler.lp_func: plt.legend(['ll','wlp','ll+wlp']) plt.subplot(232) sampler.plot_ar() plt.title('accept ratio',fontsize=fontsize) # print theta cpa_space.theta2As(theta=theta) tw.update_pat_from_Avees(level=level) tw.calc_v(level=level) tw.v_dense.gpu2cpu() src = self.src # dst = self.dst transformed = self.transformed # src_dense=self.src_dense # transformed_dense=self.transformed_dense # tw.calc_T(src_dense, transformed_dense, mysign=1, level=level, # # transformed_dense.gpu2cpu() tw.calc_T_inv(src, transformed, level=level, int_quality=+1) transformed.gpu2cpu() if interp_type_during_visualization=='gpu_linear': my_dtype = np.float64 else: my_dtype = np.float32 # For opencv img_src = self.signal.src.cpu.reshape(tw.nRows,tw.nCols) img_src = CpuGpuArray(img_src.astype(my_dtype)) img_wrapped = CpuGpuArray.zeros_like(img_src) img_dst = self.signal.dst.cpu.reshape(tw.nRows,tw.nCols) img_dst = CpuGpuArray(img_dst) if interp_type_during_visualization=='gpu_linear': tw.remap_fwd(transformed,img_src,img_wrapped) else: tw.remap_fwd_opencv(transformed,img_src,img_wrapped,interp_type_during_visualization) img_wrapped.gpu2cpu() plt.subplot(233) plt.imshow(img_src.cpu,interpolation="None") plt.gray() cpa_space.plot_cells('r') tw.config_plt(axis_on_or_off='on') plt.title(r'$I_{\mathrm{src}}$') plt.subplot(234) plt.imshow(img_wrapped.cpu,interpolation="None") plt.gray() # cpa_space.plot_cells('w') tw.config_plt(axis_on_or_off='on') plt.title(r'$I_{\mathrm{src}}\circ T^{\theta}$') plt.subplot(235) plt.imshow(img_dst.cpu,interpolation="None") plt.gray() plt.title(r'$I_{\mathrm{dst}}$') # cpa_space.plot_cells('w') tw.config_plt(axis_on_or_off='on') plt.subplot(2,6,11) self.tw.imshow_vx() pylab.jet() tw.config_plt(axis_on_or_off='on') plt.title(r'$v_x$') plt.subplot(2,6,12) self.tw.imshow_vy() pylab.jet() tw.config_plt(axis_on_or_off='on') plt.title(r'$v_y$')
pylab.ion() name = 'LFW_5_to_6' data = get_data(name) src = CpuGpuArray(data.src) dst = CpuGpuArray(data.dst) transformed = CpuGpuArray.zeros_like(src) fname_results = os.path.splitext(data.fname)[0]+'_result.pkl' FilesDirs.raise_if_dir_does_not_exist(os.path.dirname(fname_results)) print 'Loading',fname_results results=Pkl.load(fname_results) theta_est = results.theta tw = TransformWrapper(**results.tw_args) tw.create_grid_lines(step=0.1,factor=0.5) scale_quiver=1000 # The *smaller* this value is, the larger the plotted arrows will be. level=-1 # pick the finest scale cpa_space = tw.ms.L_cpa_space[level] cpa_space.theta2Avees(theta_est) cpa_space.update_pat() tw.calc_T_fwd(src,transformed,level=level) transformed.gpu2cpu() tw.calc_v(level=level) tw.v_dense.gpu2cpu() plt.close('all') disp(tw=tw,theta=theta_est,src=src,dst=dst,transformed=transformed,level=level,
if not inside_spyder(): pylab.ion() name = 'LFW_5_to_6' data = get_data(name) src = CpuGpuArray(data.src) dst = CpuGpuArray(data.dst) transformed = CpuGpuArray.zeros_like(src) fname_results = os.path.splitext(data.fname)[0] + '_result.pkl' FilesDirs.raise_if_dir_does_not_exist(os.path.dirname(fname_results)) print 'Loading', fname_results results = Pkl.load(fname_results) theta_est = results.theta tw = TransformWrapper(**results.tw_args) tw.create_grid_lines(step=0.1, factor=0.5) scale_quiver = 1000 # The *smaller* this value is, the larger the plotted arrows will be. level = -1 # pick the finest scale cpa_space = tw.ms.L_cpa_space[level] cpa_space.theta2Avees(theta_est) cpa_space.update_pat() tw.calc_T_fwd(src, transformed, level=level) transformed.gpu2cpu() tw.calc_v(level=level) tw.v_dense.gpu2cpu() plt.close('all') disp(tw=tw,
def example(img=None, tess='I', eval_cell_idx=True, eval_v=True, show_downsampled_pts=True, valid_outside=True, base=[1, 1], scale_spatial=.1, scale_value=100, permute_cell_idx_for_display=True, nLevels=3, vol_preserve=False, zero_v_across_bdry=[0, 0], use_lims_when_plotting=True): show_downsampled_pts = bool(show_downsampled_pts) eval_cell_idx = bool(eval_cell_idx) eval_v = bool(eval_cell_idx) valid_outside = bool(valid_outside) permute_cell_idx_for_display = bool(permute_cell_idx_for_display) vol_preserve = bool(vol_preserve) if img is None: img = Img(get_std_test_img()) else: img = Img(img) img = img[:, :, ::-1] # bgr2rgb tw = TransformWrapper( nRows=img.shape[0], nCols=img.shape[1], nLevels=nLevels, base=base, scale_spatial=scale_spatial, # controls the prior's smoothness scale_value=scale_value, # controls the prior's variance tess=tess, vol_preserve=vol_preserve, zero_v_across_bdry=zero_v_across_bdry, valid_outside=valid_outside) print tw # You probably want to do that: padding image border with zeros border_width = 1 img[:border_width] = 0 img[-border_width:] = 0 img[:, :border_width] = 0 img[:, -border_width:] = 0 # The tw.calc_T_fwd (or tw.calc_T_inv) is always done in gpu. # After using it to compute new pts, # you may want to use remap (to warp an image accordingly). # If you will use tw.remap_fwd (or tw.remap_inv), which is done in gpu, # then the image type can be either float32 or float64. # But if you plan to use tw.tw.remap_fwd_opencv (or tw.remap_inv_opencv), # which is done in cpu (hence slightly lower) but supports better # interpolation methods, then the image type must be np.float32. # img_original = CpuGpuArray(img.copy().astype(np.float32)) img_original = CpuGpuArray(img.copy().astype(np.float64)) img_wrapped_fwd = CpuGpuArray.zeros_like(img_original) img_wrapped_bwd = CpuGpuArray.zeros_like(img_original) seed = 0 np.random.seed(seed) ms_Avees = tw.get_zeros_PA_all_levels() ms_theta = tw.get_zeros_theta_all_levels() for level in range(tw.ms.nLevels): if level == 0: tw.sample_gaussian(level, ms_Avees[level], ms_theta[level], mu=None) # zero mean else: tw.sample_from_the_ms_prior_coarse2fine_one_level(ms_Avees, ms_theta, level_fine=level) print('\nimg shape: {}\n'.format(img_original.shape)) # You don't have use these. You can use any 2d array # that has two columns (regardless of the number of rows). pts_src = tw.pts_src_dense # Create buffers for the output pts_fwd = CpuGpuArray.zeros_like(pts_src) pts_inv = CpuGpuArray.zeros_like(pts_src) for level in range(tw.ms.nLevels): ####################################################################### # instead of the tw.sample_from_the_ms_prior() above, # you may want to use one of the following. # 1) # tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=None)# zero mean # 2) # tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=some_user_specified_mu) # The following should be used only for level>0 : # 3) # tw.sample_normal_in_one_level_using_the_coarser_as_mean(Avees_coarse=ms_Avees[level-1], # Avees_fine=ms_Avees[level], # theta_fine=ms_theta[level], # level_fine=level) # ####################################################################### # You can also change the values this way: # cpa_space = tw.ms.L_cpa_space[level] # theta = cpa_space.get_zeros_theta() # theta[:] = some values # Avees = cpa_space.get_zeros_PA() # cpa_space.theta2Avees(theta,Avees) # cpa_space.update_pat(Avees) # This step is important and must be done # before are trying to "use" the new values of # the (vectorized) A's. tw.update_pat_from_Avees(ms_Avees[level], level) if eval_v: # Evaluating the velocity field. # You don't have to do it in unless you want to visualize v. # (when evaluting the treansformation, v will be internally # evaluated anyway -- but its result won't be stored) tw.calc_v(level=level) # optional, if you want to time it timer_gpu_T_fwd = GpuTimer() # Simply calling # tic = time.clock() # and then # tic = time.clock() # won't work. # In fact, most likely you will get that toc-tic is zero. # You need to use the GpuTimer object. When you do that, # one side effect is that suddenly the toc-tic from above will # give you a more realistic result. tic = time.clock() timer_gpu_T_fwd.tic() tw.calc_T_fwd(pts_src, pts_fwd, level=level) timer_gpu_T_fwd.toc() toc = time.clock() print 'Time, in sec, for computing T_fwd:' print timer_gpu_T_fwd.secs print toc - tic # likely to be 0, unless you also used the GpuTimer. # You can also time the inv of course. Results will be similar. tw.calc_T_inv(pts_src, pts_inv, level=level) if eval_cell_idx: # cell_idx is computed here just for display. cell_idx = CpuGpuArray.zeros(len(pts_src), dtype=np.int32) tw.calc_cell_idx(pts_src, cell_idx, level, permute_for_disp=permute_cell_idx_for_display) # If may also want ro to time the remap. # However, the remap is usually very fast (e.g, about 2 milisec). # timer_gpu_remap_fwd = GpuTimer() # tic = time.clock() # timer_gpu_remap_fwd.tic() # tw.remap_fwd(pts_inv=pts_inv,img=img_original,img_wrapped_fwd=img_wrapped_fwd) tw.remap_fwd(pts_inv=pts_inv, img=img_original, img_wrapped_fwd=img_wrapped_fwd) # timer_gpu_remap_fwd.toc() # toc = time.clock() # If the img type is np.float32, you may also use # tw.remap_fwd_opencv instead of tw.remap_fw. The differences between # the two methods are explained above tw.remap_inv(pts_fwd=pts_fwd, img=img_original, img_wrapped_inv=img_wrapped_bwd) # For display purposes, do gpu2cpu transfer print("For display purposes, do gpu2cpu transfer") if eval_cell_idx: cell_idx.gpu2cpu() if eval_v: tw.v_dense.gpu2cpu() pts_fwd.gpu2cpu() pts_inv.gpu2cpu() img_wrapped_fwd.gpu2cpu() img_wrapped_bwd.gpu2cpu() figsize = (12, 12) plt.figure(figsize=figsize) if eval_v: plt.subplot(332) tw.imshow_vx() plt.title('vx') plt.subplot(333) tw.imshow_vy() plt.title('vy') if eval_cell_idx: plt.subplot(331) cell_idx_disp = cell_idx.cpu.reshape(img.shape[0], -1) plt.imshow(cell_idx_disp) plt.title('tess (type {})'.format(tess)) if show_downsampled_pts: ds = 20 pts_src_grid = pts_src.cpu.reshape(tw.nRows, -1, 2) pts_src_ds = pts_src_grid[::ds, ::ds].reshape(-1, 2) pts_fwd_grid = pts_fwd.cpu.reshape(tw.nRows, -1, 2) pts_fwd_ds = pts_fwd_grid[::ds, ::ds].reshape(-1, 2) pts_inv_grid = pts_inv.cpu.reshape(tw.nRows, -1, 2) pts_inv_ds = pts_inv_grid[::ds, ::ds].reshape(-1, 2) use_lims = use_lims_when_plotting # return tw plt.subplot(334) plt.plot(pts_src_ds[:, 0], pts_src_ds[:, 1], 'r.') plt.title('pts ds') tw.config_plt() plt.subplot(335) plt.plot(pts_fwd_ds[:, 0], pts_fwd_ds[:, 1], 'g.') plt.title('fwd(pts)') tw.config_plt(axis_on_or_off='on', use_lims=use_lims) plt.subplot(336) plt.plot(pts_inv_ds[:, 0], pts_inv_ds[:, 1], 'b.') plt.title('inv(pts)') tw.config_plt(axis_on_or_off='on', use_lims=use_lims) plt.subplot(337) plt.imshow(img_original.cpu.astype(np.uint8)) plt.title('img') # plt.axis('off') plt.subplot(338) plt.imshow(img_wrapped_fwd.cpu.astype(np.uint8)) # plt.axis('off') plt.title('fwd(img)') plt.subplot(339) plt.imshow(img_wrapped_bwd.cpu.astype(np.uint8)) # plt.axis('off') plt.title('inv(img)') return tw