当前位置: 首页>>代码示例>>Python>>正文


Python densecrf.DenseCRF2D方法代码示例

本文整理汇总了Python中pydensecrf.densecrf.DenseCRF2D方法的典型用法代码示例。如果您正苦于以下问题:Python densecrf.DenseCRF2D方法的具体用法?Python densecrf.DenseCRF2D怎么用?Python densecrf.DenseCRF2D使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pydensecrf.densecrf的用法示例。


在下文中一共展示了densecrf.DenseCRF2D方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: dense_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf(img, output_probs):
    c = output_probs.shape[0]
    h = output_probs.shape[1]
    w = output_probs.shape[2]

    U = utils.unary_from_softmax(output_probs)
    U = np.ascontiguousarray(U)

    img = np.ascontiguousarray(img)

    d = dcrf.DenseCRF2D(w, h, c)
    d.setUnaryEnergy(U)
    d.addPairwiseGaussian(sxy=POS_XY_STD, compat=POS_W)
    d.addPairwiseBilateral(sxy=Bi_XY_STD, srgb=Bi_RGB_STD, rgbim=img, compat=Bi_W)

    Q = d.inference(MAX_ITER)
    Q = np.array(Q).reshape((c, h, w))
    return Q 
开发者ID:subhc,项目名称:SPNet,代码行数:20,代码来源:crf.py

示例2: dense_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf(img, output_probs):
    h = output_probs.shape[0]
    w = output_probs.shape[1]

    output_probs = np.expand_dims(output_probs, 0)
    output_probs = np.append(1 - output_probs, output_probs, axis=0)

    d = dcrf.DenseCRF2D(w, h, 2)
    U = -np.log(output_probs)
    U = U.reshape((2, -1))
    U = np.ascontiguousarray(U)
    img = np.ascontiguousarray(img)

    d.setUnaryEnergy(U)

    d.addPairwiseGaussian(sxy=20, compat=3)
    d.addPairwiseBilateral(sxy=30, srgb=20, rgbim=img, compat=10)

    Q = d.inference(5)
    Q = np.argmax(np.array(Q), axis=0).reshape((h, w))

    return Q 
开发者ID:openseg-group,项目名称:openseg.pytorch,代码行数:24,代码来源:dense_crf.py

示例3: crf_inference

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def crf_inference(img, probs, t=10, scale_factor=1, labels=21):
    import pydensecrf.densecrf as dcrf
    from pydensecrf.utils import unary_from_softmax

    h, w = img.shape[:2]
    n_labels = labels

    d = dcrf.DenseCRF2D(w, h, n_labels)

    unary = unary_from_softmax(probs)
    unary = np.ascontiguousarray(unary)

    d.setUnaryEnergy(unary)
    d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
    d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
    Q = d.inference(t)

    return np.array(Q).reshape((n_labels, h, w)) 
开发者ID:YudeWang,项目名称:SSENet-pytorch,代码行数:20,代码来源:imutils.py

示例4: dense_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf(probs, img=None, n_classes=21, n_iters=1, scale_factor=1):
	c,h,w = probs.shape

	if img is not None:
		assert(img.shape[1:3] == (h, w))
		img = np.transpose(img,(1,2,0)).copy(order='C')

	d = dcrf.DenseCRF2D(w, h, n_classes) # Define DenseCRF model.
	
	unary = unary_from_softmax(probs)
	unary = np.ascontiguousarray(unary)
	d.setUnaryEnergy(unary)
	d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
	d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
	Q = d.inference(n_iters)

	preds = np.array(Q, dtype=np.float32).reshape((n_classes, h, w))
	return preds 
开发者ID:YudeWang,项目名称:SSENet-pytorch,代码行数:20,代码来源:visualization.py

示例5: __call__

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def __call__(self, image, probmap):
        C, H, W = probmap.shape

        U = utils.unary_from_softmax(probmap)
        U = np.ascontiguousarray(U)

        image = np.ascontiguousarray(image)

        d = dcrf.DenseCRF2D(W, H, C)
        d.setUnaryEnergy(U)
        d.addPairwiseGaussian(sxy=self.pos_xy_std, compat=self.pos_w)
        d.addPairwiseBilateral(
            sxy=self.bi_xy_std, srgb=self.bi_rgb_std, rgbim=image, compat=self.bi_w
        )

        Q = d.inference(self.iter_max)
        Q = np.array(Q).reshape((C, H, W))

        return Q 
