本文整理匯總了Python中pydensecrf.densecrf.NORMALIZE_SYMMETRIC屬性的典型用法代碼示例。如果您正苦於以下問題:Python densecrf.NORMALIZE_SYMMETRIC屬性的具體用法?Python densecrf.NORMALIZE_SYMMETRIC怎麽用?Python densecrf.NORMALIZE_SYMMETRIC使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pydensecrf.densecrf
的用法示例。
在下文中一共展示了densecrf.NORMALIZE_SYMMETRIC屬性的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [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)
示例2: get_crf_img
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例3: getCRFResult
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [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))
示例4: apply_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例5: crf_PIL
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [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
示例6: postprocessing_pydensecrf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例7: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例8: post_process_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例9: crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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])
示例10: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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)
示例11: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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)
示例12: crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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
示例13: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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)
示例14: dense_crf
# 需要導入模塊: from pydensecrf import densecrf [as 別名]
# 或者: from pydensecrf.densecrf import NORMALIZE_SYMMETRIC [as 別名]
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)