[英]Using an Accord.NET SVM for face recognition (MulticlassSupportVectorMachine)
[英]HMM using Accord.Net for gesture recognition
我正在做手势识别项目。 我的数据集包含4个不同的手势,其中每个手势集包含约70张图像。 我为每个图像提取了4个特征。 我正在尝试使用Accord.Net来实现HMM,并且我了解到我将需要4个HMM,每个手势一个,但是我不确定如何构造用于学习/训练的特征向量序列相。 有人知道如何解决吗?
这是序列的简单代码:
double[][] sequences = new double[][]
{
new double[] { 0,1,2,3,4 }, // This is the first sequence with label = 0
new double[] { 4,3,2,1,0 }, // This is the second sequence with label = 1
};
// Labels for the sequences
int[] labels = { 0, 1 };
没错,每个手势都需要一个HMM。 但是,如果您使用HiddenMarkovClassifier类,则该框架已经可以为您提供此构造(它是围绕您要检测的每个类创建的多个HMM的包装)。
如果每个图像有4个特征,则将需要假设一个概率分布,该概率分布将能够对多元特征建模。 一个简单的选择是假设您的功能彼此独立,并且每个功能都遵循正态分布。
这样,您可以使用以下示例代码创建模型。 它假定你的数据库只有两个训练序列,但现实中,你必须有更多。
double[][][] sequences = new double[][][]
{
new double[][] // This is the first sequence with label = 0
{
new double[] { 0, 1, 2, 1 }, // <-- this is the 4-features feature vector for
new double[] { 1, 2, 5, 2 }, // the first image of the first sequence
new double[] { 2, 3, 2, 5 },
new double[] { 3, 4, 1, 1 },
new double[] { 4, 5, 2, 2 },
},
new double[][] // This is the second sequence with label = 1
{
new double[] { 4, 3, 4, 1 }, // <-- this is the 4-features feature vector for
new double[] { 3, 2, 2, 2 }, // the first image of the second sequence
new double[] { 2, 1, 1, 1 },
new double[] { 1, 0, 2, 2 },
new double[] { 0, -1, 1, 2 },
}
};
// Labels for the sequences
int[] labels = { 0, 1 };
上面的代码显示了如何建立学习数据库。 现在,设置好后,您可以为4个正态特征(假设正态分布之间具有独立性)创建隐藏的马尔可夫分类器,如下所示:
// Create one base Normal distribution to be replicated accross the states
var initialDensity = new MultivariateNormalDistribution(4); // we have 4 features
// Creates a sequence classifier containing 2 hidden Markov Models with 2 states
// and an underlying multivariate mixture of Normal distributions as density.
var classifier = new HiddenMarkovClassifier<MultivariateNormalDistribution>(
classes: 2, topology: new Forward(2), initial: initialDensity);
// Configure the learning algorithms to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning<MultivariateNormalDistribution>(
classifier,
// Train each model until the log-likelihood changes less than 0.0001
modelIndex => new BaumWelchLearning<MultivariateNormalDistribution>(
classifier.Models[modelIndex])
{
Tolerance = 0.0001,
Iterations = 0,
FittingOptions = new NormalOptions()
{
Diagonal = true, // only diagonal covariance matrices
Regularization = 1e-5 // avoid non-positive definite errors
}
// PS: Setting diagonal = true means the features will be
// assumed independent of each other. This can also be
// achieved by using an Independent<NormalDistribution>
// instead of a diagonal multivariate Normal distribution
}
);
最后,我们可以训练模型并根据学习到的数据测试其输出:
// Train the sequence classifier using the algorithm
double logLikelihood = teacher.Run(sequences, labels);
// Calculate the probability that the given
// sequences originated from the model
double likelihood, likelihood2;
// Try to classify the 1st sequence (output should be 0)
int c1 = classifier.Compute(sequences[0], out likelihood);
// Try to classify the 2nd sequence (output should be 1)
int c2 = classifier.Compute(sequences[1], out likelihood2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.