open source pkg v1

This commit is contained in:
Vijay Yadev
2020-08-04 19:12:31 -04:00
parent bef213dba9
commit c389fc2c47
3708 changed files with 1624220 additions and 1 deletions

View File

@@ -0,0 +1,117 @@
function [data_train, labels_train, data_devel, labels_devel, raw_devel, PC, means_norm, stds_norm, devel_ids, devel_success] = ...
Prepare_HOG_AU_data(train_users, devel_users, au_train, rest_aus, UNBC_dir, features_dir)
%%
addpath(genpath('../data extraction/'));
% First extracting the labels
[ labels_train, valid_ids_train, filenames ] = extract_UNBC_labels(UNBC_dir, train_users, au_train);
[ labels_other, ~, ~ ] = extract_UNBC_labels(UNBC_dir, train_users, rest_aus);
labels_other = cat(1, labels_other{:});
% Reading in the HOG data (of only relevant frames)
[train_appearance_data, valid_ids_train_hog, vid_ids_train_string] = Read_HOG_files(train_users, features_dir);
[train_geom_data] = Read_geom_files(train_users, features_dir);
% Subsample the data to make training quicker
labels_train = cat(1, labels_train{:});
valid_ids_train = logical(cat(1, valid_ids_train{:}));
if(numel(train_users) > 0)
reduced_inds = false(size(labels_train,1),1);
reduced_inds(labels_train > 0) = true;
% make sure the same number of positive and negative samples is taken
pos_count = sum(labels_train > 0);
neg_count = sum(labels_train == 0);
num_other = floor(pos_count / (size(labels_other, 2)));
inds_all = 1:size(labels_train,1);
for i=1:size(labels_other, 2)+1
if(i > size(labels_other, 2))
% fill the rest with a proportion of neutral
inds_other = inds_all(sum(labels_other,2)==0 & ~labels_train );
num_other_i = min(numel(inds_other), pos_count - sum(labels_train(reduced_inds,:)==0));
else
% take a proportion of each other AU
inds_other = inds_all(labels_other(:, i) & ~labels_train );
num_other_i = min(numel(inds_other), num_other);
end
inds_other_to_keep = inds_other(round(linspace(1, numel(inds_other), num_other_i)));
reduced_inds(inds_other_to_keep) = true;
end
% Remove invalid ids based on CLM failing or AU not being labelled
reduced_inds(~valid_ids_train) = false;
reduced_inds(~valid_ids_train_hog) = false;
labels_other = labels_other(reduced_inds, :);
labels_train = labels_train(reduced_inds,:);
train_appearance_data = train_appearance_data(reduced_inds,:);
train_geom_data = train_geom_data(reduced_inds,:);
vid_ids_train_string = vid_ids_train_string(reduced_inds,:);
end
%% Extract devel data
% First extracting the labels
[ labels_devel, valid_ids_devel, vid_ids_devel ] = extract_UNBC_labels(UNBC_dir, devel_users, au_train);
% Reading in the HOG data (of only relevant frames)
[devel_appearance_data, valid_ids_devel_hog, vid_ids_devel_string] = Read_HOG_files(devel_users, features_dir);
devel_success = valid_ids_devel_hog;
devel_ids = vid_ids_devel_string;
[devel_geom_data] = Read_geom_files(devel_users, features_dir);
labels_devel = cat(1, labels_devel{:});
% Peforming zone specific masking
if(au_train < 8 || au_train == 43 || au_train == 45) % upper face AUs ignore bottom face
% normalise the data
pca_file = '../../pca_generation/generic_face_upper.mat';
load(pca_file);
elseif(au_train > 9) % lower face AUs ignore upper face and the sides
% normalise the data
pca_file = '../../pca_generation/generic_face_lower.mat';
load(pca_file);
elseif(au_train == 9) % Central face model
% normalise the data
pca_file = '../../pca_generation/generic_face_rigid.mat';
load(pca_file);
end
% Grab all data for validation as want good params for all the data
raw_devel = cat(2, devel_appearance_data, devel_geom_data);
devel_appearance_data = bsxfun(@times, bsxfun(@plus, devel_appearance_data, -means_norm), 1./stds_norm);
data_devel = devel_appearance_data * PC;
data_devel = cat(2, data_devel, devel_geom_data);
if(numel(train_users) > 0)
train_appearance_data = bsxfun(@times, bsxfun(@plus, train_appearance_data, -means_norm), 1./stds_norm);
data_train = train_appearance_data * PC;
data_train = cat(2, data_train, train_geom_data);
else
data_train = [];
end
geom_size = max(size(train_geom_data, 2), size(devel_geom_data, 2));
PC_n = zeros(size(PC)+geom_size);
PC_n(1:size(PC,1), 1:size(PC,2)) = PC;
PC_n(size(PC,1)+1:end, size(PC,2)+1:end) = eye(geom_size);
PC = PC_n;
means_norm = cat(2, means_norm, zeros(1, geom_size));
stds_norm = cat(2, stds_norm, ones(1, geom_size));
end