开发者ID:kazuto1011,项目名称:deeplab-pytorch,代码行数:21,代码来源:crf.py

示例6: dense_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf(probs, img=None, n_iters=10, n_classes=19,
              sxy_gaussian=(1, 1), compat_gaussian=4,
              sxy_bilateral=(49, 49), compat_bilateral=5,
              srgb_bilateral=(13, 13, 13)):
    import pydensecrf.densecrf as dcrf
    _, h, w, _ = probs.shape

    probs = probs[0].transpose(2, 0, 1).copy(order='C')  # Need a contiguous array.

    d = dcrf.DenseCRF2D(w, h, n_classes)  # Define DenseCRF model.
    U = -np.log(probs)  # Unary potential.
    U = U.reshape((n_classes, -1))  # Needs to be flat.
    d.setUnaryEnergy(U)
    d.addPairwiseGaussian(sxy=sxy_gaussian, compat=compat_gaussian,
                          kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
    if img is not None:
        assert (img.shape[1:3] == (h, w)), "The image height and width must coincide with dimensions of the logits."
        d.addPairwiseBilateral(sxy=sxy_bilateral, compat=compat_bilateral,
                               kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC,
                               srgb=srgb_bilateral, rgbim=img[0])
    Q = d.inference(n_iters)
    preds = np.array(Q, dtype=np.float32).reshape((n_classes, h, w)).transpose(1, 2, 0)
    return np.expand_dims(preds, 0) 
开发者ID:holyseven,项目名称:TransferLearningClassification,代码行数:25,代码来源:utils.py

示例7: dense_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf(img, probs):
    c = probs.shape[0]
    h = probs.shape[1]
    w = probs.shape[2]

    U = utils.unary_from_softmax(probs)
    U = np.ascontiguousarray(U)

    img = np.ascontiguousarray(img)

    d = dcrf.DenseCRF2D(w, h, c)
    d.setUnaryEnergy(U)
    d.addPairwiseGaussian(sxy=POS_XY_STD, compat=POS_W)
    d.addPairwiseBilateral(sxy=Bi_XY_STD, srgb=Bi_RGB_STD, rgbim=img, compat=Bi_W)

    Q = d.inference(MAX_ITER)
    Q = np.array(Q).reshape((c, h, w))

    return Q 
开发者ID:kazuto1011,项目名称:pspnet-pytorch,代码行数:21,代码来源:crf.py

示例8: run_dense_CRF

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def run_dense_CRF(self):
        # [w, h, class] to [class, w, h]
        U = self.masks.transpose(2, 0, 1).reshape((self.classes, -1))
        U = U.copy(order='C')
        # declare width, height, class
        d = dcrf.DenseCRF2D(self.height, self.width, self.classes)
        # set unary potential
        d.setUnaryEnergy(-np.log(U))
        # set pairwise potentials
        d.addPairwiseGaussian(sxy=(3, 3), compat=3)
        d.addPairwiseBilateral(sxy=80, srgb=13, rgbim=self.img, compat=10)
        # inference with 5 iterations
        Q = d.inference(5)
        # MAP prediction
        map = np.argmax(Q, axis=0).reshape((self.width, self.height))
        # class-probabilities
        proba = np.array(map)

        return proba 
开发者ID:johnnylu305,项目名称:Simple-does-it-weakly-supervised-instance-and-semantic-segmentation,代码行数:21,代码来源:dense_CRF.py

示例9: crf_processing

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def crf_processing(self, image, label, soft_label=False):
        crf = dcrf.DenseCRF2D(image.shape[1], image.shape[0], 2)
        if not soft_label:
            unary = unary_from_labels(label, 2, gt_prob=0.9, zero_unsure=False)
        else:
            if len(label.shape)==2:
                p_neg = 1.0 - label
                label = np.concatenate((p_neg[...,np.newaxis], label[...,np.newaxis]), axis=2)
            label = label.transpose((2,0,1))
            unary = unary_from_softmax(label)
        crf.setUnaryEnergy(unary)
        crf.addPairwiseGaussian(sxy=(3,3), compat=3)
        crf.addPairwiseBilateral(sxy=(40, 40), srgb=(5, 5, 5), rgbim=image, compat=10)
        crf_out = crf.inference(self.crf_infer_steps)

        # Find out the most probable class for each pixel.
        return np.argmax(crf_out, axis=0).reshape((image.shape[0], image.shape[1])) 
开发者ID:linjieyangsc,项目名称:video_seg,代码行数:19,代码来源:dataset_davis.py

示例10: run_multiclass_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def run_multiclass_crf(seq, fn, posteriors, softmax_scale, sxy1, compat1, sxy2, compat2, srgb):
  im_fn = DAVIS2017_DIR + "JPEGImages/480p/" + seq + "/" + fn.replace(".pickle", ".jpg")
  im = imread(im_fn)
  nlabels = posteriors.shape[-1]

  im = numpy.ascontiguousarray(im)
  pred = numpy.ascontiguousarray(posteriors.swapaxes(0, 2).swapaxes(1, 2))

  d = dcrf.DenseCRF2D(im.shape[1], im.shape[0], nlabels)  # width, height, nlabels
  unaries = unary_from_softmax(pred, scale=softmax_scale)
  d.setUnaryEnergy(unaries)

  d.addPairwiseGaussian(sxy=sxy1, compat=compat1)
  d.addPairwiseBilateral(sxy=sxy2, srgb=srgb, rgbim=im, compat=compat2)
  processed = d.inference(12)
  res = numpy.argmax(processed, axis=0).reshape(im.shape[:2])
  return res 
开发者ID:JonathonLuiten,项目名称:PReMVOS,代码行数:19,代码来源:combine_single_object_predictions_crf.py

示例11: apply_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def apply_crf(im, pred):
  im = numpy.ascontiguousarray(im)
  pred = numpy.ascontiguousarray(pred.swapaxes(0, 2).swapaxes(1, 2))

  d = dcrf.DenseCRF2D(854, 480, 2)  # width, height, nlabels
  unaries = unary_from_softmax(pred, scale=1.0)
  d.setUnaryEnergy(unaries)

  #print im.shape
  # print annot.shape
  #print pred.shape

  d.addPairwiseGaussian(sxy=0.220880737269, compat=1.24845093352)
  d.addPairwiseBilateral(sxy=22.3761305044, srgb=7.70254062277, rgbim=im, compat=1.40326787165)
  processed = d.inference(12)
  res = numpy.argmax(processed, axis=0).reshape(480, 854)

  return res 
开发者ID:JonathonLuiten,项目名称:PReMVOS,代码行数:20,代码来源:crf_davis.py

示例12: apply_crf

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def apply_crf(im, pred):
  im = numpy.ascontiguousarray(im)
  if im.shape[:2] != pred.shape[:2]:
    im = imresize(im, pred.shape[:2])

  pred = numpy.ascontiguousarray(pred.swapaxes(0, 2).swapaxes(1, 2))

  d = dcrf.DenseCRF2D(im.shape[1], im.shape[0], 2)  # width, height, nlabels
  unaries = unary_from_softmax(pred, scale=1.0)
  d.setUnaryEnergy(unaries)

  d.addPairwiseGaussian(sxy=0.220880737269, compat=1.24845093352)
  d.addPairwiseBilateral(sxy=22.3761305044, srgb=7.70254062277, rgbim=im, compat=1.40326787165)
  processed = d.inference(12)
  res = numpy.argmax(processed, axis=0).reshape(im.shape[:2])

  return res 
开发者ID:JonathonLuiten,项目名称:PReMVOS,代码行数:19,代码来源:crf_youtube.py

示例13: getCRFResult

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def getCRFResult(result, img):

    _d = dcrf.DenseCRF2D(config.img_height, config.img_width, config.classes)
    result = result[0]
    label = result.reshape((config.img_height, config.img_width, config.classes)).transpose((2, 0, 1))
    U = unary_from_softmax(label)
    _d.setUnaryEnergy(U)
    _d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
    _d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13),
        rgbim=img,
        compat=10,
        kernel=dcrf.DIAG_KERNEL,
        normalization=dcrf.NORMALIZE_SYMMETRIC
    )
    Q = _d.inference(1)
    return np.argmax(Q, axis=0).reshape((config.img_height, config.img_width)) 
