public void Run() { var network = new BasicNetwork(); network.AddLayer(new BasicLayer(null, true, 2)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1)); network.Structure.FinalizeStructure(); network.Reset(); IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal); IMLTrain train = new ResilientPropagation(network, trainingSet); int epoch = 1; do { train.Iteration(); Console.WriteLine(@"Epoch # " + epoch + @" Error: " + train.Error); epoch++; } while (train.Error > 0.01); train.FinishTraining(); Console.WriteLine(@"Neural Network Results: "); foreach (IMLDataPair pair in trainingSet) { IMLData output = network.Compute(pair.Input); Console.WriteLine(pair.Input[0] + @" , " + pair.Input[1] + @" , actual = " + output[0] + @" , ideal = " + pair.Ideal[0]); } EncogFramework.Instance.Shutdown(); }
public void train(List <Verse> verse) { // prepare input and ideal vectors // input <- ClassifiableText text vector // ideal <- positonValue vector // double[][] input = getInput(verse); double[][] ideal = getIdeal(verse); // train // Propagation train = new ResilientPropagation(network, new BasicMLDataSet(input, ideal)); train.ThreadCount = 16; do { train.Iteration(); notifyObservers("Training Classifier for '" + positon.getName() + "' positon. Errors: " + String.Format("%.2f", train.Error * 100) + "%. Wait..."); } while (train.Error > 0.01); train.FinishTraining(); notifyObservers("Classifier for '" + positon.getName() + "' positon trained. Wait..."); }
private void TrainNetwork(DateTime trainFrom, DateTime trainTo, TrainingStatus status) { if (_input == null || _ideal == null) { CreateTrainingSets(trainFrom, trainTo); } _trainThread = Thread.CurrentThread; int epoch = 1; ITrain train = null; try { var trainSet = new BasicNeuralDataSet(_input, _ideal); train = new ResilientPropagation(_network, trainSet); double error; do { train.Iteration(); error = train.Error; status?.Invoke(epoch, error, TrainingAlgorithm.Resilient); epoch++; } while (error > MaxError); } catch (ThreadAbortException) { _trainThread = null; } finally { train?.FinishTraining(); } _trainThread = null; }
private static EncogTrainingResponse TrainNetwork2(BasicNetwork network, TrainingData training, double maxError, CancellationToken cancelToken, double?maxSeconds = null) { //TODO: When the final layer is softmax, the error seems to be higher. Probably because the training outputs need to be run through softmax const int MAXITERATIONS = 5000; INeuralDataSet trainingSet = new BasicNeuralDataSet(training.Input, training.Output); ITrain train = new ResilientPropagation(network, trainingSet); DateTime startTime = DateTime.UtcNow; TimeSpan?maxTime = maxSeconds != null?TimeSpan.FromSeconds(maxSeconds.Value) : (TimeSpan?)null; bool success = false; //List<double> log = new List<double>(); int iteration = 1; double error = double.MaxValue; while (true) { if (cancelToken.IsCancellationRequested) { break; } train.Iteration(); error = train.Error; //log.Add(error); iteration++; if (double.IsNaN(error)) { break; } else if (error < maxError) { success = true; break; } else if (iteration >= MAXITERATIONS) { break; } else if (maxTime != null && DateTime.UtcNow - startTime > maxTime) { break; } } //string logExcel = string.Join("\r\n", log); // paste this into excel and chart it to see the error trend train.FinishTraining(); return(new EncogTrainingResponse(network, success, error, iteration, (DateTime.UtcNow - startTime).TotalSeconds)); }
void Train() { if (Memory.Count > 0) { network.Reset(); double[][] InputData = new double[Memory.Count][]; //подготовка данных для обучения сети double[][] SenseData = new double[Memory.Count][]; for (int i = 0; i < Memory.Count; i++) { InputData[i] = Memory[i]; SenseData[i] = MemorySense[i]; } IMLDataSet trainingSet = new BasicMLDataSet(InputData, SenseData); IMLTrain train = new ResilientPropagation(network, trainingSet); int epoch = 1; double old = 9999; double d = 999; do { try { train.Iteration(); } catch (Exception) { World.Remove(this); Life newlife = new Life(World, x, y); World.Add(newlife); break; } //Console.SetCursorPosition(0, 0); //вывод информации о текущем состоянии обучения //Console.Write(@"Epoch #" + epoch + @" Error:" + train.Error); epoch++; d = Math.Abs(old - train.Error); old = train.Error; } while (train.Error > 0.0001 && epoch < 3000 && d > 0.00001); train.FinishTraining(); //double sumd=0.0; //подсчет суммарной ошибки после обучения //foreach (IMLDataPair pair in trainingSet) //{ // IMLData output = network.Compute(pair.Input); // sumd = sumd + Math.Abs(pair.Ideal[0] - output[0]); // sumd = sumd / trainingSet.InputSize; //} } }
private void Train(BasicNetwork network, double[][] input, double[][] output) { IMLDataSet trainingSet = new BasicMLDataSet(input, output); IMLTrain train = new ResilientPropagation(network, trainingSet); int epoch = 1; do { train.Iteration(); epoch++; } while (train.Error > 0.04); train.FinishTraining(); }
private static void Train(BasicNetwork network) { // load all of the cards from the MagicAssistant library var cards = MtgDataLoader.GetAllCards(@"C:\Users\napol\MagicAssistantWorkspace\magiccards\MagicDB"); // build inputs and ideals var mtgData = new MtgDataLoader(cards); var input = mtgData.Inputs; var ideal = mtgData.Ideals; // create training data IMLDataSet trainingSet = new BasicMLDataSet(input, ideal); // train the neural network IMLTrain train = new ResilientPropagation(network, trainingSet); int epoch = 1; do { train.Iteration(); if (epoch % 10 == 0) { Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); } epoch++; // if we're not below 1% after 600 iterations (highly unlikely) just bow out. if (epoch > 1000) { break; } } while (train.Error > 0.01); train.FinishTraining(); Console.WriteLine($"Final Error: {train.Error}"); Console.WriteLine("Writing network to file"); if (System.IO.File.Exists("network.data")) { System.IO.File.Delete("network.data"); } Encog.Util.SerializeObject.Save("network.data", network); }
public int Train(DataSet dataSet) { Network = new BasicNetwork(); Network.AddLayer(new BasicLayer(null, true, 8 * 21)); var first = ((8 * 21 + 4) * FirstLayerParameter); Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)first)); var second = ((8 * 21 + 4) * SecondLayerParameter); Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)second)); Network.AddLayer(new BasicLayer(null, false, 1)); // Network.AddLayer(new ); Network.Structure.FinalizeStructure(); Network.Reset(); //IMLData x = new BasicNeuralData(); var set = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][]; var ideal = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][]; for (int i = 0; i < dataSet.Signatures.Count; i++) { set[i] = dataSet.Signatures[i].Data.Cast <double>().ToArray(); ideal[i] = new double[] { 1 }; } for (int i = dataSet.Signatures.Count; i < dataSet.Signatures.Count + dataSet.Forgeries.Count; i++) { set[i] = dataSet.Forgeries[i - dataSet.Signatures.Count].Data.Cast <double>().ToArray(); ideal[i] = new double[] { 0 }; } IMLDataSet trainingSet = new BasicMLDataSet(set, ideal); IMLTrain train = new ResilientPropagation(Network, trainingSet); int epoch = 1; var errors = new List <double>(); do { train.Iteration(); // Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); epoch++; errors.Add(train.Error); } while (epoch < 10000); train.FinishTraining(); return(1); }
static void Main(string[] args) { var network = new BasicNetwork(); network.AddLayer(new BasicLayer(2)); network.AddLayer(new BasicLayer(3)); network.AddLayer(new BasicLayer(1)); network.Structure.FinalizeStructure(); network.Reset(); var trainingDataSource = new CSVDataSource(@"Data\training.csv", true, ','); //var validationDataSource = new CSVDataSource(@"Data\validation.csv", true, ','); var trainingSet = new VersatileMLDataSet(trainingDataSource); //var validationSet = new VersatileMLDataSet(validationDataSource); trainingSet.Analyze(); trainingSet.Normalize(); var training = new ResilientPropagation(network, trainingSet); int epoch = 1; do { training.Iteration(); Console.WriteLine($"Epoch #{epoch}. Error: {training.Error}"); epoch++; }while (training.Error > 0.01); training.FinishTraining(); Console.WriteLine("Neural Network Results:"); foreach (var pair in trainingSet) { var output = network.Compute(pair.Input); Console.WriteLine($"{pair.Input[0]},{pair.Input[1]}, actual={output[0]}, ideal={pair.Ideal}"); } EncogFramework.Instance.Shutdown(); }
private static void Main(string[] args) { CSVReader reader = new CSVReader(); DataSet ds = reader.ReadCSVFile(FILENAME, true); dt = ds.Tables["Table1"]; // create a neural network, without using a factory var network = new BasicNetwork(); network.AddLayer(new BasicLayer(null, true, 17)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 2)); network.Structure.FinalizeStructure(); network.Reset(); Dictionarys dict = new Dictionarys(); // create training dat IMLDataSet dataSet = dict.GetDataSet(dt); // train the neural network IMLTrain train = new ResilientPropagation(network, dataSet); int epoch = 1; do { train.Iteration(); Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); epoch++; } while (train.Error > 0.01); train.FinishTraining(); // test the neural network Console.WriteLine(@"Neural Network Results:"); foreach (IMLDataPair pair in dataSet) { IMLData output = network.Compute(pair.Input); Console.WriteLine(pair.Input[0] + @"," + pair.Input[1] + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]); } EncogFramework.Instance.Shutdown(); }
public void Train(List <ClassifiableText> classifiableTexts) { // prepare input and ideal vectors // input <- ClassifiableText text vector // ideal <- characteristicValue vector // var input = GetInput(classifiableTexts); var ideal = GetIdeal(classifiableTexts); // train // Propagation train = new ResilientPropagation(_network, new BasicMLDataSet(input, ideal)); train.ThreadCount = 16; NeuroNetworkEventArgs neroMessage; // todo: throw exception if iteration count more than 1000 do { train.Iteration(); neroMessage = new NeuroNetworkEventArgs { Message = $@"Training Classifier for {_characteristic.Name} characteristic. Errors:{train.Error * 100:0.00}%." }; OnNeuroNetworkMessage(neroMessage); } while (train.Error > 0.01); train.FinishTraining(); neroMessage = new NeuroNetworkEventArgs { Message = $@"Classifier for {_characteristic.Name} characteristic trained. Wait..." }; OnNeuroNetworkMessage(neroMessage); }
public EncogNeuralNetworkQuick(LasFile file, int divisionCountX, int divisionCountY) { var sw = Stopwatch.StartNew(); _divisionCountX = divisionCountX; _divisionCountY = divisionCountY; var groupPointList = Utills.GroupPointsList(file, _divisionCountX, _divisionCountY); int count = groupPointList.Count / 5; double[][] input = new double[count][]; double[][] ideal = new double[count][]; int waterCount = 0; int groundCount = 0; int lowCount = 0; int mediumCount = 0; int highCount = 0; int buildingCount = 0; for (int i = 0; i < count; i++) { int rndNumber; ClassificationType simpleClass; while (true) { rndNumber = _rnd.Next(0, groupPointList.Count - 1); if (!Utills.QuickClassess.TryGetValue(groupPointList[rndNumber].classIndex, out simpleClass)) { continue; } if (simpleClass == LasPoint.ClassificationType.Water) { waterCount++; if (waterCount - 25 < count / 6) { break; } } else if (simpleClass == LasPoint.ClassificationType.Ground) { groundCount++; if (groundCount - 25 < count / 6) { break; } } else if (simpleClass == LasPoint.ClassificationType.Building) { buildingCount++; if (buildingCount - 25 < count / 6) { break; } } else if (simpleClass == LasPoint.ClassificationType.LowVegetation) { lowCount++; if (lowCount - 25 < count / 6) { break; } } else if (simpleClass == LasPoint.ClassificationType.MediumVegetation) { mediumCount++; if (mediumCount - 25 < count / 6) { break; } } else if (simpleClass == LasPoint.ClassificationType.HighVegetation) { highCount++; if (highCount - 25 < count / 6) { break; } } if (highCount > 5 * count) { highCount = 0; } if (buildingCount > 5 * count) { buildingCount = 0; } if (lowCount > 5 * count) { lowCount = 0; } if (mediumCount > 5 * count) { mediumCount = 0; } if (waterCount > 5 * count) { waterCount = 0; } if (groundCount > 5 * count) { groundCount = 0; } } if (i % 100 == 0) { Console.WriteLine(i); } double avgHeight = groupPointList[rndNumber].avgHeight; double avgIntensity = groupPointList[rndNumber].avgIntensity; double avgDistance = groupPointList[rndNumber].avgDistance; OpenTK.Vector3 slopeVector = groupPointList[rndNumber].slopeVector; input[i] = new double[] { avgDistance, avgHeight, avgIntensity, slopeVector[0], slopeVector[1], slopeVector[2] }; ideal[i] = Utills.ClassToVector(simpleClass); } inputNumber = input[0].Length; init(); IMLDataSet trainingSet = new BasicMLDataSet(input, ideal); IMLTrain train = new ResilientPropagation(Network, trainingSet); int epoch = 1; do { train.Iteration(); Console.WriteLine(train.Error + " | " + epoch); epoch++; } while (epoch < 1000); LearningError = train.Error; train.FinishTraining(); sw.Stop(); Console.WriteLine("Czas trwania [" + sw.Elapsed.TotalSeconds.ToString() + "s]"); }
/// <summary> // Train network /// </summary> /// <param name="status">Delegate to be invoked</param> /// <param name="trainFrom">Train from</param> /// <param name="trainTo">Train to</param> private void TrainNetwork(DateTime trainFrom, DateTime trainTo, TrainingStatus status) { if (_input == null || _ideal == null) { CreateTrainingSets(trainFrom, trainTo); /*Create training sets, according to input parameters*/ } _trainThread = Thread.CurrentThread; int epoch = 1; //LeanOverFitting.Init(); _startTime = DateTime.Now; listErr.Clear(); bool istoitu = true; ITrain train = null; try { var trainSet = new BasicNeuralDataSet(_input, _ideal); train = new ResilientPropagation(_network, trainSet); double error; do { // train.AddStrategy(); train.Iteration(); error = train.Error; if (status != null) { status.Invoke(epoch, error, TrainingAlgorithm.Resilient); } // if (LeanOverFitting.IsOverfilling(error)) // AbortTraining(); listErr.Add(new MyError { index = epoch, value = error }); if (epoch > 1500) { // istoitu = false; // break; } epoch++; } while (error > MAX_ERROR); } catch (ThreadAbortException) { /*Training aborted*/ if (_trainThread != null) { _trainThread.Abort(); } _trainThread = null; } finally { setTimeEnd(istoitu, epoch); if (train != null) { train.FinishTraining(); } } if (_trainThread != null) { _trainThread.Abort(); } _trainThread = null; }
public EncogNeuralNetworkSlow(LasFile file) { var sw = Stopwatch.StartNew(); int count = 300000; LasPointDataRecords points = file.LasPointDataRecords; double[][] input = new double[count][]; double[][] ideal = new double[count][]; int waterCount = 0; int groundCount = 0; int lowCount = 0; int mediumCount = 0; int highCount = 0; int buildingCount = 0; for (int i = 0; i < count; i++) { int rndNumber; while (true) { rndNumber = _rnd.Next(0, points.Count - 1); if (points[rndNumber].Classification == LasPoint.ClassificationType.Water) { waterCount++; if (waterCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.Ground) { groundCount++; if (groundCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.Building) { buildingCount++; if (buildingCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.LowVegetation) { lowCount++; if (lowCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.MediumVegetation) { mediumCount++; if (mediumCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.HighVegetation) { highCount++; if (highCount - 25 < count / 6) { break; } } if (highCount > 5 * count) { highCount = 0; } if (buildingCount > 5 * count) { buildingCount = 0; } if (lowCount > 5 * count) { lowCount = 0; } if (mediumCount > 5 * count) { mediumCount = 0; } if (waterCount > 5 * count) { waterCount = 0; } if (groundCount > 5 * count) { groundCount = 0; } } if (i % 1000 == 0) { Console.WriteLine("Selected point: " + i + "/" + count); } //double[] regression = LinearRegression.ComputeRegressionNumerics(file, points[rndNumber], regressionCount, regressionRange); OpenTK.Vector3 abc = LinearRegression.ComputeRegressionPoint(file, points[rndNumber], regressionCount, regressionRange); LasPoint3Short point = (LasPoint3Short)points[rndNumber]; double distanceFromPlane = Utills.DistanceFromPlane(point, abc); double green = point.Green - (point.Red + point.Blue) / 2; input[i] = new double[] { green, file.LasHeader.ScaleZ(point.Z), point.Intensity, abc.X, abc.Y, abc.Z, distanceFromPlane }; ideal[i] = Utills.ClassToVector(point.Classification); } inputNumber = input[0].Length; init(); IMLDataSet trainingSet = new BasicMLDataSet(input, ideal); IMLTrain train = new ResilientPropagation(Network, trainingSet); int epoch = 1; do { train.Iteration(); Console.WriteLine("Train error: " + train.Error + ", iteration: " + epoch); epoch++; } while (epoch < 1000); LearningError = train.Error; train.FinishTraining(); sw.Stop(); Console.WriteLine("Czas trwania [" + sw.Elapsed.TotalSeconds.ToString() + "s]"); }
//DUSANOV DEO private static void Main(string[] args) { //DUSANOV DEO Program p = new Program(); Console.WriteLine("PROGRAM POCEO"); //za stop znak Image stopSlika = Image.FromFile(@"...\stop.png"); Bitmap stopBit = new Bitmap(stopSlika); pikseliStop = p.ubaciUMatricu(pikseliStop, stopBit); Image stopSlika2 = Image.FromFile(@"...\stop2.png"); Bitmap stopBit2 = new Bitmap(stopSlika2); pikseliStop2 = p.ubaciUMatricu(pikseliStop2, stopBit2); Image stopSlika3 = Image.FromFile(@"...\stop3.jpg"); Bitmap stopBit3 = new Bitmap(stopSlika3); pikseliStop3 = p.ubaciUMatricu(pikseliStop3, stopBit3); Thread.Sleep(500); //za znak opasnosti Image znakopSlika = Image.FromFile(@"...\znak-opasnosti.jpg"); Bitmap znakopBit = new Bitmap(znakopSlika); pikseliZnakop = p.ubaciUMatricu(pikseliZnakop, znakopBit); Image znakopSlika2 = Image.FromFile(@"...\znak-opasnosti2.png"); Bitmap znakopBit2 = new Bitmap(znakopSlika2); pikseliZnakop2 = p.ubaciUMatricu(pikseliZnakop2, znakopBit2); Thread.Sleep(500); //za znak prvenstva Image prvenstvoSlika = Image.FromFile(@"...\prvenstvo.jpg"); Bitmap prvenstvoBit = new Bitmap(prvenstvoSlika); pikseliPrvenstvo = p.ubaciUMatricu(pikseliPrvenstvo, prvenstvoBit); Image prvenstvoSlika2 = Image.FromFile(@"...\prvenstvo2.png"); Bitmap prvenstvoBit2 = new Bitmap(prvenstvoSlika2); pikseliPrvenstvo2 = p.ubaciUMatricu(pikseliPrvenstvo2, prvenstvoBit2); Image prvenstvoSlika3 = Image.FromFile(@"...\prvenstvo3.png"); Bitmap prvenstvoBit3 = new Bitmap(prvenstvoSlika3); pikseliPrvenstvo3 = p.ubaciUMatricu(pikseliPrvenstvo3, prvenstvoBit3); Thread.Sleep(500); //za znak ogranicenja brzine Image brzinaSlika = Image.FromFile(@"...\ogranicenje-brzine.jpg"); Bitmap brzinaBit = new Bitmap(brzinaSlika); pikseliBrzina = p.ubaciUMatricu(pikseliBrzina, brzinaBit); Image brzinaSlika2 = Image.FromFile(@"...\ogranicenje-brzine2.jpg"); Bitmap brzinaBit2 = new Bitmap(brzinaSlika2); pikseliBrzina2 = p.ubaciUMatricu(pikseliBrzina2, brzinaBit2); Image brzinaSlika3 = Image.FromFile(@"...\ogranicenje-brzine3.jpg"); Bitmap brzinaBit3 = new Bitmap(brzinaSlika3); pikseliBrzina3 = p.ubaciUMatricu(pikseliBrzina3, brzinaBit3); double[] stopNiz = new double[stopBit.Height * stopBit.Width]; double[] stopNiz2 = new double[stopBit2.Height * stopBit2.Width]; double[] stopNiz3 = new double[stopBit3.Height * stopBit3.Width]; double[] znakopNiz = new double [znakopBit.Height * znakopBit.Width]; double[] znakopNiz2 = new double[znakopBit2.Height * znakopBit2.Width]; double[] prvenstvoNiz = new double [prvenstvoBit.Height * prvenstvoBit.Width]; double[] prvenstvoNiz2 = new double[prvenstvoBit2.Height * prvenstvoBit2.Width]; double[] prvenstvoNiz3 = new double[prvenstvoBit3.Height * prvenstvoBit3.Width]; double[] brzinaNiz = new double [brzinaBit.Height * brzinaBit.Width]; double[] brzinaNiz2 = new double[brzinaBit2.Height * brzinaBit2.Width]; double[] brzinaNiz3 = new double[brzinaBit3.Height * brzinaBit3.Width]; int uk = 0; for (int v = 0; v < stopBit.Height; v++) { for (int k = 0; k < stopBit.Width; k++) { stopNiz[uk] = pikseliStop[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < stopBit2.Height; v++) { for (int k = 0; k < stopBit2.Width; k++) { stopNiz2[uk] = pikseliStop2[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < stopBit3.Height; v++) { for (int k = 0; k < stopBit3.Width; k++) { stopNiz3[uk] = pikseliStop3[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < znakopBit.Height; v++) { for (int k = 0; k < znakopBit.Width; k++) { znakopNiz[uk] = pikseliZnakop[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < znakopBit2.Height; v++) { for (int k = 0; k < znakopBit2.Width; k++) { znakopNiz2[uk] = pikseliZnakop2[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < prvenstvoBit.Height; v++) { for (int k = 0; k < prvenstvoBit.Width; k++) { prvenstvoNiz[uk] = pikseliPrvenstvo[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < prvenstvoBit2.Height; v++) { for (int k = 0; k < prvenstvoBit2.Width; k++) { prvenstvoNiz2[uk] = pikseliPrvenstvo2[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < prvenstvoBit3.Height; v++) { for (int k = 0; k < prvenstvoBit3.Width; k++) { prvenstvoNiz3[uk] = pikseliPrvenstvo3[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < brzinaBit.Height; v++) { for (int k = 0; k < brzinaBit.Width; k++) { brzinaNiz[uk] = pikseliBrzina[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < brzinaBit2.Height; v++) { for (int k = 0; k < brzinaBit2.Width; k++) { brzinaNiz2[uk] = pikseliBrzina2[v, k]; uk++; } //uk++; } uk = 0; for (int v = 0; v < brzinaBit3.Height; v++) { for (int k = 0; k < brzinaBit3.Width; k++) { brzinaNiz3[uk] = pikseliBrzina3[v, k]; uk++; } //uk++; } double[][] znaciInput = { stopNiz, stopNiz2, stopNiz3, znakopNiz, znakopNiz2, prvenstvoNiz, prvenstvoNiz2, prvenstvoNiz3, brzinaNiz, brzinaNiz2, brzinaNiz3 }; double[][] znaciIdeal = { new[] { 1.0, 0, 0, 0 }, new[] { 1.0, 0, 0, 0 }, new[] { 1.0, 0, 0, 0 }, new[] { 0, 1.0, 0, 0 }, new[] { 0, 1.0, 0, 0 }, new[] { 0, 0, 1.0, 0 }, new[] { 0, 0, 1.0, 0 }, new[] { 0, 0, 1.0, 0 }, new[] { 0, 0, 0, 1.0 }, new[] { 0, 0, 0, 1.0 }, new[] { 0, 0, 0, 1.0 } }; //DUSANOV DEO //SMANJI SLIKE I MATRICE // create a neural network, without using a factory var network = new BasicNetwork(); //network.AddLayer(new BasicLayer(null, true, 2)); network.AddLayer(new BasicLayer(null, true, 25 * 25)); //network.AddLayer(new BasicLayer(new ActivationSigmoid(), true,1000)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 500)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 400)); // network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 400)); // network.AddLayer(new BasicLayer(new ActivationSigmoid(), true,400)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 200)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 100)); // network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 100)); // network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 100)); // network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 100)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 50)); //network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 25)); network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 4)); network.Structure.FinalizeStructure(); network.Reset(); // create training data IMLDataSet trainingSet = new BasicMLDataSet(znaciInput, znaciIdeal); // train the neural network IMLTrain train = new ResilientPropagation(network, trainingSet); int epoch = 1; do { train.Iteration(); Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error); epoch++; } while (train.Error > 0.001); train.FinishTraining(); // test the neural network Console.WriteLine(@"Neural Network Results:"); ///////////////////////////////////////////////// //TU STAVI NOVU MATRICU //DUSAN DODAO //za stop znak Image stopSlikaTest = Image.FromFile(@"...\stop_test.jpg"); Bitmap stopBitTest = new Bitmap(stopSlikaTest); pikseliStopTest = p.ubaciUMatricu(pikseliStopTest, stopBitTest); Thread.Sleep(500); //za znak opasnosti Image znakopSlikaTest = Image.FromFile(@"...\znak-opasnosti_test.png"); Bitmap znakopBitTest = new Bitmap(znakopSlikaTest); pikseliZnakopTest = p.ubaciUMatricu(pikseliZnakopTest, znakopBitTest); Thread.Sleep(500); //za znak prvenstva Image prvenstvoSlikaTest = Image.FromFile(@"...\prvenstvo_test.png"); Bitmap prvenstvoBitTest = new Bitmap(prvenstvoSlikaTest); pikseliPrvenstvoTest = p.ubaciUMatricu(pikseliPrvenstvoTest, prvenstvoBitTest); Thread.Sleep(500); //za znak ogranicenja brzine Image brzinaSlikaTest = Image.FromFile(@"...\ogranicenje-brzine_test.jpg"); Bitmap brzinaBitTest = new Bitmap(brzinaSlikaTest); pikseliBrzinaTest = p.ubaciUMatricu(pikseliBrzinaTest, brzinaBitTest); double[] stopNizTest = new double[stopBitTest.Height * stopBitTest.Width]; double[] znakopNizTest = new double[znakopBitTest.Height * znakopBitTest.Width]; double[] prvenstvoNizTest = new double[prvenstvoBitTest.Height * prvenstvoBitTest.Width]; double[] brzinaNizTest = new double[brzinaBitTest.Height * brzinaBitTest.Width]; int uk2 = 0; for (int v = 0; v < stopBitTest.Height; v++) { for (int k = 0; k < stopBitTest.Width; k++) { stopNizTest[uk2] = pikseliStopTest[v, k]; uk2++; } //uk++; } uk2 = 0; for (int v = 0; v < znakopBitTest.Height; v++) { for (int k = 0; k < znakopBitTest.Width; k++) { znakopNizTest[uk2] = pikseliZnakopTest[v, k]; uk2++; } //uk++; } uk2 = 0; for (int v = 0; v < prvenstvoBitTest.Height; v++) { for (int k = 0; k < prvenstvoBitTest.Width; k++) { prvenstvoNizTest[uk2] = pikseliPrvenstvoTest[v, k]; uk2++; } //uk++; } uk2 = 0; for (int v = 0; v < brzinaBitTest.Height; v++) { for (int k = 0; k < brzinaBitTest.Width; k++) { brzinaNizTest[uk2] = pikseliBrzinaTest[v, k]; uk2++; } //uk++; } double[][] znaciInput2 = { stopNizTest, znakopNizTest, prvenstvoNizTest, brzinaNizTest }; double[][] znaciIdeal2 = { new[] { 1.0, 0, 0, 0 }, new[] { 0, 1.0, 0, 0 }, new[] { 0, 0, 1.0, 0 }, new[] { 0, 0, 0, 1.0 } }; IMLDataSet trainingSet2 = new BasicMLDataSet(znaciInput2, znaciIdeal2); //DUSAN DODAO ///////////////////////////////////////////////// int znak = 0; foreach (IMLDataPair pair in trainingSet2) { IMLData output = network.Compute(pair.Input); //Console.WriteLine(pair.Input[0] + @"," + pair.Input[1] // + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0] ); if (znak == 0) { Console.WriteLine("test za znak stop: " + @" actual=" + output[0] + " " + output[1] + " " + output[2] + " " + output[3] + @",ideal=" + pair.Ideal[0] + " " + pair.Ideal[1] + " " + pair.Ideal[2] + " " + pair.Ideal[3]); } else if (znak == 1) { Console.WriteLine("test za znak opasnosti: " + @" actual=" + output[0] + " " + output[1] + " " + output[2] + " " + output[3] + @",ideal=" + pair.Ideal[0] + " " + pair.Ideal[1] + " " + pair.Ideal[2] + " " + pair.Ideal[3]); } else if (znak == 2) { Console.WriteLine("test za znak prvenstva: " + @" actual=" + output[0] + " " + output[1] + " " + output[2] + " " + output[3] + @",ideal=" + pair.Ideal[0] + " " + pair.Ideal[1] + " " + pair.Ideal[2] + " " + pair.Ideal[3]); } else if (znak == 3) { Console.WriteLine("test za znak brzine: " + @" actual=" + output[0] + " " + output[1] + " " + output[2] + " " + output[3] + @",ideal=" + pair.Ideal[0] + " " + pair.Ideal[1] + " " + pair.Ideal[2] + " " + pair.Ideal[3]); } znak++; } Console.ReadLine(); EncogFramework.Instance.Shutdown(); }
public EncogNeuralNetwork(LasFile file) { Stopwatch sw = Stopwatch.StartNew(); int count = 300000; LasPointDataRecords points = file.LasPointDataRecords; double[][] input = new double[count][]; double[][] ideal = new double[count][]; int waterCount = 0; int groundCount = 0; int lowCount = 0; int mediumCount = 0; int highCount = 0; int buildingCount = 0; for (int i = 0; i < count; i++) { int rndNumber; while (true) { rndNumber = _rnd.Next(0, points.Count - 1); if (points[rndNumber].Classification == LasPoint.ClassificationType.Water) { waterCount++; if (waterCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.Ground) { groundCount++; if (groundCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.Building) { buildingCount++; if (buildingCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.LowVegetation) { lowCount++; if (lowCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.MediumVegetation) { mediumCount++; if (mediumCount - 25 < count / 6) { break; } } else if (points[rndNumber].Classification == LasPoint.ClassificationType.HighVegetation) { highCount++; if (highCount - 25 < count / 6) { break; } } if (highCount > 5 * count) { highCount = 0; } if (buildingCount > 5 * count) { buildingCount = 0; } if (lowCount > 5 * count) { lowCount = 0; } if (mediumCount > 5 * count) { mediumCount = 0; } if (waterCount > 5 * count) { waterCount = 0; } if (groundCount > 5 * count) { groundCount = 0; } } if (i % 100 == 0) { Console.WriteLine(i); } LasPoint3Short point = (LasPoint3Short)points[rndNumber]; double green = point.Green - (point.Red + point.Blue) / 2; input[i] = new double[] { file.LasHeader.ScaleZ(point.Z), point.Intensity, green }; ideal[i] = Utills.ClassToVector(point.Classification); } inputNumber = input[0].Length; init(); IMLDataSet trainingSet = new BasicMLDataSet(input, ideal); IMLTrain train = new ResilientPropagation(Network, trainingSet); int epoch = 1; do { train.Iteration(); Console.WriteLine(train.Error + " | " + epoch); epoch++; } while (epoch < 1000); LearningError = train.Error; train.FinishTraining(); sw.Stop(); Console.WriteLine("Czas trwania [" + sw.Elapsed.TotalSeconds.ToString() + "s]"); }