It is now possible to train expression models with probability support. This however is a lot slower! Also, the C parameter for SVM training was set incorrectly. Now training works much better.
This commit is contained in:
parent
9abc3029e7
commit
bb845098ca
4 changed files with 46 additions and 20 deletions
|
|
@ -25,6 +25,8 @@ public class OpenSeeExpression : MonoBehaviour
|
|||
public int overRecordingSkip = 5;
|
||||
[Tooltip("This is the filename used for loading and saving expression data using the load and save flags.")]
|
||||
public string filename = "";
|
||||
[Tooltip("When enabled, training will be done in such a way that the model can output probabilities. This is a lot slower!")]
|
||||
public bool enableProbabilityTraining = false;
|
||||
[Header("Toggles")]
|
||||
[Tooltip("When enabled, calibration data will be collected from the given OpenSee component.")]
|
||||
public bool recording = false;
|
||||
|
|
@ -54,6 +56,8 @@ public class OpenSeeExpression : MonoBehaviour
|
|||
public float expressionTime = 0f;
|
||||
[Tooltip("This is currently detected expression.")]
|
||||
public string expression = "";
|
||||
[Tooltip("These are the probabilities of all expressions. The labels in order can be retrieved through the GetClassLabels methods.")]
|
||||
public double[] probabilities;
|
||||
[Header("Training statistics")]
|
||||
[Tooltip("This shows the accuracy result of the last training run.")]
|
||||
public float accuracy = 0.0f;
|
||||
|
|
@ -362,7 +366,10 @@ public class OpenSeeExpression : MonoBehaviour
|
|||
i_test++;
|
||||
}
|
||||
}
|
||||
model.TrainModel(X_train, y_train, rows_train, cols);
|
||||
int probability = 0;
|
||||
if (enableProbabilityTraining)
|
||||
probability = 1;
|
||||
model.TrainModel(X_train, y_train, rows_train, cols, probability);
|
||||
confusionMatrix = model.ConfusionMatrix(X_test, y_test, i_test, out accuracy);
|
||||
confusionMatrixString = SVMModel.FormatMatrix(confusionMatrix, classLabels);
|
||||
for (int label = 0; label < classes; label++) {
|
||||
|
|
@ -399,7 +406,7 @@ public class OpenSeeExpression : MonoBehaviour
|
|||
float[] predictionData = new float[cols];
|
||||
for (int i = 0; i < cols; i++)
|
||||
predictionData[i] = faceData[indices[i]];
|
||||
float[] prediction = model.Predict(predictionData, 1);
|
||||
float[] prediction = model.Predict(predictionData, out probabilities, 1);
|
||||
int predictedExpression = (int)Mathf.Round(prediction[0]);
|
||||
if (predictedExpression == lastPrediction)
|
||||
lastPredictionCount++;
|
||||
|
|
@ -464,6 +471,13 @@ public class OpenSeeExpression : MonoBehaviour
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string[] GetClassLabels() {
|
||||
List<string> labels = new List<string>();
|
||||
foreach (string l in classLabels)
|
||||
labels.Add(String.Copy(l));
|
||||
return labels.ToArray();
|
||||
}
|
||||
|
||||
public string[] GetExpressions() {
|
||||
List<string> keys = new List<string>();
|
||||
|
|
|
|||
|
|
@ -12,16 +12,16 @@ namespace OpenSee {
|
|||
public class SVMModel {
|
||||
#region DllImport
|
||||
[DllImport("SVMModel", CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern System.IntPtr trainModel(float[] features, float[] labels, int rows, int cols, float C);
|
||||
private static extern System.IntPtr trainModel(float[] features, float[] labels, int rows, int cols, int classes, int probability, float C);
|
||||
|
||||
[DllImport("SVMModel", CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern void predict(System.IntPtr model, float[] features, [Out] float[] predictions, int rows);
|
||||
private static extern void predict(System.IntPtr model, float[] features, [Out] float[] predictions, [Out] double[] probabilities, int rows);
|
||||
|
||||
[DllImport("SVMModel", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||
private static extern System.IntPtr loadModel([MarshalAs(UnmanagedType.LPStr)]string filename, int cols, double[] means, double[] sdevs);
|
||||
private static extern System.IntPtr loadModel([MarshalAs(UnmanagedType.LPStr)]string filename, int cols, int classes, double[] means, double[] sdevs);
|
||||
|
||||
[DllImport("SVMModel", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||
private static extern System.IntPtr loadModelString([MarshalAs(UnmanagedType.LPStr)]string modelString, int cols, double[] means, double[] sdevs);
|
||||
private static extern System.IntPtr loadModelString([MarshalAs(UnmanagedType.LPStr)]string modelString, int cols, int classes, double[] means, double[] sdevs);
|
||||
|
||||
[DllImport("SVMModel", CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern void getScales(System.IntPtr model, [Out] double[] means, [Out] double[] sdevs);
|
||||
|
|
@ -63,7 +63,7 @@ public class SVMModel {
|
|||
}
|
||||
cols = smr.cols;
|
||||
maxClasses = smr.maxClasses;
|
||||
System.IntPtr model = loadModelString(smr.modelString, smr.cols, smr.means, smr.sdevs);
|
||||
System.IntPtr model = loadModelString(smr.modelString, smr.cols, smr.maxClasses, smr.means, smr.sdevs);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +118,7 @@ public class SVMModel {
|
|||
}
|
||||
|
||||
// features is a rows*cols long float array with rows written one after another. labels is a rows long float array with the corresponding classes as integral numbers from 0 to num_classes-1, where all classes have to exist. Training on more than 10000 rows is disabled.
|
||||
public bool TrainModel(float[] features, float[] labels, int rows, int cols) {
|
||||
public bool TrainModel(float[] features, float[] labels, int rows, int cols, int probability) {
|
||||
DestroyModel();
|
||||
if (rows > 10000)
|
||||
return false;
|
||||
|
|
@ -143,7 +143,7 @@ public class SVMModel {
|
|||
|
||||
maxClasses = max + 1;
|
||||
this.cols = cols;
|
||||
model = trainModel(features, labels, rows, cols, 12f);
|
||||
model = trainModel(features, labels, rows, cols, maxClasses, probability, 2f);
|
||||
haveModel = true;
|
||||
|
||||
return true;
|
||||
|
|
@ -163,7 +163,8 @@ public class SVMModel {
|
|||
}
|
||||
int[,] confusionMatrix = new int[maxClasses, maxClasses];
|
||||
float[] predictions = new float[rows];
|
||||
predict(model, features, predictions, rows);
|
||||
double[] probabilities = new double[rows * maxClasses];
|
||||
predict(model, features, predictions, probabilities, rows);
|
||||
for (int i = 0; i < rows; i++) {
|
||||
predictions[i] = (float)Math.Round(predictions[i]);
|
||||
if (predictions[i] >= 0 && predictions[i] < maxClasses) {
|
||||
|
|
@ -219,15 +220,22 @@ public class SVMModel {
|
|||
}
|
||||
|
||||
// features is a rows*cols long float array with rows written one after another.
|
||||
public float[] Predict(float[] features, int rows) {
|
||||
public float[] Predict(float[] features, out double[] probabilities, int rows) {
|
||||
probabilities = null;
|
||||
if (!haveModel)
|
||||
return null;
|
||||
if (rows < 1 || features.Length != rows * cols)
|
||||
return null;
|
||||
float[] predictions = new float[rows];
|
||||
predict(model, features, predictions, rows);
|
||||
probabilities = new double[rows * maxClasses];
|
||||
predict(model, features, predictions, probabilities, rows);
|
||||
return predictions;
|
||||
}
|
||||
|
||||
public float[] Predict(float[] features, int rows) {
|
||||
double[] probabilities;
|
||||
return Predict(features, out probabilities, rows);
|
||||
}
|
||||
|
||||
public bool Ready() {
|
||||
return haveModel;
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue