-
Notifications
You must be signed in to change notification settings - Fork 15
/
eval_acc.m
74 lines (66 loc) · 2.46 KB
/
eval_acc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function eval_acc(data_name,data_root,eval_list,save_gray_folder,data_class,numClass)
addpath('evaluationCode');
addpath('visualizationCode');
load(data_class);
list = importdata(fullfile(data_root,eval_list));
pathPred = save_gray_folder;
pathAnno = data_root;
%% Evaluation
% initialize statistics
cnt=0;
area_intersection = double.empty;
area_union = double.empty;
pixel_accuracy = double.empty;
pixel_correct = double.empty;
pixel_labeled = double.empty;
% main loop
filesPred = dir(fullfile(pathPred, '*.png'));
for i = 1: numel(filesPred)
% check file existence
str = strsplit(list{i});
strPred = strsplit(str{2},'/');
strPred = strPred{end};
if(strcmp(data_name,'cityscapes'))
strPred = strrep(strPred,'gtFine_labelTrainIds','leftImg8bit');
end
filePred = fullfile(pathPred, strPred);
fileAnno = fullfile(pathAnno, str{2});
if ~exist(fileAnno, 'file')
fprintf('Label file [%s] does not exist!\n', fileAnno); continue;
end
% read in prediction and label
imPred = imread(filePred);
imAnno = imread(fileAnno);
imAnno = imAnno + 1;
if(strcmp(data_name,'VOC2012') || strcmp(data_name,'cityscapes'))
imPred = imPred + 1;
end
imAnno(imAnno==255) = 0;
imPred = imresize(imPred,[size(imAnno,1), size(imAnno,2)],'nearest');
% check image size
if size(imPred, 3) ~= 1
fprintf('Label image [%s] should be a gray-scale image!\n', fileAnno); continue;
end
if size(imPred, 1)~=size(imAnno, 1) || size(imPred, 2)~=size(imAnno, 2)
fprintf('Label image [%s] should have the same size as label image! Resizing...\n', fileLab);
imPred = imresize(imPred, size(imAnno));
end
% compute IoU
cnt = cnt + 1;
[area_intersection(:,cnt), area_union(:,cnt)] = intersectionAndUnion(imPred, imAnno, numClass);
% compute pixel-wise accuracy
[pixel_accuracy(i), pixel_correct(i), pixel_labeled(i)] = pixelAccuracy(imPred, imAnno);
fprintf('Evaluating %d/%d... Pixel-wise accuracy: %2.2f%%\n', cnt, numel(filesPred), pixel_accuracy(i)*100.);
end
%% Summary
IoU = sum(area_intersection,2)./sum(eps+area_union,2);
mean_IoU = mean(IoU);
accuracy = sum(pixel_correct)/sum(pixel_labeled);
fprintf('==== Summary IoU ====\n');
for i = 1:numClass
fprintf('%3d %16s: %.4f\n', i, objectNames{i}, IoU(i));
end
fprintf('Mean IoU over %d classes: %.4f\n', numClass, mean_IoU);
fprintf('Pixel-wise Accuracy: %2.2f%%\n', accuracy*100.);
sub_acc = accuracy*100.;
sub_iou = mean_IoU;