在弱监督语义分割任务中,通过分类任务可以得到CAM结果,为了进一步更加准确的得到伪分割标签,往往会使用DenseCRF方法来refine得到的CAM结果。 该方法的实现如下所示,其中包括了一个超参数 $\alpha$, 该参数越大,则像素被分类成背景区域的可能性越小,反之越大。
def _crf_with_alpha(cam_dict, name, alpha=10):
# cam_dict 是一个存放了各个类别对应的像素mask的字典,图像有多少个 gt_label 字典里就存了多少个类的 mask
orig_img = np.ascontiguousarray(np.uint8(Image.open(os.path.join('/data/data/Seg_Data/PASCAL-VOC/VOCdevkit/VOC2012/JPEGImages', name + '.jpg'))))
v = np.array(list(cam_dict.values())) # n_cls x h x w
# 计算背景得分, alpha值越大,背景得分越低,这意味着有更多的像素会被分为前景
bg_score = np.power(1 - np.max(v, axis=0, keepdims=True), alpha) # 1 x h x w
bgcam_score = np.concatenate((bg_score, v), axis=0) # (n_cls + 1) x h x w, concat the background
# densecrf inference
crf_score = crf_inference(orig_img, bgcam_score, labels=bgcam_score.shape[0])
# save desnecrf results
n_crf_al = dict()
# background score
n_crf_al[0] = crf_score[0]
for i, key in enumerate(cam_dict.keys()):
n_crf_al[key + 1] = crf_score[i + 1]
return n_crf_al
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))