sustaining_gazes/matlab_runners/Action Unit Experiments/run_AU_prediction_Bosphorus...

189 lines
5.0 KiB
Plaintext

% Perform static model prediction using images
clear
addpath('./helpers');
find_Bosphorus;
out_loc = './out_bosph/';
if(~exist(out_loc, 'dir'))
mkdir(out_loc);
end
%%
executable = '"../../x64/Release/FaceLandmarkImg.exe"';
bosph_dirs = dir([Bosphorus_dir, '/BosphorusDB/BosphorusDB/bs*']);
%%
parfor f1=1:numel(bosph_dirs)
command = executable;
input_dir = [Bosphorus_dir, '/BosphorusDB/BosphorusDB/', bosph_dirs(f1).name];
command = cat(2, command, [' -fdir "' input_dir '" -ofdir "' out_loc '"']);
command = cat(2, command, ' -multi_view 1 -wild');
dos(command);
end
%%
aus_Bosph = [1, 2, 4, 5, 6, 7, 9, 10, 12, 14, 15, 17, 20, 23, 25, 26, 45];
[ labels_gt, valid_ids, filenames] = extract_Bosphorus_labels(Bosphorus_dir, all_recs, aus_Bosph);
%% Read the predicted values
% First read the first file to get the ids and line numbers
% au occurences
fid = fopen([out_loc, filenames{1}, '_det_0.pts']);
data = fgetl(fid);
ind = 0;
beg_ind = -1;
end_ind = -1;
aus_det = [];
aus_det_id = [];
while ischar(data)
if(~isempty(findstr(data, 'au occurences:')))
num_occurences = str2num(data(numel('au occurences:')+1:end));
% Skip ahead two lines
data = fgetl(fid);
data = fgetl(fid);
ind = ind + 2;
beg_ind = ind;
end
if(beg_ind ~= -1 && end_ind == -1)
if(~isempty(findstr(data, '}')))
end_ind = ind;
else
d = strsplit(data, ' ');
aus_det = cat(1, aus_det, str2num(d{1}(3:end)));
aus_det_id = cat(1, aus_det_id, ind - beg_ind + 1);
end
end
data = fgetl(fid);
ind = ind + 1;
end
fclose(fid);
%%
labels_pred = zeros(size(labels_gt));
for i=1:numel(filenames)
% Will need to read the relevant AUs only
if(exist([out_loc, filenames{i}, '_det_0.pts'], 'file'))
fid = fopen([out_loc, filenames{i}, '_det_0.pts']);
for k=1:beg_ind
data = fgetl(fid);
end
for k=1:num_occurences
data = fgetl(fid);
if(sum(aus_Bosph == aus_det(k))>0)
d = strsplit(data, ' ');
labels_pred(i, aus_Bosph == aus_det(k)) = str2num(d{2});
end
end
fclose(fid);
end
end
%%
%%
f = fopen('Bosphorus_res_class.txt', 'w');
for au = 1:numel(aus_Bosph)
if(inds_au_int(au) ~= 0)
tp = sum(labels_gt(:,au) == 1 & preds_all_int(:, inds_au_int(au)) >= 1);
fp = sum(labels_gt(:,au) == 0 & preds_all_int(:, inds_au_int(au)) >= 1);
fn = sum(labels_gt(:,au) == 1 & preds_all_int(:, inds_au_int(au)) < 1);
tn = sum(labels_gt(:,au) == 0 & preds_all_int(:, inds_au_int(au)) < 1);
precision = tp./(tp+fp);
recall = tp./(tp+fn);
f1 = 2 * precision .* recall ./ (precision + recall);
fprintf(f, 'AU%d intensity, Precision - %.3f, Recall - %.3f, F1 - %.3f\n', aus_BP4D(au), precision, recall, f1);
end
if(inds_au_class(au) ~= 0)
tp = sum(labels_gt(:,au) == 1 & preds_all_class(:, inds_au_class(au)) == 1);
fp = sum(labels_gt(:,au) == 0 & preds_all_class(:, inds_au_class(au)) == 1);
fn = sum(labels_gt(:,au) == 1 & preds_all_class(:, inds_au_class(au)) == 0);
tn = sum(labels_gt(:,au) == 0 & preds_all_class(:, inds_au_class(au)) == 0);
precision = tp./(tp+fp);
recall = tp./(tp+fn);
f1 = 2 * precision .* recall ./ (precision + recall);
fprintf(f, 'AU%d class, Precision - %.3f, Recall - %.3f, F1 - %.3f\n', aus_BP4D(au), precision, recall, f1);
end
end
fclose(f);
%%
addpath('./helpers/');
find_BP4D;
aus_BP4D = [6, 10, 12, 14, 17];
[ labels_gt, valid_ids, vid_ids, filenames] = extract_BP4D_labels_intensity(BP4D_dir_int, devel_recs, aus_BP4D);
labels_gt = cat(1, labels_gt{:});
%% Identifying which column IDs correspond to which AU
tab = readtable([out_loc, bosph_dirs{1}, '_T1.au.txt']);
column_names = tab.Properties.VariableNames;
% As there are both classes and intensities list and evaluate both of them
aus_pred_int = [];
inds_int_in_file = [];
for c=1:numel(column_names)
if(strfind(column_names{c}, '_r') > 0)
aus_pred_int = cat(1, aus_pred_int, int32(str2num(column_names{c}(3:end-2))));
inds_int_in_file = cat(1, inds_int_in_file, c);
end
end
%%
inds_au_int = zeros(size(aus_BP4D));
for ind=1:numel(aus_BP4D)
if(~isempty(find(aus_pred_int==aus_BP4D(ind), 1)))
inds_au_int(ind) = find(aus_pred_int==aus_BP4D(ind));
end
end
preds_all_int = [];
for i=1:numel(filenames)
fname = [out_loc, filenames{i}, '.au.txt'];
preds = dlmread(fname, ',', 1, 0);
% Read all of the intensity AUs
preds_int = preds(:, inds_int_in_file);
preds_all_int = cat(1, preds_all_int, preds_int);
end
%%
f = fopen('BP4D_valid_res_int.txt', 'w');
for au = 1:numel(aus_BP4D)
[ accuracies, F1s, corrs, ccc, rms, classes ] = evaluate_au_prediction_results( preds_all_int(:, inds_au_int(au)), labels_gt(:,au));
fprintf(f, 'AU%d results - corr %.3f, ccc - %.3f\n', aus_BP4D(au), corrs, ccc);
end
fclose(f);