public ClassificationType[,] Classify(LasFile file) { var sw = Stopwatch.StartNew(); ClassificationType[,] output = new ClassificationType[_divisionCountX, _divisionCountY]; SubgroupOfPoints[,] values = Utills.GroupPoints(file, _divisionCountX, _divisionCountY); Statistics stats = new Statistics(); stats.Count = _divisionCountX * _divisionCountY; for (int i = 0; i < _divisionCountX; i++) { for (int j = 0; j < _divisionCountY; j++) { double avgHeight = values[i, j].avgHeight; double avgIntensity = values[i, j].avgIntensity; double avgDistance = values[i, j].avgDistance; OpenTK.Vector3 slopeVector = values[i, j].slopeVector; IMLData classed = Network.Compute(new BasicMLData( new double[] { avgDistance, avgHeight, avgIntensity, slopeVector[0], slopeVector[1], slopeVector[2] })); output[i, j] = Utills.QuickClassess[classed.IndexOfMax()]; ClassificationType ct; if (!Utills.QuickClassess.TryGetValue(values[i, j].classIndex, out ct)) { continue; } if (output[i, j] != ct) { stats.ClassErrors[(int)ct]++; } stats.PredictionMatrix[(int)output[i, j], (int)ct]++; stats.PredictionMatrix[(int)ct, (int)output[i, j]]++; stats.ClassCount[(int)output[i, j]]++; stats.ClassRealCount[(int)ct]++; } Console.WriteLine(i); } Console.Write(stats.ToString()); sw.Stop(); Console.WriteLine("Czas trwania [" + sw.Elapsed.TotalSeconds.ToString() + "s]"); stats.SaveMatrixAsCSV(); return(output); }
public static Tuple <double[], int> GetInputOutputFromClassificationType(LasPoint point, LasFile file) { var abc = LinearRegression.ComputeRegressionPoint(file, point, regressionCount, regressionRange); double distanceFromPlane = Utills.DistanceFromPlane(point, abc); LasPoint3Short pointShort = (LasPoint3Short)point; double green = pointShort.Green - (pointShort.Red + pointShort.Blue) / 2; var input = new double[] { green, file.LasHeader.ScaleZ(point.Z), point.Intensity, abc.X, abc.Y, abc.Z, distanceFromPlane }; int output; switch (point.Classification) { case LasPoint.ClassificationType.Ground: output = 0; break; case LasPoint.ClassificationType.HighVegetation: output = 1; break; case LasPoint.ClassificationType.Building: output = 2; break; case LasPoint.ClassificationType.MediumVegetation: output = 3; break; case LasPoint.ClassificationType.LowVegetation: output = 4; break; case LasPoint.ClassificationType.Water: output = 5; break; default: input = new double[] { -1, -1, -1, -1, -1, -1, -1 }; output = 6; break; } return(Tuple.Create(input, output)); }
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]"); }
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]"); }
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]"); }
public ClassificationType[,] Classify(LasFile file, int divCountX, int divCountY) { Stopwatch swTotal = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew(); Console.WriteLine("Preparing testing dataset..."); LasPointDataRecords points = file.LasPointDataRecords; ClassificationType[,] output = new ClassificationType[divCountX, divCountY]; SubgroupOfPoints[,] values = Utills.GroupPoints(file, divCountX, divCountY); Statistics stats = new Statistics(); stats.Count = divCountX * divCountY; sw.Stop(); Console.WriteLine("Preparing testing dataset completed [" + sw.Elapsed.TotalSeconds.ToString() + "s]"); Stopwatch sw2 = Stopwatch.StartNew(); Console.WriteLine("Classification in progress..."); int noiseCount = 0; for (int i = 0; i < divCountX; i++) { for (int j = 0; j < divCountY; j++) { if (values[i, j].classIndex == 7) { output[i, j] = ClassificationType.Noise; noiseCount++; } else { double avgHeight = values[i, j].avgHeight; double avgIntensity = values[i, j].avgIntensity; double avgDistance = values[i, j].avgDistance; OpenTK.Vector3 slopeVector = values[i, j].slopeVector; output[i, j] = Utills.ClassificationClasses[knn.Compute(new double[] { avgDistance, avgHeight, avgIntensity, slopeVector[0], slopeVector[1], slopeVector[2] })]; ClassificationType ct; if (!Utills.QuickClassess.TryGetValue(values[i, j].classIndex, out ct)) { continue; } if (output[i, j] != ct) { stats.ClassErrors[(int)ct]++; } stats.PredictionMatrix[(int)output[i, j], (int)ct]++; stats.PredictionMatrix[(int)ct, (int)output[i, j]]++; stats.ClassCount[(int)output[i, j]]++; stats.ClassRealCount[(int)ct]++; } } //Console.WriteLine(i); } Console.Write(stats.ToString()); sw2.Stop(); Console.WriteLine("Classification completed [" + sw2.Elapsed.TotalSeconds.ToString() + "s]"); swTotal.Stop(); Console.WriteLine("Total time: [" + swTotal.Elapsed.TotalSeconds.ToString() + "s]"); Console.WriteLine("Noise count: " + noiseCount.ToString()); stats.SaveMatrixAsCSV(); return(output); }