ValueError: Buffer has wrong number of dimensions (expected 3, got 2) 已解决

ValueError: Buffer has wrong number of dimensions (expected 3, got 2)
已解决
报错信息:

  File "/home/ubuntu/Code/AffinityNet/tool/imutils.py", line 214, in crf_inference
    d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
  File "pydensecrf/densecrf.pyx", line 126, in pydensecrf.densecrf.DenseCRF2D.addPairwiseBilateral (pydensecrf/densecrf.cpp:4404)
ValueError: Buffer has wrong number of dimensions (expected 3, got 2)

源代码:

def _crf_with_alpha(cam_dict, alpha):
    v = np.array(list(cam_dict.values()))
    bg_score = np.power(1 - np.max(v, axis=0, keepdims=True), alpha)
    # print(v.shape,bg_score.shape,orig_img.shape)
    bgcam_score = np.concatenate((bg_score, v), axis=0)
    # print(orig_img.shape,bgcam_score.shape)
    crf_score = imutils.crf_inference(orig_img, bgcam_score, labels=bgcam_score.shape[0])

    n_crf_al = dict()

    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=4):
    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)
    # addPairwiseBilateral只适用于RGB图像,即三个通道
    Q = d.inference(t)

    return np.array(Q).reshape((n_labels, h, w))

addPairwiseBilateral只适用于RGB图像,即三个通道
所以当使用灰度图进行训练时,不能使用addPairwiseBilateral函数进行计算。将addPairwiseBilateral所在行注释即可。

你可能感兴趣的:(学习笔记,python,开发语言)