Python pydensecrf.densecrf.DIAG_KERNEL Examples
The following are 14
code examples of pydensecrf.densecrf.DIAG_KERNEL().
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 |
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: utils.py From TransferLearningClassification with MIT License | 6 votes |
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 #3
Source File: fcn.py From lunania-ai with MIT License | 6 votes |
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: post_process_crf.py From Attention-Gated-Networks with MIT License | 5 votes |
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 #5
Source File: CRF.py From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 | 5 votes |
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 #6
Source File: helper.py From DeepBrainSeg with MIT License | 5 votes |
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 #7
Source File: crf.py From RMI with MIT License | 5 votes |
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 #8
Source File: post_crf.py From FCN-GoogLeNet with MIT License | 5 votes |
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 #9
Source File: post_process.py From DeepDIVA with GNU Lesser General Public License v3.0 | 4 votes |
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 #10
Source File: utils.py From dcsp_segmentation with MIT License | 4 votes |
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 FU LL_KERNEL). normalisation_gaussian: normalisation for the colour-independent term (possible values are NO_NORMALIZATION, NORMALIZE_BEF ORE, 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 FUL L_KERNEL). normalisation_bilateral: normalisation for the colour-dependent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFO RE, 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 #11
Source File: crf.py From MCD_DA with MIT License | 4 votes |
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)
Example #12
Source File: CRF.py From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 | 4 votes |
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 #13
Source File: utils.py From DSRG with MIT License | 4 votes |
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 #14
Source File: utils.py From tensorflow-deeplab-resnet-crf with MIT License | 4 votes |
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)