Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
pydensecrf
Commits
e4dc94d5
Commit
e4dc94d5
authored
Oct 13, 2017
by
Marvin Teichmann
Browse files
Write additional test cases for dcrf.
parent
35c7c4d7
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
16 deletions
+64
-16
pydensecrf/tests/test_dcrf.py
pydensecrf/tests/test_dcrf.py
+64
-16
No files found.
pydensecrf/tests/test_dcrf.py
View file @
e4dc94d5
import
numpy
as
np
import
numpy
as
np
import
pydensecrf.densecrf
as
dcrf
import
pydensecrf.densecrf
as
dcrf
import
pydensecrf.utils
as
utils
import
pytest
import
pytest
def
test_call_dcrf
():
def
_get_simple_unary
():
# The following is now a real unittest.
unary1
=
np
.
zeros
((
10
,
10
),
dtype
=
np
.
float32
)
# It checks whether dcrf does not crash upon call.
unary1
[:,
[
0
,
-
1
]]
=
unary1
[[
0
,
-
1
],
:]
=
1
###########################
# d = densecrf.PyDenseCRF2D(3, 2, 3)
unary2
=
np
.
zeros
((
10
,
10
),
dtype
=
np
.
float32
)
# U = np.full((3,6), 0.1, dtype=np.float32)
unary2
[
4
:
7
,
4
:
7
]
=
1
# U[0,0] = U[1,1] = U[2,2] = U[0,3] = U[1,4] = U[2,5] = 0.8
d
=
dcrf
.
DenseCRF2D
(
10
,
10
,
2
)
unary
=
np
.
vstack
([
unary1
.
flat
,
unary2
.
flat
])
unary
=
(
unary
+
1
)
/
(
np
.
sum
(
unary
,
axis
=
0
)
+
2
)
U1
=
np
.
zeros
((
10
,
10
),
dtype
=
np
.
float32
)
return
unary
U1
[:,
[
0
,
-
1
]]
=
U1
[[
0
,
-
1
],
:]
=
1
U2
=
np
.
zeros
((
10
,
10
),
dtype
=
np
.
float32
)
U2
[
4
:
7
,
4
:
7
]
=
1
U
=
np
.
vstack
([
U1
.
flat
,
U2
.
flat
])
def
_get_simple_img
():
Up
=
(
U
+
1
)
/
(
np
.
sum
(
U
,
axis
=
0
)
+
2
)
img
=
np
.
zeros
((
10
,
10
,
3
),
dtype
=
np
.
uint8
)
img
=
np
.
zeros
((
10
,
10
,
3
),
dtype
=
np
.
uint8
)
img
[
2
:
8
,
2
:
8
,
:]
=
255
img
[
2
:
8
,
2
:
8
,
:]
=
255
d
.
setUnaryEnergy
(
-
np
.
log
(
Up
))
return
img
def
test_call_dcrf2d
():
d
=
dcrf
.
DenseCRF2D
(
10
,
10
,
2
)
unary
=
_get_simple_unary
()
img
=
_get_simple_img
()
d
.
setUnaryEnergy
(
-
np
.
log
(
unary
))
# d.setUnaryEnergy(PyConstUnary(-np.log(Up)))
# d.setUnaryEnergy(PyConstUnary(-np.log(Up)))
d
.
addPairwiseBilateral
(
2
,
2
,
img
,
3
)
d
.
addPairwiseBilateral
(
sxy
=
2
,
srgb
=
2
,
rgbim
=
img
,
compat
=
3
)
# d.addPairwiseBilateral(2, 2, img, 3)
# d.addPairwiseBilateral(2, 2, img, 3)
np
.
argmax
(
d
.
inference
(
10
),
axis
=
0
).
reshape
(
10
,
10
)
np
.
argmax
(
d
.
inference
(
10
),
axis
=
0
).
reshape
(
10
,
10
)
def
test_call_dcrf
():
d
=
dcrf
.
DenseCRF
(
100
,
2
)
unary
=
_get_simple_unary
()
img
=
_get_simple_img
()
d
.
setUnaryEnergy
(
-
np
.
log
(
unary
))
# d.setUnaryEnergy(PyConstUnary(-np.log(Up)))
feats
=
utils
.
create_pairwise_bilateral
(
sdims
=
(
2
,
2
),
schan
=
2
,
img
=
img
,
chdim
=
2
)
d
.
addPairwiseEnergy
(
feats
,
compat
=
3
)
# d.addPairwiseBilateral(2, 2, img, 3)
np
.
argmax
(
d
.
inference
(
10
),
axis
=
0
).
reshape
(
10
,
10
)
def
test_call_dcrf_eq_dcrf2d
():
d
=
dcrf
.
DenseCRF
(
100
,
2
)
d2
=
dcrf
.
DenseCRF2D
(
10
,
10
,
2
)
unary
=
_get_simple_unary
()
img
=
_get_simple_img
()
d
.
setUnaryEnergy
(
-
np
.
log
(
unary
))
d2
.
setUnaryEnergy
(
-
np
.
log
(
unary
))
feats
=
utils
.
create_pairwise_bilateral
(
sdims
=
(
2
,
2
),
schan
=
2
,
img
=
img
,
chdim
=
2
)
d
.
addPairwiseEnergy
(
feats
,
compat
=
3
)
d2
.
addPairwiseBilateral
(
sxy
=
2
,
srgb
=
2
,
rgbim
=
img
,
compat
=
3
)
# d.addPairwiseBilateral(2, 2, img, 3)
res1
=
np
.
argmax
(
d
.
inference
(
10
),
axis
=
0
).
reshape
(
10
,
10
)
res2
=
np
.
argmax
(
d2
.
inference
(
10
),
axis
=
0
).
reshape
(
10
,
10
)
assert
(
np
.
all
(
res1
==
res2
))
def
test_compact_wrong
():
def
test_compact_wrong
():
# Tests whether expection is indeed raised
# Tests whether expection is indeed raised
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment