Пример #1
0
def get_init_seg(dimy, dimx, nPixels_in_square_side, use_hex):
    """
    """
    M = nPixels_in_square_side

    if use_hex:
        s = create_string(dimx, dimy, nPixels_in_square_side)
        #        print(s)
        fname = os.path.join(dirname_precomputed_hex_inits, s)
        try:
            FilesDirs.raise_if_file_does_not_exist(fname)
            print("Loading", fname)
            seg = np.load(fname)

            return seg
        except FileDoesNotExistError:
            pass
        msg = """
        I could not find a precomputed (image-independent)
        honeycomb initilization for this image size and this values of n.     
        So I will compute it from scratch and we will save the result in
        {}
        Next time you will run the code for an image of size
        nRows={}, nCols={}, with n = {},
        it will be faster.
        """.format(fname, dimy, dimx, nPixels_in_square_side)
        print(msg)
        seg = CpuGpuArray.zeros((dimy, dimx), dtype=np.int32)

        # length of each side
        a = np.sqrt(M**2 / (1.5 * np.sqrt(3)))
        H = a
        W = np.sqrt(3) * H
        # XX and YY need to be float
        YY, XX = np.mgrid[0:float(dimy) + 0 * 1.5 * H:1.5 * H,
                          0:float(dimx) + 0 * W:W]

        XX[::2] += float(W) / 2
        centers = np.vstack([XX.ravel(), YY.ravel()]).T.copy()
        centers = CpuGpuArray(centers)

        honeycomb(seg.gpu, centers.gpu, seg.size)

        seg.gpu2cpu()
        np.save(fname, seg.cpu)
        return seg.cpu

    else:
        seg_cpu = np.zeros((dimy, dimx), dtype=np.int32)
        yy, xx = np.mgrid[:dimy, :dimx]
        xx = xx.astype(np.float)
        yy = yy.astype(np.float)

        dimx = float(dimx)
        dimy = float(dimy)
        nTimesInX = np.floor(xx / M).max() + 1

        seg_cpu = np.floor(yy / M) * nTimesInX + np.floor(xx / M)
        seg_cpu = seg_cpu.astype(np.int32)
        return seg_cpu
Пример #2
0
    def set_img(self, img):
        """
        read an rgb image, set the gpu copy to be the lab image
        """
        if img.shape[0] != self.dimy or img.shape[1] != self.dimx:
            raise ValueError(img.shape, self.dimy, self.dimx)
        if img.ndim == 1:
            nChannels = 1
            isNaN = np.isnan(img)
        elif img.ndim == 3:
            nChannels = 3
            img_isNaN_r = np.isnan(img[:, :, 0])
            img_isNaN_g = np.isnan(img[:, :, 1])
            img_isNaN_b = np.isnan(img[:, :, 2])
            isNaN = np.logical_or(img_isNaN_r, np.logical_or(img_isNaN_g, img_isNaN_b))
        else:
            raise NotImplementedError()

        self.img = CpuGpuArray(arr=img)
        self.img_isNaN = CpuGpuArray(arr=isNaN)

        print('self.img', self.img)
        print('self.img_isNaN', self.img_isNaN)

        if nChannels == 3:
            rgb_to_lab(img_gpu=self.img.gpu)
Пример #3
0
    def disp_deformed_grid_lines(self,level,color=None,lw=1):
#        return
        if self.hlines is None or self.vlines is None:
            raise ValueError
        hlines,vlines=self.hlines,self.vlines
#        for lines,c in zip([hlines,vlines],['r','b']):    
#            pts_at_0=np.asarray([lines[:,0,:].flatten(),
#                                 lines[:,1,:].flatten()]).T
#            pts_at_0 = CpuGpuArray(pts_at_0.copy())        
#            pts_at_T=CpuGpuArray.zeros_like(pts_at_0)                          
#            self.calc_T_fwd(pts_src=pts_at_0,                              
#                      pts_fwd=pts_at_T,
#                      level=level,verbose=0,int_quality=1) 
#            if self.nCols != self.nCols:
#                            raise NotImplementedError 
#            pts_at_T.gpu2cpu()
#            lines_new_x=pts_at_T.cpu[:,0].reshape(lines[:,0,:].shape).copy()
#            lines_new_y=pts_at_T.cpu[:,1].reshape(lines[:,0,:].shape).copy()  
#            for line_new_x,line_new_y in zip(lines_new_x,lines_new_y):
#                         
#                        plt.plot(line_new_x,line_new_y,c)
        if color is None:
            colors=['r','b']
        else:
            colors=[color,color]
                     
        s = hlines.shape
        if s[2]<=1:
            raise ValueError
        p = 0
        L = 50000
        
        if L >=s[2]:
                
            while p < np.ceil(s[2]):     
                hlines=self.hlines[:,:,p:p+L]
                vlines=self.vlines[:,:,p:p+L]            
                p+=L
                
                
                for lines,c in zip([hlines,vlines],colors):    
                    pts_at_0=np.asarray([lines[:,0,:].flatten(),
                                         lines[:,1,:].flatten()]).T
                    if pts_at_0.size==0:
                        break
                    pts_at_0 = CpuGpuArray(pts_at_0.copy())  
                    pts_at_T=CpuGpuArray.zeros_like(pts_at_0)                          
                    self.calc_T_fwd(pts_src=pts_at_0,                              
                              pts_fwd=pts_at_T,
                              level=level,int_quality=1) 
                    if self.nCols != self.nCols:
                                    raise NotImplementedError 
                    pts_at_T.gpu2cpu()
                    lines_new_x=pts_at_T.cpu[:,0].reshape(lines[:,0,:].shape).copy()
                    lines_new_y=pts_at_T.cpu[:,1].reshape(lines[:,0,:].shape).copy()  
                    for line_new_x,line_new_y in zip(lines_new_x,lines_new_y):
                        plt.plot(line_new_x,line_new_y,c,lw=lw)                   
        else:
            raise NotImplementedError
Пример #4
0
    def __init__(self,ms,level,data,
                 sigma_lm,
                 params_flow_int,                 
#                 src=None,dst=None,transformed=None
                 ):
#        ipshell('hi') 
        """
        Cost is level-dependent.
        
        TODO: GPU in the LL part.
        """
        src=data['src']
        dst=data['dst']
        transformed=data['transformed']
        if not isinstance(src,CpuGpuArray):
            raise ObsoleteError
        if not isinstance(dst,CpuGpuArray):
            raise ObsoleteError      
        if not isinstance(transformed,CpuGpuArray):
            raise ObsoleteError
            
        self.nCalls = 0
        self.nCallbacks = 0                                         
                   
        
        self.sigma_lm=sigma_lm
        cpa_space=ms.L_cpa_space[level]  
        self.cpa_space = cpa_space
#        1/0
        if src.shape[1] != cpa_space.dim_domain:
            raise ValueError(src.shape,cpa_space.dim_domain)
#            
#        self.cpa_space = cpa_space            
#        self.cpa_cov_inv = msp.L_cpa_space_covs[level].cpa_cov_inv
        self.mu = cpa_space.get_zeros_theta()
        
       
        self.src = src
        self.dst = dst
        self.transformed = transformed
       
           
       
        nPts = len(src)
        self.nPts  = nPts                   
        self.err = CpuGpuArray.zeros_like(src)          
        self.ll = CpuGpuArray.zeros(nPts,dtype=src.dtype)           
        
        
        self.params_flow_int=params_flow_int

        self._pat = PAT(pa_space=cpa_space,
                        Avees=cpa_space.get_zeros_PA()) 
Пример #5
0
    def __init__(
        self,
        ms,
        level,
        data,
        sigma_lm,
        params_flow_int,
        #                 src=None,dst=None,transformed=None
    ):
        #        ipshell('hi')
        src = data['src']
        dst = data['dst']
        transformed = data['transformed']
        if not isinstance(src, CpuGpuArray):
            raise ObsoleteError
        if not isinstance(dst, CpuGpuArray):
            raise ObsoleteError
        if not isinstance(transformed, CpuGpuArray):
            raise ObsoleteError

        self.nCalls = 0
        self.nCallbacks = 0

        self.sigma_lm = sigma_lm
        cpa_space = ms.L_cpa_space[level]
        self.cpa_space = cpa_space

        if src.shape[1] != cpa_space.dim_domain:
            raise ValueError(src.shape, cpa_space.dim_domain)

        self.mu = cpa_space.get_zeros_theta()

        self.src = src
        self.dst = dst
        self.transformed = transformed

        nPts = len(src)
        self.nPts = nPts
        self.err = CpuGpuArray.zeros_like(src)
        self.ll = CpuGpuArray.zeros(nPts, dtype=src.dtype)

        if nPts <= 1:
            raise ValueError
        self.err_by_der = CpuGpuArray.zeros((nPts - 1, src.shape[1]),
                                            dtype=src.dtype)
        self.ll_by_der = CpuGpuArray.zeros(nPts - 1, dtype=src.dtype)

        self.params_flow_int = params_flow_int

        self._pat = PAT(pa_space=cpa_space, Avees=cpa_space.get_zeros_PA())
    def __init__(self,ms,level,data,
                 sigma_lm,
                 params_flow_int,                 
#                 src=None,dst=None,transformed=None
                 ):
#        ipshell('hi') 
        src=data['src']
        dst=data['dst']
        transformed=data['transformed']
        if not isinstance(src,CpuGpuArray):
            raise ObsoleteError
        if not isinstance(dst,CpuGpuArray):
            raise ObsoleteError      
        if not isinstance(transformed,CpuGpuArray):
            raise ObsoleteError
            
        self.nCalls = 0
        self.nCallbacks = 0                                         
                   
        
        self.sigma_lm=sigma_lm
        cpa_space=ms.L_cpa_space[level]  
        self.cpa_space = cpa_space

        if src.shape[1] != cpa_space.dim_domain:
            raise ValueError(src.shape,cpa_space.dim_domain)

        self.mu = cpa_space.get_zeros_theta()
         
        self.src = src
        self.dst = dst
        self.transformed = transformed 
       
        nPts = len(src)
        self.nPts  = nPts                   
        self.err = CpuGpuArray.zeros_like(src)          
        self.ll = CpuGpuArray.zeros(nPts,dtype=src.dtype)           
        
        if nPts <= 1:
            raise ValueError
        self.err_by_der = CpuGpuArray.zeros((nPts-1,src.shape[1]),dtype=src.dtype)          
        self.ll_by_der = CpuGpuArray.zeros(nPts-1,dtype=src.dtype)  
        
        self.params_flow_int=params_flow_int

        self._pat = PAT(pa_space=cpa_space,
                        Avees=cpa_space.get_zeros_PA())  
Пример #7
0
def get_init_seg(dimy,dimx,nPixels_in_square_side,use_hex):
    """
    """
    M=nPixels_in_square_side 

    if use_hex:
        s = create_string(dimx,dimy,nPixels_in_square_side)
#        print s
        fname = os.path.join(dirname_precomputed_hex_inits,s)
        try:
            FilesDirs.raise_if_file_does_not_exist(fname)
            print "Loading",fname
            seg = np.load(fname)
            
            return seg
        except FileDoesNotExistError:
            pass
        msg = """
        I could not find a precomputed (image-independent)
        honeycomb initilization for this image size and this values of n.     
        So I will compute it from scratch and we will save the result in
        {}
        Next time you will run the code for an image of size
        nRows={}, nCols={}, with n = {},
        it will be faster.
        """.format(fname,dimy,dimx,nPixels_in_square_side)
        print msg
        seg = CpuGpuArray.zeros((dimy,dimx),dtype=np.int32)     

        # length of each side   
        a = np.sqrt(M ** 2 / (  1.5 * np.sqrt(3)  ))      
        H =   a
        W = np.sqrt(3)*H
        # XX and YY need to be float
        YY,XX = np.mgrid[0:float(dimy)+0*1.5*H:1.5*H,0:float(dimx)+0*W:W]

        XX[::2]+= float(W)/2
        centers = np.vstack([XX.ravel(),YY.ravel()]).T.copy()
        centers = CpuGpuArray(centers)    

        honeycomb(seg.gpu,centers.gpu,seg.size) 

        seg.gpu2cpu()
        np.save(fname,seg.cpu)
        return seg.cpu

    else:
        seg_cpu = np.zeros((dimy,dimx),dtype=np.int32)  
        yy,xx = np.mgrid[:dimy,:dimx] 
        xx = xx.astype(np.float)
        yy = yy.astype(np.float)
       
        dimx = float(dimx)
        dimy=float(dimy)        
        nTimesInX = np.floor(xx / M).max() + 1
 
        seg_cpu = np.floor(yy / M)  * nTimesInX + np.floor(xx / M)
        seg_cpu = seg_cpu.astype(np.int32)
        return seg_cpu
