def crf(prob, area): import pydensecrf.densecrf as dcrf from pydensecrf.utils import unary_from_softmax, create_pairwise_bilateral s_x = 16 s_y = 16 s_x_Gaussian = 4 s_y_Gaussian = 4 s_ch = 0.01 W = prob.shape[2] H = prob.shape[1] NLABELS = prob.shape[0] image_path = ('top/top_mosaic_09cm_area%s.tif') % area dsm_path = ('dsm/dsm_09cm_matching_area%s.tif') % area image_rgb = imread(image_path) image_dsm = imread(dsm_path) image_rgb = image_rgb[:H, :W, :] image_dsm = image_dsm[:H, :W] #input_image = input_image.transpose(2,0,1) U = unary_from_softmax(prob) input_image_1 = image_rgb[:, :, 0:1] pairwise_energy_1 = create_pairwise_bilateral(sdims=(s_x, s_y), schan=(s_ch, ), img=input_image_1, chdim=2) input_image_2 = image_rgb[:, :, 1:2] pairwise_energy_2 = create_pairwise_bilateral(sdims=(s_x, s_y), schan=(s_ch, ), img=input_image_2, chdim=2) input_image_3 = image_rgb[:, :, 2:3] pairwise_energy_3 = create_pairwise_bilateral(sdims=(s_x, s_y), schan=(s_ch, ), img=input_image_3, chdim=2) input_image_4 = image_dsm[:, :] pairwise_energy_4 = create_pairwise_bilateral(sdims=(s_x, s_y), schan=(s_ch, ), img=input_image_4, chdim=2) d = dcrf.DenseCRF2D(W, H, NLABELS) d.setUnaryEnergy(U) d.addPairwiseEnergy(pairwise_energy_1, compat=10, kernel=dcrf.FULL_KERNEL) d.addPairwiseEnergy(pairwise_energy_2, compat=10, kernel=dcrf.FULL_KERNEL) d.addPairwiseEnergy(pairwise_energy_3, compat=10, kernel=dcrf.FULL_KERNEL) d.addPairwiseEnergy(pairwise_energy_4, compat=10, kernel=dcrf.FULL_KERNEL) d.addPairwiseGaussian(sxy=(s_x_Gaussian, s_y_Gaussian), compat=1) Q = d.inference(5) out_crf = np.argmax(Q, axis=0).reshape((H, W)) out_crf_expand = np.zeors(NLABELS, H, W) for i in range(NLABELS): out_crf_expand[i] = 1 * (out_crf == i) return out_crf_expand
def dense_crf_no_rgb(img, output_probs): import pydensecrf.densecrf as dcrf from pydensecrf.utils import create_pairwise_bilateral, unary_from_softmax h = output_probs.shape[0] w = output_probs.shape[1] output_probs = np.expand_dims(output_probs, 0) output_probs = np.append(1 - output_probs, output_probs, axis=0) d = dcrf.DenseCRF2D(w, h, 2) # U = -np.log(output_probs) # U = U.reshape((2, -1)) # U = np.ascontiguousarray(U) U = unary_from_softmax(output_probs) pairwise_energy = create_pairwise_bilateral(sdims=(1, 1), schan=(0.01, ), img=img) U = U.astype(np.float32) d.setUnaryEnergy(U) d.addPairwiseEnergy(pairwise_energy, compat=10) Q, tmp1, tmp2 = d.startInference() for _ in range(5): d.stepInference(Q, tmp1, tmp2) Q = np.argmax(np.array(Q), axis=0).reshape((h, w)) return Q
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 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
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)
def getCRF_justcol(img, Lc, label_lines): if np.ndim(img) == 2: img = np.dstack((img, img, img)) H = img.shape[0] W = img.shape[1] d = dcrf.DenseCRF2D(H, W, len(label_lines) + 1) U = unary_from_labels(Lc.astype('int'), len(label_lines) + 1, gt_prob=params['prob']) d.setUnaryEnergy(U) feats = create_pairwise_bilateral(sdims=(params['theta'], params['theta']), schan=(params['scale'], params['scale'], params['scale']), img=img, chdim=2) d.addPairwiseEnergy(feats, compat=params['compat_col'], kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) Q = d.inference(params['n_iter']) preds = np.array(Q, dtype=np.float32).reshape( (len(label_lines) + 1, H, W)).transpose(1, 2, 0) preds = np.expand_dims(preds, 0) preds = np.squeeze(preds) return np.argmax(Q, axis=0).reshape((H, W)), preds
def crf_refine(label, img, nclasses,theta_col=100, mu=120, theta_spat=3, mu_spat=3): """ "crf_refine(label, img)" This function refines a label image based on an input label image and the associated image Uses a conditional random field algorithm using spatial and image features INPUTS: * label [ndarray]: label image 2D matrix of integers * image [ndarray]: image 3D matrix of integers OPTIONAL INPUTS: None GLOBAL INPUTS: None OUTPUTS: label [ndarray]: label image 2D matrix of integers """ H = label.shape[0] W = label.shape[1] U = unary_from_labels(1+label,nclasses,gt_prob=0.51) d = dcrf.DenseCRF2D(H, W, nclasses) d.setUnaryEnergy(U) # to add the color-independent term, where features are the locations only: d.addPairwiseGaussian(sxy=(theta_spat, theta_spat), compat=mu_spat, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) feats = create_pairwise_bilateral( sdims=(theta_col, theta_col), schan=(2,2,2), img=img, chdim=2) d.addPairwiseEnergy(feats, compat=mu,kernel=dcrf.DIAG_KERNEL,normalization=dcrf.NORMALIZE_SYMMETRIC) Q = d.inference(10) #kl1 = d.klDivergence(Q) return np.argmax(Q, axis=0).reshape((H, W)).astype(np.uint8)#, kl1
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
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
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
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
def crf_func(imgs, plabels): outputs = [] for img, probs in zip(imgs, plabels): # Example using the DenseCRF class and the util functions d = dcrf.DenseCRF(img.shape[1] * img.shape[0], 2) # get unary potentials (neg log probability) U = unary_from_softmax(probs) d.setUnaryEnergy(U) # 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=1, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # Run five inference steps. Q = d.inference(5) # Find out the most probable class for each pixel. # MAP = np.argmax(Q, axis=0) # bb = 1-MAP.reshape(img.shape[:2]) bb = np.array(Q).reshape((2, img.shape[0], img.shape[1])) outputs.append(bb) outputs = np.array(outputs) return outputs
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
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
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_
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))
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 crf_proc(imgs, probs): _, _, H, W = imgs.shape imgs = imgs.transpose((0, 2, 3, 1)) lbls = [] for img, prob in zip(imgs, probs): prob = np.concatenate((1 - prob[None, ...], prob[None, ...]), 0) # Example using the DenseCRF class and the util functions d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], 2) # get unary potentials (neg log probability) U = unary_from_softmax(prob) d.setUnaryEnergy(U) # 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.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) d.addPairwiseEnergy(feats, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # Run five inference steps. Q = d.inference(5) # Find out the most probable class for each pixel. MAP = np.argmax(Q, axis=0).reshape((H, W)) lbls.append(MAP) lbls = np.stack(lbls) return lbls
def getCRF(image, Lc, theta, n_iter, label_lines, compat_spat=12, compat_col=40, scale=5, prob=0.5): # 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). H = image.shape[0] W = image.shape[1] d = dcrf.DenseCRF2D(H, W, len(label_lines)+1) U = unary_from_labels(Lc.astype('int'), len(label_lines)+1, gt_prob= prob) d.setUnaryEnergy(U) del U # This potential penalizes small pieces of segmentation that are # spatially isolated -- enforces more spatially consistent segmentations # This adds the color-independent term, features are the locations only. # sxy = The scaling factors per dimension. d.addPairwiseGaussian(sxy=(theta,theta), compat=compat_spat, kernel=dcrf.DIAG_KERNEL, #compat=6 normalization=dcrf.NORMALIZE_SYMMETRIC) # sdims = The scaling factors per dimension. # schan = The scaling factors per channel in the image. # This creates the color-dependent features and then add them to the CRF feats = create_pairwise_bilateral(sdims=(theta, theta), schan=(scale, scale, scale), #11,11,11 img=image, chdim=2) del image d.addPairwiseEnergy(feats, compat=compat_col, #20 kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) del feats Q = d.inference(n_iter) ## uncomment if you want an images of the posterior probabilities per label #preds = np.array(Q, dtype=np.float32).reshape( # (len(label_lines)+1, nx, ny)).transpose(1, 2, 0) #preds = np.expand_dims(preds, 0) #preds = np.squeeze(preds) return np.argmax(Q, axis=0).reshape((H, W)) #, preds
def crf_seg(img, mask, gt_prob=0.9): labels = mask.flatten() M = 2 # number of labels # Setup the CRF model d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M) # Certainty that the ground truth is correct GT_PROB = gt_prob U = unary_from_labels(labels, M, GT_PROB, False) d.setUnaryEnergy(U) feats = create_pairwise_bilateral(sdims=(10, 10), schan=(0.01, ), img=np.expand_dims(img, 2), chdim=2) d.addPairwiseEnergy(feats, compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) Q = d.inference(10) res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1])) return res
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
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'])
def crf(softmax_outputs, inputs): result = torch.zeros(softmax_outputs.shape) idx = 0 for input in inputs: # unary u = unary_from_softmax( softmax_outputs[idx].cpu().detach().numpy()).reshape( softmax_outputs.shape[1], -1) # pairwise p = create_pairwise_bilateral(sdims=(25, 25), schan=(0.05, 0.05), img=input.cpu().detach().numpy(), chdim=0) crf = dcrf.DenseCRF2D(inputs.shape[3], inputs.shape[2], softmax_outputs.shape[1]) # unary potential crf.setUnaryEnergy(u) # + pairwise potential crf.addPairwiseEnergy(p, compat=100) Q = crf.inference(10) print(Q) result[idx] = torch.tensor( np.array(Q).reshape((-1, inputs.shape[2], inputs.shape[3]))) idx += 1 return result
def dense_crf(img, output_probs): h = output_probs.shape[0] w = output_probs.shape[1] output_probs = np.expand_dims(output_probs, 0) output_probs = np.append(1 - output_probs, output_probs, axis=0) d = dcrf.DenseCRF2D(w, h, 2) # width, height, nlabels U = -np.log(output_probs) # U should be negative log-probabilities U = U.reshape((2, -1)) # Needs to be flat U = np.ascontiguousarray(U) img = np.ascontiguousarray(img) d.setUnaryEnergy(U) # 设置一元势函数 ## This adds the color-independent term, features are the locations only. d.addPairwiseGaussian(sxy=20, compat=3) # This adds the color-dependent term, i.e. features are (x,y,r,g,b). no use for this data !!! #d.addPairwiseBilateral(sxy=30, srgb=20, rgbim=img, compat=10)#仅支持RGB # Create the pairwise bilateral term from the above image. # The two `s{dims,chan}` parameters are model hyper-parameters defining # the strength of the location and image content bilaterals, respectively. # chdim代表channels通道在哪个维度 pairwise_energy = create_pairwise_bilateral(sdims=(10, 10), schan=(0.01), img=img, chdim=2) #d.addPairwiseEnergy(pairwise_energy, compat=10) # 'compat' is the "strength" of this potential Q = d.inference(5) Q = np.argmax(np.array(Q), axis=0).reshape((h, w)) return Q
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
def getCRF_justcol(img, Lc, theta, n_iter, label_lines, compat_col=40, scale=5, prob=0.5): H = img.shape[0] W = img.shape[1] d = dcrf.DenseCRF2D(H, W, len(label_lines)+1) U = unary_from_labels(Lc.astype('int'), len(label_lines)+1, gt_prob= prob) d.setUnaryEnergy(U) del U # sdims = The scaling factors per dimension. # schan = The scaling factors per channel in the image. # This creates the color-dependent features and then add them to the CRF feats = create_pairwise_bilateral(sdims=(theta, theta), schan=(scale, scale, scale), #11,11,11 img=img, chdim=2) del img d.addPairwiseEnergy(feats, compat=compat_col, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) del feats Q = d.inference(n_iter) preds = np.array(Q, dtype=np.float32).reshape( (len(label_lines)+1, H, W)).transpose(1, 2, 0) preds = np.expand_dims(preds, 0) preds = np.squeeze(preds) return np.argmax(Q, axis=0).reshape((H, W)), preds #, p, R, np.abs(d.klDivergence(Q)/ (H*W))
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 __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 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)
def main(args): device = 'cuda' if torch.cuda.is_available() else 'cpu' d = dcrf.DenseCRF2D(512, 512, 2) down_method = args.down_method up_method = args.up_method separable = args.separable ds = DataSetWrapper(args.batch_size, args.num_workers, 0.2) test_dl = ds.get_data_loaders(train=False) model = Unet(input_dim=1, separable=True, down_method='conv', up_method='transpose') model = nn.DataParallel(model).to(device) load_state = torch.load(f'./checkpoint/conv_transpose_True.ckpt') model.load_state_dict(load_state['model_state_dict']) model.eval() name = 0 with torch.no_grad(): for (img, label) in test_dl: imgs, labels = img.to(device), label.to(device) preds = model(img) name += 1 for i in range(args.batch_size): img, label, pred = imgs[i, :], labels[i, :], preds[i, :] probs = torch.stack([1 - pred, pred], dim=0).cpu().numpy() img, label = img.cpu().numpy(), label.cpu().numpy() pairwise_energy = create_pairwise_bilateral(sdims=(10, 10), schan=(0.01, ), img=img, chdim=0) U = unary_from_softmax(probs) d = dcrf.DenseCRF2D(512, 512, 2) d.setUnaryEnergy(U) d.addPairwiseEnergy(pairwise_energy, compat=10) Q = d.inference(100) map = np.argmax(Q, axis=0).reshape((512, 512)) print(map.shape) img = (255. / img.max() * (img - img.min())).astype(np.uint8) label = (255. / label.max() * (label - label.min())).astype( np.uint8) pred = (255. / map.max() * (map - map.min())).astype(np.uint8) img = Image.fromarray(img[0, :], mode='L') label = Image.fromarray(label[0, :], mode='L') pred = Image.fromarray(pred, mode='L') img.save(f'./results/{name}_{i}_i.png') label.save(f'./results/{name}_{i}_l.png') pred.save(f'./results/{name}_{i}_p.png')
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'])
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
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 ### #################################### Q = d.inference(5) map = np.argmax(Q, axis=0).reshape(img.shape[:2]) res = map.astype('float32') * 255 / map.max() plt.imshow(res) plt.show()
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)