本文整理汇总了Python中pydensecrf.utils.unary_from_softmax方法的典型用法代码示例。如果您正苦于以下问题:Python utils.unary_from_softmax方法的具体用法?Python utils.unary_from_softmax怎么用?Python utils.unary_from_softmax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydensecrf.utils
的用法示例。
在下文中一共展示了utils.unary_from_softmax方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dense_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def dense_crf(img, output_probs):
c = output_probs.shape[0]
h = output_probs.shape[1]
w = output_probs.shape[2]
U = utils.unary_from_softmax(output_probs)
U = np.ascontiguousarray(U)
img = np.ascontiguousarray(img)
d = dcrf.DenseCRF2D(w, h, c)
d.setUnaryEnergy(U)
d.addPairwiseGaussian(sxy=POS_XY_STD, compat=POS_W)
d.addPairwiseBilateral(sxy=Bi_XY_STD, srgb=Bi_RGB_STD, rgbim=img, compat=Bi_W)
Q = d.inference(MAX_ITER)
Q = np.array(Q).reshape((c, h, w))
return Q
示例2: crf_inference
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def crf_inference(img, probs, t=10, scale_factor=1, labels=21):
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_softmax
h, w = img.shape[:2]
n_labels = labels
d = dcrf.DenseCRF2D(w, h, n_labels)
unary = unary_from_softmax(probs)
unary = np.ascontiguousarray(unary)
d.setUnaryEnergy(unary)
d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
Q = d.inference(t)
return np.array(Q).reshape((n_labels, h, w))
示例3: dense_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def dense_crf(probs, img=None, n_classes=21, n_iters=1, scale_factor=1):
c,h,w = probs.shape
if img is not None:
assert(img.shape[1:3] == (h, w))
img = np.transpose(img,(1,2,0)).copy(order='C')
d = dcrf.DenseCRF2D(w, h, n_classes) # Define DenseCRF model.
unary = unary_from_softmax(probs)
unary = np.ascontiguousarray(unary)
d.setUnaryEnergy(unary)
d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
Q = d.inference(n_iters)
preds = np.array(Q, dtype=np.float32).reshape((n_classes, h, w))
return preds
示例4: __call__
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def __call__(self, image, probmap):
C, H, W = probmap.shape
U = utils.unary_from_softmax(probmap)
U = np.ascontiguousarray(U)
image = np.ascontiguousarray(image)
d = dcrf.DenseCRF2D(W, H, C)
d.setUnaryEnergy(U)
d.addPairwiseGaussian(sxy=self.pos_xy_std, compat=self.pos_w)
d.addPairwiseBilateral(
sxy=self.bi_xy_std, srgb=self.bi_rgb_std, rgbim=image, compat=self.bi_w
)
Q = d.inference(self.iter_max)
Q = np.array(Q).reshape((C, H, W))
return Q
示例5: dense_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def dense_crf(img, probs):
c = probs.shape[0]
h = probs.shape[1]
w = probs.shape[2]
U = utils.unary_from_softmax(probs)
U = np.ascontiguousarray(U)
img = np.ascontiguousarray(img)
d = dcrf.DenseCRF2D(w, h, c)
d.setUnaryEnergy(U)
d.addPairwiseGaussian(sxy=POS_XY_STD, compat=POS_W)
d.addPairwiseBilateral(sxy=Bi_XY_STD, srgb=Bi_RGB_STD, rgbim=img, compat=Bi_W)
Q = d.inference(MAX_ITER)
Q = np.array(Q).reshape((c, h, w))
return Q
示例6: crf_processing
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def crf_processing(self, image, label, soft_label=False):
crf = dcrf.DenseCRF2D(image.shape[1], image.shape[0], 2)
if not soft_label:
unary = unary_from_labels(label, 2, gt_prob=0.9, zero_unsure=False)
else:
if len(label.shape)==2:
p_neg = 1.0 - label
label = np.concatenate((p_neg[...,np.newaxis], label[...,np.newaxis]), axis=2)
label = label.transpose((2,0,1))
unary = unary_from_softmax(label)
crf.setUnaryEnergy(unary)
crf.addPairwiseGaussian(sxy=(3,3), compat=3)
crf.addPairwiseBilateral(sxy=(40, 40), srgb=(5, 5, 5), rgbim=image, compat=10)
crf_out = crf.inference(self.crf_infer_steps)
# Find out the most probable class for each pixel.
return np.argmax(crf_out, axis=0).reshape((image.shape[0], image.shape[1]))
示例7: run_multiclass_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def run_multiclass_crf(seq, fn, posteriors, softmax_scale, sxy1, compat1, sxy2, compat2, srgb):
im_fn = DAVIS2017_DIR + "JPEGImages/480p/" + seq + "/" + fn.replace(".pickle", ".jpg")
im = imread(im_fn)
nlabels = posteriors.shape[-1]
im = numpy.ascontiguousarray(im)
pred = numpy.ascontiguousarray(posteriors.swapaxes(0, 2).swapaxes(1, 2))
d = dcrf.DenseCRF2D(im.shape[1], im.shape[0], nlabels) # width, height, nlabels
unaries = unary_from_softmax(pred, scale=softmax_scale)
d.setUnaryEnergy(unaries)
d.addPairwiseGaussian(sxy=sxy1, compat=compat1)
d.addPairwiseBilateral(sxy=sxy2, srgb=srgb, rgbim=im, compat=compat2)
processed = d.inference(12)
res = numpy.argmax(processed, axis=0).reshape(im.shape[:2])
return res
示例8: apply_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def apply_crf(im, pred):
im = numpy.ascontiguousarray(im)
pred = numpy.ascontiguousarray(pred.swapaxes(0, 2).swapaxes(1, 2))
d = dcrf.DenseCRF2D(854, 480, 2) # width, height, nlabels
unaries = unary_from_softmax(pred, scale=1.0)
d.setUnaryEnergy(unaries)
#print im.shape
# print annot.shape
#print pred.shape
d.addPairwiseGaussian(sxy=0.220880737269, compat=1.24845093352)
d.addPairwiseBilateral(sxy=22.3761305044, srgb=7.70254062277, rgbim=im, compat=1.40326787165)
processed = d.inference(12)
res = numpy.argmax(processed, axis=0).reshape(480, 854)
return res
示例9: apply_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def apply_crf(im, pred):
im = numpy.ascontiguousarray(im)
if im.shape[:2] != pred.shape[:2]:
im = imresize(im, pred.shape[:2])
pred = numpy.ascontiguousarray(pred.swapaxes(0, 2).swapaxes(1, 2))
d = dcrf.DenseCRF2D(im.shape[1], im.shape[0], 2) # width, height, nlabels
unaries = unary_from_softmax(pred, scale=1.0)
d.setUnaryEnergy(unaries)
d.addPairwiseGaussian(sxy=0.220880737269, compat=1.24845093352)
d.addPairwiseBilateral(sxy=22.3761305044, srgb=7.70254062277, rgbim=im, compat=1.40326787165)
processed = d.inference(12)
res = numpy.argmax(processed, axis=0).reshape(im.shape[:2])
return res
示例10: getCRFResult
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [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))
示例11: dense_crf_process
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def dense_crf_process(self, images, outputs):
'''
Reference: https://github.com/kazuto1011/deeplab-pytorch/blob/master/libs/utils/crf.py
'''
# hyperparameters of the dense crf
# baseline = 79.5
# bi_xy_std = 67, 79.1
# bi_xy_std = 20, 79.6
# bi_xy_std = 10, 79.7
# bi_xy_std = 10, iter_max = 20, v4 79.7
# bi_xy_std = 10, iter_max = 5, v5 79.7
# bi_xy_std = 5, v3 79.7
iter_max = 10
pos_w = 3
pos_xy_std = 1
bi_w = 4
bi_xy_std = 10
bi_rgb_std = 3
b = images.size(0)
mean_vector = np.expand_dims(np.expand_dims(np.transpose(np.array([102.9801, 115.9465, 122.7717])), axis=1), axis=2)
outputs = F.softmax(outputs, dim=1)
for i in range(b):
unary = outputs[i].data.cpu().numpy()
C, H, W = unary.shape
unary = dcrf_utils.unary_from_softmax(unary)
unary = np.ascontiguousarray(unary)
image = np.ascontiguousarray(images[i]) + mean_vector
image = image.astype(np.ubyte)
image = np.ascontiguousarray(image.transpose(1, 2, 0))
d = dcrf.DenseCRF2D(W, H, C)
d.setUnaryEnergy(unary)
d.addPairwiseGaussian(sxy=pos_xy_std, compat=pos_w)
d.addPairwiseBilateral(sxy=bi_xy_std, srgb=bi_rgb_std, rgbim=image, compat=bi_w)
out_crf = np.array(d.inference(iter_max))
outputs[i] = torch.from_numpy(out_crf).cuda().view(C, H, W)
return outputs
示例12: postprocessing_pydensecrf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [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
示例13: dense_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [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
示例14: crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [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])
示例15: dense_crf
# 需要导入模块: from pydensecrf import utils [as 别名]
# 或者: from pydensecrf.utils import unary_from_softmax [as 别名]
def dense_crf(img, output_probs, compat_gaussian=3, sxy_gaussian=1,
compat_bilateral=10, sxy_bilateral=1, srgb=50, iterations=5):
"""Perform fully connected CRF.
This function performs CRF method described in the following paper:
Efficient Inference in Fully Connected CRFs with Gaussian Edge Potentials
Philipp Krähenbühl and Vladlen Koltun
NIPS 2011
https://arxiv.org/abs/1210.5644
Args:
img (numpy.ndarray): RGB image of shape (3 x H x W).
output_probs (numpy.ndarray): Probability map of shape (C x H x W).
compat_gaussian: Compat value for Gaussian case.
sxy_gaussian: x/y standard-deviation, theta_gamma from the CRF paper.
compat_bilateral: Compat value for RGB case.
sxy_bilateral: x/y standard-deviation, theta_alpha from the CRF paper.
srgb: RGB standard-deviation, theta_beta from the CRF paper.
iterations: Number of CRF iterations.
Returns:
numpy.ndarray: Probability map of shape (C x H x W) after applying CRF.
"""
height = output_probs.shape[1]
width = output_probs.shape[2]
crf = DenseCRF2D(width, height, 2)
unary = unary_from_softmax(output_probs)
org_img = denormalize_img(img, mean=MEAN, std=STD) * 255.
org_img = org_img.transpose(1, 2, 0)
org_img = np.ascontiguousarray(org_img, dtype=np.uint8)
crf.setUnaryEnergy(unary)
crf.addPairwiseGaussian(sxy=sxy_gaussian, compat=compat_gaussian)
crf.addPairwiseBilateral(sxy=sxy_bilateral, srgb=srgb, rgbim=org_img, compat=compat_bilateral)
crf_image = crf.inference(iterations)
crf_image = np.array(crf_image).reshape(output_probs.shape)
return crf_image