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 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) if verbose: print("(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 __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 __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