-
Notifications
You must be signed in to change notification settings - Fork 1
/
crf_img_bilateral.py
74 lines (59 loc) · 2.03 KB
/
crf_img_bilateral.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
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_softmax,create_pairwise_bilateral
import numpy as np
import matplotlib.pyplot as plt
### generate the probability out of network
from scipy.stats import multivariate_normal
H,W,NLABELS=400,512,2
pos=np.stack(np.mgrid[0:H,0:W],axis=2)
rv = multivariate_normal([H//2, W//2], (H//4)*(W//4))
probs = rv.pdf(pos)
probs=(probs-probs.min())/(probs.max()-probs.min())
probs=0.5+0.2*(probs-0.5)
probs=np.tile(probs[np.newaxis,:,:],(2,1,1))#if the segmentation task has 2 classes, the shape is 2,h,w.
probs[1,:,:]=1-probs[0,:,:]
### take a look at the probability
plt.figure(figsize=(10,5))
plt.subplot(121)
plt.imshow(probs[0],cmap=plt.get_cmap('gray_r'))
plt.subplot(122)
plt.imshow(probs[1],cmap=plt.get_cmap('gray_r'))
plt.show()
### generate the img
NCHAN=1
img=np.zeros((H,W,NCHAN),np.uint8)
img[H//3:2*H//3,W//4:3*W//4,:]=1
### take a look at the img
plt.imshow(np.squeeze(img),cmap=plt.get_cmap("gray_r"))
plt.show()
### use densecrf to optimize the segmentation result
pairwise_energy=create_pairwise_bilateral(sdims=(10,10),schan=(0.01,),img=img,chdim=2)
U=unary_from_softmax(probs)
d=dcrf.DenseCRF2D(W,H,NLABELS)
d.setUnaryEnergy(U)
d.addPairwiseEnergy(pairwise_energy,compat=10)
### look at intermediate solutions
Q,tmp1,tmp2=d.startInference()
for _ in range(5):
d.stepInference(Q,tmp1,tmp2)
kl1=d.klDivergence(Q)/(H*W)
map_soln1=np.argmax(Q,axis=0).reshape((H,W))
for _ in range(20):
d.stepInference(Q,tmp1,tmp2)
kl2=d.klDivergence(Q)/(H*W)
map_soln2=np.argmax(Q,axis=0).reshape((H,W))
for _ in range(50):
d.stepInference(Q,tmp1,tmp2)
kl3=d.klDivergence(Q)/(H*W)
map_soln3=np.argmax(Q,axis=0).reshape((H,W))
fig=plt.figure(figsize=(15,5))
fig.add_subplot(131)
plt.title("Step 5, KL :"+str(kl1))
plt.imshow(map_soln1,cmap=plt.get_cmap("gray_r"))
fig.add_subplot(132)
plt.title("Step 25, KL :"+str(kl2))
plt.imshow(map_soln2,cmap=plt.get_cmap("gray_r"))
fig.add_subplot(133)
plt.title("Step 75, KL :"+str(kl3))
plt.imshow(map_soln3,cmap=plt.get_cmap("gray_r"))
plt.show()