Skip to content

Commit

Permalink
Merge branch 'master' of github.com:uShadow/tabletop-private
Browse files Browse the repository at this point in the history
  • Loading branch information
ushadow committed Apr 11, 2013
2 parents 2d7b297 + ffd9cde commit d2801db
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 64 deletions.
8 changes: 7 additions & 1 deletion matlab/hyperparam.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
function hyperParam = hyperparam(paramFromData, varargin)

% Default values.
hyperParam.nS = 37;
hyperParam.nS = 11;
hyperParam.nhandFet = 3;
hyperParam.cov_type = 'diag';
hyperParam.resetS = false;
hyperParam.inferMethod = 'viterbi';

for i = 1 : 2 : length(varargin)
switch varargin{i}
case 'nS'
hyperParam.nS = varargin{i + 1};
case 'resetS'
hyperParam.resetS = varargin{i + 1};
otherwise
error(['Unrecognized parameter: ' varargin{i}]);
end
Expand All @@ -24,13 +28,15 @@
for i = 1 : nmodel
param = paramFromData;
param.learner = 'ahmm';
param.inferMethod = hyperParam.inferMethod;
param.preprocess = {@denoise @eigenhand @standardizefeature};
param.nS = hyperParam.nS(i);
param.nhandFet = hyperParam.nhandFet;
param.startHandFetNDX = hyperParam.startHandFetNDX;
param.dir = hyperParam.dir;
param.maxIter = hyperParam.maxIter;
param.cov_type = hyperParam.cov_type;
param.resetS = hyperParam.resetS;
hyperParam.model{i} = param;
end
end
2 changes: 1 addition & 1 deletion matlab/jobparam.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function jobParam = jobparam
jobParam.verbose = true;
jobParam.destroy = true;
jobParam.destroy = false;
jobParam.type = 'local';
jobParam.path = [genpath('c:/users/yingyin/workspace/tabletop/matlab-lib/lib') ';' genpath(pwd)];
end
3 changes: 3 additions & 0 deletions matlab/meet/evaluate/errorperframe.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function error = errorperframe(Ytrue, Ystar)
error = sum(1 - (cell2mat(Ytrue) == cell2mat(Ystar)), 2);
end
26 changes: 11 additions & 15 deletions matlab/meet/evaluate/evalclassification.m
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
function stat = evalclassification(Y, R, loss)
function stat = evalclassification(Y, R, evalfun)
% Args:
% - loss: loss function.
% - evalfun: evaluation function.
if iscell(R)
nfold = size(R, 2);
stat = cell(1, nfold);
for i = 1 : nfold
split1 = R{i}.split;
Ytrue.train = Y(split1{1});
Ytrue.validate = Y(split1{2});
stat{i} = evalOneFold(Ytrue, R{i}.prediction, loss);
stat{i} = evalOneFold(Ytrue, R{i}.prediction, evalfun);
end
else
stat = evalOneFold(Y, R, loss);
stat = evalOneFold(Y, R, evalfun);
end
end

function stat = evalOneFold(Y, R, loss)
function stat = evalOneFold(Y, R, evalfun)
stat = containers.Map();
stat('train') = evaluate(Y.train, R.train, loss);
stat('validate') = evaluate(Y.validate, R.validate, loss);
stat('train') = evaluate(Y.train, R.train, evalfun);
stat('validate') = evaluate(Y.validate, R.validate, evalfun);
if isfield(Y, 'test')
stat('test') = evaluate(Y.test, R.test, loss);
stat('test') = evaluate(Y.test, R.test, evalfun);
end
end

function r = evaluate(Ytrue, Ystar, loss)
function r = evaluate(Ytrue, Ystar, evalfun)
%
% Args:
% - Ytrue: cell array of sequences.
Expand All @@ -33,12 +33,8 @@
r = containers.Map();
error = zeros(nlabel, 1);
for i = 1 : length(Ytrue)
for j = 1 : size(Ytrue{i}, 2)
total = total + 1;
for k = 1 : nlabel
error(k) = error(k) + loss(Ytrue{i}{k, j}, Ystar{i}{k, j});
end
end
error = error + evalfun(Ytrue{i}, Ystar{i});
total = total + size(Ytrue{i}, 2);
end
r('error') = error / total;
end
9 changes: 9 additions & 0 deletions matlab/meet/evaluate/evalseqclass.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function stat = evalseqclass(Ytrue, Ystar)
% EVALSEQCLASS evaluates the classification as a sequence not by frame.
% Args:
% - Ytrue: a vector.
% - Ystar: a vector.
trueLabel = unique(Ytrue);
actualLabel = unqiue(Ystar);
stat = edit_distance_levenshtein(trueLabel, actualLabel);
end
6 changes: 0 additions & 6 deletions matlab/meet/evaluate/zerooneloss.m

This file was deleted.

11 changes: 10 additions & 1 deletion matlab/meet/learn/ahmm/createahmm.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
inter(G1, G1) = 1; % Node G1 in slice t - 1 connects to node G1 in slice t.
inter(F1, G1) = 1;
inter(S1, S1) = 1;
if param.resetS
inter(F1, S1) = 1;
end