Пример #8
0
    def calc_grad_alpha(self,
                        pa_space,
                        pat,
                        pts,
                        grad_alpha,
                        grad_per_point,
                        dt,
                        nTimeSteps,
                        nStepsODEsolver,
                        mysign=1,
                        transformed=None,
                        do_checks=True):
        if transformed is None:
            raise ValueError
        if pts is transformed:
            raise ValueError
        if not isinstance(pts, CpuGpuArray):
            raise TypeError
        if not isinstance(transformed, CpuGpuArray):
            raise TypeError

        afs = pat.affine_flows
        nC = pa_space.nC
        nCs = pa_space.nCs
        dim_domain = pa_space.dim_domain
        dim_range = pa_space.dim_range
        nHomoCoo = pa_space.nHomoCoo
        if len(afs) != nC:
            raise ValueError(len(afs), nC)
        xmins = pa_space._xmins_LargeNumber
        xmaxs = pa_space._xmaxs_LargeNumber

        #        BasMats=CpuGpuArray(pa_space.BasMats.reshape(pa_space.d,nC,-1))
        BasMats = CpuGpuArray(pa_space.BasMats)
        signed_sqAs = self.prepare_signedSqAs_for_gpu(pa_space, afs, nC,
                                                      nHomoCoo, mysign, dt)

        d = pa_space.d
        nPts = len(pts)
        if d != len(BasMats):
            raise ValueError

        pa_space._gpu_calcs.calc_grad_alpha(xmins,
                                            xmaxs,
                                            signed_sqAs[:, :-1].reshape(
                                                nC, -1).copy(),
                                            BasMats,
                                            pts,
                                            dt,
                                            nTimeSteps,
                                            nStepsODEsolver,
                                            pts_at_T=transformed,
                                            grad_per_point=grad_per_point,
                                            dim_domain=dim_domain,
                                            dim_range=dim_range,
                                            nCs=nCs.astype(np.int32),
                                            incs=pa_space.incs)
        return grad_per_point
Пример #9
0
 def get_x_dense_with_both_endpoints(self, nPts):
     """
     It seems the flow code has some bug with the endpoints.
     So HBD.
     """
     x = np.zeros([nPts, 1])
     x[:, 0] = np.linspace(self.XMINS[0], self.XMAXS[0], nPts)
     x = CpuGpuArray(x)
     return x
Пример #10
0
 def get_x_dense_with_the_last_point(self, nPts):
     """
     TODO: it seems the flow code has some bug with the endpoints.
     So I here I exclude the first point, 
     and pray that including the end point will be ok. 
     """
     x = np.zeros([nPts, 1])
     x[:, 0] = np.linspace(self.XMINS[0], self.XMAXS[0], nPts + 1)[1:]
     x = CpuGpuArray(x)
     return x
Пример #11
0
 def get_x_dense(self, nPts):
     """
     TODO: it seems the flow code has some bug with the endpoints.
     So for now I took them out.
     
     Remark: surely I had some reason for this... the points are not between
     0 and 1; rather, they are between XMINS[0] and self.XMAXS
     """
     x = np.zeros([nPts, 1])
     x[:, 0] = np.linspace(self.XMINS[0], self.XMAXS[0], nPts + 2)[1:-1]
     x = CpuGpuArray(x)
     return x
Пример #12
0
    def get_cartoon(self):
        """
        Replace pixels with superpixels means.

        """
        img = self.img.cpu
        nChannels = self.nChannels
        img_disp = CpuGpuArray.zeros((img.shape[0], img.shape[1], 3), dtype=np.int32)
        get_cartoon(seg_gpu=self.seg.gpu, mu_i_gpu=self.superpixels.params.mu_i.gpu,
                    img_gpu=img_disp.gpu, nChannels=nChannels)
        img_disp.gpu2cpu()
        return img_disp.cpu
Пример #13
0
    def get_cartoon(self):
        """
        Replace pixels with superpixels means.

        """   
        img = self.img.cpu   
        nChannels = self.nChannels             
        img_disp = CpuGpuArray.zeros((img.shape[0],img.shape[1],3),dtype=np.int32)     
        get_cartoon(seg_gpu = self.seg.gpu, mu_i_gpu = self.superpixels.params.mu_i.gpu, 
                    img_gpu= img_disp.gpu, nChannels=nChannels)
        img_disp.gpu2cpu()
        return img_disp.cpu
Пример #14
0
    def set_data(self, x, y, range_start, range_end):
        """
        For now, assumes dst was evaluated on evenly-space points
        """
        if x.shape != y.shape:
            raise ValueError(x.shape, y.shape)
        if x.dtype != np.float64:
            raise TypeError(x.dtype)
        if y.dtype != np.float64:
            raise TypeError(y.dtype)
        nPts = len(x)
        self.x = x
        self.y = y

        self.y_scale = range_end - range_start
        self.y_offset = range_start
        dst = (y - self.y_offset) / self.y_scale

        if dst.ndim == 1:
            dst = dst.reshape(nPts, 1).copy()
        if not isinstance(dst, CpuGpuArray):
            dst = CpuGpuArray(dst)
        self.dst = dst

        #        cpa_space = self.tw.ms.L_cpa_space[0]
        domain_start, domain_end = self.domain_start, self.domain_end

        #        self.interval = np.linspace(domain_start,domain_end,nPts)

        #        line = (x - domain_start) / ( domain_end - domain_start)

        line = self.manipulate_predictors(x)

        if line.ndim == 1:
            line = line.reshape(nPts, 1).copy()
        self.src = CpuGpuArray(line)

        self.transformed = CpuGpuArray.zeros_like(self.src)
Пример #15
0
    def set_img(self, img):
        """
        read an rgb image, set the gpu copy to be the lab image
        """
        if img.shape[0] != self.dimy or img.shape[1] != self.dimx:
            raise ValueError(img.shape, self.dimy, self.dimx)
        if img.ndim == 2:
            nChannels = 1
        elif img.ndim == 3:
            nChannels = 3
        else:
            raise NotImplementedError(nChannels)

        self.img = CpuGpuArray(arr=img.astype(np.float))
        if nChannels == 3:
            rgb_to_lab(img_gpu=self.img.gpu)
Пример #16
0
 def set_data(self,data):
     if data.kind!= 'landmarks':
         raise NotImplementedError
     self.kind=data.kind
     src=data.src
     dst=data.dst
     
     self.landmarks_are_lin_ordered = data.landmarks_are_lin_ordered
     if not isinstance(src,CpuGpuArray):
         raise TypeError(type(src))
     if not isinstance(dst,CpuGpuArray):
         raise TypeError(type(dst))      
     if src.shape != dst.shape:
         raise ValueError(src.shape,dst.shape)    
     
     self.src = src
     self.dst = dst
     self.transformed = CpuGpuArray.zeros_like(self.dst)
Пример #17
0
    def set_data(self, data):
        if data.kind != 'landmarks':
            raise NotImplementedError
        self.kind = data.kind
        src = data.src
        dst = data.dst

        self.landmarks_are_lin_ordered = data.landmarks_are_lin_ordered
        if not isinstance(src, CpuGpuArray):
            raise TypeError(type(src))
        if not isinstance(dst, CpuGpuArray):
            raise TypeError(type(dst))
        if src.shape != dst.shape:
            raise ValueError(src.shape, dst.shape)

        self.src = src
        self.dst = dst
        self.transformed = CpuGpuArray.zeros_like(self.dst)
Пример #18
0
    def set_data(self,x,y,range_start,range_end):
        """
        For now, assumes dst was evaluated on evenly-space points
        """
        if x.shape != y.shape:
            raise ValueError(x.shape,y.shape)
        if x.dtype != np.float64:
            raise TypeError(x.dtype)
        if y.dtype != np.float64:
            raise TypeError(y.dtype) 
        nPts = len(x)
        self.x=x
        self.y=y
        
        self.y_scale = range_end-range_start
        self.y_offset = range_start
        dst = (y-self.y_offset)/self.y_scale
        
        if dst.ndim == 1:
            dst = dst.reshape(nPts,1).copy()
        if not isinstance(dst,CpuGpuArray):
            dst = CpuGpuArray(dst)
        self.dst=dst
        
#        cpa_space = self.tw.ms.L_cpa_space[0]                         
        domain_start,domain_end = self.domain_start,self.domain_end
      
      
#        self.interval = np.linspace(domain_start,domain_end,nPts)         
                
#        line = (x - domain_start) / ( domain_end - domain_start)
        
        line = self.manipulate_predictors(x)        
        
        if line.ndim == 1:
            line = line.reshape(nPts,1).copy()
        self.src=CpuGpuArray(line)
        
        self.transformed = CpuGpuArray.zeros_like(self.src)
    velTess*=-1
    tw.update_pat_from_velTess(velTess,level=0)    
  
            
    plt.subplot(222)
    plt.plot(x,pts_recovered_cf,'g')
    plt.grid('on')  
    
    


    from of.gpu import CpuGpuArray
    
    
    
    pts_fwd = CpuGpuArray.zeros_like(tw.x_dense)
    tic=time.clock()
    tw.calc_T_fwd(tw.x_dense,pts_fwd,level=0,int_quality=1)     
    pts_fwd.gpu2cpu()
    toc=time.clock()
    print 'time',toc-tic
#    1/0

    pts_recovered = CpuGpuArray.zeros_like(tw.x_dense)
    tw.calc_T_inv(pts_fwd,pts_recovered,level=0)
    pts_recovered.gpu2cpu()

    
    plt.plot(tw.x_dense.cpu,pts_fwd.cpu)
    plt.plot(tw.x_dense.cpu,pts_recovered.cpu)   
    
Пример #20
0
    def __init__(self,XMINS,XMAXS,nCs,
                 zero_v_across_bdry,
                 vol_preserve,
                 warp_around=None,
                 conformal=False,
                 zero_vals=None,
                 cpa_calcs=None,
                 tess='II',
                 valid_outside=None,
                 only_local=False,
                 cont_constraints_are_separable=False):
        if conformal:
            raise ValueError("This was a bad idea")
        if not self.has_GPU:
            raise ValueError("Where is my gpu?")
        if conformal:
            raise ValueError
        if tess not in ['I','II']:
            raise ValueError(tess)
        if tess == 'I' and self.dim_domain == 1:
            raise ValueError
        if tess == 'I' and self.dim_domain not in (2,3):
            raise NotImplementedError
        if only_local and tess != 'I':
            raise NotImplementedError
        
        if zero_vals is None:
            raise ValueError            
            
        if cpa_calcs is None:
            raise ValueError("You must pass this argument")
        self._calcs = cpa_calcs    

        if len(nCs) != self.dim_domain:
            raise ValueError('len(nCs) = {0} =/= {1} = dim_domain'.format(len(nCs),self.dim_domain))
        
        if warp_around is None:
#            warp_around = [False] * self.dim_domain  
            raise ValueError("You must pass this argument")
         
        try:# Test if iterable
            zero_vals.__iter__
        except AttributeError:                    
            raise ValueError(zero_vals)
        try: # Test if iterable
            nCs.__iter__
        except AttributeError:
            raise ValueError(nCs)      
        try: # Test if iterable
            zero_v_across_bdry.__iter__
        except:
            raise  
        try: # Test if iterable
            warp_around.__iter__
        except:
            raise                  
        
        
        
        if len(warp_around) != self.dim_domain:
            raise ValueError(len(warp_around) , self.dim_domain)
        if len(zero_v_across_bdry) != self.dim_domain:
            raise ValueError(len(zero_v_across_bdry) , self.dim_domain)       
       
        if tess=='I':
            if self.dim_domain==2:
                if any(zero_v_across_bdry) and valid_outside:
                    raise ValueError("dim_domain==2","tess='I'",
                    "zero_v_across_bdry={}".format(zero_v_across_bdry),
                    "valid_outside={}".format(valid_outside),
                    "These choices are inconsistent with each other")       
                if not all(zero_v_across_bdry) and not valid_outside:
                    raise ValueError("dim_domain>1","tess='I'",
                    "zero_v_across_bdry={}".format(zero_v_across_bdry),
                    "valid_outside={}".format(valid_outside),
                    "These choices are inconsistent with each other") 
            elif self.dim_domain==3:
                if valid_outside:
                    raise NotImplementedError
                elif not all(zero_v_across_bdry):
                    raise ValueError("dim_domain==3","tess='I'",
                    "zero_v_across_bdry={}".format(zero_v_across_bdry),
                    "These choices are inconsistent with each other")
            else:
                raise NotImplementedError
                
       
        self.XMINS = np.asarray(XMINS,dtype=my_dtype)
        self.XMAXS = np.asarray(XMAXS,dtype=my_dtype)                     
        if  (self.XMINS>=self.XMAXS).any():
            raise ValueError(XMINS,XMAXS)
         
                             
        self.warp_around = warp_around            
       
        self.tess=tess
        if tess == 'II':
            nC = reduce(np.dot,nCs)  # of cells
        elif tess == 'I':                               
            if self.dim_domain == 2:
                nC = reduce(np.dot,nCs) * 4
            elif self.dim_domain == 3:
                nC = reduce(np.dot,nCs) * 5
            else:
                raise NotImplementedError
            
        else:
            raise ValueError(tess)
        self.nCs = np.asarray(nCs)
        self.nC=nC
        if self.dim_domain !=1:
            if self.dim_domain in (2,3):
                self.expm_eff = ExpmEff(nC)
            else:
                self.expm_eff = ExpmEff(nC,use_parallel=1)
        
        nHomoCoo=self.nHomoCoo
        self._signed_sqAs_times_dt= np.empty((nC,nHomoCoo,nHomoCoo),
                                              dtype=self.my_dtype)
        # In each matrix, fill last row with zeros       