开发者ID:mrm-xiefan,项目名称:lunania-ai,代码行数:18,代码来源:fcn.py

示例14: dense_crf_process

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def dense_crf_process(self, images, outputs):
        '''
        Reference: https://github.com/kazuto1011/deeplab-pytorch/blob/master/libs/utils/crf.py
        '''
        # hyperparameters of the dense crf 
        # baseline = 79.5
        # bi_xy_std = 67, 79.1
        # bi_xy_std = 20, 79.6
        # bi_xy_std = 10, 79.7
        # bi_xy_std = 10, iter_max = 20, v4 79.7
        # bi_xy_std = 10, iter_max = 5, v5 79.7
        # bi_xy_std = 5, v3 79.7
        iter_max = 10
        pos_w = 3
        pos_xy_std = 1
        bi_w = 4
        bi_xy_std = 10
        bi_rgb_std = 3

        b = images.size(0)
        mean_vector = np.expand_dims(np.expand_dims(np.transpose(np.array([102.9801, 115.9465, 122.7717])), axis=1), axis=2)
        outputs = F.softmax(outputs, dim=1)
        for i in range(b):
            unary = outputs[i].data.cpu().numpy()
            C, H, W = unary.shape
            unary = dcrf_utils.unary_from_softmax(unary)
            unary = np.ascontiguousarray(unary)
            
            image = np.ascontiguousarray(images[i]) + mean_vector
            image = image.astype(np.ubyte)
            image = np.ascontiguousarray(image.transpose(1, 2, 0))

            d = dcrf.DenseCRF2D(W, H, C)
            d.setUnaryEnergy(unary)
            d.addPairwiseGaussian(sxy=pos_xy_std, compat=pos_w)
            d.addPairwiseBilateral(sxy=bi_xy_std, srgb=bi_rgb_std, rgbim=image, compat=bi_w)
            out_crf = np.array(d.inference(iter_max))
            outputs[i] = torch.from_numpy(out_crf).cuda().view(C, H, W)

        return outputs 
