Skip to content

Commit b7bc292

Browse files
committed
Fix unary in utils_example.py and in general improve it.
1 parent 6b73766 commit b7bc292

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

examples/out1.png

616 Bytes
Loading

examples/out2.png

544 Bytes
Loading

examples/out3.png

260 Bytes
Loading

examples/utils_example.py

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
import sys
66
import numpy as np
7-
import cv2
87
import pydensecrf.densecrf as dcrf
9-
from skimage.segmentation import relabel_sequential
108

11-
from pydensecrf.utils import compute_unary, create_pairwise_bilateral, \
12-
create_pairwise_gaussian
9+
# Get im{read,write} from somewhere: cv2, skimage, or scipy.
10+
try:
11+
from cv2 import imread, imwrite
12+
except ImportError:
13+
try:
14+
from skimage.io import imread, imsave
15+
imwrite = imsave
16+
except ImportError:
17+
from scipy.misc import imread, imsave
18+
imwrite = imsave
19+
20+
from pydensecrf.utils import compute_unary, create_pairwise_bilateral, create_pairwise_gaussian
1321

1422
if len(sys.argv) != 4:
1523
print("Usage: python {} IMAGE ANNO OUTPUT".format(sys.argv[0]))
@@ -24,22 +32,43 @@
2432
##################################
2533
### Read images and annotation ###
2634
##################################
27-
img = cv2.imread(fn_im)
28-
labels, _, _ = relabel_sequential(cv2.imread(fn_anno, 0))
29-
30-
# Compute the number of classes in the label image
31-
M = len(set(labels.flat))
35+
img = imread(fn_im)
36+
37+
# Convert the annotation's RGB color to a single 32-bit integer color 0xBBGGRR
38+
anno_rgb = imread(fn_anno).astype(np.uint32)
39+
anno_lbl = anno_rgb[:,:,0] + (anno_rgb[:,:,1] << 8) + (anno_rgb[:,:,2] << 16)
40+
41+
# Convert the 32bit integer color to 1, 2, ... labels.
42+
# Note that all-black, i.e. the value 0 for background will stay 0.
43+
colors, labels = np.unique(anno_lbl, return_inverse=True)
44+
45+
# And create a mapping back from the labels to 32bit integer colors.
46+
# But remove the all-0 black, that won't exist in the MAP!
47+
colors = colors[1:]
48+
colorize = np.empty((len(colors), 3), np.uint8)
49+
colorize[:,0] = (colors & 0x0000FF)
50+
colorize[:,1] = (colors & 0x00FF00) >> 8
51+
colorize[:,2] = (colors & 0xFF0000) >> 16
52+
53+
# Compute the number of classes in the label image.
54+
# We subtract one because the number shouldn't include the value 0 which stands
55+
# for "unknown" or "unsure".
56+
M = len(set(labels.flat)) - 1
57+
print(M, " labels and \"unknown\" 0: ", set(labels.flat))
3258

3359
###########################
3460
### Setup the CRF model ###
3561
###########################
3662
use_2d = False
63+
# use_2d = True
3764
if use_2d:
65+
print("Using 2D specialized functions")
66+
3867
# Example using the DenseCRF2D code
3968
d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M)
4069

4170
# get unary potentials (neg log probability)
42-
U = compute_unary(labels, M)
71+
U = compute_unary(labels, M, GT_PROB=0.7)
4372
d.setUnaryEnergy(U)
4473

4574
# This adds the color-independent term, features are the locations only.
@@ -52,11 +81,13 @@
5281
kernel=dcrf.DIAG_KERNEL,
5382
normalization=dcrf.NORMALIZE_SYMMETRIC)
5483
else:
84+
print("Using generic 2D functions")
85+
5586
# Example using the DenseCRF class and the util functions
56-
d = dcrf.DenseCRF(img.shape[0] * img.shape[1], M)
87+
d = dcrf.DenseCRF(img.shape[1] * img.shape[0], M)
5788

5889
# get unary potentials (neg log probability)
59-
U = compute_unary(labels, M)
90+
U = compute_unary(labels, M, GT_PROB=0.7)
6091
d.setUnaryEnergy(U)
6192

6293
# This creates the color-independent features and then add them to the CRF
@@ -74,15 +105,20 @@
74105

75106

76107
####################################
77-
### Do inference and compute map ###
108+
### Do inference and compute MAP ###
78109
####################################
110+
111+
# Run five inference steps.
79112
Q = d.inference(5)
80-
MAP = np.argmax(Q, axis=0).astype('float32')
81-
MAP *= 255 / MAP.max()
82-
MAP = MAP.reshape(img.shape[:2])
83-
cv2.imwrite(fn_output, MAP.astype('uint8'))
84113

85-
# Manually inference
114+
# Find out the most probable class for each pixel.
115+
MAP = np.argmax(Q, axis=0)
116+
117+
# Convert the MAP (labels) back to the corresponding colors and save the image.
118+
MAP = colorize[MAP,:]
119+
imsave(fn_output, MAP.reshape(img.shape))
120+
121+
# Just randomly manually run inference iterations
86122
Q, tmp1, tmp2 = d.startInference()
87123
for i in range(5):
88124
print("KL-divergence at {}: {}".format(i, d.klDivergence(Q)))

0 commit comments

Comments
 (0)