#       self._signed_sqAs_times_dt[:,-1].fill(0)          
#        self._sqAs_vectorized = np.zeros((nC,nHomoCoo*nHomoCoo),
#                                         dtype=self.my_dtype) 
#        self._Tlocals_vectorized = np.empty((nC,nHomoCoo*nHomoCoo),dtype=self.my_dtype)
        
        self._As_vectorized = CpuGpuArray.zeros((nC,self.lengthAvee),dtype=self.my_dtype)
        self._signed_As_vectorized = CpuGpuArray.zeros((nC,self.lengthAvee),dtype=self.my_dtype)
        self._signed_As_times_dt_vectorized = CpuGpuArray.zeros((nC,self.lengthAvee),dtype=self.my_dtype)
        self._Tlocals_vectorized = CpuGpuArray.zeros((nC,self.lengthAvee),dtype=self.my_dtype)
        
        
        if self.has_GPU: 
            self.sharedmemory = decide_sharedmemory(self.dim_domain,
                                                    self.dim_range,
                                                    self.nC)
            self._gpu_calcs = GpuCalcs(nC,my_dtype,
                                               dim_domain=self.dim_domain,
                                               dim_range=self.dim_range,
                                               tess=self.tess,
                                               sharedmemory=self.sharedmemory) 
      
        else:
            raise NotImplementedError
        self.only_local=only_local
        
        self.zero_v_across_bdry=zero_v_across_bdry          
        self.vol_preserve=vol_preserve

        self.subspace_string=self.create_subspace_string(self.XMINS,
                                                         self.XMAXS,
                                                         nCs,
                                                         zero_v_across_bdry,
                                                         vol_preserve,
                                                         warp_around,
                                                         conformal,
                                                         zero_vals,
                                                         valid_outside=valid_outside,
                                                         cont_constraints_are_separable=cont_constraints_are_separable)
                                                                
        self.directory = os.path.join(dirnames.cpa,'{0}d'.format(self.dim_domain),
                                              self.subspace_string)                                                         
        FilesDirs.mkdirs_if_needed(self.directory)     
        if self.only_local:
            self.filename_subspace =  os.path.join(self.directory,'local.pkl') 
        else:
            self.filename_subspace =  os.path.join(self.directory,'subspace.pkl') 
    def __init__(self,ms,level,data,
                 sigma_signal,
                 params_flow_int,      
                 interp_type_for_ll,
#                 src=None,dst=None,transformed=None
                 ):
#        ipshell('hi') 
        
        
        if interp_type_for_ll not in self.supported_interp_types:
            msg =  """
            interp_type_for_ll must be in
            ['gpu_linear',
              cv2.INTER_LINEAR,
              cv2.INTER_CUBIC,
              cv2.INTER_LANCZOS4]
            """
            raise ValueError(msg,interp_type_for_ll)
        self.interp_type_for_ll=interp_type_for_ll
        
        src=data['src']
        
        transformed=data['transformed']
        signal=data['signal']
        
        for obj in [src,transformed]:
            if not isinstance(obj,CpuGpuArray):
                raise TypeError
        for obj in [signal.src,signal.dst,signal.transformed]:
            if not isinstance(obj,CpuGpuArray):
                raise TypeError         
        
        
        self.nCalls = 0
        self.nCallbacks = 0                                         
                   
        
        self.sigma_signal=sigma_signal
        cpa_space=ms.L_cpa_space[level]  
        self.cpa_space = cpa_space

        if src.shape[1] != cpa_space.dim_domain:
            raise ValueError(src.shape,cpa_space.dim_domain)

#        self.mu = cpa_simple_mean(cpa_space)
        self.my = cpa_space.get_zeros_theta()
         
        self.src = src        
        self.transformed = transformed 
        self.signal = signal
        
#        self.dim_signal = signal.src.shape[1]
        if signal.src.ndim==2:
            self.dim_signal = 2
        else:
            raise NotImplementedError
        
        
        if self.dim_signal != 2:
            raise NotImplementedError(signal.src.shape)
        if self.signal.src.shape != self.signal.dst.shape:
            raise ValueError
        if self.signal.src.shape != self.signal.transformed.shape:
            raise ValueError            
       
        nPts = len(src)
        self.nPts  = nPts                   
#        self.err = CpuGpuArray.zeros_like(src)   
#        self.signal.err = CpuGpuArray.zeros_like(src) 
        self.signal.err = CpuGpuArray.zeros_like(self.signal.src) 
        self.ll = CpuGpuArray.zeros(nPts,dtype=src.dtype)           
        
        if nPts <= 1:
            raise ValueError

 
        self.params_flow_int=params_flow_int

        self._pat = PAT(pa_space=cpa_space,
                        Avees=cpa_space.get_zeros_PA())
Пример #22
0
    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$')
Пример #23
0
    def __init__(self,nRows,nCols,vol_preserve=False,
                 nLevels=1, 
                 base=[2,2],
                 scale_spatial=1.0 * .1,
                 scale_value=100,
                 zero_v_across_bdry=[False]*2, # For now, don't change that.
                 tess = None,
                 valid_outside=True,
                 only_local=False,
                 cont_constraints_are_separable=False):
        
                    
        """
        Input params:
            nRows: number of rows in the image
            nCols: number of cols in the image
            vol_preserve: boolean flag (area-preserving or not)
            nLevels: number of levels in the multiscale representation
            base = (# of cells in X direction, # of cells in Y direction)
                   Determines the resolution of the tesselation as the base 
                   level of the multiscale representation.
            scale_spatial: paramter for the Gaussian prior. Higher val <=> more smoothness                   
            scale_value: paramter for the Gaussian prior. Higher val <=> larger covariance
            
            
        """    

         
        super(type(self),self).__init__(
                 vol_preserve=vol_preserve,
                 nLevels=nLevels, 
                 base=base,
                 scale_spatial=scale_spatial,
                 scale_value=scale_value,
                 zero_v_across_bdry=zero_v_across_bdry,
                 tess=tess,
                 valid_outside=valid_outside,
                 only_local=only_local,
                 cont_constraints_are_separable=cont_constraints_are_separable)
         
        self.nRows = self.args.nRows = nRows
        self.nCols = self.args.nCols = nCols
        print self.args
#        print
        
        
        if tess=='tri':
            tess='I'
        if tess=='rect':
            tess=='II'
        
         
        if tess not in ['I','II']:
            raise ValueError(tess,"tess must be in ['I','II']")
        if only_local and tess !='I':
            raise NotImplementedError            
        
        self.nRows=nRows
        self.nCols=nCols  
        
        XMINS=[0,0]
      
        XMAXS=[nCols,nRows] # Note: This inclusive; e.g., if your image is
                                 # 512x512, XMAXS=[512,512], not [511,511]
         
#        XMINS=[-nCols/2,-nRows/2]
#        XMAXS=[ nCols/2, nRows/2] 
         
        warp_around=[False,False] # For now, don't change that.                
#        zero_v_across_bdry=[False,False] # For now, don't change that. 
                                       
                             
        Nx = XMAXS[0]-XMINS[0]
        Ny = XMAXS[1]-XMINS[1]        
        self.config_plt = ConfigPlt(Nx=Nx,Ny=Ny)                                        
        Ngrids=[Nx,Ny]
        
        ms=Multiscale(XMINS,XMAXS,zero_v_across_bdry,
                                  vol_preserve,
                                  warp_around=warp_around,
                                  nLevels=nLevels,base=base,
                                  tess=tess,
                                  Ngrids=Ngrids,
                                  valid_outside=valid_outside,
                                  only_local=only_local,
                                  cont_constraints_are_separable=cont_constraints_are_separable)
       
        self.ms=ms
         
        if only_local == False:                        
            self.msp=MultiscaleCoarse2FinePrior(ms,scale_spatial=scale_spatial,
                                                scale_value=scale_value,
                                           left_blk_std_dev=1.0/100,right_vec_scale=1)
            
        else:
            self.msp = None

        self.pts_src_dense = CpuGpuArray(ms.L_cpa_space[0].x_dense_img.copy())            
        self.v_dense = CpuGpuArray.zeros_like(self.pts_src_dense)
        self.transformed_dense = CpuGpuArray.zeros_like(self.pts_src_dense)


        self.params_flow_int = get_params_flow_int()  
        self.params_flow_int.nStepsODEsolver = 10 # Usually this is enough.
                   
        self.params_flow_int_coarse = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_coarse.nTimeSteps /= 10
        self.params_flow_int_coarse.dt *= 10

        self.params_flow_int_fine = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_fine.nTimeSteps *= 10
        self.params_flow_int_fine.dt /= 10   

        
        self.ms_pats = ms.pats
Пример #24
0
    
    from pylab import plt
    
#    plt.close('all')
    plt.figure(1)
    plt.clf()
    plt.plot(t1.cpu,y.cpu,lw=20,color='b')
    
    t2 = CpuGpuArray(N*(t1.cpu/N)**4)
#    t2 = CpuGpuArray(N*(t1.cpu/N)/2) # I was probably trying some thing here...
                                     # but looks like I ended up just dividing by 2
    
    plt.plot(t2.cpu,y.cpu,'r',lw=20)
    
    
    y2=CpuGpuArray.zeros_like(y)
    
    resampler(pts_inv_gpu=t2.gpu,             
          signal_gpu=y.gpu,
          signal_wrapped_gpu=y2.gpu,                         
          nPts=None)

    y2.gpu2cpu()
    plt.plot(t2.cpu,y2.cpu,'g',lw=8)    
    plt.plot(t1.cpu,y2.cpu,'m',lw=8)
    
    plt.legend(['$y(t)$',"$y(t')=(y\circ T)(t)$",
                "$y_2(t')=(y\circ T)(t_1)$",'$y_2(t)$'])


Пример #25
0
    def calc_trajectory(self,
                        pa_space,
                        pat,
                        pts,
                        dt,
                        nTimeSteps,
                        nStepsODEsolver=100,
                        mysign=1):
        """
        Returns: trajectories
                    
        trajectories.shape = (nTimeSteps,nPts,pa_space.dim_domain)
        
        todo: make a more efficient use with CpuGpuArray
        """
        if not isinstance(pts, CpuGpuArray):
            raise ObsoleteError
        nC = pa_space.nC
        nCs = pa_space.nCs
        nHomoCoo = pa_space.nHomoCoo
        dim_domain = pa_space.dim_domain
        dim_range = pa_space.dim_range
        incs = pa_space.incs
        if dim_domain != 2:
            raise NotImplementedError
        if pts.ndim != 2:
            raise ValueError(pts.shape)
        if pts.shape[1] != pa_space.dim_domain:
            raise ValueError(pts.shape)

        x, y = pts.cpu.T
        x_old = x.copy()
        y_old = y.copy()
        nPts = x_old.size

        history_x = np.zeros((nTimeSteps, nPts), dtype=self.my_dtype)
        history_y = np.zeros_like(history_x)
        history_x.fill(np.nan)
        history_y.fill(np.nan)

        afs = pat.affine_flows

        signed_sqAs, Tlocals = self.prepare_signedSqAs_and_Tlocals_for_gpu(
            pa_space, afs, nC, nHomoCoo, mysign, dt)
        #        As =  mysign*np.asarray([c.A for c in afs]).astype(self.my_dtype)
        #        Trels = np.asarray([expm(dt*c.A*mysign) for c in afs ])

        xmins = np.asarray([c.xmins for c in afs]).astype(self.my_dtype)
        xmaxs = np.asarray([c.xmaxs for c in afs]).astype(self.my_dtype)
        xmins[xmins <= self.XMINS] = -self._LargeNumber
        xmaxs[xmaxs >= self.XMAXS] = +self._LargeNumber

        if pa_space.has_GPU == False or self.use_GPU_if_possible == False:
            Warning("Not using GPU!")
            raise NotImplementedError

        else:
            pts_at_0 = np.zeros((nPts, pa_space.dim_domain))
            pts_at_0[:, 0] = x_old.ravel()
            pts_at_0[:, 1] = y_old.ravel()

            trajectories = pa_space._gpu_calcs.calc_trajectory(
                xmins,
                xmaxs,
                #                                                             Trels,As,
                Tlocals[:, :-1].reshape(nC, -1).copy(),
                signed_sqAs[:, :-1].reshape(nC, -1).copy(),
                pts_at_0,
                dt,
                nTimeSteps,
                nStepsODEsolver,
                dim_domain,
                dim_range,
                nCs,
                incs)
            # add the starting points
            trajectories = np.vstack([pts_at_0, trajectories])
            # reshaping
            trajectories = trajectories.reshape(1 + nTimeSteps, nPts,
                                                pa_space.dim_domain)

        trajectories = CpuGpuArray(trajectories)
        return trajectories
Пример #26
0

class Options(object):
    axis_ij = False
    gt = None

    def __init__(self, name):
        name = name.lower()
        self.axis_ij = True
        self.nCols = 250
        self.nRows = 250


options = Options(name)

data.src = CpuGpuArray(data.src)
data.dst = CpuGpuArray(data.dst)

inference_params = InferenceParams()

# quick debug
#inference_params.MCMCniters_per_level=10

tf = TransformationFitter(
    nRows=options.nRows,
    nCols=options.nCols,
    vol_preserve=inference_params.vol_preserve,
    sigma_lm=inference_params.sigma_lm,
    base=inference_params.base,
    nLevels=inference_params.nLevels,
    valid_outside=inference_params.valid_outside,
Пример #27
0
              
if __name__ == '__main__':
    from pycuda import autoinit
    from of.gpu import CpuGpuArray
    import numpy as np


    msg="""
    The code below is for landmarks, 
    not signals"""
    raise NotImplementedError(msg)
    yy,xx = np.mgrid[-2:2:1,-2:2:1]
    x = np.vstack([xx.ravel(),yy.ravel()]).T
    del xx,yy
    x = CpuGpuArray(x.copy().astype(np.float))
    print x
    
    y = np.random.standard_normal(x.shape)
    y = CpuGpuArray(y)

    err = CpuGpuArray.zeros_like(y) 
    nPts = len(err)
    ll = CpuGpuArray.zeros(nPts)
    calc_signal_err_per_sample(x.gpu,y.gpu,err.gpu) 
    
    sigma=1.0
    calc_ll_per_sample(ll.gpu,err.gpu,sigma)
    
    
    err.gpu2cpu()
Пример #28
0
    _calc_ll_per_sample(ll, err, np.float64(sigma))


if __name__ == '__main__':
    from pycuda import autoinit
    from of.gpu import CpuGpuArray
    import numpy as np

    msg = """
    The code below is for landmarks, 
    not signals"""
    raise NotImplementedError(msg)
    yy, xx = np.mgrid[-2:2:1, -2:2:1]
    x = np.vstack([xx.ravel(), yy.ravel()]).T
    del xx, yy
    x = CpuGpuArray(x.copy().astype(np.float))
    print x

    y = np.random.standard_normal(x.shape)
    y = CpuGpuArray(y)

    err = CpuGpuArray.zeros_like(y)
    nPts = len(err)
    ll = CpuGpuArray.zeros(nPts)
    calc_signal_err_per_sample(x.gpu, y.gpu, err.gpu)

    sigma = 1.0
    calc_ll_per_sample(ll.gpu, err.gpu, sigma)

    err.gpu2cpu()
    ll.gpu2cpu()
Пример #29
0
        #        if level !=ms.nLevels-1:
        #            continue
        print 'level: ', level
        print cpa_space
        #        cpa_calcs=CpaCalcs(Nx=Nx,Ny=Ny,use_GPU_if_possible=True)

        #        As = cpa_space.Avees2As(sample_Avees_all_levels[level])
        #        As[:,0,:]=0
        #        sample_Avees_all_levels[level] = cpa_space.As2Avees(As)
        #        sample_cpa_all_levels[level] = cpa_space.project(sample_Avees_all_levels[level])
        #        ipshell('hi')

        #        pat= PAT(pa_space=cpa_space,Avees=sample_Avees_all_levels[level])
        cpa_space.update_pat(Avees=sample_Avees_all_levels[level])

        pts = CpuGpuArray(cpa_space.x_dense_img)
        v_dense = CpuGpuArray.zeros_like(pts)
        cpa_space.calc_v(pts=pts, out=v_dense)

        v_dense.gpu2cpu()  # for display

        plt.figure(level)
        of.plt.maximize_figure()

        scale = [.4, 0.25][cpa_space.vol_preserve]
        scale = 1 * Nx * 30
        scale = np.sqrt((v_dense.cpu**2).sum(axis=1)).mean() / 10

        for h in [233, 236][:1]:
            plt.subplot(h)
            cpa_space.quiver(cpa_space.x_dense_grid_img,
Пример #30
0
    def __init__(self,nCols,vol_preserve=False,
                 nLevels=1, 
                 base=[5],
#                 scale_spatial=1.0 * .1,
#                 scale_value=100,
                 scale_spatial=1.0 * 10,
                 scale_value=2,         
                 zero_v_across_bdry=[True] # For now, don't change that. 
                 ,nPtsDense=None,
                 only_local=False
                 ):
        """
                
            
        """  
        super(type(self),self).__init__(
                         vol_preserve=vol_preserve,
                         nLevels=nLevels, 
                         base=base,
                         scale_spatial=scale_spatial,
                         scale_value=scale_value,
                         zero_v_across_bdry=zero_v_across_bdry,
                         tess=None,
                         valid_outside=None,
                         only_local=only_local)

        
        self.nCols = self.args.nCols = nCols        
        self.args.nPtsDense=nPtsDense 

        self.nCols=nCols         
        XMINS=[0]
        XMAXS=[nCols] # Note: This is inclusive 
              
        warp_around=[False] # For now, don't change that.                
#        zero_v_across_bdry=[True] # For now, don't change that.                                
                             
        Nx = XMAXS[0]-XMINS[0]
         
                                               
        Ngrids=[Nx]
            
        ms=Multiscale(XMINS,XMAXS,zero_v_across_bdry,
                                  vol_preserve,
                                  warp_around=warp_around,
                                  nLevels=nLevels,base=base ,
                                  Ngrids=Ngrids)
        
        self.ms=ms
                             
        self.msp=MultiscaleCoarse2FinePrior(ms,scale_spatial=scale_spatial,scale_value=scale_value,
#                                       left_blk_std_dev=1.0/100,
#                                       right_vec_scale=1
                                       left_blk_std_dev=1.0,
                                       right_vec_scale=1.0                                       
                                       )
        
#        self.pts_src_dense = ms.L_cpa_space[0].get_x_dense(1000)
        
#        self.x_dense= ms.L_cpa_space[0].get_x_dense(1000)
#        self.v_dense = CpuGpuArray.zeros_like(self.x_dense)
        self.nPtsDense=nPtsDense
        if nPtsDense is None:
            raise ObsoleteError
        else:
            self.x_dense= ms.L_cpa_space[0].get_x_dense(nPtsDense)
            self.v_dense = CpuGpuArray.zeros_like(self.x_dense)
            self.transformed_dense = CpuGpuArray.zeros_like(self.x_dense)
            
        self.params_flow_int = get_params_flow_int()  
        self.params_flow_int.nStepsODEsolver = 10 # Usually this is enough.
                                                  # 
        self.params_flow_int_coarse = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_coarse.nTimeSteps /= 10
        self.params_flow_int_coarse.dt *= 10

        self.params_flow_int_fine = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_fine.nTimeSteps *= 10
        self.params_flow_int_fine.dt /= 10        
        
        self.ms_pats = ms.pats
Пример #31
0
 
          
            # This needs to be evenly spaced. 
            interval = np.linspace(-3,3,x_dense.size)   


#            # There is actually a reason why I do the funny thing below    
##            x_select = x[::100/2].copy()
#            x_select = x.copy()[::-1][::100/2][::-1]
#            
#            
#            # but now that I need to copy, it may not be relevant any more...
#            x_select = x_select.copy()
            
#            x_select = x[::100/2].copy()
            x_select = CpuGpuArray(x_dense.cpu[::100/2].copy())
            
    #        x_select[:]=np.linspace(.1,1.0,len(x_select))
    #        x_select[3:-1] +=.1
    #        x_select[6:-1] -=.05 
    
            
                                                    
#            pat = PAT(pa_space=cpa_space,Avees=sample_Avees_all_levels[level]) 
            cpa_space.update_pat(Avees=sample_Avees_all_levels[level])
#           
            
            v_dense = CpuGpuArray.zeros_like(x_dense)
            cpa_space.calc_v(pts=x_dense,out=v_dense)
            
               
Пример #32
0
class SuperpixelsWrapper(object):

    _calc_seg = _update_seg_iter
    _calc_param = _update_param_iter

    def __init__(self,
                 dimy,
                 dimx,
                 nPixels_in_square_side=None,
                 permute_seg=False,
                 s_std=20,
                 i_std=20,
                 prior_count=1,
                 calc_s_cov=True,
                 use_hex=False):
        """
        arguments:
        nPixels_in_square_side: number of the pixels on the side of a superpixels
        permute_seg: only for display purpose, whether to permute the superpixels labling, 
        s_std: fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        calc_s_cov:  whether or not to update the covariance of color component of Gaussians
        use_hex: initialize the superpixels as hexagons or squares
        
        """

        # init the field "nPixels_in_square_side"
        if nPixels_in_square_side is None:
            raise NotImplementedError
        self.nPixels_in_square_side = nPixels_in_square_side
        if not (3 < nPixels_in_square_side < min(dimx, dimy)):
            msg = """
            Expected
            3<nPixels_in_square_side<min(dimx,dimy)={1}
            but nPixels_in_square_side={0}
            """.format(nPixels_in_square_side, min(dimx, dimy))
            raise ValueError(msg)

        self.dimx = dimx
        self.dimy = dimy
        self.nChannels = 3  # RGB/LAB

        #init the field "seg"
        self.use_hex = use_hex
        self.permute_seg = permute_seg
        seg = get_init_seg(dimy=dimy,
                           dimx=dimx,
                           nPixels_in_square_side=nPixels_in_square_side,
                           use_hex=use_hex)
        if permute_seg:
            seg = random_permute_seg(seg)
        self.seg = CpuGpuArray(arr=seg)

        # keep the initial segmentation for use in images of the same size
        # so that we won't re-calculate the initial segmenation
        self.seg_ini = self.seg.cpu.copy()

        #init the field nSuperpixels
        self.nSuperpixels = self.seg.cpu.max() + 1
        if self.nSuperpixels <= 1:
            raise ValueError(self.nSuperpixels)

        #init the field "superpixels"
        self.superpixels = Superpixels(self.nSuperpixels,
                                       s_std=s_std,
                                       i_std=i_std,
                                       prior_count=prior_count,
                                       nChannels=self.nChannels)

        #init the field "border"(bool array, true for pixels on the superpixel boundary)
        border = gpuarray.zeros((dimy, dimx), dtype=np.bool)
        self.border = CpuGpuArray(arr=border)
        find_border_pixels(seg_gpu=self.seg.gpu, border_gpu=self.border.gpu)
        self.border.gpu2cpu()
        self.border_ini = self.border.cpu.copy()

        print('dimy,dimx=', dimy, dimx)
        print('nSuperpixels =', self.nSuperpixels)

    def set_img(self, img):
        """
        read an rgb image, set the gpu copy to be the lab image
        """
        if img.shape[0] != self.dimy or img.shape[1] != self.dimx:
            raise ValueError(img.shape, self.dimy, self.dimx)
        if img.ndim == 2:
            nChannels = 1
        elif img.ndim == 3:
            nChannels = 3
        else:
            raise NotImplementedError(nChannels)

        self.img = CpuGpuArray(arr=img.astype(np.float))
        if nChannels == 3:
            rgb_to_lab(img_gpu=self.img.gpu)

    def initialize_seg(self):
        """ 
        set the self.seg/border to the initial segmentation/border 
        """
        np.copyto(dst=self.seg.cpu, src=self.seg_ini)
        np.copyto(dst=self.border.cpu, src=self.border_ini)
        self.seg.cpu2gpu()
        self.border.cpu2gpu()

    def set_superpixels(self, s_std, i_std, prior_count):
        """ 
        set the self.superpixels 
        """
        self.superpixels = Superpixels(self.nSuperpixels,
                                       s_std=s_std,
                                       i_std=i_std,
                                       prior_count=prior_count,
                                       nChannels=self.nChannels)

    def calc_seg(self,
                 nEMIters,
                 nItersInner=10,
                 calc_s_cov=True,
                 prior_weight=0.5,
                 verbose=False):
        """ 
        Inference:
        hard-EM (Expectation & Maximization)
        Alternate between the E step (update superpixel assignments) 
        and the M step (update parameters)

        Arguments:
        calc_s_cov: whether or not to update the covariance of color component of Gaussians
        prior_weight: the weight placed on the prior probability of a superpixel

        """
        if verbose:
            print('start')

        for i in range(nEMIters):
            if verbose:
                print('iteration', i)
            "M step"
            self._calc_param(img_gpu=self.img.gpu,
                             seg_gpu=self.seg.gpu,
                             sp=self.superpixels,
                             calculate_s_cov=calc_s_cov)
            "(Hard) E step"
            self._calc_seg(
                img_gpu=self.img.gpu,
                seg_gpu=self.seg.gpu,
                border_gpu=self.border.gpu,
                counts_gpu=self.superpixels.params.counts.gpu,
                log_count_gpu=self.superpixels.gpu_helper.log_count_helper,
                superpixels=self.superpixels,
                nIters=nItersInner,
                calculate_cov=calc_s_cov,
                prior_prob_weight=prior_weight)
        # update the border for display the single border image
        find_border_pixels(seg_gpu=self.seg.gpu,
                           border_gpu=self.border.gpu,
                           single_border=1)

    def gpu2cpu(self):
        """
        Transfer the needed parameters from gpu to cpu.
        Note this is usually slow (when the image and/or nSuperpixels is large)
        """
        if self.img.ndim == 3:
            lab_to_rgb(self.superpixels.params.mu_i.gpu
                       )  #convert mu_i.gpu from lab to rgb
            lab_to_rgb(self.img.gpu)  # convert img.gpu from lab to rgb

        params = self.superpixels.params
        for arg in [
                self.seg, self.border, self.img, params.counts, params.mu_s,
                params.mu_i, params.Sigma_s
        ]:
            arg.gpu2cpu()

    def get_img_overlaid(self):
        """
        Set the pixels on the superpixel boundary to red

        """
        img = self.img.cpu
        border = self.border.cpu
        nChannels = self.nChannels
        if nChannels == 1:
            img_disp = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
            img_disp[:, :, 0] = img_disp[:, :, 1] = img_disp[:, :, 2] = img
        elif nChannels == 3:
            img_disp = img.copy().astype(np.uint8)
        img_disp[:, :, 0][border] = 255
        img_disp[:, :, 1][border] = 0
        img_disp[:, :, 2][border] = 0
        return img_disp

    def get_cartoon(self):
        """
        Replace pixels with superpixels means.

        """
        img = self.img.cpu
        nChannels = self.nChannels
        img_disp = CpuGpuArray.zeros((img.shape[0], img.shape[1], 3),
                                     dtype=np.int32)
        get_cartoon(seg_gpu=self.seg.gpu,
                    mu_i_gpu=self.superpixels.params.mu_i.gpu,
                    img_gpu=img_disp.gpu,
                    nChannels=nChannels)
        img_disp.gpu2cpu()
        return img_disp.cpu
Пример #33
0
    params_flow_int_ref = copy.deepcopy(params_flow_int)
    
    N = int(params_flow_int_ref.nTimeSteps) * 1
    
    # in general, this doesn't have to evenly spaced. Just in the right range.
    x_dense=cpa_space.get_x_dense(nPts=1000) 
    # This needs to be evenly spaced. 
    interval = np.linspace(-3,3,x_dense.size)
    
    
    Nvals = range(N)    
    Nvals=Nvals[-1:]
    
#    x_dense = CpuGpuArray(x_dense)
    v_dense = CpuGpuArray.empty_like(x_dense)
    src = x_dense
    transformed = CpuGpuArray.empty_like(src) 
    for i,N in enumerate(Nvals):        
        print 'i =',i
        params_flow_int.nTimeSteps = float(N+1)            
        if i == 0:                          
            cpa_space.calc_v(pts=src,out=v_dense)
            
           
        
       
        
#        for j in range(1):
#            if j % 100 == 0:
#                print j
Пример #34
0
    def __init__(self,
                 dimy,
                 dimx,
                 nPixels_in_square_side=None,
                 permute_seg=False,
                 s_std=20,
                 i_std=20,
                 prior_count=1,
                 calc_s_cov=True,
                 use_hex=False):
        """
        arguments:
        nPixels_in_square_side: number of the pixels on the side of a superpixels
        permute_seg: only for display purpose, whether to permute the superpixels labling, 
        s_std: fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        calc_s_cov:  whether or not to update the covariance of color component of Gaussians
        use_hex: initialize the superpixels as hexagons or squares
        
        """

        # init the field "nPixels_in_square_side"
        if nPixels_in_square_side is None:
            raise NotImplementedError
        self.nPixels_in_square_side = nPixels_in_square_side
        if not (3 < nPixels_in_square_side < min(dimx, dimy)):
            msg = """
            Expected
            3<nPixels_in_square_side<min(dimx,dimy)={1}
            but nPixels_in_square_side={0}
            """.format(nPixels_in_square_side, min(dimx, dimy))
            raise ValueError(msg)

        self.dimx = dimx
        self.dimy = dimy
        self.nChannels = 3  # RGB/LAB

        #init the field "seg"
        self.use_hex = use_hex
        self.permute_seg = permute_seg
        seg = get_init_seg(dimy=dimy,
                           dimx=dimx,
                           nPixels_in_square_side=nPixels_in_square_side,
                           use_hex=use_hex)
        if permute_seg:
            seg = random_permute_seg(seg)
        self.seg = CpuGpuArray(arr=seg)

        # keep the initial segmentation for use in images of the same size
        # so that we won't re-calculate the initial segmenation
        self.seg_ini = self.seg.cpu.copy()

        #init the field nSuperpixels
        self.nSuperpixels = self.seg.cpu.max() + 1
        if self.nSuperpixels <= 1:
            raise ValueError(self.nSuperpixels)

        #init the field "superpixels"
        self.superpixels = Superpixels(self.nSuperpixels,
                                       s_std=s_std,
                                       i_std=i_std,
                                       prior_count=prior_count,
                                       nChannels=self.nChannels)

        #init the field "border"(bool array, true for pixels on the superpixel boundary)
        border = gpuarray.zeros((dimy, dimx), dtype=np.bool)
        self.border = CpuGpuArray(arr=border)
        find_border_pixels(seg_gpu=self.seg.gpu, border_gpu=self.border.gpu)
        self.border.gpu2cpu()
        self.border_ini = self.border.cpu.copy()

        print('dimy,dimx=', dimy, dimx)
        print('nSuperpixels =', self.nSuperpixels)
Пример #35
0
        print 'level: ',level
        print cpa_space
#        cpa_calcs=CpaCalcs(Nx=Nx,Ny=Ny,use_GPU_if_possible=True)   

        
#        As = cpa_space.Avees2As(sample_Avees_all_levels[level])        
#        As[:,0,:]=0               
#        sample_Avees_all_levels[level] = cpa_space.As2Avees(As)
#        sample_cpa_all_levels[level] = cpa_space.project(sample_Avees_all_levels[level])
#        ipshell('hi') 
         
#        pat= PAT(pa_space=cpa_space,Avees=sample_Avees_all_levels[level])         
        cpa_space.update_pat(Avees=sample_Avees_all_levels[level])
       
        pts = CpuGpuArray(cpa_space.x_dense_img) 
        v_dense = CpuGpuArray.zeros_like(pts) 
        cpa_space.calc_v(pts=pts,out=v_dense   )
    
        
        v_dense.gpu2cpu()  # for display

        plt.figure(level);
        of.plt.maximize_figure()  
        
         
         
        scale=[.4,0.25][cpa_space.vol_preserve]
        scale = 1 * Nx * 30
        scale = np.sqrt((v_dense.cpu**2).sum(axis=1)).mean() / 10

        for h in [233,236][:1]:
def example(base=[5],
            scale_spatial=100,
            nLevels=2,
            zero_v_across_bdry=[1],
            use_local_basis=True):   
    nPtsDense = 10000   
    tw = TransformWrapper(nCols=100,
                          nLevels=nLevels,  
                          base=base,
                          scale_spatial=scale_spatial,
                          nPtsDense=nPtsDense,
                          zero_v_across_bdry=zero_v_across_bdry)
     
    print_iterable(tw.ms.L_cpa_space)

    seed=0
    np.random.seed(seed)    
               

    
    
    for level in range(tw.ms.nLevels):
        cpa_space = tw.ms.L_cpa_space[level]
        Avees = cpa_space.Avees
        velTess = cpa_space.zeros_velTess()
        
        if use_local_basis:
            if 0:
                tw.sample_gaussian_velTess(level,Avees,velTess,mu=None) 
                Avees*=0.001
                velTess*=0.001  
            else:
                
                if not zero_v_across_bdry[0]:
                    velTess[:]=10*np.random.standard_normal(velTess.shape)
                    cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)
                else:
                    velTess[1:-1]=10*np.random.standard_normal(velTess[1:-1].shape)
                    cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)
                    
            cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)  
            
        else:
            theta= cpa_space.get_zeros_theta()
            tw.sample_gaussian(level,Avees,theta,mu=None)    
#            theta/=10             
            cpa_space.theta2Avees(theta=theta,Avees=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(Avees,level) 
        
        pts_src = tw.x_dense        
            
        tw.calc_v(level=level,pts=pts_src,v=tw.v_dense)
        tw.v_dense.gpu2cpu()
            
        
        pts_fwd = CpuGpuArray.zeros_like(pts_src) # Create a buffer for the output      
        
        tw.calc_T_fwd(pts_src,pts_fwd,level=level)    
        pts_fwd.gpu2cpu()  

        pts_inv = CpuGpuArray.zeros_like(pts_src) # Create a buffer for the output
        tw.calc_T_inv(pts_src,pts_inv,level=level)    
        pts_inv.gpu2cpu()     
     
        plt.figure(level)
        plt.clf()   
        interval = pts_src.cpu # interval doesn't have to be pts_src.cpu
        Visualize.simple(tw.x_dense,tw.v_dense,interval,pts_src,
                         transformed_fwd=pts_fwd,transformed_inv=pts_inv,
                         cpa_space=cpa_space)
        
    return tw
Пример #37
0
from of.utils import *
from of.gpu import CpuGpuArray
import of.plt
from cpab.cpa2d.TransformWrapper import TransformWrapper

from get_data_LFW import get_data
from disp import disp

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]
Пример #38
0
    def disp_orig_grid_lines(self,level,color=None,lw=1):
#        return
        try:
            self.hlines
            self.vlines
        except AttributeError:
            raise Exception("You need to call create_grid_lines first")
            
        if self.hlines is None:
            raise ValueError
        if self.vlines is None:
            self.vlines = self.hlines
        hlines,vlines=self.hlines,self.vlines
        
         
        s = hlines.shape
        if s[2]<=1:
            raise ValueError
        p = 0
        L = 50000
        if color is None:
            colors=['r','b']
        else:
            colors=[color,color]
        if L >=s[2]:  
            while p < np.ceil(s[2]):     
                hlines=self.hlines[:,:,p:p+L]
                vlines=self.vlines[:,:,p:p+L]            
                p+=L-1
                
                
                for lines,c in zip([hlines,vlines],colors):    
                    pts_at_0=np.asarray([lines[:,0,:].flatten(),
                                         lines[:,1,:].flatten()]).T
                    if pts_at_0.size==0:
                        break
        #            print _pts_at_0.shape
                    
                    pts_at_0 = CpuGpuArray(pts_at_0.copy())        
                    
                    if self.nCols != self.nCols:
                        raise NotImplementedError 
                    pts_at_0.gpu2cpu()
                     
                    lines_new_x=pts_at_0.cpu[:,0].reshape(lines[:,0,:].shape).copy()
                    lines_new_y=pts_at_0.cpu[:,1].reshape(lines[:,0,:].shape).copy()  
                    for line_new_x,line_new_y in zip(lines_new_x,lines_new_y):
                        plt.plot(line_new_x,line_new_y,c,lw=lw)
        else:
                hlines=self.hlines
                vlines=self.vlines       
                
                for lines,c in zip([hlines,vlines],colors):    
                    pts_at_0=np.asarray([lines[:,0,:].flatten(),
                                         lines[:,1,:].flatten()]).T
                    if pts_at_0.size==0:
                        break
        #            print _pts_at_0.shape
                    
                    pts_at_0 = CpuGpuArray(pts_at_0.copy())        
                    
                    if self.nCols != self.nCols:
                        raise NotImplementedError 
                    pts_at_0.gpu2cpu()
                     
                    lines_new_x=pts_at_0.cpu[:,0].reshape(lines[:,0,:].shape).copy()
                    lines_new_y=pts_at_0.cpu[:,1].reshape(lines[:,0,:].shape).copy()  
                    for line_new_x,line_new_y in zip(lines_new_x,lines_new_y):
                        plt.plot(line_new_x,line_new_y,c,lw=lw)
Пример #39
0
if __name__ == '__main__':
    from pycuda import autoinit
    from of.gpu import CpuGpuArray
    import numpy as np
    #    x = CpuGpuArray(np.arange(4).astype(np.float))
    #    y = CpuGpuArray(np.arange(4)[::-1].copy().astype(np.float))
    #
    #    print x
    #    print y
    #
    #    err_by_der = CpuGpuArray.zeros(3,dtype=x.dtype)
    #    calc_err_by_der_per_sample(x.gpu,y.gpu,err_by_der.gpu,0.01)
    #
    #    print err_by_der

    x = CpuGpuArray((np.arange(-3, 4)**2).astype(np.float))
    print x

    print 'sum diff'

    print np.diff(x.cpu).sum()
    print calc_sum_prime(x.gpu, np.int32(len(x)))
    print 'sum ddiff'
    print np.diff(x.cpu, n=2).sum()
    print calc_sum_double_prime(x.gpu, np.int32(len(x)))

    print 'sum abs(ddiff)'
    print np.abs(np.diff(x.cpu, n=2)).sum()
    print calc_sum_abs_double_prime(x.gpu, np.int32(len(x)))
Пример #40
0
 def set_dense(self):
     self.src_dense =  self.tw.pts_src_dense
     self.transformed_dense = CpuGpuArray.zeros_like(self.src_dense)
Пример #41
0
 #   pts_grid = pts_grid[:,5:-5,5:-5,5:-5]

    pts=np.vstack([pts_grid[0].ravel(),
                   pts_grid[1].ravel(),
                   pts_grid[2].ravel()]).T.copy()
              
    
    if 0:
        ds0,ds1,ds2=10,10,10
    #    pts_grid=cpa_space.x_dense_grid[:,::ds0,::ds1,50:51]
        pts_grid=cpa_space.x_dense_grid[:,::ds0,::ds1,::ds2]
        pts=np.vstack([pts_grid[0].ravel(),
                   pts_grid[1].ravel(),
                   pts_grid[2].ravel()]).T.copy()        
    
    pts = CpuGpuArray(pts)
    
    print pts_grid.shape
    print pts.shape
     
    pts_transformed = CpuGpuArray.zeros_like(pts)



    mu = cpa_space.get_zeros_theta()
    
    
    np.random.seed(0)     
#    theta *= 4
#    
Пример #42
0
    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 __init__(
        self,
        ms,
        level,
        data,
        sigma_signal,
        params_flow_int,
        interp_type_for_ll,
        #                 src=None,dst=None,transformed=None
    ):
        #        ipshell('hi')

        if interp_type_for_ll not in self.supported_interp_types:
            msg = """
            interp_type_for_ll must be in
            ['gpu_linear',
              cv2.INTER_LINEAR,
              cv2.INTER_CUBIC,
              cv2.INTER_LANCZOS4]
            """
            raise ValueError(msg, interp_type_for_ll)
        self.interp_type_for_ll = interp_type_for_ll

        src = data['src']

        transformed = data['transformed']
        signal = data['signal']

        for obj in [src, transformed]:
            if not isinstance(obj, CpuGpuArray):
                raise TypeError
        for obj in [signal.src, signal.dst, signal.transformed]:
            if not isinstance(obj, CpuGpuArray):
                raise TypeError

        self.nCalls = 0
        self.nCallbacks = 0

        self.sigma_signal = sigma_signal
        cpa_space = ms.L_cpa_space[level]
        self.cpa_space = cpa_space

        if src.shape[1] != cpa_space.dim_domain:
            raise ValueError(src.shape, cpa_space.dim_domain)

#        self.mu = cpa_simple_mean(cpa_space)
        self.my = cpa_space.get_zeros_theta()

        self.src = src
        self.transformed = transformed
        self.signal = signal

        #        self.dim_signal = signal.src.shape[1]
        if signal.src.ndim == 2:
            self.dim_signal = 2
        else:
            raise NotImplementedError

        if self.dim_signal != 2:
            raise NotImplementedError(signal.src.shape)
        if self.signal.src.shape != self.signal.dst.shape:
            raise ValueError
        if self.signal.src.shape != self.signal.transformed.shape:
            raise ValueError

        nPts = len(src)
        self.nPts = nPts
        #        self.err = CpuGpuArray.zeros_like(src)
        #        self.signal.err = CpuGpuArray.zeros_like(src)
        self.signal.err = CpuGpuArray.zeros_like(self.signal.src)
        self.ll = CpuGpuArray.zeros(nPts, dtype=src.dtype)

        if nPts <= 1:
            raise ValueError

        self.params_flow_int = params_flow_int

        self._pat = PAT(pa_space=cpa_space, Avees=cpa_space.get_zeros_PA())
Пример #44
0
              
    
    if 0:
        ds0,ds1,ds2=10,10,10
    #    pts_grid=cpa_space.x_dense_grid[:,::ds0,::ds1,50:51]
        pts_grid=cpa_space.x_dense_grid[:,::ds0,::ds1,::ds2]
        pts=np.vstack([pts_grid[0].ravel(),
                   pts_grid[1].ravel(),
                   pts_grid[2].ravel()]).T.copy()        
    
    pts = CpuGpuArray(pts)
    
    
    
     
    pts_fwd = CpuGpuArray.zeros_like(pts)



    mu = cpa_space.get_zeros_theta()
    
    
#    np.random.seed(0)     
    theta = np.random.multivariate_normal(mean=mu,cov=cpa_covs.cpa_cov)
    theta *=100
##    
#    theta.fill(0)
#    theta[8]=1
#    theta[10]=1
    
    cpa_space.theta2Avees(theta=theta)           
def example(tess='I',base=[2,2,2],nLevels=1,
            zero_v_across_bdry=[True]*3,
            vol_preserve=False,
           nRows=100, nCols=100,nSlices=100,
           use_mayavi=False,
           eval_v=False,
           eval_cell_idx=False):  
     
    tw = TransformWrapper(nRows=nRows,
                          nCols=nCols,
                          nSlices=nSlices,
                          nLevels=nLevels,  
                          base=base,
                          zero_v_across_bdry=zero_v_across_bdry,
                          tess=tess,
                          valid_outside=False,
                          only_local=False,
                          vol_preserve=vol_preserve)
     
     
    print_iterable(tw.ms.L_cpa_space)
    print tw
    
    # create some fake 3D image.
    img = np.zeros((nCols,nRows,nSlices),dtype=np.float64)
    
#    img[:]=np.random.random_integers(0,255,img.shape)
    
    # Fill the image with the x coordinates as fake values
    img[:]=tw.pts_src_dense.cpu[:,0].reshape(img.shape)
    
    img0 = CpuGpuArray(img.copy().astype(np.float64))
    img_wrapped_fwd= CpuGpuArray.zeros_like(img0)
    img_wrapped_inv= CpuGpuArray.zeros_like(img0)
    
     
    seed=0
    np.random.seed(seed)    
    
                  
    ms_Avees=tw.get_zeros_PA_all_levels()
    ms_theta=tw.get_zeros_theta_all_levels() 
    
    
    if tess == 'II' :        
        for level in range(tw.ms.nLevels): 
            cpa_space = tw.ms.L_cpa_space[level]  
            Avees = ms_Avees[level]    
#            1/0
            if level==0:
                tw.sample_gaussian(level,ms_Avees[level],ms_theta[level],mu=None)# zero mean
