Commit c15c0e55 authored by lucasb-eyer's avatar lucasb-eyer
Browse files

Tests I found that I never commited. Issue #26

parent afda975a
# coding: utf-8
# In[1]:
# import sys
# sys.path.insert(0,'/home/dlr16/Applications/anaconda2/envs/PyDenseCRF/lib/python2.7/site-packages')
# In[2]:
import numpy as np
import matplotlib.pyplot as plt
# get_ipython().magic(u'matplotlib inline')
plt.rcParams['figure.figsize'] = (20, 20)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_softmax, create_pairwise_bilateral, create_pairwise_gaussian
# ## Start from scratch
# In[3]:
from scipy.stats import multivariate_normal
x, y = np.mgrid[0:512, 0:512]
pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x; pos[:, :, 1] = y
rv = multivariate_normal([256, 256], 128*128)
# In[4]:
probs = rv.pdf(pos)
probs = (probs-probs.min()) / (probs.max()-probs.min())
probs = 0.2 * (probs-0.5) + 0.5
probs = np.tile(probs[:,:,np.newaxis],(1,1,2))
probs[:,:,1] = 1 - probs[:,:,0]
# plt.plot(probs[256,:,0])
# transpose for graph
probs = np.transpose(probs,(2,0,1))
# In[17]:
# XX:IF NCHANNELS != 3, I GET ERRONEOUS OUTPUT
nchannels=4
U = unary_from_softmax(probs) # note: num classes is first dim
d = dcrf.DenseCRF2D(probs.shape[1],probs.shape[2],probs.shape[0])
d.setUnaryEnergy(U)
Q_Unary = d.inference(10)
map_soln_Unary = np.argmax(Q_Unary, axis=0).reshape((probs.shape[1],probs.shape[2]))
tmp_img = np.zeros((probs.shape[1],probs.shape[2],nchannels)).astype(np.uint8)
tmp_img[150:362,150:362,:] = 1
energy = create_pairwise_bilateral(sdims=(10,10), schan=0.01, img=tmp_img, chdim=2)
d.addPairwiseEnergy(energy, compat=10)
# This is wrong and will now raise a ValueError:
#d.addPairwiseBilateral(sxy=(10,10),
# srgb=0.01,
# rgbim=tmp_img,
# compat=10)
Q = d.inference(100)
map_soln = np.argmax(Q, axis=0).reshape((probs.shape[1],probs.shape[2]))
plt.subplot(2,2,1)
plt.imshow(probs[0,:,:])
plt.colorbar()
plt.subplot(2,2,2)
plt.imshow(map_soln_Unary)
plt.colorbar()
plt.subplot(2,2,3)
plt.imshow(tmp_img[:,:,0])
plt.colorbar()
plt.subplot(2,2,4)
plt.imshow(map_soln)
plt.colorbar()
plt.show()
import unittest
import numpy as np
import pydensecrf.utils as utils
class TestUnary(unittest.TestCase):
def test_unary(self):
M = 3
U, P, N = 1./M, 0.8, 0.2/(M-1) # Uniform, Positive, Negative
labels = np.array([
[0, 1, 2, 3],
[3, 2, 1, 0],
])
unary = -np.log(np.array([
[U, P, N, N, N, N, P, U],
[U, N, P, N, N, P, N, U],
[U, N, N, P, P, N, N, U],
]))
np.testing.assert_almost_equal(utils.compute_unary(labels, M, GT_PROB=P), unary)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment