Skip to content

Commit 1a8f343

Browse files
authored
Add files via upload
1 parent 911c219 commit 1a8f343

10 files changed

+535
-0
lines changed

eval/GCE.m

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function [GCE_score] = GCE( assig1, assig2 )
2+
3+
k1 = max( assig1 ); % number clusters
4+
k2 = max( assig2 );
5+
n = length( assig1 ); % nuber data points
6+
7+
confusion = zeros( k1, k2 ); % build confusion matrix
8+
9+
for i1 = 1:k1;
10+
for i2 = 1:k2;
11+
confusion( i1, i2 ) = length( find( (assig1 == i1) & (assig2 == i2 )));
12+
end;
13+
end;
14+
15+
GCE_score1 = 0; GCE_score2 = 0;
16+
for i1 = 1:k1;
17+
for i2 = 1:k2;
18+
idx1 = find(assig1== i1);
19+
idx2 = find(assig2== i2);
20+
21+
diff_idx1 = setdiff(idx1,idx2);
22+
diff_idx2 = setdiff(idx2,idx1);
23+
24+
GCE_score1 = GCE_score1 + size(diff_idx1,1)*confusion( i1, i2 )/size(idx1,1);
25+
GCE_score2 = GCE_score2 + size(diff_idx2,1)*confusion( i1, i2 )/size(idx2,1);
26+
clear idx1 idx2
27+
end;
28+
end;
29+
GCE_score = min(GCE_score1,GCE_score2)/n;

eval/Rand_index.m

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
function ri=Rand_index(sampleLabels1,sampleLabels2)
2+
3+
% compare_segmentations
4+
%
5+
% Computes several simple segmentation benchmarks. Written for use with
6+
% images, but works for generic segmentation as well (i.e. if the
7+
% sampleLabels inputs are just lists of labels, rather than rectangular
8+
% arrays).
9+
%
10+
% The measures:
11+
% Rand Index
12+
%
13+
% The Rand Index can be easily extended to the Probabilistic Rand Index
14+
% by averaging the result across all human segmentations of a given
15+
% image:
16+
% PRI = 1/K sum_1^K RI( seg, humanSeg_K ).
17+
% With a little more work, this can also be extended to the Normalized
18+
% PRI.
19+
%
20+
% Inputs:
21+
% sampleLabels1 - n x m array whose entries are integers between 1
22+
% and K1
23+
% sampleLabels2 - n x m (sample size as sampleLabels1) array whose
24+
% entries are integers between 1 and K2 (not
25+
% necessarily the same as K1).
26+
% Outputs:
27+
% ri - Rand Index
28+
% gce - Global Consistency Error
29+
% vi - Variation of Information
30+
%
31+
% NOTE:
32+
% There are a few formulas here that look less-straightforward (e.g.
33+
% the log2_quotient function). These exist to handle corner cases
34+
% where some of the groups are empty, and quantities like 0 *
35+
% log(0/0) arise...
36+
%
37+
% Oct. 2006
38+
% Questions? John Wright - [email protected]
39+
40+
[imWidth,imHeight]=size(sampleLabels1);
41+
[imWidth2,imHeight2]=size(sampleLabels2);
42+
N=imWidth*imHeight;
43+
if (imWidth~=imWidth2)||(imHeight~=imHeight2)
44+
disp( 'Input sizes: ' );
45+
disp( size(sampleLabels1) );
46+
disp( size(sampleLabels2) );
47+
error('Input sizes do not match in compare_segmentations.m');
48+
end;
49+
50+
% make the group indices start at 1
51+
if min(min(sampleLabels1)) < 1
52+
sampleLabels1 = sampleLabels1 - min(min(sampleLabels1)) + 1;
53+
end
54+
if min(min(sampleLabels2)) < 1
55+
sampleLabels2 = sampleLabels2 - min(min(sampleLabels2)) + 1;
56+
end
57+
58+
segmentcount1=max(max(sampleLabels1));
59+
segmentcount2=max(max(sampleLabels2));
60+
61+
% compute the count matrix
62+
% from this we can quickly compute rand index, GCE, VOI, ect...
63+
n=zeros(segmentcount1,segmentcount2);
64+
65+
for i=1:imWidth
66+
for j=1:imHeight
67+
u=sampleLabels1(i,j);
68+
v=sampleLabels2(i,j);
69+
n(u,v)=n(u,v)+1;
70+
end;
71+
end;
72+
73+
ri = rand_index(n);
74+
75+
return;
76+
77+
78+
% the performance measures
79+
80+
% the rand index, in [0,1] ... higher => better
81+
% fast computation is based on equation (2.2) of Rand's paper.
82+
function ri = rand_index(n)
83+
N = sum(sum(n));
84+
n_u=sum(n,2);
85+
n_v=sum(n,1);
86+
N_choose_2=N*(N-1)/2;
87+
ri = 1 - ( sum(n_u .* n_u)/2 + sum(n_v .* n_v)/2 - sum(sum(n.*n)) )/N_choose_2;
88+
89+
% log2_quotient
90+
% helper function for computing the mutual information
91+
% returns a matrix whose ij entry is
92+
% log2( a_ij / b_ij ) if a_ij, b_ij > 0
93+
% 0 if a_ij is 0
94+
% log2( a_ij + 1 ) if a_ij > 0 but b_ij = 0 (this behavior should
95+
% not be encountered in practice!
96+
function lq = log2_quotient( A, B )
97+
lq = log2( (A + ((A==0).*B) + (B==0)) ./ (B + (B==0)) );

eval/VI.m

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function [ distance, H1given2, H2given1, I12 ] = VI( assig1, assig2 )
2+
3+
%function distance = VI( assig1, assig2 )
4+
%
5+
% Computes the Variation of Information distance between two clusterings
6+
% given by assig1,2. The values in assig1 must range between 1 and k1,
7+
% the number of clusters in assig1. Similarly for assig2.
8+
%
9+
%
10+
%assig1, assig2(1,n) = vectors of integers from 1:k that represent the
11+
% assignment to clusters. must have the same length
12+
%
13+
14+
k1 = max( assig1 ); % number clusters
15+
k2 = max( assig2 );
16+
n = length( assig1 ); % nuber data points
17+
18+
confusion = zeros( k1, k2 ); % build confusion matrix
19+
20+
for i1 = 1:k1;
21+
for i2 = 1:k2;
22+
confusion( i1, i2 ) = length( find( (assig1 == i1) & (assig2 == i2 )));
23+
end;
24+
end;
25+
26+
confusion = confusion/n;
27+
p1 = sum( confusion, 1 );
28+
p2 = sum( confusion, 2 );
29+
30+
idummy = find( confusion > 0 );
31+
pdummy = p2*p1;
32+
cdummy = confusion;
33+
cdummy( idummy ) = confusion( idummy )./pdummy( idummy );
34+
I12 = sum( sum( confusion.* locallog( cdummy )));
35+
H1given2 = -sum( p1.*locallog( p1 ))-I12;
36+
H2given1 = -sum( p2.*locallog( p2 ))-I12;
37+
distance = H1given2 + H2given1;
38+
39+
%distance = -sum( p1.*locallog( p1 ))-sum( p2.*locallog( p2 ))-2*sum( sum( confusion.* locallog( cdummy )));
40+
distance = distance/log(2);
41+
I12 = I12/log(2);
42+
H1given2 = H1given2/log(2);
43+
H2given1 = H2given1/log(2);
44+
45+
46+

eval/compare_image_boundary_error.m

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
%A MATLAB Toolbox
2+
%
3+
%Compare two segmentation results using the Boundary Displacement Error
4+
%
5+
%IMPORTANT: The input two images must have the same size!
6+
%
7+
%Authors: John Wright, and Allen Y. Yang
8+
%Contact: Allen Y. Yang <[email protected]>
9+
%
10+
%(c) Copyright. University of California, Berkeley. 2007.
11+
%
12+
%Notice: The packages should NOT be used for any commercial purposes
13+
%without direct consent of their author(s). The authors are not responsible
14+
%for any potential property loss or damage caused directly or indirectly by the usage of the software.
15+
16+
function [averageError, returnStatus] = compare_image_boundary_error(imageLabels1, imageLabels2);
17+
18+
returnStatus = 0;
19+
20+
[imageX, imageY] = size(imageLabels1);
21+
if imageX~=size(imageLabels2,1) | imageY~=size(imageLabels2,2)
22+
error('The sizes of the two comparing images must be the same.');
23+
end
24+
25+
if isempty(find(imageLabels1~=imageLabels1(1)))
26+
% imageLabels1 only has one group
27+
boundary1 = zeros(size(imageLabels1));
28+
boundary1(1,:) = 1;
29+
boundary1(:,1) = 1;
30+
boundary1(end,:) = 1;
31+
boundary1(:,end) = 1;
32+
else
33+
% Generate boundary maps
34+
[cx,cy] = gradient(imageLabels1);
35+
[boundaryPixelX{1},boundaryPixelY{1}] = find((abs(cx)+abs(cy))~=0);
36+
37+
boundary1 = abs(cx) + abs(cy) > 0;
38+
end
39+
40+
if isempty(find(imageLabels2~=imageLabels2(1)))
41+
% imageLabels2 only has one group
42+
boundary2 = zeros(size(imageLabels2));
43+
boundary2(1,:) = 1;
44+
boundary2(:,1) = 1;
45+
boundary2(end,:) = 1;
46+
boundary2(:,end) = 1;
47+
else
48+
% Generate boundary maps
49+
[cx,cy] = gradient(imageLabels2);
50+
[boundaryPixelX{2},boundaryPixelY{2}] = find((abs(cx)+abs(cy))~=0);
51+
52+
boundary2 = abs(cx) + abs(cy) > 0;
53+
end
54+
55+
% boundary1 and boundary2 are now binary boundary masks. compute their
56+
% distance transforms:
57+
D1 = bwdist(boundary1);
58+
D2 = bwdist(boundary2);
59+
60+
% compute the distance of the pixels in boundary1 to the nearest pixel in
61+
% boundary2:
62+
dist_12 = sum(sum(boundary1 .* D2 ));
63+
dist_21 = sum(sum(boundary2 .* D1 ));
64+
65+
avgError_12 = dist_12 / sum(sum(boundary1));
66+
avgError_21 = dist_21 / sum(sum(boundary2));
67+
68+
averageError = (avgError_12 + avgError_21) / 2;

eval/compare_segmentations.m

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
%A MATLAB Toolbox
2+
%
3+
%Compare two segmentation results using
4+
%1. Probabilistic Rand Index
5+
%2. Variation of Information
6+
%3. Global Consistency Error
7+
%
8+
%IMPORTANT: The two input images must have the same size!
9+
%
10+
%Authors: John Wright, and Allen Y. Yang
11+
%Contact: Allen Y. Yang <[email protected]>
12+
%
13+
%(c) Copyright. University of California, Berkeley. 2007.
14+
%
15+
%Notice: The packages should NOT be used for any commercial purposes
16+
%without direct consent of their author(s). The authors are not responsible
17+
%for any potential property loss or damage caused directly or indirectly by the usage of the software.
18+
19+
function [ri,gce,vi]=compare_segmentations(sampleLabels1,sampleLabels2)
20+
21+
% compare_segmentations
22+
%
23+
% Computes several simple segmentation benchmarks. Written for use with
24+
% images, but works for generic segmentation as well (i.e. if the
25+
% sampleLabels inputs are just lists of labels, rather than rectangular
26+
% arrays).
27+
%
28+
% The measures:
29+
% Rand Index
30+
% Global Consistency Error
31+
% Variation of Information
32+
%
33+
% The Rand Index can be easily extended to the Probabilistic Rand Index
34+
% by averaging the result across all human segmentations of a given
35+
% image:
36+
% PRI = 1/K sum_1^K RI( seg, humanSeg_K ).
37+
% With a little more work, this can also be extended to the Normalized
38+
% PRI.
39+
%
40+
% Inputs:
41+
% sampleLabels1 - n x m array whose entries are integers between 1
42+
% and K1
43+
% sampleLabels2 - n x m (sample size as sampleLabels1) array whose
44+
% entries are integers between 1 and K2 (not
45+
% necessarily the same as K1).
46+
% Outputs:
47+
% ri - Rand Index
48+
% gce - Global Consistency Error
49+
% vi - Variation of Information
50+
%
51+
% NOTE:
52+
% There are a few formulas here that look less-straightforward (e.g.
53+
% the log2_quotient function). These exist to handle corner cases
54+
% where some of the groups are empty, and quantities like 0 *
55+
% log(0/0) arise...
56+
%
57+
% Oct. 2006
58+
% Questions? John Wright - [email protected]
59+
60+
[imWidth,imHeight]=size(sampleLabels1);
61+
[imWidth2,imHeight2]=size(sampleLabels2);
62+
N=imWidth*imHeight;
63+
if (imWidth~=imWidth2)||(imHeight~=imHeight2)
64+
disp( 'Input sizes: ' );
65+
disp( size(sampleLabels1) );
66+
disp( size(sampleLabels2) );
67+
error('Input sizes do not match in compare_segmentations.m');
68+
end;
69+
70+
% make the group indices start at 1
71+
if min(min(sampleLabels1)) < 1
72+
sampleLabels1 = sampleLabels1 - min(min(sampleLabels1)) + 1;
73+
end
74+
if min(min(sampleLabels2)) < 1
75+
sampleLabels2 = sampleLabels2 - min(min(sampleLabels2)) + 1;
76+
end
77+
78+
segmentcount1=max(max(sampleLabels1));
79+
segmentcount2=max(max(sampleLabels2));
80+
81+
% compute the count matrix
82+
% from this we can quickly compute rand index, GCE, VOI, ect...
83+
n=zeros(segmentcount1,segmentcount2);
84+
85+
for i=1:imWidth
86+
for j=1:imHeight
87+
u=sampleLabels1(i,j);
88+
v=sampleLabels2(i,j);
89+
n(u,v)=n(u,v)+1;
90+
end;
91+
end;
92+
93+
ri = rand_index(n);
94+
gce = global_consistancy_error(n);
95+
vi = variation_of_information(n);
96+
97+
return;
98+
99+
100+
% the performance measures
101+
102+
% the rand index, in [0,1] ... higher => better
103+
% fast computation is based on equation (2.2) of Rand's paper.
104+
function ri = rand_index(n)
105+
N = sum(sum(n));
106+
n_u=sum(n,2);
107+
n_v=sum(n,1);
108+
N_choose_2=N*(N-1)/2;
109+
ri = 1 - ( sum(n_u .* n_u)/2 + sum(n_v .* n_v)/2 - sum(sum(n.*n)) )/N_choose_2;
110+
111+
% global consistancy error (from BSDS ICCV 01 paper) ... lower => better
112+
function gce = global_consistancy_error(n)
113+
N = sum(sum(n));
114+
marginal_1 = sum(n,2);
115+
marginal_2 = sum(n,1);
116+
% the hackery is to protect against cases where some of the marginals are
117+
% zero (should never happen, but seems to...)
118+
E1 = 1 - sum( sum(n.*n,2) ./ (marginal_1 + (marginal_1 == 0)) ) / N;
119+
E2 = 1 - sum( sum(n.*n,1) ./ (marginal_2 + (marginal_2 == 0)) ) / N;
120+
gce = min( E1, E2 );
121+
122+
123+
% variation of information a "distance", in (0,vi_max] ... lower => better
124+
function vi = variation_of_information(n)
125+
N = sum(sum(n));
126+
joint = n / N; % the joint pmf of the two labels
127+
marginal_2 = sum(joint,1); % row vector
128+
marginal_1 = sum(joint,2); % column vector
129+
H1 = - sum( marginal_1 .* log2(marginal_1 + (marginal_1 == 0) ) ); % entropy of the first label
130+
H2 = - sum( marginal_2 .* log2(marginal_2 + (marginal_2 == 0) ) ); % entropy of the second label
131+
MI = sum(sum( joint .* log2_quotient( joint, marginal_1*marginal_2 ) )); % mutual information
132+
vi = H1 + H2 - 2 * MI;
133+
134+
% log2_quotient
135+
% helper function for computing the mutual information
136+
% returns a matrix whose ij entry is
137+
% log2( a_ij / b_ij ) if a_ij, b_ij > 0
138+
% 0 if a_ij is 0
139+
% log2( a_ij + 1 ) if a_ij > 0 but b_ij = 0 (this behavior should
140+
% not be encountered in practice!
141+
function lq = log2_quotient( A, B )
142+
lq = log2( (A + ((A==0).*B) + (B==0)) ./ (B + (B==0)) );

0 commit comments

Comments
 (0)