#                ms_theta[level].fill(0)
#                ms_theta[level][-4]=10
                cpa_space.theta2Avees(theta=ms_theta[level],Avees=Avees)
            else:
                tw.sample_from_the_ms_prior_coarse2fine_one_level(ms_Avees,ms_theta,
                                                                    level_fine=level)
    else:
        # For tess='I' in 3D, I have yet to implement the coarse-to-fine sampling.
        for level in range(tw.ms.nLevels): 
            cpa_space = tw.ms.L_cpa_space[level]
            velTess = cpa_space.zeros_velTess()
            ms_Avees[level].fill(0)
            Avees = ms_Avees[level]
            tw.sample_gaussian_velTess(level,Avees,velTess,mu=None)
    
       
    
    
    print 'img shape:',img0.shape
   
   
    # You don't have use these. You can use any 2d array
    # that has 3 columns (regardless of the number of rows).   
    pts_src = tw.pts_src_dense       
    pts_src=CpuGpuArray(pts_src.cpu[::1].copy())
	
    # Create a buffer for the output
    pts_fwd = CpuGpuArray.zeros_like(pts_src) 
    pts_inv = CpuGpuArray.zeros_like(pts_src)  
   
   
    for level in range(tw.ms.nLevels):              
        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) 
        
        
        print 'level',level
        print
        print 'number of points:',len(pts_src)   
        print 'number of cells:',tw.ms.L_cpa_space[level].nC    
        
        
        
        # 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)
    
        tw.remap_fwd(pts_inv,img0,img_wrapped_fwd)
        tw.remap_inv(pts_fwd,img0,img_wrapped_inv)
        
         
    
        # 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_inv.gpu2cpu()
        
         
    
       
       
    
    
        if use_mayavi:
            ds=1 # downsampling factor
            i= 17
            pts_src_grid = pts_src.cpu.reshape(tw.nRows,tw.nCols,-1,3)
            pts_src_ds=pts_src_grid[::ds,::ds,i].reshape(-1,3)
            pts_fwd_grid = pts_fwd.cpu.reshape(tw.nRows,tw.nCols,-1,3)
            pts_fwd_ds=pts_fwd_grid[::ds,::ds,i].reshape(-1,3)
            pts_inv_grid = pts_inv.cpu.reshape(tw.nRows,tw.nCols,-1,3)
            pts_inv_ds=pts_inv_grid[::ds,::ds,i].reshape(-1,3)
        
        
            from of.my_mayavi import *
            mayavi_mlab_close_all()
            mayavi_mlab_figure_bgwhite('src')
            x,y,z=pts_src_ds.T
            mayavi_mlab_plot3d(x,y,z)
            mayavi_mlab_figure_bgwhite('fwd')
            x,y,z=pts_fwd_ds.T
            mayavi_mlab_plot3d(x,y,z)    
         
        figsize = (12,12)
        plt.figure(figsize=figsize)               
        i= 17 # some slice
        plt.subplot(131)
        plt.imshow(img0.cpu[:,:,i].astype(np.uint8),interpolation="Nearest")  
        plt.title('slice from img')
        plt.subplot(132)
        plt.imshow(img_wrapped_fwd.cpu[:,:,i].astype(np.uint8),interpolation="Nearest")  
        plt.axis('off') 
        plt.title('slice from fwd(img)')
        plt.subplot(133)
        plt.imshow(img_wrapped_inv.cpu[:,:,i].astype(np.uint8),interpolation="Nearest")    
        plt.axis('off') 
        plt.title('slice from inv(img)')
        
    
    if 0: # debug    
        
        cpa_space=tw.ms.L_cpa_space[level]
        if eval_v:
            vx=tw.v_dense.cpu[:,0].reshape(cpa_space.x_dense_grid_img.shape[1:])
            vy=tw.v_dense.cpu[:,1].reshape(cpa_space.x_dense_grid_img.shape[1:])
            vz=tw.v_dense.cpu[:,2].reshape(cpa_space.x_dense_grid_img.shape[1:])
        
        
            plt.figure()
            plt.imshow(vz[:,:,17],interpolation="Nearest");plt.colorbar()
            plt.title('vz in some slice')
     
    return tw
    velTess *= -1
    tw.update_pat_from_velTess(velTess, level=0)
    closed_form_int.calc_phi_multiple_pts(x=pts_fwd_cf,
                                          velTess=velTess,
                                          pts_fwd=pts_recovered_cf,
                                          t=t)
    velTess *= -1
    tw.update_pat_from_velTess(velTess, level=0)

    plt.subplot(222)
    plt.plot(x, pts_recovered_cf, 'g')
    plt.grid('on')

    from of.gpu import CpuGpuArray

    pts_fwd = CpuGpuArray.zeros_like(tw.x_dense)
    tic = time.clock()
    tw.calc_T_fwd(tw.x_dense, pts_fwd, level=0, int_quality=1)
    pts_fwd.gpu2cpu()
    toc = time.clock()
    print 'time', toc - tic
    #    1/0

    pts_recovered = CpuGpuArray.zeros_like(tw.x_dense)
    tw.calc_T_inv(pts_fwd, pts_recovered, level=0)
    pts_recovered.gpu2cpu()

    plt.plot(tw.x_dense.cpu, pts_fwd.cpu)
    plt.plot(tw.x_dense.cpu, pts_recovered.cpu)

    plt.legend([r'$T(x)$', r'$(T^{-1}\circ T)(x)$', r'$T_{\mathrm{alg}}(x)$'],
Пример #47
0
class SuperpixelsWrapper(object):
 
    _calc_seg = _update_seg_iter
    _calc_param = _update_param_iter

    def __init__(self, dimy, dimx, nPixels_in_square_side=None, permute_seg=False,
                 s_std = 20, i_std=20, prior_count = 1,
                 calc_s_cov=True,
                 use_hex=False):
        """
        arguments:
        nPixels_in_square_side: number of the pixels on the side of a superpixels
        permute_seg: only for display purpose, whether to permute the superpixels labling, 
        s_std: fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        calc_s_cov:  whether or not to update the covariance of color component of Gaussians
        use_hex: initialize the superpixels as hexagons or squares
        
        """

        # init the field "nPixels_in_square_side"
        if nPixels_in_square_side is None:
            raise NotImplementedError
        self.nPixels_in_square_side=nPixels_in_square_side
        if not (3<nPixels_in_square_side<min(dimx,dimy)):
            msg = """
            Expected
            3<nPixels_in_square_side<min(dimx,dimy)={1}
            but nPixels_in_square_side={0}
            """.format(nPixels_in_square_side,min(dimx,dimy))
            raise ValueError(msg)

        self.dimx=dimx
        self.dimy=dimy
        self.nChannels = 3 # RGB/LAB

        #init the field "seg"
        self.use_hex = use_hex
        self.permute_seg = permute_seg
        seg = get_init_seg(dimy=dimy,dimx=dimx,
                          nPixels_in_square_side=nPixels_in_square_side, use_hex = use_hex)   
        if permute_seg:
            seg = random_permute_seg(seg)  
        self.seg = CpuGpuArray(arr=seg)     
             
        # keep the initial segmentation for use in images of the same size 
        # so that we won't re-calculate the initial segmenation
        self.seg_ini = self.seg.cpu.copy() 

        #init the field nSuperpixels
        self.nSuperpixels = self.seg.cpu.max()+1 
        if self.nSuperpixels <= 1:
            raise ValueError(self.nSuperpixels)     

        #init the field "superpixels"
        self.superpixels = Superpixels(self.nSuperpixels,
                            s_std=s_std, i_std=i_std, prior_count = prior_count, 
                            nChannels=self.nChannels)

        #init the field "border"(bool array, true for pixels on the superpixel boundary)
        border = gpuarray.zeros((dimy,dimx),dtype=np.bool)
        self.border = CpuGpuArray(arr=border)
        find_border_pixels(seg_gpu=self.seg.gpu, border_gpu=self.border.gpu) 
        self.border.gpu2cpu()        
        self.border_ini = self.border.cpu.copy() 
        
        print 'dimy,dimx=',dimy,dimx
        print 'nSuperpixels =',self.nSuperpixels
        

    def set_img(self,img):   
        """
        read an rgb image, set the gpu copy to be the lab image
        """
        if img.shape[0] != self.dimy or img.shape[1] != self.dimx:
            raise ValueError(img.shape,self.dimy,self.dimx)
        if img.ndim == 1:
            nChannels = 1
            isNaN = np.isnan( img)
        elif img.ndim == 3:
            nChannels = 3
            img_isNaN_r = np.isnan( img[:,:,0] )
            img_isNaN_g = np.isnan( img[:,:,1] )
            img_isNaN_b = np.isnan( img[:,:,2] )
            isNaN =  np.logical_or(img_isNaN_r, np.logical_or(img_isNaN_g,img_isNaN_b))
        else:
            raise NotImplementedError(nChannels)         

        self.img = CpuGpuArray(arr=img)
        self.img_isNaN = CpuGpuArray(arr=isNaN)

        print 'self.img',self.img
        print 'self.img_isNaN',self.img_isNaN

        if nChannels==3:
            rgb_to_lab(img_gpu=self.img.gpu)

        
        
    def initialize_seg(self):
        """ 
        set the self.seg/border to the initial segmentation/border 
        """
        np.copyto(dst=self.seg.cpu,src=self.seg_ini)     
        np.copyto(dst=self.border.cpu,src=self.border_ini)
        self.seg.cpu2gpu()
        self.border.cpu2gpu() 

    def set_superpixels(self, s_std, i_std, prior_count):
        """ 
        set the self.superpixels 
        """  
        self.superpixels = Superpixels(self.nSuperpixels,
                                s_std=s_std, i_std=i_std, prior_count = prior_count,
                                nChannels=self.nChannels)

        
    def calc_seg(self, nEMIters, nItersInner=10, calc_s_cov=True, prior_weight=0.5, verbose=False):
        """ 
        Inference:
        hard-EM (Expectation & Maximization)
        Alternate between the E step (update superpixel assignments) 
        and the M step (update parameters)

        Arguments:
        calc_s_cov: whether or not to update the covariance of color component of Gaussians
        prior_weight: the weight placed on the prior probability of a superpixel

        """
        if verbose:
            print 'start'
            
        for i in range(nEMIters): 
            if verbose:
                print 'iteration',i
            "M step"
            self._calc_param(img_gpu=self.img.gpu, img_isNaN_gpu =self.img_isNaN.gpu, seg_gpu=self.seg.gpu, 
                            sp=self.superpixels, 
                            calculate_s_cov=calc_s_cov)
            "(Hard) E step"
            self._calc_seg(img_gpu=self.img.gpu,  img_isNaN_gpu =self.img_isNaN.gpu,  seg_gpu=self.seg.gpu,
                            border_gpu=self.border.gpu,
                            counts_gpu=self.superpixels.params.counts.gpu,
                            log_count_gpu = self.superpixels.gpu_helper.log_count_helper,
                            superpixels=self.superpixels,
                            nIters=nItersInner,
                            calculate_cov=calc_s_cov,
                            prior_prob_weight = prior_weight)       
        # update the border for display the single border image
        find_border_pixels(seg_gpu=self.seg.gpu, border_gpu=self.border.gpu, single_border=1)


    def gpu2cpu(self):
        """
        Transfer the needed parameters from gpu to cpu.
        Note this is usually slow (when the image and/or nSuperpixels is large)
        """
        if self.img.ndim == 3:
            lab_to_rgb(self.superpixels.params.mu_i.gpu) #convert mu_i.gpu from lab to rgb
            lab_to_rgb(self.img.gpu) # convert img.gpu from lab to rgb
        
        params = self.superpixels.params
        for arg in [self.seg, self.border, self.img, params.counts, params.mu_s, params.mu_i, params.Sigma_s]:
            arg.gpu2cpu()


    def get_img_overlaid(self):
        """
        Set the pixels on the superpixel boundary to red

        """
        img = self.img.cpu    
        border = self.border.cpu    
        nChannels = self.nChannels                 
        if nChannels==1: 
            img_disp = np.zeros((img.shape[0],img.shape[1],3),np.uint8)
            img_disp[:,:,0]=img_disp[:,:,1]=img_disp[:,:,2]=img
        elif nChannels==3:
            img_disp = img.copy().astype(np.uint8)
        img_disp[:,:,0][border] = 255
        img_disp[:,:,1][border] = 0
        img_disp[:,:,2][border] = 0 
        return img_disp             
             

    def get_cartoon(self):
        """
        Replace pixels with superpixels means.

        """   
        img = self.img.cpu   
        nChannels = self.nChannels             
        img_disp = CpuGpuArray.zeros((img.shape[0],img.shape[1],3),dtype=np.int32)     
        get_cartoon(seg_gpu = self.seg.gpu, mu_i_gpu = self.superpixels.params.mu_i.gpu, 
                    img_gpu= img_disp.gpu, nChannels=nChannels)
        img_disp.gpu2cpu()
        return img_disp.cpu
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
Пример #49
0
    def __init__(self, dimy, dimx, nPixels_in_square_side=None, permute_seg=False,
                 s_std = 20, i_std=20, prior_count = 1,
                 calc_s_cov=True,
                 use_hex=False):
        """
        arguments:
        nPixels_in_square_side: number of the pixels on the side of a superpixels
        permute_seg: only for display purpose, whether to permute the superpixels labling, 
        s_std: fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        calc_s_cov:  whether or not to update the covariance of color component of Gaussians
        use_hex: initialize the superpixels as hexagons or squares
        
        """

        # init the field "nPixels_in_square_side"
        if nPixels_in_square_side is None:
            raise NotImplementedError
        self.nPixels_in_square_side=nPixels_in_square_side
        if not (3<nPixels_in_square_side<min(dimx,dimy)):
            msg = """
            Expected
            3<nPixels_in_square_side<min(dimx,dimy)={1}
            but nPixels_in_square_side={0}
            """.format(nPixels_in_square_side,min(dimx,dimy))
            raise ValueError(msg)

        self.dimx=dimx
        self.dimy=dimy
        self.nChannels = 3 # RGB/LAB

        #init the field "seg"
        self.use_hex = use_hex
        self.permute_seg = permute_seg
        seg = get_init_seg(dimy=dimy,dimx=dimx,
                          nPixels_in_square_side=nPixels_in_square_side, use_hex = use_hex)   
        if permute_seg:
            seg = random_permute_seg(seg)  
        self.seg = CpuGpuArray(arr=seg)     
             
        # keep the initial segmentation for use in images of the same size 
        # so that we won't re-calculate the initial segmenation
        self.seg_ini = self.seg.cpu.copy() 

        #init the field nSuperpixels
        self.nSuperpixels = self.seg.cpu.max()+1 
        if self.nSuperpixels <= 1:
            raise ValueError(self.nSuperpixels)     

        #init the field "superpixels"
        self.superpixels = Superpixels(self.nSuperpixels,
                            s_std=s_std, i_std=i_std, prior_count = prior_count, 
                            nChannels=self.nChannels)

        #init the field "border"(bool array, true for pixels on the superpixel boundary)
        border = gpuarray.zeros((dimy,dimx),dtype=np.bool)
        self.border = CpuGpuArray(arr=border)
        find_border_pixels(seg_gpu=self.seg.gpu, border_gpu=self.border.gpu) 
        self.border.gpu2cpu()        
        self.border_ini = self.border.cpu.copy() 
        
        print 'dimy,dimx=',dimy,dimx
        print 'nSuperpixels =',self.nSuperpixels
Пример #50
0
    def __init__(self, nSuperpixels, s_std, i_std, prior_count, nChannels):
        """
        Initilize the parameters for the superpixels:

        The means are set to zeros at this point, 
        and will be set later in the first M step.
        The space/color covariances (and their inverse), however, are being set 
        to initial values here.    
        We use a Inverse-Wishart prior on the space covariance

        Arguments:
        nSuperpixels: the number of superpixels to generate
        s_std: should be fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        nChannels: the number of channels of the input image (gray:1, LAB/RGB: 3)

        """
        if nChannels not in (1,3):
            raise NotImplementedError(nChannels)
        dim_i=nChannels
        dim_s=2 
        self.dim_i=dim_i
        self.dim_s=dim_s

        self.nSuperpixels=nSuperpixels 
        
        self.s_std, self.i_std, self.prior_count = s_std,i_std,prior_count
   
        
        mu_s = CpuGpuArray.zeros((nSuperpixels,dim_s)) 
        mu_i = CpuGpuArray.zeros((nSuperpixels,dim_i)) 
     
        Sigma_s = CpuGpuArray.zeros(shape = (nSuperpixels,dim_s,dim_s))
        J_s = CpuGpuArray.zeros_like(Sigma_s)
        
        Sigma_i = CpuGpuArray.zeros((nSuperpixels,dim_i,dim_i))      
        J_i = CpuGpuArray.zeros_like(Sigma_i)

        logdet_Sigma_i = CpuGpuArray.zeros((nSuperpixels,1)) # scalars
        logdet_Sigma_s = CpuGpuArray.zeros((nSuperpixels,1)) 

        # start with unnormalized counts (uniform)        
        counts = np.ones(nSuperpixels,dtype=np.int32)
        counts = CpuGpuArray(counts)
        
        self.params = Bunch()
        self.params.mu_i = mu_i
        self.params.mu_s = mu_s
        self.params.Sigma_i = Sigma_i
        self.params.Sigma_s = Sigma_s  
        self.params.prior_sigma_s_sum = Sigma_s
        self.params.J_i = J_i
        self.params.J_s = J_s      
        self.params.logdet_Sigma_i = logdet_Sigma_i
        self.params.logdet_Sigma_s = logdet_Sigma_s
        self.params.counts = counts   
        
        # set those parameters related to covariance
        self.initialize_params()
        
        # intermediate arrays needed for the Gaussian parameter calculation on GPU
        self.gpu_helper = Bunch()
        
        self.gpu_helper.mu_i_helper = gpuarray.zeros((nSuperpixels,dim_i),dtype=np.int64)
        self.gpu_helper.mu_s_helper = gpuarray.zeros((nSuperpixels,dim_s),dtype=np.int64)               
        self.gpu_helper.prior_sigma_s = self.params.prior_sigma_s_sum.gpu.copy() 
        self.gpu_helper.sigma_s_helper = gpuarray.zeros((nSuperpixels,3),dtype=np.int64)

        self.gpu_helper.log_count_helper = gpuarray.zeros(nSuperpixels,dtype=np.double)
        self.gpu_helper.non_NaN_count = gpuarray.zeros(nSuperpixels,dtype=np.int32)
        self.gpu_helper.NaN_count = gpuarray.zeros(nSuperpixels,dtype=np.int32)
Пример #51
0
    def __init__(
            self,
            nRows,
            nCols,
            vol_preserve=False,
            nLevels=1,
            base=[2, 2],
            scale_spatial=1.0 * .1,
            scale_value=100,
            zero_v_across_bdry=[False] * 2,  # For now, don't change that.
            tess=None,
            valid_outside=True,
            only_local=False,
            cont_constraints_are_separable=False):
        """
        Input params:
            nRows: number of rows in the image
            nCols: number of cols in the image
            vol_preserve: boolean flag (area-preserving or not)
            nLevels: number of levels in the multiscale representation
            base = (# of cells in X direction, # of cells in Y direction)
                   Determines the resolution of the tesselation as the base 
                   level of the multiscale representation.
            scale_spatial: paramter for the Gaussian prior. Higher val <=> more smoothness                   
            scale_value: paramter for the Gaussian prior. Higher val <=> larger covariance
            
            
        """

        super(type(self), self).__init__(
            vol_preserve=vol_preserve,
            nLevels=nLevels,
            base=base,
            scale_spatial=scale_spatial,
            scale_value=scale_value,
            zero_v_across_bdry=zero_v_across_bdry,
            tess=tess,
            valid_outside=valid_outside,
            only_local=only_local,
            cont_constraints_are_separable=cont_constraints_are_separable)

        self.nRows = self.args.nRows = nRows
        self.nCols = self.args.nCols = nCols
        print self.args
        #        print

        if tess == 'tri':
            tess = 'I'
        if tess == 'rect':
            tess == 'II'

        if tess not in ['I', 'II']:
            raise ValueError(tess, "tess must be in ['I','II']")
        if only_local and tess != 'I':
            raise NotImplementedError

        self.nRows = nRows
        self.nCols = nCols

        XMINS = [0, 0]

        XMAXS = [nCols, nRows]  # Note: This inclusive; e.g., if your image is
        # 512x512, XMAXS=[512,512], not [511,511]

        #        XMINS=[-nCols/2,-nRows/2]
        #        XMAXS=[ nCols/2, nRows/2]

        warp_around = [False, False]  # For now, don't change that.
        #        zero_v_across_bdry=[False,False] # For now, don't change that.

        Nx = XMAXS[0] - XMINS[0]
        Ny = XMAXS[1] - XMINS[1]
        self.config_plt = ConfigPlt(Nx=Nx, Ny=Ny)
        Ngrids = [Nx, Ny]

        ms = Multiscale(
            XMINS,
            XMAXS,
            zero_v_across_bdry,
            vol_preserve,
            warp_around=warp_around,
            nLevels=nLevels,
            base=base,
            tess=tess,
            Ngrids=Ngrids,
            valid_outside=valid_outside,
            only_local=only_local,
            cont_constraints_are_separable=cont_constraints_are_separable)

        self.ms = ms

        if only_local == False:
            self.msp = MultiscaleCoarse2FinePrior(ms,
                                                  scale_spatial=scale_spatial,
                                                  scale_value=scale_value,
                                                  left_blk_std_dev=1.0 / 100,
                                                  right_vec_scale=1)

        else:
            self.msp = None

        self.pts_src_dense = CpuGpuArray(ms.L_cpa_space[0].x_dense_img.copy())
        self.v_dense = CpuGpuArray.zeros_like(self.pts_src_dense)
        self.transformed_dense = CpuGpuArray.zeros_like(self.pts_src_dense)

        self.params_flow_int = get_params_flow_int()
        self.params_flow_int.nStepsODEsolver = 10  # Usually this is enough.

        self.params_flow_int_coarse = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_coarse.nTimeSteps /= 10
        self.params_flow_int_coarse.dt *= 10

        self.params_flow_int_fine = copy.deepcopy(self.params_flow_int)
        self.params_flow_int_fine.nTimeSteps *= 10
        self.params_flow_int_fine.dt /= 10

        self.ms_pats = ms.pats
Пример #52
0
def example(tess='I',
            base=[2, 2, 2],
            nLevels=1,
            zero_v_across_bdry=[True] * 3,
            vol_preserve=False,
            nRows=100,
            nCols=100,
            nSlices=100,
            use_mayavi=False,
            eval_v=False,
            eval_cell_idx=False):

    tw = TransformWrapper(nRows=nRows,
                          nCols=nCols,
                          nSlices=nSlices,
                          nLevels=nLevels,
                          base=base,
                          zero_v_across_bdry=zero_v_across_bdry,
                          tess=tess,
                          valid_outside=False,
                          only_local=False,
                          vol_preserve=vol_preserve)

    print_iterable(tw.ms.L_cpa_space)
    print tw

    # create some fake 3D image.
    img = np.zeros((nCols, nRows, nSlices), dtype=np.float64)

    #    img[:]=np.random.random_integers(0,255,img.shape)

    # Fill the image with the x coordinates as fake values
    img[:] = tw.pts_src_dense.cpu[:, 0].reshape(img.shape)

    img0 = CpuGpuArray(img.copy().astype(np.float64))
    img_wrapped_fwd = CpuGpuArray.zeros_like(img0)
    img_wrapped_inv = CpuGpuArray.zeros_like(img0)

    seed = 0
    np.random.seed(seed)

    ms_Avees = tw.get_zeros_PA_all_levels()
    ms_theta = tw.get_zeros_theta_all_levels()

    if tess == 'II':
        for level in range(tw.ms.nLevels):
            cpa_space = tw.ms.L_cpa_space[level]
            Avees = ms_Avees[level]
            #            1/0
            if level == 0:
                tw.sample_gaussian(level,
                                   ms_Avees[level],
                                   ms_theta[level],
                                   mu=None)  # zero mean
                #                ms_theta[level].fill(0)
                #                ms_theta[level][-4]=10
                cpa_space.theta2Avees(theta=ms_theta[level], Avees=Avees)
            else:
                tw.sample_from_the_ms_prior_coarse2fine_one_level(
                    ms_Avees, ms_theta, level_fine=level)
    else:
        # For tess='I' in 3D, I have yet to implement the coarse-to-fine sampling.
        for level in range(tw.ms.nLevels):
            cpa_space = tw.ms.L_cpa_space[level]
            velTess = cpa_space.zeros_velTess()
            ms_Avees[level].fill(0)
            Avees = ms_Avees[level]
            tw.sample_gaussian_velTess(level, Avees, velTess, mu=None)

    print 'img shape:', img0.shape

    # You don't have use these. You can use any 2d array
    # that has 3 columns (regardless of the number of rows).
    pts_src = tw.pts_src_dense
    pts_src = CpuGpuArray(pts_src.cpu[::1].copy())

    # Create a buffer for the output
    pts_fwd = CpuGpuArray.zeros_like(pts_src)
    pts_inv = CpuGpuArray.zeros_like(pts_src)

    for level in range(tw.ms.nLevels):
        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)

        print 'level', level
        print
        print 'number of points:', len(pts_src)
        print 'number of cells:', tw.ms.L_cpa_space[level].nC

        # 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)

        tw.remap_fwd(pts_inv, img0, img_wrapped_fwd)
        tw.remap_inv(pts_fwd, img0, img_wrapped_inv)

        # 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_inv.gpu2cpu()

        if use_mayavi:
            ds = 1  # downsampling factor
            i = 17
            pts_src_grid = pts_src.cpu.reshape(tw.nRows, tw.nCols, -1, 3)
            pts_src_ds = pts_src_grid[::ds, ::ds, i].reshape(-1, 3)
            pts_fwd_grid = pts_fwd.cpu.reshape(tw.nRows, tw.nCols, -1, 3)
            pts_fwd_ds = pts_fwd_grid[::ds, ::ds, i].reshape(-1, 3)
            pts_inv_grid = pts_inv.cpu.reshape(tw.nRows, tw.nCols, -1, 3)
            pts_inv_ds = pts_inv_grid[::ds, ::ds, i].reshape(-1, 3)

            from of.my_mayavi import *
            mayavi_mlab_close_all()
            mayavi_mlab_figure_bgwhite('src')
            x, y, z = pts_src_ds.T
            mayavi_mlab_plot3d(x, y, z)
            mayavi_mlab_figure_bgwhite('fwd')
            x, y, z = pts_fwd_ds.T
            mayavi_mlab_plot3d(x, y, z)

        figsize = (12, 12)
        plt.figure(figsize=figsize)
        i = 17  # some slice
        plt.subplot(131)
        plt.imshow(img0.cpu[:, :, i].astype(np.uint8), interpolation="Nearest")
        plt.title('slice from img')
        plt.subplot(132)
        plt.imshow(img_wrapped_fwd.cpu[:, :, i].astype(np.uint8),
                   interpolation="Nearest")
        plt.axis('off')
        plt.title('slice from fwd(img)')
        plt.subplot(133)
        plt.imshow(img_wrapped_inv.cpu[:, :, i].astype(np.uint8),
                   interpolation="Nearest")
        plt.axis('off')
        plt.title('slice from inv(img)')

    if 0:  # debug

        cpa_space = tw.ms.L_cpa_space[level]
        if eval_v:
            vx = tw.v_dense.cpu[:, 0].reshape(
                cpa_space.x_dense_grid_img.shape[1:])
            vy = tw.v_dense.cpu[:, 1].reshape(
                cpa_space.x_dense_grid_img.shape[1:])
            vz = tw.v_dense.cpu[:, 2].reshape(
                cpa_space.x_dense_grid_img.shape[1:])

            plt.figure()
            plt.imshow(vz[:, :, 17], interpolation="Nearest")
            plt.colorbar()
            plt.title('vz in some slice')

    return tw
