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
0d10e2bf
Commit
0d10e2bf
authored
Jan 10, 2016
by
Lucas Beyer
Browse files
Merge pull request #4 from kevin-keraudren/example-dense-inference
Example dense inference
parents
5c05e2db
e980866d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
+47
-0
dense_inference.py
dense_inference.py
+47
-0
No files found.
dense_inference.py
0 → 100644
View file @
0d10e2bf
#!/usr/bin/python
"""
Usage: python dense_inference.py image annotations output
Adapted from the original C++ example: densecrf/examples/dense_inference.cpp
http://www.philkr.net/home/densecrf Version 2.2
"""
import
numpy
as
np
import
cv2
import
densecrf
as
dcrf
from
skimage.segmentation
import
relabel_sequential
import
sys
img
=
cv2
.
imread
(
sys
.
argv
[
1
],
1
)
labels
=
relabel_sequential
(
cv2
.
imread
(
sys
.
argv
[
2
],
0
))[
0
].
flatten
()
output
=
sys
.
argv
[
3
]
M
=
labels
.
max
()
+
1
# number of labels
# Setup the CRF model
d
=
dcrf
.
DenseCRF2D
(
img
.
shape
[
0
],
img
.
shape
[
1
],
M
)
# Certainty that the ground truth is correct
GT_PROB
=
0.5
# Simple classifier that is 50% certain that the annotation is correct
u_energy
=
-
np
.
log
(
1.0
/
M
)
n_energy
=
-
np
.
log
((
1.0
-
GT_PROB
)
/
(
M
-
1
))
p_energy
=
-
np
.
log
(
GT_PROB
)
U
=
np
.
zeros
((
M
,
img
.
shape
[
0
]
*
img
.
shape
[
1
]),
dtype
=
'float32'
)
U
[:,
labels
>
0
]
=
n_energy
U
[
labels
,
np
.
arange
(
U
.
shape
[
1
])]
=
p_energy
U
[:,
labels
==
0
]
=
u_energy
d
.
setUnaryEnergy
(
U
)
d
.
addPairwiseGaussian
(
sxy
=
3
,
compat
=
3
)
d
.
addPairwiseBilateral
(
sxy
=
80
,
srgb
=
13
,
rgbim
=
img
,
compat
=
10
)
# Do the inference
res
=
np
.
argmax
(
d
.
inference
(
5
),
axis
=
0
).
astype
(
'float32'
)
res
*=
255
/
res
.
max
()
res
=
res
.
reshape
(
img
.
shape
[:
2
])
cv2
.
imwrite
(
output
,
res
.
astype
(
'uint8'
))
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