Condition Random Field (CRF)条件随机场
CRF的基本思路与双边滤波的核心是一致的,即通过pixel之间的value(在feature map中就是feature vector)的相似度以及空间位置之间的相似度,施加一个正则化约束,使得类似的点应该被分到同一个类别,CRF最初在segmentation任务重,用来消除直接seg model输出结果过于平滑从而丢失细节的问题。
CRF的实现可以用pydensecrf
实现,一个构造分割中的DenseCRF层的样例如下:
(refer:https://github.com/kazuto1011/deeplab-pytorch/blob/master/libs/utils/crf.py)
#!/usr/bin/env python
# coding: utf-8
#
# Author: Kazuto Nakashima
# URL: https://kazuto1011.github.io
# Date: 09 January 2019
import numpy as np
import pydensecrf.densecrf as dcrf
import pydensecrf.utils as utils
class DenseCRF(object):
def __init__(self, iter_max, pos_w, pos_xy_std, bi_w, bi_xy_std, bi_rgb_std):
self.iter_max = iter_max
self.pos_w = pos_w
self.pos_xy_std = pos_xy_std
self.bi_w = bi_w
self.bi_xy_std = bi_xy_std
self.bi_rgb_std = bi_rgb_std
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