View File

@@ -0,0 +1,117 @@
function [data_train, labels_train, data_devel, labels_devel, raw_devel, PC, means_norm, stds_norm, devel_ids, devel_success] = ...
Prepare_HOG_AU_data_dynamic(train_users, devel_users, au_train, rest_aus, UNBC_dir, features_dir)
%%
addpath(genpath('../data extraction/'));
% First extracting the labels
[ labels_train, valid_ids_train, filenames ] = extract_UNBC_labels(UNBC_dir, train_users, au_train);
[ labels_other, ~, ~ ] = extract_UNBC_labels(UNBC_dir, train_users, rest_aus);
labels_other = cat(1, labels_other{:});
% Reading in the HOG data (of only relevant frames)
[train_appearance_data, valid_ids_train_hog, vid_ids_train_string] = Read_HOG_files_dynamic(train_users, features_dir);
[train_geom_data] = Read_geom_files_dynamic(train_users, features_dir);
% Subsample the data to make training quicker
labels_train = cat(1, labels_train{:});
valid_ids_train = logical(cat(1, valid_ids_train{:}));
if(numel(train_users) > 0)
reduced_inds = false(size(labels_train,1),1);
reduced_inds(labels_train > 0) = true;
% make sure the same number of positive and negative samples is taken
pos_count = sum(labels_train > 0);
neg_count = sum(labels_train == 0);
num_other = floor(pos_count / (size(labels_other, 2)));
inds_all = 1:size(labels_train,1);
for i=1:size(labels_other, 2)+1
if(i > size(labels_other, 2))
% fill the rest with a proportion of neutral
inds_other = inds_all(sum(labels_other,2)==0 & ~labels_train );
num_other_i = min(numel(inds_other), pos_count - sum(labels_train(reduced_inds,:)==0));
else
% take a proportion of each other AU
inds_other = inds_all(labels_other(:, i) & ~labels_train );
num_other_i = min(numel(inds_other), num_other);
end
inds_other_to_keep = inds_other(round(linspace(1, numel(inds_other), num_other_i)));
reduced_inds(inds_other_to_keep) = true;
end
% Remove invalid ids based on CLM failing or AU not being labelled
reduced_inds(~valid_ids_train) = false;
reduced_inds(~valid_ids_train_hog) = false;
labels_other = labels_other(reduced_inds, :);
labels_train = labels_train(reduced_inds,:);
train_appearance_data = train_appearance_data(reduced_inds,:);
train_geom_data = train_geom_data(reduced_inds,:);
vid_ids_train_string = vid_ids_train_string(reduced_inds,:);
end
%% Extract devel data
% First extracting the labels
[ labels_devel, valid_ids_devel, vid_ids_devel ] = extract_UNBC_labels(UNBC_dir, devel_users, au_train);
% Reading in the HOG data (of only relevant frames)
[devel_appearance_data, valid_ids_devel_hog, vid_ids_devel_string] = Read_HOG_files_dynamic(devel_users, features_dir);
devel_success = valid_ids_devel_hog;
devel_ids = vid_ids_devel_string;
[devel_geom_data] = Read_geom_files_dynamic(devel_users, features_dir);
labels_devel = cat(1, labels_devel{:});
% Peforming zone specific masking
if(au_train < 8 || au_train == 43 || au_train == 45) % upper face AUs ignore bottom face
% normalise the data
pca_file = '../../pca_generation/generic_face_upper.mat';
load(pca_file);
elseif(au_train > 9) % lower face AUs ignore upper face and the sides
% normalise the data
pca_file = '../../pca_generation/generic_face_lower.mat';
load(pca_file);
elseif(au_train == 9) % Central face model
% normalise the data
pca_file = '../../pca_generation/generic_face_rigid.mat';
load(pca_file);
end
% Grab all data for validation as want good params for all the data
raw_devel = cat(2, devel_appearance_data, devel_geom_data);
devel_appearance_data = bsxfun(@times, bsxfun(@plus, devel_appearance_data, -means_norm), 1./stds_norm);
data_devel = devel_appearance_data * PC;
data_devel = cat(2, data_devel, devel_geom_data);
if(numel(train_users) > 0)
train_appearance_data = bsxfun(@times, bsxfun(@plus, train_appearance_data, -means_norm), 1./stds_norm);
data_train = train_appearance_data * PC;
data_train = cat(2, data_train, train_geom_data);
else
data_train = [];
end
geom_size = max(size(train_geom_data, 2), size(devel_geom_data, 2));
PC_n = zeros(size(PC)+geom_size);
PC_n(1:size(PC,1), 1:size(PC,2)) = PC;
PC_n(size(PC,1)+1:end, size(PC,2)+1:end) = eye(geom_size);
PC = PC_n;
means_norm = cat(2, means_norm, zeros(1, geom_size));
stds_norm = cat(2, stds_norm, ones(1, geom_size));
end

