} /* MakePredictions */ public static void MakePredictions(PicesFeatureVector featureVector, ref PicesPrediction model1Prediction1, ref PicesPrediction model1Prediction2, ref PicesPrediction model2Prediction1, ref PicesPrediction model2Prediction2, PicesRunLog runLog ) { if (model1Prediction1 == null) { model1Prediction1 = new PicesPrediction(null, 0, 0.0f); } if (model1Prediction2 == null) { model1Prediction2 = new PicesPrediction(null, 0, 0.0f); } if (model2Prediction1 == null) { model2Prediction1 = new PicesPrediction(null, 0, 0.0f); } if (model2Prediction2 == null) { model2Prediction2 = new PicesPrediction(null, 0, 0.0f); } if (model1 != null) { model1.PredictClass(featureVector, model1Prediction1, model1Prediction2); } if (model2 != null) { model2.PredictClass(featureVector, model2Prediction1, model2Prediction2); } } /* MakePredictions */
private PicesPredictionList PredictClasses(PicesFeatureVector fv, PicesPrediction pred1, PicesPrediction pred2, String imageFileName, String imageFileRootName ) { PicesFeatureVector fvToUse = fv; PicesPredictionList probPredList = null; if (fvToUse.FeatureDataMissing) { fvToUse = null; // Feature data is missing. we need to compute from original image. PicesDataBaseImage pdbi = DbConn().ImageLoad(imageFileRootName); if (pdbi != null) { PicesRaster pr = DbConn().ImageFullSizeFind(imageFileRootName); if (pr == null) { ErrorMsgsAdd("Failed to Load FullSize image [" + imageFileRootName + "]"); } else { fvToUse = new PicesFeatureVector(pr, imageFileName, null, runLog); // Since we had to compute the FeatureDatya from the raster image we now need to // get the instrument data that matches it. PicesInstrumentData id = DbConn().InstrumentDataGetByScanLine(pdbi.SipperFileName, pdbi.TopLeftRow); if (id != null) { fvToUse.AddInstrumentData(id); } DbConn().FeatureDataInsertRow(pdbi.SipperFileName, fvToUse); pr.Dispose(); } pr = null; } } if (fvToUse != null) { classifier.PredictClass(fvToUse, pred1, pred2); probPredList = classifier.PredictProbabilities(fvToUse); } else { pred1.MLClass = PicesClassList.GetUnKnownClassStatic(); pred1.Probability = 1.0f; pred2.MLClass = PicesClassList.GetUnKnownClassStatic(); pred2.Probability = 0.0f; } return(probPredList); } /* PredictClasses */
} /* LoadData */ private void PerformPredictions() { PicesPrediction p1 = new PicesPrediction(null, 0, 0.0f); PicesPrediction p2 = new PicesPrediction(null, 0, 0.0f); foreach (PicesFeatureVector fv in groundTruth) { PicesClass knownClass = fv.MLClass; if (cancelProcessing) { break; } trainingModel.PredictClass(fv, p1, p2); PicesClass predClass = p1.MLClass; confusionMatrix.MakePrediction(knownClass, predClass, p1.Probability, fv, trainingModel); numGroundTruthImagesProcessed++; } } /* PerformPredictions*/
private void ClassInitialization() { sizeDistributionDown = new SizeDistribution2(0.1f, 1.2f, 10.0f); sizeDistributionUp = new SizeDistribution2(0.1f, 1.2f, 10.0f); depthDistribution_1 = new SizeDistribution(500, 1); depthDistribution_1Down = new SizeDistribution(500, 1); depthDistribution_1Up = new SizeDistribution(500, 1); depthDistribution_10 = new SizeDistribution(50, 10); classCounts = new ClassStatSortedList(); numImagesClassified = 0; pred1 = new PicesPrediction(null, 0, 0.0f); pred2 = new PicesPrediction(null, 0, 0.0f); if (classifier != null) { classesInClassifier = classifier.MLClasses(); } }
} /* MakePredictions */ public static void MakePredictions(PicesRaster raster, ref PicesPrediction model1Prediction1, ref PicesPrediction model1Prediction2, ref PicesPrediction model2Prediction1, ref PicesPrediction model2Prediction2, PicesRunLog runLog ) { String imageFileName = raster.FileName; ParsedImageFileName pifn = PicesFeatureVector.ParseImageFileName(imageFileName); String sipperFileName = pifn.sipperFileName; uint scanLineNum = pifn.scanLineNum; PicesFeatureVector fv = new PicesFeatureVector(raster, imageFileName, null, runLog); { PicesDataBase dbConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog); PicesInstrumentData pid = dbConn.InstrumentDataGetByScanLine(sipperFileName, scanLineNum); if (pid != null) { fv.AddInstrumentData(pid); } pid = null; dbConn.Close(); dbConn = null; } MakePredictions(fv, ref model1Prediction1, ref model1Prediction2, ref model2Prediction1, ref model2Prediction2, runLog ); fv = null; }
} /* UpdatedataFields*/ private void MakePredictions() { if (!ActiveTrainingLibraries.ModelsAreAvailable()) { // Since there are NO training models loaded; there is no point doing the work for a prediction. return; } featureVector = dbConn.FeatureDataRecLoad(image); if (featureVector == null) { featureVector = new PicesFeatureVector(raster, image.ImageFileName, null, runLog); // Since we had to compute the FeatureDatya from the raster we now need to // get the instrument data that matches it. if (instrumentData != null) { featureVector.AddInstrumentData(instrumentData); } dbConn.FeatureDataInsertRow(image.SipperFileName, featureVector); } float esd = 0.0f; float eBv = 0.0f; float filledArea = image.PixelCount; float cropWidth = 3800.0f; // (3900.0f - 200.0f); float flowRate2 = 0.5f; if (featureVector != null) { filledArea = featureVector.FilledArea; } if (sipperFile != null) { scanRate = sipperFile.ScanRate; } esd = (float)(2.0 * Math.Sqrt(filledArea * (0.096 / cropWidth) * 1000.0 * (flowRate1 / sipperFile.ScanRate) * 1000.0 / 3.1415926)); eBv = (float)((4.0 / 3.0) * Math.PI * Math.Pow(Math.Sqrt(filledArea * (chamberWidth / cropWidth) * 1000 * (flowRate1 / scanRate) * 1000.0 / Math.PI), 3)); PicesPrediction model1Prediction1 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model1Prediction2 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model2Prediction1 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model2Prediction2 = new PicesPrediction(null, 0, 0.0f); ActiveTrainingLibraries.MakePredictions(featureVector, ref model1Prediction1, ref model1Prediction2, ref model2Prediction1, ref model2Prediction2, runLog ); if (model1Prediction1 != null) { Lib1Pred1Class.Text = model1Prediction1.ClassName; Lib1Pred1Prob.Text = model1Prediction1.Probability.ToString("##0.00%"); Lib1Pred1Votes.Text = model1Prediction1.Votes.ToString("#,##0"); } if (model1Prediction2 != null) { Lib1Pred2Class.Text = model1Prediction2.ClassName; Lib1Pred2Prob.Text = model1Prediction2.Probability.ToString("##0.00%"); Lib1Pred2Votes.Text = model1Prediction2.Votes.ToString("#,##0"); } if (model2Prediction1 != null) { Lib2Pred1Class.Text = model2Prediction1.ClassName; Lib2Pred1Prob.Text = model2Prediction1.Probability.ToString("##0.00%"); Lib2Pred1Votes.Text = model2Prediction1.Votes.ToString("#,##0"); } if (model1Prediction2 != null) { Lib2Pred2Class.Text = model2Prediction2.ClassName; Lib2Pred2Prob.Text = model2Prediction2.Probability.ToString("##0.00%"); Lib2Pred2Votes.Text = model2Prediction2.Votes.ToString("#,##0"); } if (featureVector != null) { AreaMMSquare.Text = featureVector.AreaMMSquare.ToString("#,##0.000"); } ESD.Text = esd.ToString("#,##0.00"); EBv.Text = eBv.ToString("##0.0000"); return; } /* MakePredictions */
} /* GradeAgainstCrossValidationRun */ void CrossValidate(PicesFeatureVectorList testExamples, PicesFeatureVectorList trainingExamples, int foldNum ) { msgQueue.AddMsg("\n\n" + "Starting Fold[" + (foldNum + 1).ToString() + "] of Fold[" + numOfFolds + "]" + "\n"); if (trainingModel != null) { trainingModel = null; } msgQueue.AddMsg("Building Classifier"); try { trainingModel = new TrainingModel2(runLog, curSelModel); trainingModel.BuildTrainingModel(trainingExamples); } catch (Exception e) { msgQueue.AddMsg("\n\n\n" + "Exception occur building training model" + "\n" + e.ToString() + "\n\n"); MessageBox.Show(this, "Exception Occurred Building Training Model." + "\n" + e.ToString(), "CrossValidate", MessageBoxButtons.OK); trainingModel = null; return; } if (cancelProcessing) { trainingModel = null; return; } if (!trainingModel.Valid) { MessageBox.Show(this, "Training Model is Invalid.", "Ground Thruthing", MessageBoxButtons.OK); } else { msgQueue.AddMsg("\n" + "Predictions for fold[" + (foldNum + 1).ToString() + "]"); PicesClass knownClass = null; foreach (PicesFeatureVector example in testExamples) { if (cancelProcessing) { break; } knownClass = example.MLClass; PicesPrediction prediction = trainingModel.PredictClass(example); if ((otherClass != null) && (prediction.MLClass == noAgreementClass)) { prediction.MLClass = otherClass; } /**@todo Add code for "NoAgreement:" to be treeted as a Other Class when there is "NoAgreement". */ confusionMatrix.MakePrediction(knownClass, prediction.MLClass, prediction.Probability, example, trainingModel); } } trainingModel.CleanUp(); trainingModel = null; } /* CrossValidate */
} /* PerformAllOperations */ private void MakePredictions() { pred1ClassName.Text = ""; pred2ClassName.Text = ""; BlobDetector bd = new BlobDetector(raster, 20); BlobList blobs = bd.ExtractBlobs(); if ((blobs == null) || (blobs.Count < 1)) { return; } Blob largestBlob = blobs.LocateLargestBlob(); if (largestBlob == null) { return; } byte[][] largestRaster = bd.ExtractedBlobImage(largestBlob); PicesRunLog runLog = new PicesRunLog(); PicesRaster picesRaster = new PicesRaster(largestRaster); //PicesPredictionList trainLibrary1Predictions = null; //PicesPredictionList trainLibrary2Predictions = null; //ActiveTrainingLibraries.MakePredictions (picesRaster, // instrumentData, // imageFileName, // ref trainLibrary1Predictions, // ref trainLibrary2Predictions, // runLog // ); picesRaster.FileName = imageFileName; PicesPrediction model1Prediction1 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model1Prediction2 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model2Prediction1 = new PicesPrediction(null, 0, 0.0f); PicesPrediction model2Prediction2 = new PicesPrediction(null, 0, 0.0f); ActiveTrainingLibraries.MakePredictions(picesRaster, ref model1Prediction1, ref model1Prediction2, ref model2Prediction1, ref model2Prediction2, runLog ); if (model1Prediction1 != null) { TrainLib1Name.Text = ActiveTrainingLibraries.Model1Name; pred1Prob.Text = model1Prediction1.Probability.ToString("p"); pred1ClassName.Text = model1Prediction1.ClassName; this.Text = model1Prediction1.ClassName; if (model1Prediction2 != null) { pred2Prob.Text = model1Prediction2.Probability.ToString("p"); pred2ClassName.Text = model1Prediction2.ClassName; } } if (model2Prediction1 != null) { TrainLib2Name.Text = ActiveTrainingLibraries.Model2Name; lib2Pred1Prob.Text = model2Prediction1.Probability.ToString("p"); lib2Pred1ClassName.Text = model2Prediction1.ClassName; if (model2Prediction2 != null) { lib2Pred2Prob.Text = model2Prediction2.Probability.ToString("p"); lib2Pred2ClassName.Text = model2Prediction2.ClassName; } } picesRaster = null; blobs.Dispose(); largestBlob.Dispose(); bd.Dispose(); bd = null; } /* MakePredictions */