Python pydensecrf.densecrf.DenseCRF() Examples

The following are 5 code examples of pydensecrf.densecrf.DenseCRF(). 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: utils.py    From BraTs with MIT License 7 votes vote down vote up
def get_crf_img(inputs, outputs):
    for i in range(outputs.shape[0]):
        img = inputs[i]
        softmax_prob = outputs[i]
        unary = unary_from_softmax(softmax_prob)
        unary = np.ascontiguousarray(unary)
        d = dcrf.DenseCRF(img.shape[0] * img.shape[1], 2)
        d.setUnaryEnergy(unary)
        feats = create_pairwise_gaussian(sdims=(10,10), shape=img.shape[:2])
        d.addPairwiseEnergy(feats, compat=3, kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        feats = create_pairwise_bilateral(sdims=(50,50), schan=(20,20,20),
                                          img=img, chdim=2)
        d.addPairwiseEnergy(feats, compat=10, kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        Q = d.inference(5)
        res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))
        if i == 0:
            crf = np.expand_dims(res,axis=0)
        else:
            res = np.expand_dims(res,axis=0)
            crf = np.concatenate((crf,res),axis=0)
    return crf 
Example #2
Source File: post_process_crf.py    From Attention-Gated-Networks with MIT License 5 votes vote down vote up
def apply_crf(input_image, input_prob, theta_a, theta_b, theta_r, mu1, mu2):
    n_slices = input_image.shape[2]
    output = np.zeros(input_image.shape)
    for slice_id in range(n_slices):
        image = input_image[:,:,slice_id]
        prob = input_prob[:,:,slice_id,:]

        n_pixel = image.shape[0] * image.shape[1]
        n_class = prob.shape[-1]

        P = np.transpose(prob, axes=(2, 0, 1))

        # Setup the CRF model
        d = dcrf.DenseCRF(n_pixel, n_class)

        # Set unary potentials (negative log probability)
        U = - np.log(P + 1e-10)
        U = np.ascontiguousarray(U.reshape((n_class, n_pixel)))
        d.setUnaryEnergy(U)

        # Set edge potential
        # This creates the color-dependent features and then add them to the CRF
        feats = create_pairwise_bilateral(sdims=(theta_a, theta_a), schan=(theta_b,), img=image, chdim=-1)
        d.addPairwiseEnergy(feats, compat=mu1, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)

        # This creates the color-independent features and then add them to the CRF
        feats = create_pairwise_gaussian(sdims=(theta_r, theta_r), shape=image.shape)
        d.addPairwiseEnergy(feats, compat=mu2, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)

        # Perform the inference
        Q = d.inference(5)
        res = np.argmax(Q, axis=0).astype('float32')
        res = np.reshape(res, image.shape).astype(dtype='int8')
        output[:,:,slice_id] = res

    return output 
Example #3
Source File: helper.py    From DeepBrainSeg with MIT License 5 votes vote down vote up
def postprocessing_pydensecrf(logits):
    # probs of shape 3d image per class: Nb_classes x Height x Width x Depth
    shape = logits.shape[1:]
    new_image = np.empty(shape)
    d = dcrf.DenseCRF(np.prod(shape), logits.shape[0])
    U = unary_from_softmax(logits)
    d.setUnaryEnergy(U)
    feats = create_pairwise_gaussian(sdims=(1.0, 1.0, 1.0), shape=shape)
    d.addPairwiseEnergy(feats, compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
    Q = d.inference(5) 
    new_image = np.argmax(Q, axis=0).reshape((shape[0], shape[1],shape[2]))
    return new_image 
Example #4
Source File: post_crf.py    From FCN-GoogLeNet with MIT License 5 votes vote down vote up
def post_process_crf(image, final_probabilities, num_cl):
	softmax = final_probabilities.squeeze()
	softmax = softmax.transpose((2, 0, 1))

	# The input should be the negative of the logarithm of probability values
	# Look up the definition of the unary_from_softmax for more information
	unary = unary_from_softmax(softmax, scale=None, clip=1e-5)

	# The inputs should be C-continious -- we are using Cython wrapper
	unary = np.ascontiguousarray(unary)

	d = dcrf.DenseCRF(image.shape[0] * image.shape[1], num_cl)

	d.setUnaryEnergy(unary)

	# This potential penalizes small pieces of segmentation that are
	# spatially isolated -- enforces more spatially consistent segmentations
	feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

	d.addPairwiseEnergy(feats, compat=3,
                    	kernel=dcrf.DIAG_KERNEL,
                    	normalization=dcrf.NORMALIZE_SYMMETRIC)

	# This creates the color-dependent features --
	# because the segmentation that we get from CNN are too coarse
	# and we can use local color features to refine them
	feats = create_pairwise_bilateral(sdims=(50, 50), schan=(20, 20, 20),
									img=image, chdim=2)

	d.addPairwiseEnergy(feats, compat=10,
                     	kernel=dcrf.DIAG_KERNEL,
                     	normalization=dcrf.NORMALIZE_SYMMETRIC)
	Q = d.inference(10)
	res = np.argmax(Q, axis=0).reshape((image.shape[0], image.shape[1]))

	return res 
Example #5
Source File: fullcrf.py    From ConvCRF with MIT License 4 votes vote down vote up
def compute_lattice(self, img, num_classes=None):
        """
        Compute indices for the lattice approximation.

         Arguments:
            img: np.array with shape [height, width, 3]
                The input image. Default config assumes image
                data in [0, 255]. For normalized images adapt
                `schan`. Try schan = 0.1 for images in [-0.5, 0.5]
        """

        if num_classes is not None:
            self.num_classes = num_classes

        assert self.num_classes is not None

        npixels = self.shape[0] * self.shape[1]
        crf = dcrf.DenseCRF(npixels, self.num_classes)

        sdims = self.conf['pos_feats']['sdims']

        feats = utils.create_pairwise_gaussian(
            sdims=(sdims, sdims),
            shape=img.shape[:2])

        self.smooth_feats = feats

        self.crf = crf

        self.crf.addPairwiseEnergy(
            self.smooth_feats, compat=self.conf['pos_feats']['compat'])

        sdims = self.conf['col_feats']['sdims']
        schan = self.conf['col_feats']['schan']

        feats = utils.create_pairwise_bilateral(sdims=(sdims, sdims),
                                                schan=(schan, schan, schan),
                                                img=img, chdim=2)

        self.appear_feats = feats

        self.crf.addPairwiseEnergy(
            self.appear_feats, compat=self.conf['pos_feats']['compat'])