View File

@@ -0,0 +1,89 @@
function [hog_data, valid_data, vid_id] = Read_HOG_files(users, hog_data_dir)
hog_data = [];
vid_id = {};
valid_data = [];
feats_filled = 0;
for i=1:numel(users)
hog_files = dir([hog_data_dir, '/' users{i} '*.hog']);
for f_num=1:numel(hog_files)
f = fopen([hog_data_dir, '/', hog_files(f_num).name], 'r');
curr_data = [];
curr_ind = 0;
while(~feof(f))
if(curr_ind == 0)
num_cols = fread(f, 1, 'int32');
if(isempty(num_cols))
break;
end
num_rows = fread(f, 1, 'int32');
num_chan = fread(f, 1, 'int32');
curr_ind = curr_ind + 1;
% preallocate some space
if(curr_ind == 1)
curr_data = zeros(5000, 1 + num_rows * num_cols * num_chan);
num_feats = 1 + num_rows * num_cols * num_chan;
end
if(curr_ind > size(curr_data,1))
curr_data = cat(1, curr_data, zeros(6000, 1 + num_rows * num_cols * num_chan));
end
feature_vec = fread(f, [1, 1 + num_rows * num_cols * num_chan], 'float32');
curr_data(curr_ind, :) = feature_vec;
else
% Reading in batches of 5000
feature_vec = fread(f, [4 + num_rows * num_cols * num_chan, 5000], 'float32');
feature_vec = feature_vec(4:end,:)';
num_rows_read = size(feature_vec,1);
curr_data(curr_ind+1:curr_ind+num_rows_read,:) = feature_vec;
curr_ind = curr_ind + size(feature_vec,1);
end
end
fclose(f);
curr_data = curr_data(1:curr_ind,:);
vid_id_curr = cell(curr_ind,1);
vid_id_curr(:) = users(i);
vid_id = cat(1, vid_id, vid_id_curr);
% Assume same number of frames per video
if(i==1)
hog_data = zeros(curr_ind*numel(users), num_feats);
end
if(size(hog_data,1) < feats_filled+curr_ind)
hog_data = cat(1, hog_data, zeros(size(hog_data,1), num_feats));
end
hog_data(feats_filled+1:feats_filled+curr_ind,:) = curr_data;
feats_filled = feats_filled + curr_ind;
end
end
if(numel(users) > 0)
valid_data = hog_data(1:feats_filled,1) > 0;
hog_data = hog_data(1:feats_filled,2:end);
end
end

View File

