Python pydensecrf.densecrf.DenseCRF2D() Examples

The following are 30 code examples of pydensecrf.densecrf.DenseCRF2D(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pydensecrf.densecrf , or try the search function .
Example #1
Source File: crf.py    From SPNet with MIT License 7 votes vote down vote up
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 
Example #2
Source File: crf.py    From PanopticSegmentation with MIT License 6 votes vote down vote up
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 
Example #3
Source File: fcn.py    From lunania-ai with MIT License 6 votes vote down vote up
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)) 
Example #4
Source File: crf_youtube.py    From PReMVOS with MIT License 6 votes vote down vote up
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 
Example #5
Source File: crf_davis.py    From PReMVOS with MIT License 6 votes vote down vote up
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 
Example #6
Source File: combine_single_object_predictions_crf.py    From PReMVOS with MIT License 6 votes vote down vote up
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 
Example #7
Source File: dataset_davis.py    From video_seg with Apache License 2.0 6 votes vote down vote up
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])) 
Example #8
Source File: dense_CRF.py    From Simple-does-it-weakly-supervised-instance-and-semantic-segmentation with Apache License 2.0 6 votes vote down vote up
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 
Example #9
Source File: imutils.py    From psa with MIT License 6 votes vote down vote up
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)) 
Example #10
Source File: utils.py    From TransferLearningClassification with MIT License 6 votes vote down vote up
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) 
Example #11
Source File: dense_crf.py    From openseg.pytorch with MIT License 6 votes vote down vote up
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 
Example #12
Source File: crf.py    From deeplab-pytorch with MIT License 6 votes vote down vote up
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 
Example #13
Source File: imutils.py    From SSENet-pytorch with MIT License 6 votes vote down vote up
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)) 
Example #14
Source File: crf.py    From pspnet-pytorch with MIT License 6 votes vote down vote up
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 
Example #15
Source File: visualization.py    From SSENet-pytorch with MIT License 6 votes vote down vote up
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 
Example #16
Source File: segment_densecrf.py    From SketchyScene with MIT License 5 votes vote down vote up
def seg_densecrf(prob_arr_, im_arr_, nlabels):
    """
    :param prob_arr_: shape = [nlabels, H, W], contains prob
    :param im_arr_:  shape = [H, W, 3], dtype == np.uint8
    :param nlabels:
    :return label_map: shape=[H, W], contains [0 ~ nlabels-1]
    """
    H = im_arr_.shape[0]
    W = im_arr_.shape[1]

    prob_arr = np.array(prob_arr_, np.float32)
    prob_min = np.amin(prob_arr)
    # print("prob_min", prob_min)
    prob_arr -= prob_min  # [0, ?]
    prob_arr += 0.0001    # [0.0001, ?]

    d = dcrf.DenseCRF2D(W, H, nlabels)

    U = np.array(-np.log(prob_arr), dtype=np.float32)  #shape = (nlabels, W, H), dtype('float32')
    U = U.reshape((nlabels, -1))  # Needs to be flat.
    U = U.copy(order='C')
    # print(U.flags)
    d.setUnaryEnergy(U)
    d.addPairwiseGaussian(sxy=3, compat=3)
    d.addPairwiseBilateral(sxy=7, srgb=3, rgbim=im_arr_, compat=10)

    Q = d.inference(5)
    label_map = np.argmax(Q, axis=0).reshape((H, W))  # shape=[H, W], contains [0 ~ nlabels-1]
    return label_map 
Example #17
Source File: misc.py    From R3Net with MIT License 5 votes vote down vote up
def crf_refine(img, annos):
    def _sigmoid(x):
        return 1 / (1 + np.exp(-x))

    assert img.dtype == np.uint8
    assert annos.dtype == np.uint8
    assert img.shape[:2] == annos.shape

    # img and annos should be np array with data type uint8

    EPSILON = 1e-8

    M = 2  # salient or not
    tau = 1.05
    # Setup the CRF model
    d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M)

    anno_norm = annos / 255.

    n_energy = -np.log((1.0 - anno_norm + EPSILON)) / (tau * _sigmoid(1 - anno_norm))
    p_energy = -np.log(anno_norm + EPSILON) / (tau * _sigmoid(anno_norm))

    U = np.zeros((M, img.shape[0] * img.shape[1]), dtype='float32')
    U[0, :] = n_energy.flatten()
    U[1, :] = p_energy.flatten()

    d.setUnaryEnergy(U)

    d.addPairwiseGaussian(sxy=3, compat=3)
    d.addPairwiseBilateral(sxy=60, srgb=5, rgbim=img, compat=5)

    # Do the inference
    infer = np.array(d.inference(1)).astype('float32')
    res = infer[1, :]

    res = res * 255
    res = res.reshape(img.shape[:2])
    return res.astype('uint8') 
Example #18
Source File: crf.py    From Deeplab-Large-FOV with Apache License 2.0 5 votes vote down vote up
def crf(img, scores):
    '''
    scores: prob numpy array after softmax with shape (_, C, H, W)
    img: image of shape (H, W, C)
    CRF parameters: bi_w = 4, bi_xy_std = 121, bi_rgb_std = 5, pos_w = 3, pos_xy_std = 3
    '''
    pos_w = 3
    pos_xy_std = 3
    bi_w = 4
    bi_xy_std = 121
    bi_rgb_std = 5

    scores = np.ascontiguousarray(scores[0])
    img = np.ascontiguousarray(img)
    n_classes, h, w = scores.shape

    d = dcrf.DenseCRF2D(w, h, n_classes)
    U = -np.log(scores)
    U = U.reshape((n_classes, -1))
    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(10)
    Q = np.argmax(np.array(Q), axis=0).reshape((h, w))

    return Q 
Example #19
Source File: crf_refine.py    From unsupervised_detection with MIT License 5 votes vote down vote up
def refine(mask, image, gk, sxy, srgb, compat, gtmask):
    dfield = dcrf.DenseCRF2D( mask.shape[1], mask.shape[0], 2 )

    U = gaussian_filter(mask, sigma=gk)
    U = U / (np.amax(U)+1e-8)
    U = np.clip(U, 1e-6, 1.0-1e-6)
    UU = np.zeros((2,mask.shape[0],mask.shape[1]))
    UU[1,:,:] = U
    UU[0,:,:] = 1.0-U        
    UU = -np.log(UU)
    UU = np.float32(UU)
    UU = UU.reshape((2,-1))
    dfield.setUnaryEnergy(UU)

    im = np.ascontiguousarray( image )
    dfield.addPairwiseBilateral(sxy=sxy, srgb=srgb, rgbim=im, compat=compat)
    Refine_num = 50
    Q = dfield.inference(Refine_num)
    new_mask = np.argmax(Q, axis=0).reshape((mask.shape[0],mask.shape[1]))
    new_mask = np.float32(new_mask)

    gt = gtmask > 0.1
    bmask = new_mask > 0.1

    inter_new = gt & bmask
    union_new = gt | bmask
    iou_new = np.float32(np.sum(inter_new)) / np.float32(np.sum(union_new))

    return new_mask, iou_new 
Example #20
Source File: tester.py    From openseg.pytorch with MIT License 5 votes vote down vote up
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 
Example #21
Source File: imutils.py    From irn with MIT License 5 votes vote down vote up
def crf_inference_label(img, labels, t=10, n_labels=21, gt_prob=0.7):

    h, w = img.shape[:2]

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

    unary = unary_from_labels(labels, n_labels, gt_prob=gt_prob, zero_unsure=False)

    d.setUnaryEnergy(unary)
    d.addPairwiseGaussian(sxy=3, compat=3)
    d.addPairwiseBilateral(sxy=50, srgb=5, rgbim=np.ascontiguousarray(np.copy(img)), compat=10)

    q = d.inference(t)

    return np.argmax(np.array(q).reshape((n_labels, h, w)), axis=0) 
Example #22
Source File: crf.py    From RMI with MIT License 5 votes vote down vote up
def dense_crf(real_image, probs, iter_steps=10):
	"""
	Args:
		real_image  		:   the real world RGB image, numpy array, shape [H, W, 3]
		probs       		:   the predicted probability in (0, 1), shape [H, W, C]
		iter_steps			:	the iterative steps
	Returns:
		return the refined segmentation map in [0,1,2,...,N_label]
	ref:
		https://github.com/milesial/Pytorch-UNet/blob/master/utils/crf.py
		https://github.com/lucasb-eyer/pydensecrf/blob/master/examples/Non%20RGB%20Example.ipynb
	"""
	# converting real -world image to RGB if it is gray
	if(len(real_image.shape) < 3):
		#real_image = cv2.cvtColor(real_image, cv2.COLOR_GRAY2RGB)
		raise ValueError("The input image should be RGB image.")
	# shape, and transpose to [C, H, W]
	H, W, N_classes = probs.shape[0], probs.shape[1], probs.shape[2]
	probs = probs.transpose((2, 0, 1))
	# get unary potentials from the probability distribution
	unary = unary_from_softmax(probs)
	#unary = np.ascontiguousarray(unary)
	# CRF
	d = dcrf.DenseCRF2D(W, H, N_classes)
	d.setUnaryEnergy(unary)
	# add pairwise potentials
	#real_image = np.ascontiguousarray(real_image)
	d.addPairwiseGaussian(sxy=3, compat=3)
	d.addPairwiseBilateral(sxy=30, srgb=13, rgbim=real_image, compat=10)
	#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=real_image,
	#						compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
	# inference
	Q = d.inference(iter_steps)
	Q = np.argmax(np.array(Q), axis=0).reshape((H, W))
	return Q 
