예제 #1
0
        public override void Train(float[,] data, List <float> labels)
        {
            if (TrainedModel != null)
            {
                throw new InvalidOperationException("May only train/load a model once");
            }
            if (data.GetLength(0) != labels.Count)
            {
                throw new InvalidOperationException("Input data and label length must match");
            }

            var dataInput  = InputArray.Create <float>(data);
            var labelInput = InputArray.Create <float>(labels);

            TrainedModel = RTrees.Create();
            TrainedModel.Train(dataInput, SampleTypes.RowSample, labelInput);
        }
예제 #2
0
        // train
        public ModelOpenCV(List <ModelDataSet> input, ModelValue prediction)
        {
            if (input == null || input.Count == 0)
            {
                throw new Exception("Must have valid input");
            }

            // convert features into proper form
            var features = new float[input.Count, input[0].Features()];
            var labels   = new float[input.Count];

            for (int i = 0; i < input.Count; i++)
            {
                for (int j = 0; j < input[i].Features(); j++)
                {
                    features[i, j] = input[i].Feature(j);
                }

                switch (prediction)
                {
                case ModelValue.Action: labels[i] = input[i].Action; break;

                case ModelValue.Angle: labels[i] = input[i].FaceAngle; break;

                case ModelValue.XY: labels[i] = input[i].MoveAngle; break;

                default: throw new Exception("Unknown prediction type : " + prediction);
                }
            }

            // train
            var labelInput = InputArray.Create <float>(labels);
            var dataInput  = InputArray.Create <float>(features);

            TrainedModel = RTrees.Create();
            TrainedModel.RegressionAccuracy = 0.00001f;
            // RTrees.MaxDepath (r^2)
            //  default - action 0.3424, xy 0.1735, angle 0.2208
            //    5     -
            //   20     - action 0.6482, xy 0.5912, angle 0.6414 (new default)
            //  100     - action 0.6408, xy 0.5914, angle 0.6419
            TrainedModel.MaxDepth = 20;
            // RTress.MinSampleCount
            //  default(10) - see 20 above
            //     1        - actions 0.6625, xy 0.5077, angle 0.6376
            //    50        - actions 0.6464, xy 0.5627, angle 0.6217
            //TrainedModel.MinSampleCount = 1;

            // fails
            //TrainedModel = LogisticRegression.Create();

            //  fails
            // TrainedModel = DTrees.Create();

            // failed
            //TrainedModel = SVM.Create();
            //TrainedModel.KernelType = SVM.KernelTypes.Linear;
            //TrainedModel.Type = SVM.Types.NuSvr;
            //TrainedModel.C = 1;
            //TrainedModel.P = 0.01;
            //TrainedModel.Gamma = 10f;
            //TrainedModel.Degree = 0.1;
            //TrainedModel.Coef0 = 0;
            //TrainedModel.Nu = 0.1;

            TrainedModel.Train(dataInput, SampleTypes.RowSample, labelInput);
        }