-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrfforinference.py
111 lines (89 loc) · 3.67 KB
/
crfforinference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 17:10:58 2017
@author: sy
"""
import pydensecrf.densecrf as dense_crf
from cv2 import imread
import matplotlib.pyplot as plt
from densecrf2 import crf_model, potentials
import cv2
import os
import numpy as np
# Create unary potential
unary = potentials.UnaryPotentialFromProbabilities(gt_prob=0.7)
bilateral_pairwise = potentials.BilateralPotential(
sdims=80,
schan=13,
compatibility=4,
kernel=dense_crf.DIAG_KERNEL,
normalization=dense_crf.NORMALIZE_SYMMETRIC
)
gaussian_pairwise = potentials.GaussianPotential(
sigma=3,
compatibility=2,
kernel=dense_crf.DIAG_KERNEL,
normalization=dense_crf.NORMALIZE_SYMMETRIC
)
# =============================================================================
# Create CRF model and add potentials
# =============================================================================
#zero_unsure: whether zero is a class, if its False, it means zero canb be any of other classes
# =============================================================================
# crf = crf_model.DenseCRF(
# num_classes = 3,
# zero_unsure = True, # The number of output classes
# unary_potential=unary,
# pairwise_potentials=[bilateral_pairwise, gaussian_pairwise],
# use_2d = 'rgb-2d' #'rgb-1d' or 'rgb-2d' or 'non-rgb'
# )
# =============================================================================
crf = crf_model.DenseCRF(
num_classes =5,
zero_unsure = False, # The number of output classes
unary_potential=unary,
pairwise_potentials=[bilateral_pairwise, gaussian_pairwise],
use_2d = 'rgb-1d' #'rgb-1d' or 'rgb-2d' or 'non-rgb'
)
def crfing(rootpath,image,probabilities,count):
# =============================================================================
# Set the CRF model
# =============================================================================
#label_source: whether label is from softmax, or other type of label.
crf.set_image(
image=image,
probabilities=probabilities,
colour_axis=-1, # The axis corresponding to colour in the image numpy shape
class_axis=-1, # The axis corresponding to which class in the probabilities shape
label_source = 'label' # where the label come from, 'softmax' or 'label'
)
crf.perform_inference(10) # The CRF model will restart run.
new_mask10 = crf.segmentation_map
print(crf.kl_divergence)
cv2.imwrite(rootpath+str(count)+".png",new_mask10)
print(count)
# =============================================================================
# Load image and probabilities
# =============================================================================
# name,imgnum=[],[]
# rootpath='./dataset/inference'
# for f in os.listdir(rootpath+'/test1result'):
# name.append(f)
# imgnum.append(f[:-9])
# image_name=f[:-9]+'.jpg'
# probabilities_name=f
# image_name_path=rootpath+'/test1/'+image_name
# probabilities_name_path=rootpath+'/test1result/'+probabilities_name
# image = imread(image_name_path)
# probabilities = imread(probabilities_name_path)
#
# colors, labels = np.unique(probabilities, return_inverse=True)
# HAS_UNK = (50 or 100 or 150) in colors
# count = f[:-9]
# if HAS_UNK:
# crfing(rootpath,image, probabilities,count)
# else:
# cv2.imwrite(rootpath + '/720crf3/' + str(count) + ".png", probabilities)
# print(count)
# print(len(name))