private void loadAllModels(string modelDirectoryName) { if (Directory.Exists(modelDirectoryName)) { // Get all files of type .rec in the directory string[] files = Directory.GetFiles(modelDirectoryName, "*.dat"); this.positionModels = new List<HiddenMarkovModel<MultivariateNormalDistribution>>(); this.jointModels = new List<HiddenMarkovModel<MultivariateNormalDistribution>>(); this.positionClasses = new List<string>(); this.jointClasses = new List<string>(); foreach (string s in files) { string actName = Path.GetFileNameWithoutExtension(s); // strip off the hmm_ from the beginning actName = actName.Substring(4); bool isJointAction = Util.shouldUseJVals(actName); // Deserialize the model and add it to the correct classifier SerializableHmm serMod = new SerializableHmm(actName, modelDirectoryName); HiddenMarkovModel<MultivariateNormalDistribution> hmm = serMod.LoadFromDisk(); if (isJointAction) { jointModels.Add(hmm); jointClasses.Add(actName); } else { positionModels.Add(hmm); positionClasses.Add(actName); } } jointActionClassifier = new SequenceClassifier<MultivariateNormalDistribution>(jointModels.ToArray()); positionActionClassifier = new SequenceClassifier<MultivariateNormalDistribution>(positionModels.ToArray()); // TODO(pbrook) if there is time we should add a threshold model to these SequenceClassifiers } }
private void train(List<string> datafiles) { double[][][] sequences = new double[datafiles.Count][][]; // For each file string actName = ""; string dirName = ""; for (int i = 0; i < datafiles.Count; i++) { string fname = datafiles[i]; actName = System.IO.Path.GetFileNameWithoutExtension(fname); dirName = System.IO.Path.GetDirectoryName(fname); // Read the file List<HumanSkeleton> seq = new List<HumanSkeleton>(); using (StreamReader s = new StreamReader(fname)) { while (!s.EndOfStream) { seq.Add(new HumanSkeleton(s.ReadLine())); } } // convert it into an actionSequence of humanskeletons ActionSequence<HumanSkeleton> actSeq = new ActionSequence<HumanSkeleton>(seq); // Convert that actionSequence in to a double[][] bool useJointVals = Util.shouldUseJVals(actName); double[][] trainSeq = actSeq.toArray(useJointVals); // add that to the sequences array sequences[i] = trainSeq; } bool doTrain = true; if (doTrain) { if (datafiles.Count > 0) { //PrincipalComponentAnalysis pca = computePCA(sequences); // train a HMM //double[][][] projSeqs = getProjectedSequences(sequences, pca); HiddenMarkovModel<MultivariateNormalDistribution> hmm = trainHMM(sequences); // save it to filename.hmm //SerializableHmm s = new SerializableHmm(actName, hmm, pca); SerializableHmm s = new SerializableHmm(actName, hmm); s.SaveToDisk(); } } else { SerializableHmm ser = new SerializableHmm("wave right", MODEL_LIB_PATH); HiddenMarkovModel<MultivariateNormalDistribution> hmm = ser.LoadFromDisk(); PrincipalComponentAnalysis pca = ser.getPCA(); foreach (double[][] seq in sequences) { double[,] data = jaggedToMulti(seq); string fn = System.IO.Path.GetRandomFileName(); using (StreamWriter sr = new StreamWriter("Z:/WindowsFolders/Desktop/" + fn)) { for (int i = 0; i < data.GetLength(0); i++) { for (int j = 0; j < data.GetLength(1); j++) { sr.Write(data[i, j] + " "); } sr.WriteLine(); } } //double[][] projSeq = getProjectedSequence(seq, pca); double l = hmm.Evaluate(seq, false); Console.WriteLine("Likelihood: " + l); } } }