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