nodeSize = double([param.nG param.nS param.nF param.nX]);
dnodes = [G1 S1 F1];
Expand Down Expand Up @@ -78,5 +81,11 @@
% Tablular CPD are stored as multidimentional arrays where the dimensions
% are arranged in the same order as the nodes. Nodes in the 2nd slice is
% is after the ndoes in the 1st slice.
ahmm.CPD{S2} = tabular_CPD(ahmm, S2, param.Stransprob);
if param.resetS
ahmm.CPD{S2} = hhmmQ_CPD(ahmm, S2, 'Fself', F1, 'Fbelow', [], ...
'Qps', G2, 'startprob', param.Sstartprob, ...
'transprob', param.Stransprob);
else
ahmm.CPD{S2} = tabular_CPD(ahmm, S2, param.Stransprob);
end
end
19 changes: 12 additions & 7 deletions matlab/meet/learn/ahmm/inferenceahmm.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
function R = inferenceahmm(ahmm, data, predictNode)
function R = inferenceahmm(ahmm, data, predictNode, method)

engine = smoother_engine(jtree_2TBN_inf_engine(ahmm));

nseq = length(data);
R = cell(1, nseq);
for i = 1 : nseq
evidence = data{i};
engine = enter_evidence(engine, evidence);
R{i} = mapest(engine, predictNode, length(evidence));

% Find the most probable explanation (Viterbi).
%mpe = find_mpe(engine, evidence);
%R{i} = mpe(predictNode, :);
switch method
case 'fixed-interval-smoothing'
engine = enter_evidence(engine, evidence);
R{i} = mapest(engine, predictNode, length(evidence));
case 'viterbi'
% Find the most probable explanation (Viterbi).
mpe = find_mpe(engine, evidence);
R{i} = mpe(predictNode, :);
otherwise
error(['Inference method not implemented: ' method]);
end
end
end
3 changes: 2 additions & 1 deletion matlab/meet/learn/ahmm/initahmmparam.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
% - param: struct with the following fields
% -- fold: the current fold.

param.nF = 2;
param.nX = param.nconFet + param.nhandFet;
assert(all(size(mean) == [param.nX param.nS]));

Expand All @@ -30,7 +31,7 @@
param.Gtransprob(3 : param.nG, 2 : param.nG) = ...
(1 - delta) / (param.nG - 1);

% Parameters related to S.
% Parameters related to S. Uniform initialization.
param.Sstartprob = ones(param.nG, param.nS) / param.nS;
param.Stransprob = ones(param.nS, param.nG, param.nS) / param.nS;
param.Stermprob = ones(param.nG, param.nS, param.nF) / param.nF;
Expand Down
10 changes: 6 additions & 4 deletions matlab/meet/learn/ahmm/learnahmm.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
% case{l}{i, t} is the value of node i in slice t in sequel l, or [] if
% unobserved.
nfeature = param.nconFet + param.nhandFet;
filename = sprintf('%sfeature-%d-%d-mean-%d.csv', param.dir, nfeature, ...
param.fold, param.nS);
filename = sprintf('%s%s-feature-%d-%d-mean-%d.csv', param.dir, ...
param.userId, nfeature, param.fold, param.nS);
logdebug('learnahmm', 'read file', filename);
imported = importdata(filename, ',', 1);
mean = imported.data;
Expand All @@ -24,10 +24,12 @@
checkahmm(finalAhmm);

trainData = createInputData(Y.train, X.train, param);
R.train = inferenceahmm(finalAhmm, trainData, predictNode);
R.train = inferenceahmm(finalAhmm, trainData, predictNode, ...
param.inferMethod);

validateData = createInputData(Y.validate, X.validate, param);
R.validate = inferenceahmm(finalAhmm, validateData, predictNode);
R.validate = inferenceahmm(finalAhmm, validateData, predictNode, ...
param.inferMethod);
end

function data = createInputData(Y, X, param)
Expand Down
9 changes: 5 additions & 4 deletions matlab/meet/runexperiment.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
% - R : struct, result

R.split = split;
R.param = param;
param.userId = data.userId;

% Step 1: Prepare data
if exist('data','var')
Expand Down Expand Up @@ -47,7 +49,7 @@
case 'svr'
R = learn_svr(Y, X, param);
case 'ahmm'
[R.prediction R.learnedModel] = learnahmm(Y, X, param);
[R.prediction, R.learnedModel] = learnahmm(Y, X, param);
otherwise
error('%s Not implemented yet', param.model);
end
Expand All @@ -66,11 +68,10 @@
case 'svr'
R.stat = eval_svr(Y, R.prediction, param);
case 'ahmm'
R.stat = evalclassification(Y, R.prediction, @zerooneloss);
R.stat = evalclassification(Y, R.prediction, @errorperframe);
otherwise
error('%s Not implemented yet', param.model);
error('%s Not implemented yet', param.learner);
end

end