Example #23
Source File: CRF.py    From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 5 votes vote down vote up
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 
Example #24
Source File: misc.py    From DSC-PyTorch with MIT License 5 votes vote down vote up
def crf_refine(img, annos):
    assert img.dtype == np.uint8
    assert annos.dtype == np.uint8
    assert img.shape[:2] == annos.shape

    # img and annos should be np array with data type uint8

    EPSILON = 1e-8

    M = 2  # salient or not
    tau = 1.05
    # Setup the CRF model
    d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M)

    anno_norm = annos / 255.

    n_energy = -np.log((1.0 - anno_norm + EPSILON)) / (tau * _sigmoid(1 - anno_norm))
    p_energy = -np.log(anno_norm + EPSILON) / (tau * _sigmoid(anno_norm))

    U = np.zeros((M, img.shape[0] * img.shape[1]), dtype='float32')
    U[0, :] = n_energy.flatten()
    U[1, :] = p_energy.flatten()

    d.setUnaryEnergy(U)

    d.addPairwiseGaussian(sxy=3, compat=3)
    d.addPairwiseBilateral(sxy=60, srgb=5, rgbim=img, compat=5)

    # Do the inference
    infer = np.array(d.inference(1)).astype('float32')
    res = infer[1, :]

    res = res * 255
    res = res.reshape(img.shape[:2])
    return res.astype('uint8') 
Example #25
Source File: utils.py    From tensorflow-deeplab-resnet-crf with MIT License 4 votes vote down vote up
def dense_crf(probs, img=None, n_iters=10, 
              sxy_gaussian=(1, 1), compat_gaussian=4,
              kernel_gaussian=dcrf.DIAG_KERNEL,
              normalisation_gaussian=dcrf.NORMALIZE_SYMMETRIC,
              sxy_bilateral=(49, 49), compat_bilateral=5,
              srgb_bilateral=(13, 13, 13),
              kernel_bilateral=dcrf.DIAG_KERNEL,
              normalisation_bilateral=dcrf.NORMALIZE_SYMMETRIC):
    """DenseCRF over unnormalised predictions.
       More details on the arguments at https://github.com/lucasb-eyer/pydensecrf.
    
    Args:
      probs: class probabilities per pixel.
      img: if given, the pairwise bilateral potential on raw RGB values will be computed.
      n_iters: number of iterations of MAP inference.
      sxy_gaussian: standard deviations for the location component of the colour-independent term.
      compat_gaussian: label compatibilities for the colour-independent term (can be a number, a 1D array, or a 2D array).
      kernel_gaussian: kernel precision matrix for the colour-independent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_gaussian: normalisation for the colour-independent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      sxy_bilateral: standard deviations for the location component of the colour-dependent term.
      compat_bilateral: label compatibilities for the colour-dependent term (can be a number, a 1D array, or a 2D array).
      srgb_bilateral: standard deviations for the colour component of the colour-dependent term.
      kernel_bilateral: kernel precision matrix for the colour-dependent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_bilateral: normalisation for the colour-dependent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      
    Returns:
      Refined predictions after MAP inference.
    """
    _, 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=kernel_gaussian, normalization=normalisation_gaussian)
    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=kernel_bilateral, normalization=normalisation_bilateral,
                               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) 
