當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。