开发者ID:openseg-group,项目名称:openseg.pytorch,代码行数:42,代码来源:tester.py

示例15: crf_PIL

# 需要导入模块: from pydensecrf import densecrf [as 别名]
# 或者: from pydensecrf.densecrf import DenseCRF2D [as 别名]
def crf_PIL(original_image, annotated_image,output_image, use_2d = True):

    annotated_labels = annotated_image.flatten()
    colors,labels = np.unique(annotated_labels, return_inverse=True)
    print(len(labels))
    print(labels)
    print(len(colors))
    print(colors)
    #Setting up the CRF model
    if use_2d :
        d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], len(colors))

        # get unary potentials (neg log probability)
        U = unary_from_labels(labels, len(colors), gt_prob=0.7, zero_unsure=False)
        d.setUnaryEnergy(U)

        # This adds the color-independent term, features are the locations only.
        d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
                          normalization=dcrf.NORMALIZE_SYMMETRIC)

        # This adds the color-dependent term, i.e. features are (x,y,r,g,b).
        d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,
                           compat=10,
                           kernel=dcrf.DIAG_KERNEL,
                           normalization=dcrf.NORMALIZE_SYMMETRIC)
        
    #Run Inference for 5 steps 
    Q = d.inference(5)

    # Find out the most probable class for each pixel.
    MAP = np.argmax(Q, axis=0)
    print(MAP.shape)

    # C将地图(标签)转换回相应的颜色并保存图像。
    # 注意,这里不再有“未知”,不管我们一开始拥有什么。
    MAP = MAP.reshape(original_image.shape[1],original_image.shape[0],1)
    cv2.imwrite(output_image,MAP)
    #MAP = array_to_img(MAP)
    #MAP.save(output_image)
    return MAP 
开发者ID:1044197988,项目名称:Semantic-segmentation-of-remote-sensing-images,代码行数:42,代码来源:CRF.py


注:本文中的pydensecrf.densecrf.DenseCRF2D方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。