Пример #1
0
    def CRF(self,
            image,
            softmax,
            iterations=1,
            sdims_g=(1, 1),
            sdims_b=(3, 3),
            schan=0.5):
        softmax = softmax.transpose(2, 0, 1)
        n_classes = softmax.shape[0]
        unary = unary_from_softmax(softmax)
        d = dcrf.DenseCRF(image.shape[1] * image.shape[0], n_classes)
        d.setUnaryEnergy(unary)
        feats = create_pairwise_gaussian(sdims=sdims_g, shape=image.shape[:2])
        d.addPairwiseEnergy(feats,
                            compat=3,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)

        # This creates the color-dependent features and then add them to the CRF
        feats = create_pairwise_bilateral(sdims=sdims_b,
                                          schan=schan,
                                          img=image,
                                          chdim=2)
        d.addPairwiseEnergy(feats,
                            compat=10,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        Q = d.inference(iterations)
        probabilities = np.array(Q).reshape((n_classes, image.shape[0], -1))
        labels = np.argmax(Q, axis=0).reshape(image.shape[0:2])

        return probabilities.transpose(1, 2, 0), labels
Пример #2
0
 def apply(self, image, colour_axis):
     spatial_sigmas = [self.sigma for _ in range(len(image.shape) - 1)]
     shape = [
         image.shape[i] for i in range(len(image.shape)) if i != colour_axis
     ]
     return crf_utils.create_pairwise_gaussian(sdims=spatial_sigmas,
                                               shape=shape)
Пример #3
0
def CRF(x, image):
    unary = unary_from_softmax(x)  #C,H,W
    unary = np.ascontiguousarray(unary)
    d = dcrf.DenseCRF(image.shape[-2] * image.shape[-1], 10)
    d.setUnaryEnergy(unary)

    gau_feats = create_pairwise_gaussian(sdims=(3, 3), shape=image.shape[-2:])
    d.addPairwiseEnergy(gau_feats,
                        compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    bil_feats = create_pairwise_bilateral(sdims=(10, 10),
                                          schan=[5],
                                          img=np.array(image).transpose(
                                              1, 2, 0),
                                          chdim=2)
    d.addPairwiseEnergy(bil_feats,
                        compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    Q = d.inference(5)
    x = np.argmax(Q, axis=0).reshape(image.shape[-2], image.shape[-1])
    Q = np.array(Q)
    Q = Q.reshape(-1, image.shape[-2], image.shape[-1])
    return Q, x
Пример #4
0
    def adjust_prediction(self, probability, image):
        crf = dcrf.DenseCRF(np.prod(probability.shape), 2)
        # crf = dcrf.DenseCRF(np.prod(probability.shape), 1)

        binary_prob = np.stack((1 - probability, probability), axis=0)
        unary = unary_from_softmax(binary_prob)
        # unary = unary_from_softmax(np.expand_dims(probability, axis=0))
        crf.setUnaryEnergy(unary)

        # per dimension scale factors
        sdims = [self.sdims] * 3
        smooth = create_pairwise_gaussian(sdims=sdims, shape=probability.shape)
        crf.addPairwiseEnergy(smooth, compat=2)

        if self.schan:
            # per channel scale factors
            schan = [self.schan] * 6
            appearance = create_pairwise_bilateral(sdims=sdims,
                                                   schan=schan,
                                                   img=image,
                                                   chdim=3)
            crf.addPairwiseEnergy(appearance, compat=2)

        result = crf.inference(self.iter)
        crf_prediction = np.argmax(result, axis=0).reshape(
            probability.shape).astype(np.float32)

        return crf_prediction
Пример #5
0
def test_complete_inference2():

    dcrf = densecrf.DenseCRF(100, 2)
    pcrf = pycrf.DenseCRF(100, 2)

    unary = test_utils._get_simple_unary()
    img = test_utils._get_simple_img()

    dcrf.setUnaryEnergy(-np.log(unary))
    pcrf.set_unary_energy(-np.log(unary))

    feats = utils.create_pairwise_gaussian(sdims=(1.5, 1.5),
                                           shape=img.shape[:2])

    dcrf.addPairwiseEnergy(feats, compat=3)
    pcrf.add_pairwise_energy(feats, compat=3)

    feats = utils.create_pairwise_bilateral(sdims=(2, 2), schan=2,
                                            img=img, chdim=2)

    dcrf.addPairwiseEnergy(feats, compat=3)
    pcrf.add_pairwise_energy(feats, compat=3)
    # d.addPairwiseBilateral(2, 2, img, 3)
    res1 = np.argmax(dcrf.inference(10), axis=0).reshape(10, 10)
    res2 = np.argmax(pcrf.inference(10), axis=0).reshape(10, 10)

    if False:
        # Save the result for visual inspection

        import scipy as scp
        import scipy.misc

        scp.misc.imsave("test.png", res1)

    assert(np.all(res1 == res2))
Пример #6
0
def CRF(img, probabilities, K):
    #print("Probabilities shape : " , probabilities.shape)
    processed_probabilities = probabilities.squeeze()
    #print("Processed : " , processed_probabilities.shape)
    softmax                 = processed_probabilities.transpose((2, 0, 1))
    #print(softmax.shape)
    unary                   = unary_from_softmax(softmax)
    #print(unary.shape)
    unary                   = np.ascontiguousarray(unary)
    #print(unary.shape)
    d                       = dcrf.DenseCRF(img.shape[0] * img.shape[1], K)

    d.setUnaryEnergy(unary)
    #d.addPairwiseGaussian(sxy=3, compat=3)
    feats                   = create_pairwise_gaussian(sdims=(3, 3), shape=(img.shape[1], img.shape[0]))
    #feats                   = create_pairwise_bilateral(sdims=(5, 5), schan=(10, 10, 10), img=img.reshape(img.shape[1], img.shape[0], 3), chdim=2)
    #print("Feats : \n", feats)
    d.addPairwiseEnergy(feats, compat=5, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
    Q = d.inference(5)

    res = np.argmax(Q, axis=0).reshape(img.shape[1], img.shape[0]).astype('float32')
    res *= (255. / res.max())
    res.reshape(img.shape[:2])
    #print("Res \n", res)
    return res
def dense_crf(img, output_probs_1, output_probs_0):
    h = output_probs_1.shape[0]
    w = output_probs_1.shape[1]
    output_probs_1 = np.expand_dims(output_probs_1, 0)
    output_probs_0 = np.expand_dims(output_probs_0, 0)
    output_probs = np.append(output_probs_0, output_probs_1, axis=0)

    d = dcrf.DenseCRF2D(w, h, 2)
    U = -np.log(output_probs)
    U = U.reshape((2, -1))
    U = np.ascontiguousarray(U)
    img = np.ascontiguousarray(img).astype(np.uint8)
    d.setUnaryEnergy(U)

    gaussian_energy = create_pairwise_gaussian(sdims=(3, 3),
                                               shape=img.shape[:2])
    d.addPairwiseEnergy(gaussian_energy, compat=3)
    pairwise_energy = create_pairwise_bilateral(sdims=(50, 50),
                                                schan=(5, ),
                                                img=img,
                                                chdim=2)
    d.addPairwiseEnergy(pairwise_energy, compat=10)

    Q = d.inference(3)
    map_q = np.array(Q)[1, :].reshape(h, w)
    Q = np.argmax(np.array(Q), axis=0).reshape((h, w))

    return Q
Пример #8
0
def crf(labels,rgb,classes=None,ls=[0,1,2,3,4,5]):

    if classes==None:
        labels += np.amin(labels)
        labels /= np.amax(labels)
        classes = labels.shape[2]      
        c_img = np.rollaxis(labels,2)
        U = unary_from_softmax(c_img)
    else:
        c_img = cl.classimg(labels,map=True,labels=ls)
        c_img = np.reshape(c_img,(c_img.shape[0]*c_img.shape[1])).astype(int)
        U = unary_from_labels(np.array([0,3]),2,.7,zero_unsure=True)
        
    d = dcrf.DenseCRF2D(labels.shape[0], labels.shape[1], classes)

    d.setUnaryEnergy(U)
    
    PG = create_pairwise_gaussian((3,3),labels.shape[:2])
    d.addPairwiseEnergy(PG,3)
    
    PB = create_pairwise_bilateral((59,59), (13,13,13,13), rgb,chdim=2)
    d.addPairwiseEnergy(PB,6)
    
    Q = d.inference(5)
    map = np.argmax(Q, axis=0)
    map = np.reshape(map,labels.shape[:2])
    
    return map
Пример #9
0
def densecrf(logits):
    """
        applies coditional random fields on predictions
        The idea is consider the nbr voxels in making 
        class prediction of current pixel
        
        refer CRF and MRF papers for more theoretical idea

        args
            logits: Nb_classes x Height x Width x Depth

        returns
            tensor of size 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
Пример #10
0
def dense_crf(original_image, annotated_image, NUM_OF_CLASSESS, use_2d=True):
    # Converting annotated image to RGB if it is Gray scale
    print(original_image.shape, annotated_image.shape)

    # Gives no of class labels in the annotated image
    #n_labels = len(set(labels.flat))
    n_labels = NUM_OF_CLASSESS

    # Setting up the CRF model

    d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0],
                        n_labels)

    # get unary potentials (neg log probability)
    processed_probabilities = annotated_image
    softmax = processed_probabilities.transpose((2, 0, 1))
    print(softmax.shape)
    U = unary_from_softmax(softmax, scale=None, clip=1e-5)

    U = np.ascontiguousarray(U)
    #U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False)
    d.setUnaryEnergy(U)

    # This potential penalizes small pieces of segmentation that are
    # spatially isolated -- enforces more spatially consistent segmentations
    feats = create_pairwise_gaussian(sdims=(3, 3),
                                     shape=original_image.shape[:2])

    d.addPairwiseEnergy(feats,
                        compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features --
    # because the segmentation that we get from CNN are too coarse
    # and we can use local color features to refine them
    feats = create_pairwise_bilateral(sdims=(80, 80),
                                      schan=(13, 13, 13),
                                      img=original_image,
                                      chdim=2)

    d.addPairwiseEnergy(feats,
                        compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # Run Inference for 5 steps
    Q = d.inference(5)
    print(Q)
    #print(">>>>>>>>Qshape: ", Q.shape)
    # Find out the most probable class for each pixel.
    output = np.argmax(Q, axis=0).reshape(
        (original_image.shape[0], original_image.shape[1]))
    print(output.shape)

    plt.subplot(240 + 1)
    plt.imshow(output, cmap=plt.get_cmap('nipy_spectral'))
    plt.show()

    return output
Пример #11
0
	def crf(self, input_vol, softmax_vol):
		# prob shape: classes, width, height, depth
		# vol shape: width, height, depth

		assert len(softmax_vol.shape) == 4
		assert len(input_vol.shape) == 4
		return_ = np.empty((input_vol.shape[1:]))
		for slice_idx in range(input_vol.shape[3]):
			image   = input_vol[:, :, :, slice_idx]
			softmax = softmax_vol[:, :, :, slice_idx]

			# softmax = softmax.transpose(2, 0, 1)
			# image   = image.transpose(2, 0, 1)

			n_classes = softmax.shape[0]
			unary = unary_from_softmax(softmax)
			d = dcrf.DenseCRF(image.shape[1]*image.shape[0], n_classes)
			d.setUnaryEnergy(unary)
			feats = create_pairwise_gaussian(sdims=(1., 1.), shape=image.shape[:2])
			d.addPairwiseEnergy(feats, compat=3,
			                    kernel=dcrf.DIAG_KERNEL,
			                    normalization=dcrf.NORMALIZE_SYMMETRIC)


			# This creates the color-dependent features and then add them to the CRF
			feats = create_pairwise_bilateral(sdims=(3., 3.), schan=0.5,
			                                  img=image, chdim=2)
			d.addPairwiseEnergy(feats, compat=10,
			                    kernel=dcrf.DIAG_KERNEL,
			                    normalization=dcrf.NORMALIZE_SYMMETRIC)
			Q = d.inference(iterations)
			probabilities = np.array(Q).reshape((n_classes,image.shape[0],-1))
			labels = np.argmax(Q, axis=0).reshape(image.shape[0:2])
			return_[:,:,slice_idx] = labels
		return return_
Пример #12
0
def get_crf_seg(img, labels, n_labels, opts):
    '''
        crf_out_final = get_crf_seg(img, labels, n_labels)
    '''
    crf = dcrf.DenseCRF(img.shape[1] * img.shape[0], n_labels)
    U = unary_from_softmax(labels)
    crf.setUnaryEnergy(U)

    # pairwise positional (gaussian) terms
    feats = create_pairwise_gaussian(sdims=(opts.gaussian_sx, opts.gaussian_sx), shape=img.shape[:2])
    crf.addPairwiseEnergy(feats, compat=opts.crf_gaussian_weight,
                    kernel=dcrf.DIAG_KERNEL,
                    normalization=dcrf.NORMALIZE_SYMMETRIC)

    # pairwise bilateral (color + position) terms
    feats = create_pairwise_bilateral(sdims=(opts.bilateral_sx, opts.bilateral_sx), \
                                      schan=(opts.bilateral_color, opts.bilateral_color, opts.bilateral_color),
                                      img=img, chdim=2)
    crf.addPairwiseEnergy(feats, compat=opts.crf_bilateral_weight,
                    kernel=dcrf.DIAG_KERNEL,
                    normalization=dcrf.NORMALIZE_SYMMETRIC)

    # run inference
    Q = crf.inference(5)

    return Q
def run_CRF(patient, sdimsB, sdimsG, schan):
    
    
    path0 = path + 'patient_' + str(patient) +'_ProbMapClass0.nii.gz'
    path1 = path + 'patient_' + str(patient) +'_ProbMapClass1.nii.gz'
    ims, npoints, nlabels, affine = niiToNumpyCRFinput(path0, path1)
    shape1 = ims.shape[1:]
    
    name = 'patient_' + str(patient) + '_CRF'
    d = dcrf.DenseCRF(npoints, nlabels)  # npoints, nlabels
    U =  ims
    shape = U.shape[1:]
   # print (np.sum(U))
    U = unary_from_softmax(ims)
    d.setUnaryEnergy(U)
    G = create_pairwise_gaussian(sdimsG, shape)
    d.addPairwiseEnergy(w2*G, compat=compat1)
    B = create_pairwise_bilateral(sdimsB, schan, ims, chdim=0)
    d.addPairwiseEnergy(w1*B, compat=compat2)
 #   Q = d.inference(1)
    Q, tmp1, tmp2 = d.startInference()
    for i in range(5):
       # print("KL-divergence at {}: {}".format(i, d.klDivergence(Q)))
        d.stepInference(Q, tmp1, tmp2)
    patientCRF = np.argmax(Q, axis=0).reshape(shape1).astype(np.float32)
    
    #print (patientCRF.shape, patientCRF.dtype, np.sum(patientCRF))
  #  if patient == 48:
  #  im = nib.Nifti1Image(patientCRF, affine=affine)
  #  nib.save(im, os.path.join(CRFpath, name + '.nii.gz'))
       # im1 = nib.load(CRFpath + name + '.nii.gz')
      #  print(im1.get_data().shape)
     #   print(im1.get_data().dtype)
     #   print(im1.header)
    return patientCRF
Пример #14
0
def dencecrf(x, y, nlabels, w1, w2, alpha, beta, gamma, iteration=5):
    
    _x = np.array(x)
    _y = np.array(y, np.float32)

    assert len(_x.shape) == len(_y.shape), 'Shape of x and y should be (..., nchannels), (..., nlabels)'
    assert _y.shape[-1] == nlabels, 'Expect y.shape (...,{}), got {}'.format(nlabels, _y.shape)

    _y = _y.reshape((-1, nlabels))
    _y = np.transpose(_y, (1,0))

    d = dcrf.DenseCRF(np.prod(_x.shape[0:-1]), nlabels)
    d.setUnaryEnergy(_y.copy(order='C'))

    imgdim = len(_x.shape)-1

    gamma = (gamma,) * imgdim
    alpha = (alpha,) * imgdim
    beta = (beta,) * _x.shape[-1]

    featG = create_pairwise_gaussian(gamma, _x.shape[0:-1]) # g
    featB = create_pairwise_bilateral(alpha, beta, _x, imgdim) #a b

    d.addPairwiseEnergy(featG, w2) # w2
    d.addPairwiseEnergy(featB, w1) # w1

    out = d.inference(iteration)
    out = np.transpose(out, (1,0))
    out = np.reshape(out, y.shape)

    return out
def pp_denseCRF(imag, y_pred,
                pairwise_gauss=True, pairwise_bilateral=True,
                pw_gauss_sdims=(10, 10), pw_gauss_compat=3,
                pw_bilat_sdims=(20, 20), pw_bilat_schan=(0.005,),
                pw_bilat_compat=10, inf_steps=5):
    '''Dense CRF postprocessing using 2D image / softmax prediction matrix'''
    
    height, width, channels = imag.shape
    n_classes = y_pred.shape[-1]
    
    # Set unary energy
    d = dcrf.DenseCRF2D(width, height, n_classes)
    U = unary_from_softmax(np.moveaxis(y_pred, -1, 0))
    d.setUnaryEnergy(U)
    # Create the (color)-independent features and add them to the CRF
    if pairwise_gauss == True:    
        pw_gauss = create_pairwise_gaussian(sdims=pw_gauss_sdims,
                                            shape=y_pred.shape[:2])
        d.addPairwiseEnergy(pw_gauss, compat=pw_gauss_compat)
    # Create the (color)-dependent features and add them to the CRF
    if pairwise_bilateral == True:
        pw_bilateral = create_pairwise_bilateral(sdims=pw_bilat_sdims,
                                                 schan=pw_bilat_schan,
                                                 img=imag, chdim=2)
        d.addPairwiseEnergy(pw_bilateral, compat=pw_bilat_compat)
    # Inference
    Q = d.inference(inf_steps)
    # Reshape eigen matrix and return prediction in original shape 
    pred_Q = np.reshape(Q, (n_classes, height, width))
    pred_orig_shape = np.moveaxis(pred_Q, 0, -1)
    return pred_orig_shape
def post_processing(data, probas):
    [h,w] = data.shape
    n_labels = 2
    pred_maps = np.zeros(data.shape)
    #print 'postprocessing:', data.shape, probas.shape
    img = np.uint8(data*255)
    img = data[...,np.newaxis]
    #proba = probas
    proba = probas + 0.1
    #proba[proba>1] = 1
    labels = np.zeros((2,img.shape[0],img.shape[1]))
    labels[0] = 1-proba
    labels[1] = proba
    #U=labels
    U = unary_from_softmax(labels)  # note: num classes is first dim
    pairwise_energy = create_pairwise_bilateral(sdims=(7,7), schan=(0.001,), img=img, chdim=2)
    pairwise_gaussian = create_pairwise_gaussian(sdims=(3,3), shape=img.shape[:2])

    d = dcrf.DenseCRF2D(w, h, n_labels)
    d.setUnaryEnergy(U)
    d.addPairwiseEnergy(pairwise_gaussian, compat=1, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)
    d.addPairwiseEnergy(pairwise_energy, compat=1, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC)  # `compat` is the "strength" of this potential.

    Q = d.inference(5)
    pred_maps = np.argmax(Q, axis=0).reshape((h,w))
    return pred_maps
Пример #17
0
def process(final_probabilities,image,iters):

	softmax = final_probabilities.squeeze()
	print softmax.shape
	processed_probabilities = softmax.transpose((2, 0, 1))

	# The input should be the negative of the logarithm of probability values
	# Look up the definition of the softmax_to_unary for more information
	unary = softmax_to_unary(processed_probabilities)

	# The inputs should be C-continious -- we are using Cython wrapper
	unary = np.ascontiguousarray(unary)

	d = dcrf.DenseCRF(image.shape[0] * image.shape[1], 2)

	d.setUnaryEnergy(unary)

	# This potential penalizes small pieces of segmentation that are
	# spatially isolated -- enforces more spatially consistent segmentations
	feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

	d.addPairwiseEnergy(feats, compat=3,
	                    kernel=dcrf.DIAG_KERNEL,
	                    normalization=dcrf.NORMALIZE_SYMMETRIC)

	# This creates the color-dependent features --
	# because the segmentation that we get from CNN are too coarse
	# and we can use local color features to refine them
	feats = create_pairwise_bilateral(sdims=(50, 50), schan=(20, 20, 20),
	                                   img=image, chdim=2)

	d.addPairwiseEnergy(feats, compat=10,
	                     kernel=dcrf.DIAG_KERNEL,
	                     normalization=dcrf.NORMALIZE_SYMMETRIC)
	return d.inference(iters)
Пример #18
0
    def compute_lattice(self, img, num_classes=None):

        if num_classes is not None:
            self.num_classes = num_classes

        assert self.num_classes is not None

        npixels = self.shape[0] * self.shape[1]
        crf = dcrf.DenseCRF(npixels, self.num_classes)

        sdims = self.conf['pos_feats']['sdims']

        feats = utils.create_pairwise_gaussian(sdims=(sdims, sdims),
                                               shape=img.shape[:2])

        self.smooth_feats = feats

        self.crf = crf

        self.crf.addPairwiseEnergy(self.smooth_feats,
                                   compat=self.conf['pos_feats']['compat'])

        sdims = self.conf['col_feats']['sdims']
        schan = self.conf['col_feats']['schan']

        feats = utils.create_pairwise_bilateral(sdims=(sdims, sdims),
                                                schan=(schan, schan, schan),
                                                img=img,
                                                chdim=2)

        self.appear_feats = feats

        self.crf.addPairwiseEnergy(self.appear_feats,
                                   compat=self.conf['pos_feats']['compat'])
Пример #19
0
def get_crf_img(inputs, outputs):
    for i in range(outputs.shape[0]):
        img = inputs[i]
        softmax_prob = outputs[i]
        unary = unary_from_softmax(softmax_prob)
        unary = np.ascontiguousarray(unary)
        d = dcrf.DenseCRF(img.shape[0] * img.shape[1], 2)
        d.setUnaryEnergy(unary)
        feats = create_pairwise_gaussian(sdims=(10, 10), shape=img.shape[:2])
        d.addPairwiseEnergy(feats,
                            compat=3,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        feats = create_pairwise_bilateral(sdims=(50, 50),
                                          schan=(20, 20, 20),
                                          img=img,
                                          chdim=2)
        d.addPairwiseEnergy(feats,
                            compat=10,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        Q = d.inference(5)
        res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))
        if i == 0:
            crf = np.expand_dims(res, axis=0)
        else:
            res = np.expand_dims(res, axis=0)
            crf = np.concatenate((crf, res), axis=0)
    return crf
Пример #20
0
def dense_crf(probs,
              img=None,
              n_iters=10,
              n_classes=2,
              sxy_gaussian=(1, 1),
              compat_gaussian=4,
              kernel_gaussian=dcrf.DIAG_KERNEL,
              normalisation_gaussian=dcrf.NORMALIZE_SYMMETRIC,
              sxy_bilateral=(10, 10),
              compat_bilateral=5,
              srgb_bilateral=(5, 5, 5),
              kernel_bilateral=dcrf.DIAG_KERNEL,
              normalisation_bilateral=dcrf.NORMALIZE_SYMMETRIC):
    """DenseCRF over unnormalised predictions.
       More details on the arguments at https://github.com/lucasb-eyer/pydensecrf.
    
    Args:
      probs: class probabilities per pixel.
      img: if given, the pairwise bilateral potential on raw RGB values will be computed.
      n_iters: number of iterations of MAP inference.
      sxy_gaussian: standard deviations for the location component of the colour-independent term.
      compat_gaussian: label compatibilities for the colour-independent term (can be a number, a 1D array, or a 2D array).
      kernel_gaussian: kernel precision matrix for the colour-independent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_gaussian: normalisation for the colour-independent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      sxy_bilateral: standard deviations for the location component of the colour-dependent term.
      compat_bilateral: label compatibilities for the colour-dependent term (can be a number, a 1D array, or a 2D array).
      srgb_bilateral: standard deviations for the colour component of the colour-dependent term.
      kernel_bilateral: kernel precision matrix for the colour-dependent term (can take values CONST_KERNEL, DIAG_KERNEL, or FULL_KERNEL).
      normalisation_bilateral: normalisation for the colour-dependent term (possible values are NO_NORMALIZATION, NORMALIZE_BEFORE, NORMALIZE_AFTER, NORMALIZE_SYMMETRIC).
      
    Returns:
      Refined predictions after MAP inference.
    """
    _, h, w, _ = probs.shape

    probs = probs[0].transpose(2, 0,
                               1).copy(order='C')  # Need a contiguous array.

    d = dcrf.DenseCRF2D(w, h, n_classes)  # Define DenseCRF model.
    U = unary_from_softmax(probs)  # Unary potential.
    U = U.reshape((n_classes, -1))  # Needs to be flat.
    d.setUnaryEnergy(U)
    energy = create_pairwise_gaussian(sxy_gaussian, [w, h])
    d.addPairwiseEnergy(energy, compat=compat_gaussian)

    if img is not None:
        assert (
            img.shape[1:3] == (h, w)
        ), "The image height and width must coincide with dimensions of the logits."
        energy = create_pairwise_bilateral(sdims=sxy_bilateral,
                                           schan=srgb_bilateral[0],
                                           img=img,
                                           chdim=-1)
        d.addPairwiseEnergy(energy, compat=compat_bilateral)

    Q = d.inference(n_iters)
    preds = np.array(Q, dtype=np.float32).reshape(
        (n_classes, h, w)).transpose(1, 2, 0)
    return np.expand_dims(preds, 0)
Пример #21
0
    def __call__(self, image: np.ndarray, keypoints: np.ndarray) -> np.ndarray:
        if not imageutils.is_in_range(image, [0, 255]):
            raise imageutils.RangeError(image, "image", [0, 255])

        dynamic_range = image.max() / image.min()
        is_low_range = dynamic_range <= 2.0
        if is_low_range:
            import warnings

            warnings.warn(
                Warning(
                    "image has low dynamic range. Maybe convert to [0, 255]?"))

        h, w, c = image.shape
        keypoint_probs = imageutils.keypoints_to_heatmaps((h, w),
                                                          keypoints,
                                                          var=self.var)
        keypoint_probs = np.rollaxis(keypoint_probs, 2, 0)
        bg_prob = 1.0 - np.amax(keypoint_probs, axis=0, keepdims=True)
        probs = np.concatenate([bg_prob, keypoint_probs], axis=0)
        n_labels = probs.shape[0]
        probs_flat = np.reshape(probs, (n_labels, -1))

        d = dcrf.DenseCRF(h * w, n_labels)

        # flatten everything for dense crf

        # Set unary according to keypoints
        U = -np.log(probs_flat + 1.0e-6).astype(np.float32)
        d.setUnaryEnergy(U.copy(order="C"))

        # This creates the color-independent features and then add them to the CRF
        feats = create_pairwise_gaussian(sdims=(3, 3), shape=image.shape[:2])
        d.addPairwiseEnergy(
            feats,
            compat=3,
            kernel=dcrf.DIAG_KERNEL,
            normalization=dcrf.NORMALIZE_SYMMETRIC,
        )

        # This creates the color-dependent features and then add them to the CRF
        feats = create_pairwise_bilateral(sdims=(80, 80),
                                          schan=(13, 13, 13),
                                          img=image,
                                          chdim=2)
        d.addPairwiseEnergy(
            feats,
            compat=10,
            kernel=dcrf.DIAG_KERNEL,
            normalization=dcrf.NORMALIZE_SYMMETRIC,
        )

        # Run five inference steps.
        Q = d.inference(self.n_steps)

        # Find out the most probable class for each pixel.
        MAP = np.argmax(Q, axis=0)
        MAP = MAP.reshape((h, w))
        return MAP
 def apply(self, image, colour_axis):
     if colour_axis == -1:
         colour_axis = len(image.shape) - 1
     shape = [
         image.shape[i] for i in range(len(image.shape)) if i != colour_axis
     ]
     return crf_utils.create_pairwise_gaussian(sdims=self.sigmas,
                                               shape=shape)
Пример #23
0
def crfInference(imageGT, fgMask, probs):
    ##################################
    ### Read images and annotation ###
    ##################################
    # img = np.uint8(img*255)
    img = skimage.color.rgb2lab(imageGT)

    M = 3  # forground, background, occluding object.

    ###########################
    ### Setup the CRF model ###
    ###########################

    # Example using the DenseCRF class and the util functions
    crfmodel = dcrf.DenseCRF(img.shape[0] * img.shape[1], M)

    # get unary potentials (neg log probability)
    # U = compute_unary(labels, M)
    U = unaryOcclusionModel(img, fgMask, probs)

    U = superpixelUnary(imageGT, U, fgMask, 0.8)

    crfmodel.setUnaryEnergy(U)

    # This creates the color-independent features and then add them to the CRF
    feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
    crfmodel.addPairwiseEnergy(feats,
                               compat=3,
                               kernel=dcrf.DIAG_KERNEL,
                               normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features and then add them to the CRF
    feats = create_pairwise_bilateral(sdims=(30, 30),
                                      schan=(13, 13, 13),
                                      img=img,
                                      chdim=2)
    crfmodel.addPairwiseEnergy(feats,
                               compat=10,
                               kernel=dcrf.DIAG_KERNEL,
                               normalization=dcrf.NORMALIZE_SYMMETRIC)

    ####################################
    ### Do inference and compute map ###
    ####################################
    Q = crfmodel.inference(5)
    mapseg = np.argmax(Q, axis=0).reshape(img.shape[:2])

    # res = map.astype('float32') * 255 / map.max()
    # plt.imshow(res)
    # plt.show()

    # # Manually inference
    # Q, tmp1, tmp2 = crfmodel.startInference()
    # for i in range(5):
    #     print("KL-divergence at {}: {}".format(i, crfmodel.klDivergence(Q)))
    #     crfmodel.stepInference(Q, tmp1, tmp2)

    return mapseg, np.array(Q)
def apply_crf(seg_pred_probs,imgs):
    
    # Appearance parameters
#    a_sxy=160 # theta_alpha (refer website for equation terminology)
#    a_srgb= 3 # theta_beta
#    a_w1= 5 # weight term for bilateral term
#    # Gaussian smoothness term
#    g_sxy= 10 # theta_gamma
#    g_w2=30 # weight term for Gaussian smoothness
    a_w1, a_sxy, a_srgb = 5, 2, 0.1
    g_w2, g_sxy = 10, 5

    num_batch = seg_pred_probs.shape[0]
    masks_out = np.zeros_like(imgs)
    for k in range(num_batch):
        seg_pred_prob = seg_pred_probs[k,:,:,:]
        img = imgs[k,:,:,:]
        seg_pred_prob_tmp = np.swapaxes(np.swapaxes( seg_pred_prob,0,2 ),1,2)# to above mentioned shape - you could use np.swapaxes to achieve this.
    
        unary = unary_from_softmax(seg_pred_prob_tmp)
        
        d = dcrf.DenseCRF2D(exp_config.image_size[0],exp_config.image_size[1], exp_config.nlabels)
        d.setUnaryEnergy(unary)
        
        ###########################
        #Calculate Bilateral term
        ###########################
        
        ################################################
#        img_re = np.squeeze(img) #img_test_slice- 2D image containing intensity values
        img_re = np.copy(img)
        gaussian_pairwise_energy = create_pairwise_gaussian(sdims=(g_sxy,g_sxy), shape=img_re.shape[:2])
        d.addPairwiseEnergy(gaussian_pairwise_energy, compat=g_w2)
        
        bilateral_pairwise_energy = create_pairwise_bilateral(sdims=(a_sxy,a_sxy), schan=(a_srgb,), img=img_re, chdim=2)
        d.addPairwiseEnergy(bilateral_pairwise_energy, compat=a_w1) 

#        gaussian_pairwise_energy = create_pairwise_gaussian(shape=img_re.shape[:2])
#        d.addPairwiseEnergy(gaussian_pairwise_energy)
#        
#        bilateral_pairwise_energy = create_pairwise_bilateral(img=img_re, chdim=2)
#        d.addPairwiseEnergy(bilateral_pairwise_energy) 
        ################################################
        
        ######################
        # Inference 
        ######################
        # Run inference for 100 iterations
        Q_final = d.inference(100)
        
        # The Q is now the approximate posterior, we can get a MAP estimate using argmax.
        crf_seg_soln = np.argmax(Q_final, axis=0)
        
        # Unfortunately, the DenseCRF flattens everything, so get it back into picture form (width,height).
        crf_seg_soln = crf_seg_soln.reshape((exp_config.image_size[0],exp_config.image_size[1]))
        masks_out[k,:,:,:] = np.copy(np.expand_dims(crf_seg_soln,2))
    return masks_out
Пример #25
0
def crf(image, softmax):
    unary = softmax_to_unary(softmax)  # 转为一元.
    unary = np.ascontiguousarray(unary)
    d = dcrf.DenseCRF2D(image.shape[0], image.shape[1], 2)
    d.setUnaryEnergy(unary)
    feats = create_pairwise_gaussian(sdims=(4, 4), shape=image.shape[:2])    # (5,5)  #(10,10)

        d.addPairwiseEnergy(feats, compat=3,

                            kernel=dcrf.DIAG_KERNEL,

                            normalization=dcrf.NORMALIZE_SYMMETRIC)
Пример #26
0
def crf(img, prob):

    func_start = time.time()

    img = np.swapaxes(img, 0, 2)
    # img.shape: (width, height, num of channels)

    num_iter = 5

    prob = np.swapaxes(prob, 1, 2)  # shape: (1, width, height)

    # preprocess prob to (num_classes, width, height) since we have 2 classes: car and background.
    num_classes = 2
    probs = np.tile(prob, (num_classes, 1, 1))  # shape: (2, width, height)
    probs[0] = np.subtract(1, prob) # class 0 is background
    probs[1] = prob                 # class 1 is car

    d = dcrf.DenseCRF(img.shape[0] * img.shape[1], num_classes)

    unary = unary_from_softmax(probs)  # shape: (num_classes, width * height)
    unary = np.ascontiguousarray(unary)
    d.setUnaryEnergy(unary)

    # This potential penalizes small pieces of segmentation that are
    # spatially isolated -- enforces more spatially consistent segmentations
    feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
    d.addPairwiseEnergy(feats, compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    # Note that this potential is not dependent on the image itself.

    # This creates the color-dependent features --
    # because the segmentation that we get from CNN are too coarse
    # and we can use local color features to refine them
    feats = create_pairwise_bilateral(sdims=(80, 80), schan=(13, 13, 13),
                                      img=img, chdim=2)

    d.addPairwiseEnergy(feats, compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)


    Q = d.inference(num_iter)  # set the number of iterations
    res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))
    # res.shape: (width, height)

    res = np.swapaxes(res, 0, 1)  # res.shape:    (height, width)
    res = res[np.newaxis, :, :]   # res.shape: (1, height, width)

    func_end = time.time()
    # print('{:.2f} sec spent on CRF with {} iterations'.format(func_end - func_start, num_iter))
    # about 2 sec for a 1280 * 960 image with 5 iterations
    return res
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
Пример #28
0
def crf(img, scores):
    '''
    scores: prob numpy array after softmax with shape (_, C, H, W)
    img: image of shape (H, W, C)
    CRF parameters: bi_w = 4, bi_xy_std = 121, bi_rgb_std = 5, pos_w = 3, pos_xy_std = 3
    '''

    img = np.asarray(img, dtype=np.uint8)  # image.shape = [366,500,3]
    scores = np.asarray(scores.cpu().detach(), dtype=np.float32)

    scores = np.ascontiguousarray(scores)
    img = np.ascontiguousarray(img)

    n_classes, h, w = scores.shape

    d = dcrf.DenseCRF2D(w, h, n_classes)
    U = -np.log(scores)
    U = U.reshape((n_classes, -1))
    d.setUnaryEnergy(U)

    feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
    d.addPairwiseEnergy(feats,
                        compat=3,
                        kernel=dcrf.FULL_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features --
    # because the segmentation that we get from CNN are too coarse
    # and we can use local color features to refine them
    feats = create_pairwise_bilateral(sdims=(40, 40),
                                      schan=(7, 7, 7),
                                      img=img,
                                      chdim=2)
    d.addPairwiseEnergy(feats,
                        compat=10,
                        kernel=dcrf.FULL_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    Q = d.inference(5)

    Q = np.array(Q).reshape((2, h, w))

    for ii in range(Q.shape[0]):
        Q[ii] = cv2.medianBlur(Q[ii], 5)
        # Q[ii] = cv2.GaussianBlur(Q[ii], (7, 7), 1)

    Q_score = copy.deepcopy(Q)
    Q = np.argmax(Q, axis=0)

    return Q, Q_score
Пример #29
0
def crf(train_image, final_probabilities, train_annotation, number_class):

    for index_image in xrange(1):
        image = train_image
        softmax = final_probabilities[0].squeeze()
        #softmax_to_unary
        softmax = softmax.transpose((2, 0, 1))
        unary = unary_from_softmax(softmax)
        #softmax_to_unary
        unary = np.ascontiguousarray(unary)
        d = dcrf.DenseCRF(image.shape[0] * image.shape[1], number_class)
        d.setUnaryEnergy(unary)
        # This potential penalizes small pieces of segmentation that are
        # spatially isolated -- enforces more spatially consistent segmentations
        feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

        d.addPairwiseEnergy(feats,
                            compat=3,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        # This creates the color-dependent features --
        # because the segmentation that we get from CNN are too coarse
        # and we can use local color features to refine them
        feats = create_pairwise_bilateral(sdims=(50, 50),
                                          schan=(20, 20, 20),
                                          img=image,
                                          chdim=2)

        d.addPairwiseEnergy(feats,
                            compat=10,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)
        Q = d.inference(5)

        res = np.argmax(Q, axis=0).reshape((image.shape[0], image.shape[1]))

        cmap = plt.get_cmap('bwr')

        f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
        ax1.imshow(res, vmax=1.5, vmin=-0.4, cmap=cmap)
        ax1.set_title('Segmentation with CRF post-processing')
        probability_graph = ax2.imshow(
            np.dstack((train_annotation, ) * 3) * 100)
        ax2.set_title('Ground-Truth Annotation')
        plt.savefig('annotation_%d.png' % index_image,
                    bbox_inches='tight',
                    pad_inches=0)
        plt.gcf().clear()
        plt.show()
Пример #30
0
def get_prediction(pred_after_cnn, image):
    unary = pred_after_cnn.transpose((2, 0, 1))

    unary = -unary.reshape((unary.shape[0],-1))

    unary = np.ascontiguousarray(unary).astype(np.float32)

    d = dcrf.DenseCRF(image.shape[0] * image.shape[1], pred_after_cnn.shape[2])

    d.setUnaryEnergy(unary)

    feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

    d.addPairwiseEnergy(feats, compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    feats = create_pairwise_bilateral(sdims=(50, 50), schan=(20, 20, 20),
                                      img=image, chdim=2)

    d.addPairwiseEnergy(feats, compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    Q = d.inference(5)

    res = np.argmax(Q, axis=0).reshape((image.shape[0], image.shape[1]))

    return res

# def get_prediction(pred_after_cnn, image):
#     d = dcrf.DenseCRF2D(pred_after_cnn.shape[1], pred_after_cnn.shape[0], pred_after_cnn.shape[2])
#
#     U = pred_after_cnn.transpose((2, 0, 1))
#     U = -U.reshape((U.shape[0],-1))
#
#     U = np.ascontiguousarray(U).astype(np.float32)
#
#     d.setUnaryEnergy(U)
#
#     d.addPairwiseGaussian(sxy=3, compat=3)
#     d.addPairwiseBilateral(sxy=80, srgb=13, rgbim=image, compat=10)
#
#     Q = d.inference(5)
#
#     map = np.argmax(Q, axis=0).reshape(image.shape[0:2])
#
#     return map
Пример #31
0
    def compute_lattice(self, img, num_classes=None):
        """
        Compute indices for the lattice approximation.

         Arguments:
            img: np.array with shape [height, width, 3]
                The input image. Default config assumes image
                data in [0, 255]. For normalized images adapt
                `schan`. Try schan = 0.1 for images in [-0.5, 0.5]
        """

        if num_classes is not None:
            self.num_classes = num_classes

        assert self.num_classes is not None

        npixels = self.shape[0] * self.shape[1]
        crf = dcrf.DenseCRF(npixels, self.num_classes)

        sdims = self.conf['pos_feats']['sdims']

        feats = utils.create_pairwise_gaussian(
            sdims=(sdims, sdims),
            shape=img.shape[:2])

        self.smooth_feats = feats

        self.crf = crf

        self.crf.addPairwiseEnergy(
            self.smooth_feats, compat=self.conf['pos_feats']['compat'])

        sdims = self.conf['col_feats']['sdims']
        schan = self.conf['col_feats']['schan']

        feats = utils.create_pairwise_bilateral(sdims=(sdims, sdims),
                                                schan=(schan, schan, schan),
                                                img=img, chdim=2)

        self.appear_feats = feats

        self.crf.addPairwiseEnergy(
            self.appear_feats, compat=self.conf['pos_feats']['compat'])
Пример #32
0
def perform_crf(image, probabilities):

    image = image.squeeze()
    softmax = probabilities.squeeze().transpose((2, 0, 1))

    # The input should be the negative of the logarithm of probability values
    # Look up the definition of the softmax_to_unary for more information
    unary = unary_from_softmax(softmax)

    # The inputs should be C-continious -- we are using Cython wrapper
    unary = np.ascontiguousarray(unary)

    d = dcrf.DenseCRF(image.shape[0] * image.shape[1], number_of_classes)

    d.setUnaryEnergy(unary)

    # This potential penalizes small pieces of segmentation that are
    # spatially isolated -- enforces more spatially consistent segmentations
    feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

    d.addPairwiseEnergy(feats, compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features --
    # because the segmentation that we get from CNN are too coarse
    # and we can use local color features to refine them
    feats = create_pairwise_bilateral(sdims=(50, 50), schan=(20, 20, 20),
                                      img=image, chdim=2)

    d.addPairwiseEnergy(feats, compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    Q = d.inference(5)

    res = np.argmax(Q, axis=0).reshape((image.shape[0], image.shape[1]))
    return res
Пример #33
0
def crf(fn_im,fn_anno,fn_output,colorful_fn_output):
    ##################################
    ### Read images and annotation ###
    ##################################
    img = imread(fn_im)
    #truth_img = imread(truth_image).astype(np.uint8)
    # Convert the annotation's RGB color to a single 32-bit integer color 0xBBGGRR
    anno_rgb = imread(fn_anno)


    #anno_lbl = anno_rgb[:,:,0] + (anno_rgb[:,:,1] << 8) + (anno_rgb[:,:,2] << 16)
    anno_lbl = anno_rgb

    # Convert the 32bit integer color to 1, 2, ... labels.
    # Note that all-black, i.e. the value 0 for background will stay 0.
    colors, labels = np.unique(anno_lbl, return_inverse=True)


    # And create a mapping back from the labels to 32bit integer colors.
    # But remove the all-0 black, that won't exist in the MAP!
    colors = colors[1:]

    colorize = np.empty((len(colors), 1), np.uint8)

    colorize[:,0] = (colors & 0x0000FF)
    #colorize[:,1] = (colors & 0x00FF00) >> 8
    #colorize[:,2] = (colors & 0xFF0000) >> 16




    # Compute the number of classes in the label image.
    # We subtract one because the number shouldn't include the value 0 which stands
    # for "unknown" or "unsure".
    n_labels = len(set(labels.flat)) - 1
    print(n_labels, " labels and \"unknown\" 0: ", set(labels.flat))

    ###########################
    ### Setup the CRF model ###
    ###########################
    use_2d = False
    # use_2d = True
    if use_2d:
        print("Using 2D specialized functions")

        # Example using the DenseCRF2D code
        if n_labels==1:
            n_labels+=1
        d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], n_labels)

        # get unary potentials (neg log probability)
        U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=False)
        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:
        print("Using generic 2D functions")

        # Example using the DenseCRF class and the util functions
        d = dcrf.DenseCRF(img.shape[1] * img.shape[0], n_labels)

        # get unary potentials (neg log probability)
        U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=True)
        d.setUnaryEnergy(U)

        # This creates the color-independent features and then add them to the CRF
        feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
        d.addPairwiseEnergy(feats, compat=3,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)

        # This creates the color-dependent features and then add them to the CRF
        feats = create_pairwise_bilateral(sdims=(80, 80), schan=(13, 13, 13),
                                          img=img, chdim=2)
        d.addPairwiseEnergy(feats, compat=10,
                            kernel=dcrf.DIAG_KERNEL,
                            normalization=dcrf.NORMALIZE_SYMMETRIC)


    ####################################
    ### Do inference and compute MAP ###
    ####################################

    # Run five inference steps.

    Q = d.inference(10)

    # Find out the most probable class for each pixel.
    MAP = np.argmax(Q, axis=0)
    # Convert the MAP (labels) back to the corresponding colors and save the image.
    MAP = colorize[MAP,:]


    ####################################
    ###     Convert to greyscale     ###
    ####################################


    crf_img = MAP.reshape(anno_lbl.shape)
    ########change to rgb########
    
    label = anno_lbl
    
    
    ind = crf_img
    
    
    r = ind.copy()
    g = ind.copy()
    b = ind.copy()

    r_gt = label.copy()
    g_gt = label.copy()
    b_gt = label.copy()


    wall = [139,181,248]
    building = [251,209,244]
    sky = [44,230,121]
    floor = [156,40,149]
    tree = [166,219,98]
    ceiling = [35,229,138]
    road = [143,56,194]
    bed  = [144,223,70]
    windowpane = [200,162,57]
    grass = [120,225,199]
    cabinet = [87,203,13]
    sidewalk = [185,1,136]
    person = [16,167,16]
    earth = [29,249,241]
    door = [17,192,40]
    table = [199,44,241]
    mountain = [193,196,159]
    plant = [241,172,78]
    curtain = [56,94,128]
    chair = [231,166,116]
    car = [50,209,252]
    water = [217,56,227]
    painting = [168,198,178]
    sofa = [77,179,188]
    shelf = [236,191,103]
    house = [248,138,151]
    sea = [214,251,89]
    mirror = [208,204,187]
    rug = [115,104,49]
    field = [29,202,113]
    armchair = [159,160,95]
    seat = [78,188,13]
    fence = [83,203,82]
    desk = [8,234,116]
    rock = [80,159,200]
    wardrobe = [124,194,2]
    lamp = [192,146,237]
    bathtub = [64,3,73]
    railing = [17,213,58]
    cushion = [106,54,105]
    base = [125,72,155]
    box = [202,36,231]
    column = [79,144,4]
    signboard = [118,185,128]
    chest = [138,61,178]
    counter = [23,182,182]
    sand = [154,114,4]
    sink = [201,0,83]
    skyscraper = [21,134,53]
    fireplace = [194,77,237]
    refrigerator = [198,81,106]
    grandstand = [37,222,181]
    path = [203,185,14]
    stairs = [134,140,113]
    runway = [220,196,79]
    case = [64,26,68]
    pooltable = [128,89,2]
    pillow = [199,228,65]
    screen = [62,215,111]
    stairway = [124,148,166]
    river = [221,119,245]
    bridge = [68,57,158]
    bookcase = [80,47,26]
    blind = [143,59,56]
    coffeetable = [14,80,215]
    toilet = [212,132,31]
    flower = [2,234,129]
    book = [134,179,44]
    hill = [53,21,129]
    bench = [80,176,236]
    countertop = [154,39,168]
    stove = [221,44,139]
    palm = [103,56,185]
    kitchenisland = [224,138,83]
    computer = [243,93,235]
    swivelchair = [80,158,63]
    boat = [81,229,38]
    bar = [116,215,38]
    arcademachine = [103,69,182]
    hovel = [66,81,5]
    bus = [96,157,229]
    towel = [164,49,170]
    light = [14,42,146]
    truck = [164,67,44]
    tower = [108,116,151]
    chandelier = [144,8,144]
    awning = [85,68,228]
    streetlight = [16,236,72]
    booth = [108,7,86]
    television = [172,27,94]
    airplane = [119,247,193]
    dirttrack = [155,240,152]
    apparel = [49,158,204]
    pole = [23,193,204]
    land = [228,66,107]
    bannister = [69,36,163]
    escalator = [238,158,228]
    ottoman = [202,226,35]
    bottle = [194,243,151]
    buffet = [192,56,76]
    poster = [16,115,240]
    stage = [61,190,185]
    van = [7,134,32]
    ship = [192,87,171]
    fountain = [45,11,254]
    conveyerbelt = [179,183,31]
    canopy = [181,175,146]
    washer = [13,187,133]
    plaything = [12,1,2]
    swimmingpool = [63,199,190]
    stool = [221,248,32]
    barrel = [183,221,51]
    basket = [90,111,162]
    waterfall = [82,0,6]
    tent = [40,0,239]
    bag = [252,81,54]
    minibike = [110,245,152]
    cradle = [0,187,93]
    oven = [163,154,153]
    ball = [134,66,99]
    food = [123,150,242]
    step = [38,144,137]
    tank = [59,180,230]
    tradename = [144,212,16]
    microwave = [132,125,200]
    pot = [26,3,35]
    animal = [199,56,92]
    bicycle = [83,223,224]
    lake = [203,47,137]
    dishwasher = [74,74,251]
    screen = [246,81,197]
    blanket = [168,130,178]
    sculpture = [136,85,200]
    hood = [186,147,103]
    sconce = [170,21,85]
    vase = [104,52,182]
    trafficlight = [166,147,202]
    tray = [103,119,71]
    ashcan = [74,161,165]
    fan = [14,9,83]
    pier = [129,194,43]
    crtscreen = [7,100,55]
    plate = [13,12,170]
    monitor = [30,21,22]
    bulletinboard = [224,189,139]
    shower = [40,77,25]
    radiator = [194,14,94]
    glass = [178,8,231]
    clock = [234,166,8]
    flag = [248,25,7]   
    unlabelled = [0,0,0]

    label_colours = np.array([wall,building,sky,floor,tree,ceiling,road,bed ,windowpane,grass,cabinet,sidewalk,person,earth,door,table,mountain,plant,curtain,chair,car,water,painting,sofa,shelf,house,sea,mirror,rug,field,armchair,seat,fence,desk,rock,wardrobe,lamp,bathtub,railing,cushion,base,box,column,signboard,chest,counter,sand,sink,skyscraper,fireplace,refrigerator,grandstand,path,stairs,runway,case,pooltable,pillow,screen,stairway,river,bridge,bookcase,blind,coffeetable,toilet,flower,book,hill,bench,countertop,stove,palm,kitchenisland,computer,swivelchair,boat,bar,arcademachine,hovel,bus,towel,light,truck,tower,chandelier,awning,streetlight,booth,television,airplane,dirttrack,apparel,pole,land,bannister,escalator,ottoman,bottle,buffet,poster,stage,van,ship,fountain,conveyerbelt,canopy,washer,plaything,swimmingpool,stool,barrel,basket,waterfall,tent,bag,minibike,cradle,oven,ball,food,step,tank,tradename,microwave,pot,animal,bicycle,lake,dishwasher,screen,blanket,sculpture,hood,sconce,vase,trafficlight,tray,ashcan,fan,pier,crtscreen,plate,monitor,bulletinboard,shower,radiator,glass,clock,flag,unlabelled])
    
    for l in range(0,150):
        r[ind==l] = label_colours[l,0]
        g[ind==l] = label_colours[l,1]
        b[ind==l] = label_colours[l,2]
        r_gt[label==l] = label_colours[l,0]
        g_gt[label==l] = label_colours[l,1]
        b_gt[label==l] = label_colours[l,2]
        # r_truth[truth_img==l] = label_colours[l,0]
        # g_truth[truth_img==l] = label_colours[l,1]
        # b_truth[truth_img==l] = label_colours[l,2]


    rgb = np.zeros((ind.shape[0], ind.shape[1], 3))
    rgb[:,:,0] = r
    rgb[:,:,1] = g
    rgb[:,:,2] = b
    rgb_gt = np.zeros((label.shape[0], label.shape[1], 3))

    rgb_gt[:,:,0] = r_gt
    rgb_gt[:,:,1] = g_gt
    rgb_gt[:,:,2] = b_gt

    
    cv2.imwrite(colorful_fn_output,rgb)






    ########change to rgb########
    imsave(fn_output,crf_img)
    # Just randomly manually run inference iterations
    Q, tmp1, tmp2 = d.startInference()
    for i in range(10):
        print("KL-divergence at {}: {}".format(i, d.klDivergence(Q)))
        d.stepInference(Q, tmp1, tmp2)
Пример #34
0
    # 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)

    # get unary potentials (neg log probability)
    U = compute_unary(labels, M)
    d.setUnaryEnergy(U)

    # This creates the color-independent features and then add them to the CRF
    feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
    d.addPairwiseEnergy(feats, compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features and then add them to the CRF
    feats = create_pairwise_bilateral(sdims=(80, 80), schan=(13, 13, 13),
                                      img=img, chdim=2)
    d.addPairwiseEnergy(feats, compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)


####################################
### Do inference and compute map ###
####################################