Skip to content

Commit c15c0e5

Browse files
committed
Tests I found that I never commited. Issue lucasb-eyer#26
1 parent afda975 commit c15c0e5

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

tests/issue26.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
# import sys
7+
# sys.path.insert(0,'/home/dlr16/Applications/anaconda2/envs/PyDenseCRF/lib/python2.7/site-packages')
8+
9+
10+
# In[2]:
11+
12+
import numpy as np
13+
import matplotlib.pyplot as plt
14+
# get_ipython().magic(u'matplotlib inline')
15+
plt.rcParams['figure.figsize'] = (20, 20)
16+
plt.rcParams['image.interpolation'] = 'nearest'
17+
plt.rcParams['image.cmap'] = 'gray'
18+
19+
import pydensecrf.densecrf as dcrf
20+
from pydensecrf.utils import unary_from_softmax, create_pairwise_bilateral, create_pairwise_gaussian
21+
22+
23+
# ## Start from scratch
24+
25+
# In[3]:
26+
27+
from scipy.stats import multivariate_normal
28+
29+
x, y = np.mgrid[0:512, 0:512]
30+
pos = np.empty(x.shape + (2,))
31+
pos[:, :, 0] = x; pos[:, :, 1] = y
32+
rv = multivariate_normal([256, 256], 128*128)
33+
34+
35+
# In[4]:
36+
37+
probs = rv.pdf(pos)
38+
probs = (probs-probs.min()) / (probs.max()-probs.min())
39+
probs = 0.2 * (probs-0.5) + 0.5
40+
probs = np.tile(probs[:,:,np.newaxis],(1,1,2))
41+
probs[:,:,1] = 1 - probs[:,:,0]
42+
# plt.plot(probs[256,:,0])
43+
44+
# transpose for graph
45+
probs = np.transpose(probs,(2,0,1))
46+
47+
48+
# In[17]:
49+
50+
# XX:IF NCHANNELS != 3, I GET ERRONEOUS OUTPUT
51+
nchannels=4
52+
53+
U = unary_from_softmax(probs) # note: num classes is first dim
54+
d = dcrf.DenseCRF2D(probs.shape[1],probs.shape[2],probs.shape[0])
55+
d.setUnaryEnergy(U)
56+
57+
Q_Unary = d.inference(10)
58+
map_soln_Unary = np.argmax(Q_Unary, axis=0).reshape((probs.shape[1],probs.shape[2]))
59+
60+
tmp_img = np.zeros((probs.shape[1],probs.shape[2],nchannels)).astype(np.uint8)
61+
tmp_img[150:362,150:362,:] = 1
62+
63+
energy = create_pairwise_bilateral(sdims=(10,10), schan=0.01, img=tmp_img, chdim=2)
64+
d.addPairwiseEnergy(energy, compat=10)
65+
66+
# This is wrong and will now raise a ValueError:
67+
#d.addPairwiseBilateral(sxy=(10,10),
68+
# srgb=0.01,
69+
# rgbim=tmp_img,
70+
# compat=10)
71+
72+
Q = d.inference(100)
73+
map_soln = np.argmax(Q, axis=0).reshape((probs.shape[1],probs.shape[2]))
74+
75+
plt.subplot(2,2,1)
76+
plt.imshow(probs[0,:,:])
77+
plt.colorbar()
78+
plt.subplot(2,2,2)
79+
plt.imshow(map_soln_Unary)
80+
plt.colorbar()
81+
plt.subplot(2,2,3)
82+
plt.imshow(tmp_img[:,:,0])
83+
plt.colorbar()
84+
plt.subplot(2,2,4)
85+
plt.imshow(map_soln)
86+
plt.colorbar()
87+
plt.show()

tests/issue26_problem.png

128 KB
Loading

tests/issue26_solution.png

126 KB
Loading

tests/test_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
3+
import numpy as np
4+
5+
import pydensecrf.utils as utils
6+
7+
8+
class TestUnary(unittest.TestCase):
9+
10+
def test_unary(self):
11+
M = 3
12+
U, P, N = 1./M, 0.8, 0.2/(M-1) # Uniform, Positive, Negative
13+
labels = np.array([
14+
[0, 1, 2, 3],
15+
[3, 2, 1, 0],
16+
])
17+
unary = -np.log(np.array([
18+
[U, P, N, N, N, N, P, U],
19+
[U, N, P, N, N, P, N, U],
20+
[U, N, N, P, P, N, N, U],
21+
]))
22+
23+
np.testing.assert_almost_equal(utils.compute_unary(labels, M, GT_PROB=P), unary)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

0 commit comments

Comments
 (0)