how to train HMM with 8 states and 20 double numbers as input sequence?

174 views
Skip to first unread message

PARSA

unread,
Jul 25, 2013, 9:37:33 AM7/25/13
to accor...@googlegroups.com
Hello everybody
I am working on a project to recognize human activities (8 activities) in real-time through body motion and HMM. I collect human data in the txt file as bellow with 20 double numbers:

(
0.2 0.3 0.2 0.6 1 2 5 0.8 1 1 0 0 0.4 0.3 0.2 0.2 0.6 1 2 5
....
....
...
...
.....
.....
)

I need to design a HMM that get as input a sequence with 20 double number and recognize one of the 8 activities as output in real time.

Does anyone have any sample code or at least a tutorial to learn how it can be done?
I have all data and I need to know how to train network with multiple states and continues data. The tutorial is not really well-explained to understand the process of training and using HMM.

Thanks
Regards

Message has been deleted

César

unread,
Jul 27, 2013, 3:54:54 PM7/27/13
to accor...@googlegroups.com
Hi Parsa,

Some months ago I published a tutorial for HMMs for mouse gesture recognition. It is just a "smaller" and simpler version of what you are trying to achieve. In your project, you are using 20 numbers. In the tutorial I wrote, I was using 2 (the x and y screen coordinates). But it should be not much complicated to expand it to incorporate more features. I have actually used the same mechanism to perform sign recognition in sign languages.


Hope it helps!

Best regards,
Cesar

PARSA

unread,
Jul 30, 2013, 9:48:38 AM7/30/13
to accor...@googlegroups.com
following your tutorial code I wrote my code but it gets error on this line  "double error = teacher.Run(inputs, outputs);" 
do you know how I can solve it?

private void Button_Click(object sender, RoutedEventArgs e)
 {          
            //Defining Input and output
  
          // hand movement from top to buttom
            double[] fvA1 = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            double[] fvA2 = new double[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 };
            double[] fvA3 = new double[] { 4, 3, 1, 2, 5, 6, 7, 8, 9, 10 };
            double[] fvA4 = new double[] { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 };
            double[][] action1 = { fvA1, fvA2, fvA3, fvA4 };

            // hand movement from left to right
            double[] fvB1 = new double[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10 };
            double[] fvB2 = new double[] { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.10 };
            double[] fvB3 = new double[] { 0.4, 0.3, 0.1, 0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10 };
            double[] fvB4 = new double[] { 0.6, 0.7, 0.8, 0.9, 0.10, 0.1, 0.2, 0.3, 0.4, 0.5 };
            double[][] action2 = { fvB1, fvB2, fvB3, fvB4 };

            // hand movement from top to buttom
            double[] fvC1 = new double[] { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 };
            double[] fvC2 = new double[] { -2, -3, -4, -5, -6, -7, -8, -9, -10, -10 };
            double[] fvC3 = new double[] { -4, -3, -1, -2, -5, -6, -7, -8, -9, -10 };
            double[] fvC4 = new double[] { -6, -7, -8, -9, -10, -1, -2, -3, -4, -5 };
            double[][] action3 = { fvC1, fvC2, fvC3, fvC4 };

            // hand movement from left to right
            double[] fvD1 = new double[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10 };
            double[] fvD2 = new double[] { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.10 };
            double[] fvD3 = new double[] { 0.4, 0.3, 0.1, 0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10 };
            double[] fvD4 = new double[] { 0.6, 0.7, 0.8, 0.9, 0.10, 0.1, 0.2, 0.3, 0.4, 0.5 };
            double[][] action4 = { fvD1, fvD2, fvD3, fvD4 };

            double[][][] inputs = new double[4][][] {action1, action2, action3, action4 };
            
            int[] outputs = new int[] 
            {
                0,
                1,
                2,
                3
            };
            
            
            string[] classes = new string[]{"A","B","C","D"};

            int states = 4;
            int iterations = 0;
            double tolerance = 0.01;
            bool rejection = false;

            hmm = new HiddenMarkovClassifier<MultivariateNormalDistribution>(4,
                new Forward(states), new MultivariateNormalDistribution(2), classes);


            // Create the learning algorithm for the ensemble classifier
            var teacher = new HiddenMarkovClassifierLearning<MultivariateNormalDistribution>(hmm,

                // Train each model using the selected convergence criteria
                i => new BaumWelchLearning<MultivariateNormalDistribution>(hmm.Models[i])
                {
                    Tolerance = tolerance,
                    Iterations = iterations,

                    FittingOptions = new NormalOptions()
                    {
                        Regularization = 1e-5
                    }
                }
            );

            teacher.Empirical = true;
            teacher.Rejection = rejection;

            // Run the learning algorithm
            double error = teacher.Run(inputs, outputs);

            double result = hmm.Compute(fvC1);
            Console.WriteLine("Result > " + result);
        } 

César

unread,
Jul 30, 2013, 10:03:19 AM7/30/13
to accor...@googlegroups.com
Hi Parsa,

From what I can see, it seems that you are using 10-dimensional feature vectors, but the code is specifying a multivariate Normal distributions with only 2 dimensions. I haven't tested yet, but if this is the cause then perhaps you would need to change

new MultivariateNormalDistribution(2)

to

new MultivariateNormalDistribution(10)

and see if it works better.

I hope it helps!

Best regards,
Cesar

PARSA

unread,
Jul 30, 2013, 10:24:59 AM7/30/13
to accor...@googlegroups.com
You are right, it resolved but another error on result. I am using last version of Accord 2,9

 double[] testinput = new double[] { -1, -2, -1, -1, 2, -6, -7, -0.8, -9, -10 };
            double result = hmm.Compute(testinput);

any idea about that?

PARSA

unread,
Aug 1, 2013, 7:58:41 AM8/1/13
to accor...@googlegroups.com
Thank you very much
The problem resolved. It was in input and output dimentions;

César

unread,
Aug 1, 2013, 12:23:30 PM8/1/13
to accor...@googlegroups.com
Hi Parsa,

Please accept my apologies for the late response; I couldn't answer it on time. I am glad you could solve the issue!

Best regards,
Cesar
Reply all
Reply to author
Forward
0 new messages