private Tuple<double[], double[], double[]> TrainModel(ModelSettings settings)
        {
            var data = GetConvNetSharpData();
            _images = data.Item1;
            _labels = data.Item2;
            var confusionMatrix = new int[settings.NoEpochs, 4, 4];
            var validationConfusionMatrix = new int[settings.NoEpochs, 4, 4];
            var threshold = (int)Math.Floor(0.9*_images.Count);
            for (var k = 0; k < settings.NoEpochs; k++)
            {
                for (var i = 0; i < threshold; i++)
                {
                    var image = _images[i];
                    var vol = BuilderInstance.Volume.From(_labels[i], new Shape(1, 1, 4, 1));
                    try
                    {
                        _trainer.Train(BuilderInstance.Volume.From(image, new Shape(80, 60, 1)), vol);
                    }
                    catch(ArgumentException)
                    {

                    }
                    var prediction = _trainer.Net.GetPrediction()[0];
                    confusionMatrix[k, LabelFromOneHot(_labels[i]), prediction]++;
                }
                for (var i = threshold; i < _images.Count; i++)
                {
                    var image = _images[i];
                    _trainer.Net.Forward(BuilderInstance.Volume.From(image, new Shape(80, 60, 1)));
                    var prediction = _trainer.Net.GetPrediction()[0];
                    validationConfusionMatrix[k, LabelFromOneHot(_labels[i]), prediction]++;
                }
            }
            return GetEpochsAndAccuracies(confusionMatrix, settings.NoEpochs, validationConfusionMatrix, threshold);
        }
 public Tuple<double[], double[], double[]> Train(ModelSettings settings)
 {
     _framework = settings.Framework;
     if (_framework == "keras")
         return TrainKerasModel(settings);
     else
         return TrainConvNetSharpModel(settings);
 }
 private Tuple<double[], double[], double[]> TrainKerasModel(ModelSettings settings)
 {
     var data = GetData();
     var xTrain = data.Item1.Take((int)Math.Floor(0.8 * data.Item1.Count));
     var yTrain = data.Item2.Take((int)Math.Floor(0.8 * data.Item2.Count));
     var xVal = data.Item1.TakeLast(data.Item1.Count - (int)Math.Floor(0.8 * data.Item1.Count));
     var yVal = data.Item2.TakeLast(data.Item2.Count - (int)Math.Floor(0.8 * data.Item2.Count));
     var xValidation = xVal.Take(xVal.Count() / 2);
     var yValidation = yVal.Take(yVal.Count() / 2);
     var history = _model.Fit(np.array(xTrain), np.array(yTrain), epochs: settings.NoEpochs, validation_data: new NDarray[]
         { np.array(xValidation), np.array(yValidation)});
     var accuracies = history.HistoryLogs["accuracy"];
     var valAccuracies = history.HistoryLogs["val_accuracy"];
     var epochs = new double[history.Epoch.Length];
     for (var i = 0; i < history.Epoch.Length; i++)
         epochs[i] = history.Epoch[i] + 1;
     return new Tuple<double[], double[], double[]>(epochs, accuracies, valAccuracies);
 }
 private Tuple<double[], double[], double[]> TrainConvNetSharpModel(ModelSettings settings)
 {
     EditTrainerSettings(settings);
     return TrainModel(settings);
 }
 private void EditTrainerSettings(ModelSettings settings)
 {
     _trainer.LearningRate = settings.LearningRate;
     _trainer.Momentum = 0.34;
 }