@@ -0,0 +1,93 @@
function [hog_data, valid_data, vid_id] = Read_HOG_files_dynamic(users, hog_data_dir)
hog_data = [];
vid_id = {};
valid_data = [];
for i=1:numel(users)
hog_files = dir([hog_data_dir, '/' users{i} '*.hog']);
hog_data_curr_p = [];
feats_filled = 0;
for f_num=1:numel(hog_files)
f = fopen([hog_data_dir, '/', hog_files(f_num).name], 'r');
curr_data = [];
curr_ind = 0;
while(~feof(f))
if(curr_ind == 0)
num_cols = fread(f, 1, 'int32');
if(isempty(num_cols))
break;
end
num_rows = fread(f, 1, 'int32');
num_chan = fread(f, 1, 'int32');
curr_ind = curr_ind + 1;
% preallocate some space
if(curr_ind == 1)
curr_data = zeros(5000, 1 + num_rows * num_cols * num_chan);
num_feats = 1 + num_rows * num_cols * num_chan;
end
if(curr_ind > size(curr_data,1))
curr_data = cat(1, curr_data, zeros(6000, 1 + num_rows * num_cols * num_chan));
end
feature_vec = fread(f, [1, 1 + num_rows * num_cols * num_chan], 'float32');
curr_data(curr_ind, :) = feature_vec;
else
% Reading in batches of 5000
feature_vec = fread(f, [4 + num_rows * num_cols * num_chan, 5000], 'float32');
feature_vec = feature_vec(4:end,:)';
num_rows_read = size(feature_vec,1);
curr_data(curr_ind+1:curr_ind+num_rows_read,:) = feature_vec;
curr_ind = curr_ind + size(feature_vec,1);
end
end
fclose(f);
curr_data = curr_data(1:curr_ind,:);
vid_id_curr = cell(curr_ind,1);
vid_id_curr(:) = users(i);
vid_id = cat(1, vid_id, vid_id_curr);
% Assume same number of frames per video
if(f_num==1)
hog_data_curr_p = zeros(curr_ind*numel(users), num_feats);
end
if(size(hog_data_curr_p,1) < feats_filled+curr_ind)
hog_data_curr_p = cat(1, hog_data_curr_p, zeros(size(hog_data_curr_p,1), num_feats));
end
hog_data_curr_p(feats_filled+1:feats_filled+curr_ind,:) = curr_data;
feats_filled = feats_filled + curr_ind;
end
hog_data_curr_p(1:feats_filled,2:end) = bsxfun(@plus, hog_data_curr_p(1:feats_filled,2:end), -median(hog_data_curr_p(1:feats_filled,2:end)));
hog_data = cat(1, hog_data, hog_data_curr_p(1:feats_filled,:));
end
if(numel(users) > 0)
valid_data = hog_data(:,1) > 0;
hog_data = hog_data(:,2:end);
end
end

View File

@@ -0,0 +1,46 @@
function [geom_data, valid_ids] = Read_geom_files(users, model_param_data_dir)
geom_data = [];
valid_ids = [];
load('../../pca_generation/pdm_68_aligned_wild.mat');
for i=1:numel(users)
geom_files = dir([model_param_data_dir, '/' users{i} '*.csv']);
for g=1:numel(geom_files)
m_file = [model_param_data_dir, '/', geom_files(g).name, '.mat'];
if(~exist(m_file, 'file'))
if(~exist('shape_inds', 'var'))
tab = readtable([model_param_data_dir, '/', geom_files(g).name]);
column_names = tab.Properties.VariableNames;
valid_ind = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'success'));
shape_inds = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'p_'));
end
res = dlmread([model_param_data_dir, '/', geom_files(g).name], ',', 1, 0);
valid = res(:, valid_ind) > 0.7;
res = res(:, shape_inds);
% Do not consider global parameters
res = res(:, 7:end);
save(m_file, 'res', 'valid');
else
load(m_file);
end
actual_locs = res * V';
res = cat(2, actual_locs, res);
valid_ids = cat(1, valid_ids, valid);
geom_data = cat(1, geom_data, res);
end
end
end

View File

@@ -0,0 +1,51 @@
function [geom_data, valid_ids] = Read_geom_files_dynamic(users, model_param_data_dir)
geom_data = [];
valid_ids = [];
load('../../pca_generation/pdm_68_aligned_wild.mat');
for i=1:numel(users)
geom_files = dir([model_param_data_dir, '/' users{i} '*.csv']);
geom_data_curr_p = [];
for g=1:numel(geom_files)
m_file = [model_param_data_dir, '/', geom_files(g).name, '.mat'];
if(~exist(m_file, 'file'))
if(~exist('shape_inds', 'var'))
tab = readtable([model_param_data_dir, '/', geom_files(g).name]);
column_names = tab.Properties.VariableNames;
valid_ind = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'success'));
shape_inds = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'p_'));
end
res = dlmread([model_param_data_dir, '/', geom_files(g).name], ',', 1, 0);
valid = res(:, valid_ind) > 0.7;
res = res(:, shape_inds);
% Do not consider global parameters
res = res(:, 7:end);
save(m_file, 'res', 'valid');
else
load(m_file);
end
actual_locs = res * V';
res = cat(2, actual_locs, res);
valid_ids = cat(1, valid_ids, valid);
geom_data_curr_p = cat(1, geom_data_curr_p, res);
end
geom_data_curr_p = bsxfun(@plus, geom_data_curr_p, -median(geom_data_curr_p));
geom_data = cat(1, geom_data, geom_data_curr_p);
end
end

View File

@@ -0,0 +1,83 @@
function Script_HOG_SVM_train()
% Change to your downloaded location
addpath('C:\liblinear\matlab')
addpath('../training_code/');
addpath('../utilities/');
addpath('../../data extraction/');
%% load shared definitions and AU data
shared_defs;
% Set up the hyperparameters to be validated
hyperparams.c = 10.^(-9:0.5:1);
hyperparams.e = 10.^(-3);
hyperparams.validate_params = {'c', 'e'};
% Set the training function
svm_train = @svm_train_linear;
% Set the test function (the first output will be used for validation)
svm_test = @svm_test_linear;
all_recs = cat(2, train_recs, devel_recs);
%%
for a=1:numel(aus)
au = aus(a);
rest_aus = setdiff(all_aus, au);
[users_train, users_valid] = get_balanced_fold(UNBC_dir, all_recs, au, 1/3, 1);
% load the training and testing data for the current fold
[train_samples, train_labels, valid_samples, valid_labels, ~, PC, means, scaling] = Prepare_HOG_AU_data(users_train, users_valid, au, rest_aus, UNBC_dir, features_dir);
% Binarizing the data
train_labels(train_labels > 1) = 1;
valid_labels(valid_labels > 1) = 1;
train_samples = sparse(train_samples);
valid_samples = sparse(valid_samples);
%% Cross-validate here
[ best_params, ~ ] = validate_grid_search_no_par(svm_train, svm_test, false, train_samples, train_labels, valid_samples, valid_labels, hyperparams);
model = svm_train(train_labels, train_samples, best_params);
[prediction, a, actual_vals] = predict(valid_labels, valid_samples, model);
% Go from raw data to the prediction
w = model.w(1:end-1)';
b = model.w(end);
svs = bsxfun(@times, PC, 1./scaling') * w;
name = sprintf('models/AU_%d_static.dat', au);
pos_lbl = model.Label(1);
neg_lbl = model.Label(2);
write_lin_svm(name, means, svs, b, pos_lbl, neg_lbl);
name = sprintf('results_UNBC_devel/AU_%d_static.mat', au);
tp = sum(valid_labels == 1 & prediction == 1);
fp = sum(valid_labels == 0 & prediction == 1);
fn = sum(valid_labels == 1 & prediction == 0);
tn = sum(valid_labels == 0 & prediction == 0);
precision = tp/(tp+fp);
recall = tp/(tp+fn);
f1 = 2 * precision * recall / (precision + recall);
save(name, 'model', 'f1', 'precision', 'recall', 'best_params', 'valid_labels', 'prediction', 'users_valid');
end
end

View File

@@ -0,0 +1,83 @@
function Script_HOG_SVM_train_dyn()
% Change to your downloaded location
addpath('C:\liblinear\matlab')
addpath('../training_code/');
addpath('../utilities/');
addpath('../../data extraction/');
%% load shared definitions and AU data
shared_defs;
% Set up the hyperparameters to be validated
hyperparams.c = 10.^(-9:0.5:1);
hyperparams.e = 10.^(-3);
hyperparams.validate_params = {'c', 'e'};
% Set the training function
svm_train = @svm_train_linear;
% Set the test function (the first output will be used for validation)
svm_test = @svm_test_linear;
all_recs = cat(2, train_recs, devel_recs);
%%
for a=1:numel(aus)
au = aus(a);
rest_aus = setdiff(all_aus, au);
[users_train, users_valid] = get_balanced_fold(UNBC_dir, all_recs, au, 1/3, 1);
% load the training and testing data for the current fold
[train_samples, train_labels, valid_samples, valid_labels, ~, PC, means, scaling] = Prepare_HOG_AU_data_dynamic(users_train, users_valid, au, rest_aus, UNBC_dir, features_dir);
train_samples = sparse(train_samples);
valid_samples = sparse(valid_samples);
% Binarizing the data
train_labels(train_labels > 1) = 1;
valid_labels(valid_labels > 1) = 1;
%% Cross-validate here
[ best_params, ~ ] = validate_grid_search_no_par(svm_train, svm_test, false, train_samples, train_labels, valid_samples, valid_labels, hyperparams);
model = svm_train(train_labels, train_samples, best_params);
[prediction, a, actual_vals] = predict(valid_labels, valid_samples, model);
% Go from raw data to the prediction
w = model.w(1:end-1)';
b = model.w(end);
svs = bsxfun(@times, PC, 1./scaling') * w;
name = sprintf('models/AU_%d_dyn.dat', au);
pos_lbl = model.Label(1);
neg_lbl = model.Label(2);
write_lin_dyn_svm(name, means, svs, b, pos_lbl, neg_lbl);
name = sprintf('results_UNBC_devel/AU_%d_dyn.mat', au);
tp = sum(valid_labels == 1 & prediction == 1);
fp = sum(valid_labels == 0 & prediction == 1);
fn = sum(valid_labels == 1 & prediction == 0);
tn = sum(valid_labels == 0 & prediction == 0);
precision = tp/(tp+fp);
recall = tp/(tp+fn);
f1 = 2 * precision * recall / (precision + recall);
save(name, 'model', 'f1', 'precision', 'recall', 'best_params', 'valid_labels', 'prediction', 'users_valid');
end
end

View File

@@ -0,0 +1,70 @@
function Script_HOG_SVR_train()
% Change to your downloaded location
addpath('C:\liblinear\matlab')
addpath('../training_code/');
addpath('../utilities/');
addpath('../../data extraction/');
%% load shared definitions and AU data
shared_defs;
% Set up the hyperparameters to be validated
hyperparams.c = 10.^(-7:1:4);
hyperparams.p = 10.^(-2);
hyperparams.validate_params = {'c', 'p'};
% Set the training function
svr_train = @svr_train_linear;
% Set the test function (the first output will be used for validation)
svr_test = @svr_test_linear;
all_recs = cat(2, train_recs, devel_recs);
%%
for a=1:numel(aus)
au = aus(a);
rest_aus = setdiff(all_aus, au);
[users_train, users_valid] = get_balanced_fold(UNBC_dir, all_recs, au, 1/3, 1);
% load the training and testing data for the current fold
[train_samples, train_labels, valid_samples, valid_labels, ~, PC, means, scaling, valid_ids, valid_success] = Prepare_HOG_AU_data(users_train, users_valid, au, rest_aus, UNBC_dir, features_dir);
train_samples = sparse(train_samples);
valid_samples = sparse(valid_samples);
hyperparams.success = valid_success;
%% Cross-validate here
[ best_params, ~ ] = validate_grid_search_no_par(svr_train, svr_test, false, train_samples, train_labels, valid_samples, valid_labels, hyperparams);
model = svr_train(train_labels, train_samples, best_params);
[~, prediction] = svr_test(valid_labels, valid_samples, model);
% Go from raw data to the prediction
w = model.w(1:end-1)';
b = model.w(end);
svs = bsxfun(@times, PC, 1./scaling') * w;
name = sprintf('models/AU_%d_static_intensity.dat', au);
write_lin_svr(name, means, svs, b);
name = sprintf('results_UNBC_devel/AU_%d_static_intensity.mat', au);
[ accuracies, F1s, corrs, ccc, rms, classes ] = evaluate_regression_results( prediction, valid_labels );
save(name, 'model', 'F1s', 'corrs', 'accuracies', 'ccc', 'rms', 'prediction', 'valid_labels', 'users_valid');
end
end

View File

@@ -0,0 +1,70 @@
function Script_HOG_SVR_train_dyn()
% Change to your downloaded location
addpath('C:\liblinear\matlab')
addpath('../training_code/');
addpath('../utilities/');
addpath('../../data extraction/');
%% load shared definitions and AU data
shared_defs;
% Set up the hyperparameters to be validated
hyperparams.c = 10.^(-7:1:4);
hyperparams.p = 10.^(-2);
hyperparams.validate_params = {'c', 'p'};
% Set the training function
svr_train = @svr_train_linear;
% Set the test function (the first output will be used for validation)
svr_test = @svr_test_linear;
all_recs = cat(2, train_recs, devel_recs);
%%
for a=1:numel(aus)
au = aus(a);
rest_aus = setdiff(all_aus, au);
[users_train, users_valid] = get_balanced_fold(UNBC_dir, all_recs, au, 1/3, 1);
% load the training and testing data for the current fold
[train_samples, train_labels, valid_samples, valid_labels, ~, PC, means, scaling, valid_ids, valid_success] = Prepare_HOG_AU_data_dynamic(users_train, users_valid, au, rest_aus, UNBC_dir, features_dir);
train_samples = sparse(train_samples);
valid_samples = sparse(valid_samples);
hyperparams.success = valid_success;
%% Cross-validate here
[ best_params, ~ ] = validate_grid_search_no_par(svr_train, svr_test, false, train_samples, train_labels, valid_samples, valid_labels, hyperparams);
model = svr_train(train_labels, train_samples, best_params);
[~, prediction] = svr_test(valid_labels, valid_samples, model);
% Go from raw data to the prediction
w = model.w(1:end-1)';
b = model.w(end);
svs = bsxfun(@times, PC, 1./scaling') * w;
name = sprintf('models/AU_%d_dyn_intensity.dat', au);
write_lin_dyn_svr(name, means, svs, b, 0);
name = sprintf('results_UNBC_devel/AU_%d_dyn_intensity.mat', au);
[ accuracies, F1s, corrs, ccc, rms, classes ] = evaluate_regression_results( prediction, valid_labels );
save(name, 'model', 'F1s', 'corrs', 'accuracies', 'ccc', 'rms', 'prediction', 'valid_labels', 'users_valid');
end
end

View File

@@ -0,0 +1,33 @@
function [train_users, dev_users] = get_balanced_fold(UNBC_dir, users, au, prop_test, offset)
[ labels_train, valid_ids_train, filenames ] = extract_UNBC_labels(UNBC_dir, users, au);
% trimming the filenames a bit
for f=1:numel(filenames)
filenames{f} = filenames{f}(1:5);
end
counts = zeros(numel(users),1);
for k=1:numel(users)
counts(k) = sum(cat(1, labels_train{strcmp(filenames, users{k}(5:end))}));
end
[sorted, inds] = sort(counts);
dev_users = users(inds(offset:round(1/prop_test):end));
train_users = setdiff(users, dev_users);
count_dev = 0;
count_train = 0;
for k=1:numel(users)
if(any(strcmp(dev_users, users{k})))
count_dev = count_dev + counts(k);
else
count_train = count_train + counts(k);
end
end
fprintf('Mean train %.2f, mean test %.2f\n', count_train / numel(train_users), count_dev / numel(dev_users));
end

View File

@@ -0,0 +1,11 @@
% this is data defined across the experiments (to make sure all of them have same user conventions)
% Defining which AU's we are extracting (all corrs above 0.5)
all_aus = [6, 7, 9, 10, 12, 25, 26];
aus = [6, 7, 9, 10, 12, 25, 26];
addpath('../../data extraction/');
find_UNBC;
features_dir = 'E:\datasets\face_datasets_processed\unbc';