本文整理汇总了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'])