Example #26
Source File: post_process.py    From DeepDIVA with GNU Lesser General Public License v3.0 4 votes vote down vote up
def crf(original_image, output, nb_iterations=1, sxy1=(3, 3), sxy2=(80, 80), compat=3, srgb=(13, 13, 13)):
    """

    Parameters explained https://github.com/lucasb-eyer/pydensecrf

    Parameters
    ----------
    original_image : H x W x RGB
         [0:255]
    output : C x H x W
        float confidence of the network


    Returns
    -------
    H x W
        map of the selected labels, [0..C] where C is the number of classes
    """
    import pydensecrf.densecrf as dcrf
    from pydensecrf.utils import unary_from_softmax

    original_image = original_image.astype(np.uint8)

    # The output needs to be between 0 and 1
    if np.max(output) > 1 or np.min(output) < 0:
        output = softmax(output, axis=0)

    # Make the array contiguous in memory
    output = output.copy(order='C')

    d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], output.shape[0])
    U = unary_from_softmax(output)
    d.setUnaryEnergy(U)

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

    # This adds the color-dependent term, i.e. features are (x,y,r,g,b).
    # im is an image-array, e.g. im.dtype == np.uint8 and im.shape == (640,480,3)
    d.addPairwiseBilateral(sxy=sxy2,
                           srgb=srgb,
                           rgbim=original_image,
                           compat=compat*3,
                           kernel=dcrf.DIAG_KERNEL,
                           normalization=dcrf.NORMALIZE_SYMMETRIC)

    Q = d.inference(nb_iterations)

    return np.argmax(Q, axis=0).reshape(original_image.shape[0], original_image.shape[1]) 
Example #27
Source File: postprocessing.py    From open-solution-mapping-challenge with MIT License 4 votes vote down vote up
def dense_crf(img, output_probs, compat_gaussian=3, sxy_gaussian=1,
              compat_bilateral=10, sxy_bilateral=1, srgb=50, iterations=5):
    """Perform fully connected CRF.

    This function performs CRF method described in the following paper:

        Efficient Inference in Fully Connected CRFs with Gaussian Edge Potentials
        Philipp Krähenbühl and Vladlen Koltun
        NIPS 2011
        https://arxiv.org/abs/1210.5644

    Args:
        img (numpy.ndarray): RGB image of shape (3 x H x W).
        output_probs (numpy.ndarray): Probability map of shape (C x H x W).
        compat_gaussian: Compat value for Gaussian case.
        sxy_gaussian: x/y standard-deviation, theta_gamma from the CRF paper.
        compat_bilateral: Compat value for RGB case.
        sxy_bilateral: x/y standard-deviation, theta_alpha from the CRF paper.
        srgb: RGB standard-deviation, theta_beta from the CRF paper.
        iterations: Number of CRF iterations.

    Returns:
        numpy.ndarray: Probability map of shape (C x H x W) after applying CRF.

    """
    height = output_probs.shape[1]
    width = output_probs.shape[2]

    crf = DenseCRF2D(width, height, 2)
    unary = unary_from_softmax(output_probs)
    org_img = denormalize_img(img, mean=MEAN, std=STD) * 255.
    org_img = org_img.transpose(1, 2, 0)
    org_img = np.ascontiguousarray(org_img, dtype=np.uint8)

    crf.setUnaryEnergy(unary)

    crf.addPairwiseGaussian(sxy=sxy_gaussian, compat=compat_gaussian)
    crf.addPairwiseBilateral(sxy=sxy_bilateral, srgb=srgb, rgbim=org_img, compat=compat_bilateral)

    crf_image = crf.inference(iterations)
    crf_image = np.array(crf_image).reshape(output_probs.shape)

    return crf_image 
Example #28
Source File: utils.py    From DSRG with MIT License 4 votes vote down vote up
def dense_crf(probs, img=None, n_iters=10,
              sxy_gaussian=(3, 3), compat_gaussian=3,
              kernel_gaussian=dcrf.DIAG_KERNEL,
              normalisation_gaussian=dcrf.NORMALIZE_SYMMETRIC,
              sxy_bilateral=(49, 49), compat_bilateral=4,
              srgb_bilateral=(5, 5, 5),
              kernel_bilateral=dcrf.DIAG_KERNEL,
              normalisation_bilateral=dcrf.NORMALIZE_SYMMETRIC):
    """DenseCRF over unnormalised predictions.
       More details on the arguments at https://github.com/lucasb-eyer/pydensecrf.
    Args:
      probs: class probabilities per pixel.
      img: if given, the pairwise bilateral potential on raw RGB values will be computed.
      n_iters: number of iterations of MAP inference.
      sxy_gaussian: standard deviations for the location component of the colour-independent term.
      compat_gaussian: label compatibilities for the colour-independent term (can be a number, a 1D array, or a 2D array).
      kernel_gaussian: kernel precision matrix for the colour-independent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_gaussian: normalisation for the colour-independent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      sxy_bilateral: standard deviations for the location component of the colour-dependent term.
      compat_bilateral: label compatibilities for the colour-dependent term (can be a number, a 1D array, or a 2D array).
      srgb_bilateral: standard deviations for the colour component of the colour-dependent term.
      kernel_bilateral: kernel precision matrix for the colour-dependent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_bilateral: normalisation for the colour-dependent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
    Returns:
      Refined predictions after MAP inference.
    """
    h, w, n_classes = probs.shape
    print(probs.shape, img.shape)

    probs = probs.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=kernel_gaussian, normalization=normalisation_gaussian)
    if img is not None:
        assert(img.shape[0:2] == (h, w)), "The image height and width must coincide with dimensions of the logits."
        d.addPairwiseBilateral(sxy=sxy_bilateral, compat=compat_bilateral,
                               kernel=kernel_bilateral, normalization=normalisation_bilateral,
                               srgb=srgb_bilateral, rgbim=img)
    Q = d.inference(n_iters)
    preds = np.array(Q, dtype=np.float32).reshape((n_classes, h, w)).transpose(1, 2, 0)
    return probs.transpose(1, 2, 0) 