Пример #53
0
    def disp_orig_grid_lines(self, level, color=None, lw=1):
        #        return
        try:
            self.hlines
            self.vlines
        except AttributeError:
            raise Exception("You need to call create_grid_lines first")

        if self.hlines is None:
            raise ValueError
        if self.vlines is None:
            self.vlines = self.hlines
        hlines, vlines = self.hlines, self.vlines

        s = hlines.shape
        if s[2] <= 1:
            raise ValueError
        p = 0
        L = 50000
        if color is None:
            colors = ['r', 'b']
        else:
            colors = [color, color]
        if L >= s[2]:
            while p < np.ceil(s[2]):
                hlines = self.hlines[:, :, p:p + L]
                vlines = self.vlines[:, :, p:p + L]
                p += L - 1

                for lines, c in zip([hlines, vlines], colors):
                    pts_at_0 = np.asarray(
                        [lines[:, 0, :].flatten(), lines[:, 1, :].flatten()]).T
                    if pts_at_0.size == 0:
                        break
        #            print _pts_at_0.shape

                    pts_at_0 = CpuGpuArray(pts_at_0.copy())

                    if self.nCols != self.nCols:
                        raise NotImplementedError
                    pts_at_0.gpu2cpu()

                    lines_new_x = pts_at_0.cpu[:, 0].reshape(
                        lines[:, 0, :].shape).copy()
                    lines_new_y = pts_at_0.cpu[:, 1].reshape(
                        lines[:, 0, :].shape).copy()
                    for line_new_x, line_new_y in zip(lines_new_x,
                                                      lines_new_y):
                        plt.plot(line_new_x, line_new_y, c, lw=lw)
        else:
            hlines = self.hlines
            vlines = self.vlines

            for lines, c in zip([hlines, vlines], colors):
                pts_at_0 = np.asarray(
                    [lines[:, 0, :].flatten(), lines[:, 1, :].flatten()]).T
                if pts_at_0.size == 0:
                    break
        #            print _pts_at_0.shape

                pts_at_0 = CpuGpuArray(pts_at_0.copy())

                if self.nCols != self.nCols:
                    raise NotImplementedError
                pts_at_0.gpu2cpu()

                lines_new_x = pts_at_0.cpu[:, 0].reshape(
                    lines[:, 0, :].shape).copy()
                lines_new_y = pts_at_0.cpu[:, 1].reshape(
                    lines[:, 0, :].shape).copy()
                for line_new_x, line_new_y in zip(lines_new_x, lines_new_y):
                    plt.plot(line_new_x, line_new_y, c, lw=lw)
Пример #54
0
    def __init__(self, nSuperpixels, s_std, i_std, prior_count, nChannels):
        """
        Initilize the parameters for the superpixels:

        The means are set to zeros at this point, 
        and will be set later in the first M step.
        The space/color covariances (and their inverse), however, are being set 
        to initial values here.    
        We use a Inverse-Wishart prior on the space covariance

        Arguments:
        nSuperpixels: the number of superpixels to generate
        s_std: should be fixed as nPixels_on_side            
        i_std: control the relative importance between RGB and location.
               The smaller it is, bigger the RGB effect is / more irregular the superpixels are.
        prior_count: determines the weight of Inverse-Wishart prior of space covariance(ex:1,5,10)
        nChannels: the number of channels of the input image (gray:1, LAB/RGB: 3)

        """
        if nChannels not in (1,3):
            raise NotImplementedError(nChannels)
        dim_i=nChannels
        dim_s=2 
        self.dim_i=dim_i
        self.dim_s=dim_s

        self.nSuperpixels=nSuperpixels 
        
        self.s_std, self.i_std, self.prior_count = s_std,i_std,prior_count
   
        
        mu_s = CpuGpuArray.zeros((nSuperpixels,dim_s)) 
        mu_i = CpuGpuArray.zeros((nSuperpixels,dim_i)) 
     
        Sigma_s = CpuGpuArray.zeros(shape = (nSuperpixels,dim_s,dim_s))
        J_s = CpuGpuArray.zeros_like(Sigma_s)
        
        Sigma_i = CpuGpuArray.zeros((nSuperpixels,dim_i,dim_i))      
        J_i = CpuGpuArray.zeros_like(Sigma_i)

        logdet_Sigma_i = CpuGpuArray.zeros((nSuperpixels,1)) # scalars
        logdet_Sigma_s = CpuGpuArray.zeros((nSuperpixels,1)) 

        # start with unnormalized counts (uniform)        
        counts = np.ones(nSuperpixels,dtype=np.int32)
        counts = CpuGpuArray(counts)
        
        self.params = Bunch()
        self.params.mu_i = mu_i
        self.params.mu_s = mu_s
        self.params.Sigma_i = Sigma_i
        self.params.Sigma_s = Sigma_s  
        self.params.prior_sigma_s_sum = Sigma_s
        self.params.J_i = J_i
        self.params.J_s = J_s      
        self.params.logdet_Sigma_i = logdet_Sigma_i
        self.params.logdet_Sigma_s = logdet_Sigma_s
        self.params.counts = counts   
        
        # set those parameters related to covariance
        self.initialize_params()
        
        # intermediate arrays needed for the Gaussian parameter calculation on GPU
        self.gpu_helper = Bunch()
        self.gpu_helper.mu_i_helper = gpuarray.zeros((nSuperpixels,dim_i),dtype=np.int32)
        self.gpu_helper.mu_s_helper = gpuarray.zeros((nSuperpixels,dim_s),dtype=np.int32)               
        self.gpu_helper.prior_sigma_s = self.params.prior_sigma_s_sum.gpu.copy() 
        self.gpu_helper.sigma_s_helper = gpuarray.zeros((nSuperpixels,3),dtype=np.int64)
        self.gpu_helper.log_count_helper = gpuarray.zeros((nSuperpixels,1),dtype=np.double)
Пример #55
0
    def disp_deformed_grid_lines(self, level, color=None, lw=1):
        #        return
        if self.hlines is None or self.vlines is None:
            raise ValueError
        hlines, vlines = self.hlines, self.vlines
        #        for lines,c in zip([hlines,vlines],['r','b']):
        #            pts_at_0=np.asarray([lines[:,0,:].flatten(),
        #                                 lines[:,1,:].flatten()]).T
        #            pts_at_0 = CpuGpuArray(pts_at_0.copy())
        #            pts_at_T=CpuGpuArray.zeros_like(pts_at_0)
        #            self.calc_T_fwd(pts_src=pts_at_0,
        #                      pts_fwd=pts_at_T,
        #                      level=level,verbose=0,int_quality=1)
        #            if self.nCols != self.nCols:
        #                            raise NotImplementedError
        #            pts_at_T.gpu2cpu()
        #            lines_new_x=pts_at_T.cpu[:,0].reshape(lines[:,0,:].shape).copy()
        #            lines_new_y=pts_at_T.cpu[:,1].reshape(lines[:,0,:].shape).copy()
        #            for line_new_x,line_new_y in zip(lines_new_x,lines_new_y):
        #
        #                        plt.plot(line_new_x,line_new_y,c)
        if color is None:
            colors = ['r', 'b']
        else:
            colors = [color, color]

        s = hlines.shape
        if s[2] <= 1:
            raise ValueError
        p = 0
        L = 50000

        if L >= s[2]:

            while p < np.ceil(s[2]):
                hlines = self.hlines[:, :, p:p + L]
                vlines = self.vlines[:, :, p:p + L]
                p += L

                for lines, c in zip([hlines, vlines], colors):
                    pts_at_0 = np.asarray(
                        [lines[:, 0, :].flatten(), lines[:, 1, :].flatten()]).T
                    if pts_at_0.size == 0:
                        break
                    pts_at_0 = CpuGpuArray(pts_at_0.copy())
                    pts_at_T = CpuGpuArray.zeros_like(pts_at_0)
                    self.calc_T_fwd(pts_src=pts_at_0,
                                    pts_fwd=pts_at_T,
                                    level=level,
                                    int_quality=1)
                    if self.nCols != self.nCols:
                        raise NotImplementedError
                    pts_at_T.gpu2cpu()
                    lines_new_x = pts_at_T.cpu[:, 0].reshape(
                        lines[:, 0, :].shape).copy()
                    lines_new_y = pts_at_T.cpu[:, 1].reshape(
                        lines[:, 0, :].shape).copy()
                    for line_new_x, line_new_y in zip(lines_new_x,
                                                      lines_new_y):
                        plt.plot(line_new_x, line_new_y, c, lw=lw)
        else:
            raise NotImplementedError