Skip to content

Commit 51d31c2

Browse files
committed
Fix the inference example when no black in anno.
Now it explicitly checks for there to be an all-0 pixel meaning "unknown" and prints an info about it if it finds one. This is a try at fixing lucasb-eyer#30 and lucasb-eyer#31.
1 parent 903c331 commit 51d31c2

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

examples/anno1-b.png

936 Bytes
Loading

examples/inference.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
print("Usage: python {} IMAGE ANNO OUTPUT".format(sys.argv[0]))
2323
print("")
2424
print("IMAGE and ANNO are inputs and OUTPUT is where the result should be written.")
25+
print("If there's at least one single full-black pixel in ANNO, black is assumed to mean unknown.")
2526
sys.exit(1)
2627

2728
fn_im = sys.argv[1]
@@ -41,9 +42,15 @@
4142
# Note that all-black, i.e. the value 0 for background will stay 0.
4243
colors, labels = np.unique(anno_lbl, return_inverse=True)
4344

44-
# And create a mapping back from the labels to 32bit integer colors.
4545
# But remove the all-0 black, that won't exist in the MAP!
46-
colors = colors[1:]
46+
HAS_UNK = 0 in colors
47+
if HAS_UNK:
48+
print("Found a full-black pixel in annotation image, assuming it means 'unknown' label!")
49+
colors = colors[1:]
50+
#else:
51+
# print("No single full-black pixel found in annotation image. Assuming there's no 'unknown' label!")
52+
53+
# And create a mapping back from the labels to 32bit integer colors.
4754
colorize = np.empty((len(colors), 3), np.uint8)
4855
colorize[:,0] = (colors & 0x0000FF)
4956
colorize[:,1] = (colors & 0x00FF00) >> 8
@@ -52,8 +59,8 @@
5259
# Compute the number of classes in the label image.
5360
# We subtract one because the number shouldn't include the value 0 which stands
5461
# for "unknown" or "unsure".
55-
n_labels = len(set(labels.flat)) - 1
56-
print(n_labels, " labels and \"unknown\" 0: ", set(labels.flat))
62+
n_labels = len(set(labels.flat)) - int(HAS_UNK)
63+
print(n_labels, " labels", (" plus \"unknown\" 0: " if HAS_UNK else ""), set(labels.flat))
5764

5865
###########################
5966
### Setup the CRF model ###
@@ -67,7 +74,7 @@
6774
d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], n_labels)
6875

6976
# get unary potentials (neg log probability)
70-
U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=True)
77+
U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=HAS_UNK)
7178
d.setUnaryEnergy(U)
7279

7380
# This adds the color-independent term, features are the locations only.
@@ -86,7 +93,7 @@
8693
d = dcrf.DenseCRF(img.shape[1] * img.shape[0], n_labels)
8794

8895
# get unary potentials (neg log probability)
89-
U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=True)
96+
U = unary_from_labels(labels, n_labels, gt_prob=0.7, zero_unsure=HAS_UNK)
9097
d.setUnaryEnergy(U)
9198

9299
# This creates the color-independent features and then add them to the CRF
@@ -114,8 +121,9 @@
114121
MAP = np.argmax(Q, axis=0)
115122

116123
# Convert the MAP (labels) back to the corresponding colors and save the image.
124+
# Note that there is no "unknown" here anymore, no matter what we had at first.
117125
MAP = colorize[MAP,:]
118-
imsave(fn_output, MAP.reshape(img.shape))
126+
imwrite(fn_output, MAP.reshape(img.shape))
119127

120128
# Just randomly manually run inference iterations
121129
Q, tmp1, tmp2 = d.startInference()

examples/out1-b.png

3.16 KB
Loading

0 commit comments

Comments
 (0)