21 changes: 9 additions & 12 deletions matlab/meet/runexperimentparallel.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function R = runexperimentparallel(data, split, modelParam, jobParam)
function R = runexperimentparallel(data, modelParam, jobParam)
%
% Args
% - modelParam: a cell array of model parameter.
Expand All @@ -13,7 +13,7 @@
distributed = false;
end


split = data.split;
R = cell(numel(modelParam),size(split, 2));
nargout = 1; % to be returned from each task

Expand Down Expand Up @@ -59,17 +59,14 @@
if verbose, fprintf('Collect results'); tid=tic(); end
rows = cellfun(@(x) getfield(x,'row'), job.JobData.job_log);
cols = cellfun(@(x) getfield(x,'col'), job.JobData.job_log);
for r=unique(rows)
for c=unique(cols)
tasks = find(rows==r & cols==c);
R{r,c} = cell(1,numel(tasks));
for t=1:numel(tasks)
if verbose, fprintf('.'); end
if ~isempty(job.Tasks(tasks(t)).OutputArguments)
R{r,c}{t} = job.Tasks(tasks(t)).OutputArguments{1};
end
end
for r = unique(rows)
for c = unique(cols)
task = rows == r & cols == c;
if verbose, fprintf('.'); end
if ~isempty(job.Tasks(task).OutputArguments)
R{r,c} = job.Tasks(task).OutputArguments{1};
end
end
end
if verbose, t=toc(tid); fprintf('done (%.2f secs)\n', t); end

Expand Down
30 changes: 19 additions & 11 deletions matlab/prepdata.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
function [data segFrameId dataParam] = prepdata(dirname, suffix)
function [data, segFrameId, dataParam] = prepdata(dirname, suffix, userId)
% PREPTRAININGDATA prepares the training data into right structure for
% preprocesssing.
%
% Args:
% - suffix: suffix of the feature file, e.g. '-100'.
% - userID: an optional string or cell array of userIDs indicating the user
% data to be imported.
%
% Return
% - data: a struct. data.Y is the label and data.X is the feature.
% Each field is a cell array.
Expand All @@ -12,9 +17,9 @@
name = file.name;
indices = strfind(name, '.');
last_index = indices(1);
basename = name(1 : last_index - 1);
basename = name(1 : last_index - 1); % without extension.
ext = name(last_index + 1 : end);
if strcmp(ext, 'glab.csv')
if strcmp(ext, 'glab.csv') && isuserfile(basename, userId)
labelFile = [dirname name];
label = importdata(labelFile, ',', 1);
logdebug('prepdata', 'label file', labelFile);
Expand All @@ -26,28 +31,31 @@
% Init parameters.
dataParam.nconFet = header{3};
dataParam.nG = length(unique(label.data(:, 2)));
dataParam.nF = length(unique(label.data(:, 3)));
imageWidth = header{5};
dataParam.imageSize = imageWidth * imageWidth;
dataParam.dir = dirname;

[data segFrameId] = combinelabelfeature(label.data, feature.data, ...
dataParam);
[data, segFrameId] = combinelabelfeature(label.data, feature.data, ...
dataParam);
data.userId = userId;
data.split = getnfoldindex(numel(data.X), 0, 10);
end
end
end
end

function [data segFrameId] = combinelabelfeature(label, feature, param)
function b = isuserfile(basename, userID)
b = isempty(userID) || any(strncmp(basename, userID, length(basename)));
end

function [data, segFrameId] = combinelabelfeature(label, feature, param)
labelFrameId = label(:, 1);
featureFrameId = feature(:, 1);
[frameids, labelNDX, featureNDX] = intersect(labelFrameId, featureFrameId);
logdebug('preptrainingdata', 'number of frames', length(frameids));
[segment segFrameId] = createsegment(frameids); % cell array
[segment, segFrameId] = createsegment(frameids); % cell array
label = label(labelNDX, 2 : end); % Removes frame ID.
feature = feature(featureNDX, 2 : end); % Each row is a feature vector.

assert(checklabel(label), 'Label is not valid.');
assert(size(label, 1) == size(feature, 1));

data.Y = cell(1, length(segment));
Expand All @@ -65,7 +73,7 @@
end
end

function [seg segFrameId] = createsegment(frameid)
function [seg, segFrameId] = createsegment(frameid)
% segments = segment(frameids) finds the segments in a vector of frame
% IDs. A segment is a sequence of continuous frame IDs.
%
Expand Down
3 changes: 2 additions & 1 deletion matlab/savefeature.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
standardized = standardizefeature(result, 0, 'retMat', true);
nfeature = size(standardized, 1);
assert(nfeature == hyperParam.nhandFet + hyperParam.startHandFetNDX - 1);
filename = sprintf('%sfeature-%d-%d.csv', hyperParam.dir, nfeature, i);
filename = sprintf('%s%s-feature-%d-%d.csv', hyperParam.dir, ...
hyperParam.userId, nfeature, i);
csvwrite(filename, standardized');
end
end

0 comments on commit d2801db

Please sign in to comment.