def test_unary(self): M = 3 U, P, N = 1. / M, 0.8, 0.2 / (M - 1) # Uniform, Positive, Negative labels = np.array([ [0, 1, 2, 3], [3, 2, 1, 0], ]) unary = -np.log( np.array([ [U, P, N, N, N, N, P, U], [U, N, P, N, N, P, N, U], [U, N, N, P, P, N, N, U], ])) np.testing.assert_almost_equal( utils.compute_unary(labels, M, GT_PROB=P), unary)
def refine_crf(im, lb, gtProb=0.5, posTh=None, negTh=None, crfParams=0): """ [ NOTE: Currently only supports n=2 i.e. FG/BG. For general n: Remove this line: `MAP*=1/MAP.max()` at the end] Convert a given video into number of shots im: (h,w,c): 0-255: np.uint8: RGB lb: (h,w): 0-255: int or float => int: it should have labels 1,..,n and must have one '0' special label which is not a label, but the special value indicating no-confidence region. => float: it should probabilities in [0,1]. Func will assign: label=2 to region with prob>=posTh label=1 to region with prob<=negTh label=0 to region with negTh<prob<posTh crfParams: value: 0: default crf params value: 1: deeplab crf params value: 2: ccnn crf params out: (h,w): np.uint8: For n=2: output labels are 0 and 1 0 means BG or uncertain (i.e. lb=0,1) 1 means FG (i.e. lb=2) For general n: Remove this line: `MAP*=1/MAP.max()` at the end Then, label space is same as input i.e. in 0..n """ # Hard coded CRF parameters iters = 5 if crfParams == 1: # Deeplab Params xy_gauss = 19 wt_gauss = 15 xy_bilateral = 61 rgb_bilateral = 10 wt_bilateral = 35 elif crfParams == 2: # untuned ccnn params xy_gauss = 6 wt_gauss = 6 xy_bilateral = 50 rgb_bilateral = 4 wt_bilateral = 5 else: # Default Params xy_gauss = 3 wt_gauss = 3 xy_bilateral = 80 rgb_bilateral = 13 wt_bilateral = 10 # take care of probability mask if lb.dtype == np.float32 or lb.dtype == np.float64: if posTh is None or negTh is None: print('For probability mask, labels are not given !') return lb1 = np.zeros(lb.shape, dtype=np.uint8) lb1[lb >= posTh] = 2 lb1[lb <= negTh] = 1 presentLb = np.unique(lb1) if presentLb.size < 3: if 2 not in presentLb: y, x = int(lb.shape[0] / 2), int(lb.shape[1] / 2) lb1[y - 1:y + 1, x - 1:x + 1] = 2 # center area as FG if 1 not in presentLb: lb1[0, :] = 1 # top row as BG if 0 not in presentLb: lb1[1, :] = 0 # second row: doesn't matter lb = lb1 # convert to BGR im = np.ascontiguousarray(im[..., ::-1]) # Compute the number of classes in the label image M = len(set(lb.flat)) # Setup the 2D-CRF model d = dcrf.DenseCRF2D(im.shape[1], im.shape[0], M) # get unary potentials (neg log probability) U = compute_unary(lb, M) d.setUnaryEnergy(U) # This adds the color-independent term, features are the locations only. d.addPairwiseGaussian(sxy=(xy_gauss, xy_gauss), compat=wt_gauss) # This adds the color-dependent term, i.e. features are (x,y,r,g,b). d.addPairwiseBilateral(sxy=(xy_bilateral, xy_bilateral), srgb=(rgb_bilateral, rgb_bilateral, rgb_bilateral), rgbim=im, compat=wt_bilateral) # Do inference and compute map Q = d.inference(iters) MAP = np.argmax(Q, axis=0).astype('float32') MAP *= 1 / MAP.max() MAP = MAP.reshape(im.shape[:2]) out = MAP.astype('uint8') return out
### Read images and annotation ### ################################## img = cv2.imread(fn_im) labels = relabel_sequential(cv2.imread(fn_anno, 0))[0].flatten() M = 21 # 21 Classes to match the C++ example ########################### ### Setup the CRF model ### ########################### use_2d = False if use_2d: # Example using the DenseCRF2D code d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M) # get unary potentials (neg log probability) U = compute_unary(labels, M) d.setUnaryEnergy(U) # This adds the color-independent term, features are the locations only. d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # This adds the color-dependent term, i.e. features are (x,y,r,g,b). d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=img, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
### Read images and annotation ### ################################## img = cv2.imread(fn_im) labels = relabel_sequential(cv2.imread(fn_anno, 0))[0].flatten() M = 21 # 21 Classes to match the C++ example ########################### ### Setup the CRF model ### ########################### use_2d = False if use_2d: # Example using the DenseCRF2D code d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M) # get unary potentials (neg log probability) U = compute_unary(labels, M) d.setUnaryEnergy(U) # This adds the color-independent term, features are the locations only. d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # This adds the color-dependent term, i.e. features are (x,y,r,g,b). d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=img, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) else: # Example using the DenseCRF class and the util functions d = dcrf.DenseCRF(img.shape[0] * img.shape[1], M)