Example #29
Source File: CRF.py    From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 4 votes vote down vote up
def crf(original_image, annotated_image,output_image, use_2d = True):
        
    ##将注释RGB颜色转换为单个32位整数
    #annotated_label = annotated_image[:,:,0] + (annotated_image[:,:,1]<<8) + (annotated_image[:,:,2]<<16)
    #print(annotated_label.shape)
    
    # 将32位整数颜色转换为0、1、2、…标签。
    colors, labels = np.unique(annotated_label, return_inverse=True)
    print(len(labels))
    print(labels)
    print(len(colors))
    print(colors)
    n_labels = len(set(labels.flat))
    
    #创建一个32位颜色的映射
##    colorize = np.empty((len(colors), 3), np.uint8)
##    colorize[:,0] = (colors & 0x0000FF)
##    colorize[:,1] = (colors & 0x00FF00) >> 8
##    colorize[:,2] = (colors & 0xFF0000) >> 16
##    
##    #在带注释的图像中不给出任何类标签
##    n_labels = len(set(labels.flat)) 
##    
##    print("图像中没有标签")
##    print(n_labels)
    
    
    #Setting up the CRF model
    if use_2d :
        d = dcrf.DenseCRF2D(original_image.shape[1]*original_image.shape[0], n_labels)

        # get unary potentials (neg log probability)
        U = unary_from_labels(labels, n_labels, 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)

    # C将地图(标签)转换回相应的颜色并保存图像。
    # 注意,这里不再有“未知”,不管我们一开始拥有什么。
    MAP = colorize[MAP,:]
    MAP = MAP.reshape(original_image.shape)
    MAP = array_to_img(MAP)
    MAP.save(output_image)
    return MAP 
Example #30
Source File: crf.py    From MCD_DA with MIT License 4 votes vote down vote up
def dense_crf(probs, img=None, n_iters=10,
              sxy_gaussian=(1, 1), compat_gaussian=4,
              kernel_gaussian=dcrf.DIAG_KERNEL,
              normalisation_gaussian=dcrf.NORMALIZE_SYMMETRIC,
              sxy_bilateral=(49, 49), compat_bilateral=5,
              srgb_bilateral=(13, 13, 13),
              kernel_bilateral=dcrf.DIAG_KERNEL,
              normalisation_bilateral=dcrf.NORMALIZE_SYMMETRIC):
    """DenseCRF over unnormalised predictions.
       More details on the arguments at https://github.com/lucasb-eyer/pydensecrf.

    Args:
      probs: class probabilities per pixel.
      img: if given, the pairwise bilateral potential on raw RGB values will be computed.
      n_iters: number of iterations of MAP inference.
      sxy_gaussian: standard deviations for the location component of the colour-independent term.
      compat_gaussian: label compatibilities for the colour-independent term (can be a number, a 1D array, or a 2D array).
      kernel_gaussian: kernel precision matrix for the colour-independent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_gaussian: normalisation for the colour-independent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      sxy_bilateral: standard deviations for the location component of the colour-dependent term.
      compat_bilateral: label compatibilities for the colour-dependent term (can be a number, a 1D array, or a 2D array).
      srgb_bilateral: standard deviations for the colour component of the colour-dependent term.
      kernel_bilateral: kernel precision matrix for the colour-dependent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_bilateral: normalisation for the colour-dependent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).

    Returns:
      Refined predictions after MAP inference.
    """
    #     _, h, w, _ = probs.shape
    _, h, w, n_classes = 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.x
    U = U.reshape((n_classes, -1))  # Needs to be flat.
    d.setUnaryEnergy(U)
    d.addPairwiseGaussian(sxy=sxy_gaussian, compat=compat_gaussian,
                          kernel=kernel_gaussian, normalization=normalisation_gaussian)
    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=kernel_bilateral, normalization=normalisation_bilateral,
                               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)