-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.m
More file actions
173 lines (144 loc) · 5.11 KB
/
Copy pathevaluate.m
File metadata and controls
173 lines (144 loc) · 5.11 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
lambda1_list = [1.6];
lambda2_list = [5e-2];
mu_list = [5e-2];
% names = ["sofa", "pedestrians", "PETS2006"];
names = ["office", "highway"];
for t = 1:length(names)
origin_file = names(t);
disp(origin_file);
method = "nonlinear";
groundtruth_file = origin_file+"_gt";
load(groundtruth_file+".mat");
groundtruth = gray_images;
tensor2video(groundtruth, groundtruth_file+".avi");
load(origin_file+".mat");
origin = gray_images;
tensor2video(origin, origin_file+".avi");
for i = 1:length(lambda1_list)
for j = 1:length(lambda2_list)
for k = 1:length(mu_list)
opts.lambda = lambda1_list(i);
opts.lambda2 = lambda2_list(j);
opts.mu = mu_list(k);
configName = sprintf('%s_l1%.2e_l2%.2e_mu%.2e', names(t), lambda1_list(i), lambda2_list(j), mu_list(k));
name = sprintf("%s_acceler_%s", method,names(t));
disp(configName);
load(sprintf("%s_acceler_%s_ABC.mat", method,configName));
result_ADMM_reg = block_term_product(A1, B1, C1);
disp(size(A1{1}));
disp(size(B1{1}));
disp(size(C1{1}));
tensor2video(result_ADMM_reg,sprintf("%s_acceler_%s_ABC.avi", method,names(t)));
load(sprintf("%s_acceler_%s_E.mat", method,configName));
tensor2video(E1,sprintf("%s_acceler_%s_E.avi", method,names(t)));
eval(origin, groundtruth, result_ADMM_reg, E1);
mAP(origin, groundtruth, result_ADMM_reg, E1);
tensor2video(result_ADMM_reg, origin_file+"_bg.avi");
tensor2video(E1, origin_file+"_fg.avi");
% load(sprintf("%s_acceler_%s_total.mat", method,configName));
% tensor2video(total1,sprintf("%s_acceler_%s_total.avi", method,configName));
end
end
end
end
function [] = tensor2video(X, video_name)
X_uint8 = im2uint8(mat2gray(X));
v = VideoWriter(video_name);
open(v);
numFrames = size(X_uint8, 3);
for t = 1:numFrames
frame = X_uint8(:, :, t);
writeVideo(v, frame);
end
close(v);
end
function Xhat = block_term_product(A, B, C)
[R, ~] = size(A);
[I, ~] = size(A{1});
[J, ~] = size(B{1});
[K, ~] = size(C{1});
Xhat = zeros(I, J, K);
for r = 1:R
AB = A{r} * B{r}';
for k = 1:K
Xhat(:,:,k) = Xhat(:,:,k) + C{r}(k) * AB;
end
end
end
function [] = eval(origin, groundtruth, background, foreground)
if ~isequal(size(origin), size(background), size(groundtruth))
error('Unmatched origin, background and groundtruth');
end
numFrames = size(origin, 3);
pred_mask = zeros(size(origin));
for i = 1:numFrames
frame_origin = double(origin(:,:,i));
frame_bg = double(background(:,:,i));
diff_img = abs(frame_origin-frame_bg);
% diff_img = abs(foreground(:, :, i));
level = graythresh(diff_img);
thresh_val = level * 255;
pred_mask(:,:,i) = diff_img > 50;
end
% tensor2video(pred_mask, "highway_foreground.avi");
gt_mask = (groundtruth == 255) ;
ns_mask = (groundtruth == 170);
TP_total = 0; FP_total = 0; FN_total = 0; TN_total = 0;
total_pixels = numel(gt_mask);
for i = 1:numFrames
p_mask = pred_mask(:,:,i);
g_mask = gt_mask(:,:,i);
m_mask = ns_mask(:, :, i);
TP = sum(p_mask(:) & (g_mask(:)|m_mask(:)));
FP = sum(p_mask(:) & (~g_mask(:)));
FN = sum(~p_mask(:) & (g_mask(:)));
TN = sum(~p_mask(:) & ((~g_mask(:))|m_mask(:)));
TP_total = TP_total + TP;
FP_total = FP_total + FP;
FN_total = FN_total + FN;
TN_total = TN_total + TN;
end
precision = TP_total / (TP_total + FP_total);
recall = TP_total / (TP_total + FN_total);
F1 = 2 * precision * recall / (precision + recall);
PWC = 100 * (FP_total + FN_total) / total_pixels;
fprintf("TP: %d FP: %d TN: %d FN: %d \n", TP_total, FP_total, TN_total, FN_total);
fprintf('Precision: %.4f\n', precision);
fprintf('Recall : %.4f\n', recall);
fprintf('F1 Score : %.4f\n', F1);
fprintf('PWC : %.4f%%\n', PWC);
end
function [] = mAP(origin, groundtruth, background, foreground)
numFrames = size(origin, 3);
APs = zeros(numFrames, 1);
alpha = 0.5;
cnt = 0;
for i = 1:numFrames
frame_origin = double(origin(:,:,i));
frame_bg = double(background(:,:,i));
% diff_img = abs(foreground(:, :, i));
diff_img = abs(frame_origin-frame_bg);
level = graythresh(diff_img);
T = level * 255;
pred_prob = 1 ./ (1 + exp(-alpha * (diff_img - T)));
gt_mask = double(groundtruth(:,:,i) == 255);
if numel(unique(gt_mask)) < 2
APs(i) = NaN;
continue;
end
[recall, precision, ~] = perfcurve(gt_mask(:), pred_prob(:), 1, 'xCrit', 'reca', 'yCrit', 'prec');
if mod(i, 1000) == 0
disp(recall);
disp(precision);
end
validIdx = ~isnan(precision) & ~isnan(recall);
if sum(validIdx) > 1
APs(i) = trapz(recall(validIdx), precision(validIdx));
cnt = cnt + 1;
else
APs(i) = 0; % hoặc một giá trị phù hợp nếu không đủ điểm để tính tích phân
end
end
mAP = mean(APs(~isnan(APs)));
fprintf('Mean Average Precision (mAP): %.4f\n\n', mAP);
end