Beispiel #1
0
        } /* ExploreMatrixCell */

        public void  MakePrediction(PicesClass knownClass,
                                    PicesClass predictedClass,
                                    double predProb,
                                    PicesFeatureVector fv,
                                    TrainingModel2 trainModel
                                    )
        {
            bool partOfOriginalClassifier = ClassInClassifierClasses(knownClass);

            if (!partOfOriginalClassifier)
            {
                String notPartClassName = "(V)" + knownClass.Name;
                knownClass = PicesClassList.GetUniqueClass(notPartClassName, "");
            }

            int x = classes.LookUpIndex(knownClass);

            if (x < 0)
            {
                classes.Add(knownClass);
            }

            x = classes.LookUpIndex(predictedClass);
            if (x < 0)
            {
                classes.Add(predictedClass);
            }

            Prediction p = new Prediction(knownClass, predictedClass, predProb, fv, trainModel, partOfOriginalClassifier);

            predictions.Add(p);
        } /* featureVector */
        } /* UpdateButton_Click */

        private void  LoadTrainingModelNames()
        {
            string[] modelNames = TrainingModel2.GetListOfTrainingModels();;
            if (modelNames == null)
            {
                TrainingLibrary1.Items.Add("None");
                TrainingLibrary2.Items.Add("None");
            }
            else
            {
                foreach (string s in modelNames)
                {
                    TrainingLibrary1.Items.Add(s);
                    TrainingLibrary2.Items.Add(s);
                }
            }

            if (!String.IsNullOrEmpty(activeTrainingModel1Name))
            {
                SetTrainingLibrarySelected(activeTrainingModel1Name, TrainingLibrary1);
            }

            if (!String.IsNullOrEmpty(activeTrainingModel2Name))
            {
                SetTrainingLibrarySelected(activeTrainingModel2Name, TrainingLibrary2);
            }
        } /* LoadTrainingModelNames */
        } /* LoadTrainingModels */

        private void  LoadData()
        {
            loadingThreadRunning = true;

            if (trainingModel != null)
            {
                BlockStart();
                trainingModel = null;
                BlockEnd();
            }

            if (groundTruth != null)
            {
                BlockStart();
                groundTruth = null;
                BlockEnd();
            }

            if (crossValidation)
            {
                GradeAgainstCrossValidationRun();
            }
            else
            {
                GradeAgainstGroundTruth();
            }

            if (!cancelProcessing)
            {
                displayConfusionMatrix = true;
            }

            loadingThreadRunning = false;
        } /* LoadData */
Beispiel #4
0
        public ClassifierProcess(PicesDataBaseServer _dbServer,
                                 int _processorNum,
                                 String _classifierName,
                                 bool _reClassify,
                                 bool _validatedTrumps,
                                 bool _excludeNoise,
                                 Queue <PredictionResult> _predictionUpdateQueue,
                                 Queue <PicesFeatureVector> _featureVectorQueue,
                                 DateTime _midPoint,
                                 Queue <String> _errorMsgs
                                 )
        {
            errorMsgs             = _errorMsgs;
            processorNum          = _processorNum;
            classifier            = null;
            classifierName        = _classifierName;
            reClassify            = _reClassify;
            validatedTrumps       = _validatedTrumps;
            excludeNoise          = _excludeNoise;
            predictionUpdateQueue = _predictionUpdateQueue;
            featureVectorQueue    = _featureVectorQueue;
            dbServer = _dbServer;
            midPoint = _midPoint;

            classifierWeOwnIt = false;

            ClassInitialization();
        }
Beispiel #5
0
        public ConfusionMatrix(PicesTrainingConfigManaged _config,
                               PicesClassList _classifierClasses,            /**< List of classes in origianl Classifier before reduction due to training level. */
                               TrainingModel2 _trainingModel,
                               String _modelName,
                               String _groundTruthRootDir,
                               uint _trainLevel,
                               uint _predLevel
                               )
        {
            config             = _config;
            trainingModel      = _trainingModel;
            groundTruthRootDir = _groundTruthRootDir;
            trainLevel         = _trainLevel;
            predLevel          = _predLevel;
            classifierClasses  = _classifierClasses;

            if (config != null)
            {
                otherClass = config.OtherClass();
            }

            InitializeComponent();
            classes = new PicesClassList();
            classes.Clear();

            ModelName.Text   = _modelName;
            GroundTruth.Text = _groundTruthRootDir;
        }
        public PredictionBreakDownDisplay(PicesDataBase _dbConn,
                                          PicesDataBaseImage _image,
                                          PicesRaster _raster,
                                          TrainingModel2 _trainingModel,
                                          PicesInstrumentData _instrumentData, /*!<  Instrument data that goes with image, if null will read from database. */
                                          PicesRunLog _runLog
                                          )
        {
            if (_runLog == null)
            {
                runLog = new PicesRunLog();
            }
            else
            {
                runLog = _runLog;
            }

            if (_image == null)
            {
                return;
            }

            fileName       = _image.ImageFileName;
            instrumentData = _instrumentData;
            trainingModel  = _trainingModel;

            PicesRaster raster = _raster;

            if (_dbConn != null)
            {
                if (raster == null)
                {
                    raster = _dbConn.ImageFullSizeFind(fileName);
                }

                if (instrumentData == null)
                {
                    instrumentData = _dbConn.InstrumentDataGetByScanLine(_image.SipperFileName, _image.TopLeftRow);
                }

                dbFeatureVector = _dbConn.FeatureDataRecLoad(fileName);
            }

            if (raster == null)
            {
                return;
            }

            //raster = raster.ConnectedComponent ();

            featureCalcImages = new PicesRasterList();
            featureVector     = new PicesFeatureVector(raster, fileName, featureCalcImages, runLog);
            if (instrumentData != null)
            {
                featureVector.AddInstrumentData(instrumentData);
            }

            InitializeComponent();
        }
Beispiel #7
0
        public void  SetTrainingModel(TrainingModel2 _trainingModel)
        {
            trainingModel = _trainingModel;

            if (otherClass == null)
            {
                otherClass = PicesClassList.GetUniqueClass("Other", "");
            }
        }
        public static TrainingModel2  Model2()
        {
            TrainingModel2 model = null;

            BlockStart();
            model = model2;
            BlockEnd();
            return(model);
        }
Beispiel #9
0
        public ImageListDisplay(String _rootDir,
                                Dictionary <String, Prediction> _predictions,
                                TrainingModel2 _trainingModel
                                )
        {
            predictions   = _predictions;
            rootDir       = _rootDir;
            trainingModel = _trainingModel;

            InitializeComponent();
        }
        public PredictionBreakDownDisplayDual(PicesDataBase _dbConn,
                                              String _fileName,
                                              TrainingModel2 _trainingModel,
                                              PicesInstrumentData _instrumentData, /**<  Instrument data that goes with image,  if null will read from dtabase. */
                                              PicesRunLog _runLog
                                              )
        {
            if (_runLog == null)
            {
                runLog = new PicesRunLog();
            }
            else
            {
                runLog = _runLog;
            }

            fileName       = _fileName;
            instrumentData = _instrumentData;
            trainingModel  = _trainingModel;

            PicesDataBase dbConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);
            PicesRaster   raster = dbConn.ImageFullSizeFind(fileName);

            if (raster == null)
            {
                return;
            }

            if (_dbConn != null)
            {
                dbFeatureVector = _dbConn.FeatureDataRecLoad(_fileName);
                if (instrumentData == null)
                {
                    PicesDataBaseImage image = dbConn.ImageLoad(fileName);
                    if (image != null)
                    {
                        instrumentData = dbConn.InstrumentDataGetByScanLine(image.SipperFileName, image.TopLeftRow);
                    }
                }
            }

            featureVector = new PicesFeatureVector(raster, fileName, null, runLog);

            if (instrumentData != null)
            {
                featureVector.AddInstrumentData(instrumentData);
            }

            dbConn.Close();
            dbConn = null;
            GC.Collect();

            InitializeComponent();
        }
        } /* SetConfigurationFileName */

        public static void  LoadConfiguration()
        {
            BlockStart();

            SetConfigurationFileName();

            model1 = null;
            model2 = null;

            StreamReader sr = null;

            try  { sr = new StreamReader(configFileName); }  catch (Exception)  { sr = null; }
            if (sr != null)
            {
                String line = "";

                while ((line = sr.ReadLine()) != null)
                {
                    String[] fields = line.Split('\t', '=');
                    if (fields.Length < 2)
                    {
                        continue;
                    }

                    String fieldName = fields[0].ToLower();
                    if (fieldName == "model1name")
                    {
                        model1Name = fields[1];
                    }

                    else if (fieldName == "model2name")
                    {
                        model2Name = fields[1];
                    }
                }

                sr.Close();
                sr = null;

                //if  (!String.IsNullOrEmpty (model1Name))
                //  SetModel1 (model1Name, false, null);

                //if  (!String.IsNullOrEmpty (model2Name))
                //  SetModel2 (model2Name, false, null);
            }

            configLoaded = true;

            BlockEnd();
        } /* LoadConfiguration */
Beispiel #12
0
 Prediction(PicesClass _knownClass,
            PicesClass _predClass,
            double _prob,
            PicesFeatureVector _fv,
            TrainingModel2 _trainModel,
            bool _partOfClassifier
            )
 {
     knowClass        = _knownClass;
     predClass        = _predClass;
     prob             = _prob;
     fv               = _fv;
     trainModel       = _trainModel;
     partOfClassifier = _partOfClassifier;
 }
        private void CreateFeatureSelectionDataSets_Load(object sender, EventArgs e)
        {
            modelNames = TrainingModel2.GetListOfTrainingModels();
            if (modelNames != null)
            {
                foreach (String s in modelNames)
                {
                    ModelName.Items.Add(s);
                }
            }

            ModelName.Text = initialModelName;

            UpdatePercentageFields();
            DestinationDirectory.Text = OSservices.AddSlash(PicesSipperVariables.DataFilesDirectory()) + "FeatureSelection";
        }
        } /* Model1LoadCompletion */

        static private void  Model2LoadCompletion(TrainingModelStatus sender)
        {
            if (!sender.ValidModel())
            {
                model2 = null;
            }
            else
            {
                model2 = sender.TrainiedModel();
            }

            model2LoadStatus = null;
            if (doneLoadingModel2Event != null)
            {
                doneLoadingModel2Event(model2);
            }
        } /* Model2LoadCompletion */
        } /* LoadConfiguration */

        public static void  SetModel1(String _model1Name,
                                      bool _forceRebuild,
                                      DoneLoadingEvent _doneLoadingEvent
                                      )
        {
            bool model1CurentlyLoading = false;

            BlockStart();

            if (model1LoadStatus != null)
            {
                model1CurentlyLoading = true;
            }
            else
            {
                if ((model1 != null) && (model1Name == _model1Name) && (!_forceRebuild))
                {
                    // We already have this model loaded;  there is nothing to do.
                    // But we still have to the call back to let it know that the model is loaded.
                    if (_doneLoadingEvent != null)
                    {
                        _doneLoadingEvent(model1);
                    }
                }
                else
                {
                    if (model1 != null)
                    {
                        model1 = null;
                        GC.Collect();
                    }
                    GC.Collect();

                    Model1LoadStart(_model1Name, _forceRebuild, _doneLoadingEvent);
                    SaveConfiguration();
                }
            }

            BlockEnd();

            if (model1CurentlyLoading)
            {
                MessageBox.Show("Can not set a new Model;  one is currently loading now.");
                return;
            }
        } /* SetModel1 */
Beispiel #16
0
        private void  LoadTrainingModel()
        {
            trainingModelRunning = true;
            PicesDataBase.ThreadInit();

            if (trainingModel != null)
            {
                trainingModel = null;
                GC.Collect();
            }

            trainingModel            = new TrainingModel2(runLog, trainingModelName);
            trainingModelLogFileName = trainingModel.RunLogFileName;
            trainingModel.AttachMsgQueueToRunLog(logMessages);

            if (forceRebuild)
            {
                trainingModel.LoadTrainigLibrary(true); // true = Force Rebuild of Model
            }
            else
            {
                trainingModel.LoadExistingModelOtherwiseBuild(null);
            }

            trainingModel.AttachMsgQueueToRunLog(null);

            if (trainingModel.Valid)
            {
                validModel = true;
            }
            else
            {
                List <String> errMsgs = trainingModel.ErrorMsgs;
                if (errMsgs != null)
                {
                    logMessages.AddMsgs(errMsgs);
                }
                validModel    = false;
                trainingModel = null;
            }

            PicesDataBase.ThreadEnd();
            trainingModelRunning = false;
        } /* LoadTrainingModel */
Beispiel #17
0
        } /* LoadPlanktonImage */

        private void  AddImageToTraningLibraries()
        {
            if (!ActiveTrainingLibraries.ModelsAreAvailable())
            {
                MessageBox.Show("There are no selected Training Models to add Image to.");
                return;
            }

            if (image.ValidatedClass == null)
            {
                MessageBox.Show("No Validated Class Selected.");
                return;
            }

            TrainingModel2 tm1 = ActiveTrainingLibraries.Model1();

            if (tm1 == null)
            {
                tm1 = ActiveTrainingLibraries.Model2();
            }

            if (tm1 == null)
            {
                MessageBox.Show("No active training library specified.");
                return;
            }

            if (!tm1.IncludesClass(image.ValidatedClass))
            {
                MessageBox.Show("Training Model[" + tm1.ModelName + "]  does not include a definition for class[" + image.ValidatedClass.Name + "].", "ImageViewer.AddImageToTraningLibraries");
            }
            else
            {
                try
                {
                    tm1.AddImageToTrainingLibray(image.ImageFileName, raster, image.ValidatedClass, true);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error adding image to TrainingModel2[" + tm1.ModelName + "]" + "\n\n" + e.ToString(), "ImageViewer.AddImageToTraningLibraries");
                }
            }
        } /* AddImageToTraningLibraries */
Beispiel #18
0
        } /* GetNextFeatureVector*/

        private void  ThreadProcess()
        {
            threadRunning = true;
            PicesDataBase.ThreadInit();


            if ((classifier == null) && (!String.IsNullOrEmpty(classifierName)))
            {
                PicesRunLog log = new PicesRunLog();
                classifier = new TrainingModel2(log, classifierName);
                classifier.LoadExistingModelOtherwiseBuild(null);
                classifierWeOwnIt = true;
            }

            while (!cancelFlag)
            {
                PicesFeatureVector fv = GetNextFeatureVector();
                if (fv != null)
                {
                    ProcessOneFeatureVector(fv);
                }
                else
                {
                    if (threadTerminate)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
            }

            if (dbConn != null)
            {
                dbConn.Close();
                dbConn = null;
                GC.Collect();
            }

            PicesDataBase.ThreadEnd();

            threadRunning = false;
            threadDone    = true;
        } /* ThreadProcess */
Beispiel #19
0
        } /* BlockEnd */

        private void  CleanUpMemory()
        {
            if (loadingThreadRunning)
            {
                CancelProcessing();
            }

            if (confusionMatrix != null)
            {
                confusionMatrix.Close();
                confusionMatrix = null;
            }

            if (trainingModel != null)
            {
                BlockStart();
                trainingModel = null;
                BlockEnd();
            }
        } /* CleanUpMemory */
        private void SaveActiveTrainingModelFeatureData_Load(object sender, EventArgs e)
        {
            modelNames = TrainingModel2.GetListOfTrainingModels();
            if (modelNames != null)
            {
                foreach (String s in modelNames)
                {
                    ModelName.Items.Add(s);
                }
            }
            ModelName.Text = OSservices.GetRootName(initialModelName);

            String[] fileFormats = PicesFeatureFileIO.RegisteredDriverNames(false, true);
            if (fileFormats != null)
            {
                foreach (String ff in fileFormats)
                {
                    FileFormat.Items.Add(ff);
                }
            }
            FileFormat.Text = "Pices";

            DestinationFileName.Text = OSservices.AddSlash(PicesSipperVariables.TempDirectory()) + ModelName.Text + ".data";
        }
Beispiel #21
0
        } /* 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 */
Beispiel #22
0
        } /* PerformPredictions*/

        private void  GradeAgainstGroundTruth()
        {
            if (!cancelProcessing)
            {
                if (trainingModel != null)
                {
                    trainingModel = null;
                }

                trainingModel = new TrainingModel2(runLog, trainingModelName);
                if (trainingModel.Valid)
                {
                    if (trainingLevel < maxLevel)
                    {
                        trainingModel.LoadTrainingModelForGivenLevel(trainingLevel, msgQueue);
                    }
                    else
                    {
                        trainingModel.LoadExistingModelOtherwiseBuild(msgQueue);
                    }
                }
            }

            if (!cancelProcessing)
            {
                groundTruth = new PicesFeatureVectorList();
                groundTruth.LoadInSubDirectoryTree(GroundTruth.Text,
                                                   true, // true = useDirectoryNameForClassName,
                                                   runLog,
                                                   false // false = Don't rewiteRootFeatureFile
                                                   );

                numGroundTruthImages          = groundTruth.Count;
                numGroundTruthImagesProcessed = 0;
            }

            if (cancelProcessing)
            {
                if (trainingModel != null)
                {
                    BlockStart();
                    trainingModel = null;
                    BlockEnd();
                }
            }
            else
            {
                if (!trainingModel.Valid)
                {
                    MessageBox.Show("Error Loading Training Model;  refer to dialog box");
                    cancelProcessing = true;
                }

                else if (!cancelProcessing)
                {
                    confusionMatrix.SetTrainingModel(trainingModel);
                    PerformPredictions();
                }
            }

            if (groundTruth != null)
            {
                BlockStart();
                groundTruth = null;
                BlockEnd();
            }
        } /* GradeAgainstGroundTruth */
Beispiel #23
0
        } /* GradeAgainstGroundTruth */

        private void  GradeAgainstCrossValidationRun()
        {
            if (curSelModel == null)
            {
                curSelModel = new PicesTrainingConfigManaged(configFileName, runLog);
            }

            if (!curSelModel.Valid())
            {
                MessageBox.Show("Configuration File[" + configFileName + "] is not valid");
                return;
            }

            bool changesMade            = false;
            PicesFeatureVectorList data = curSelModel.LoadFeatureDataFromTrainingLibraries(changesMade);

            if (cancelProcessing)
            {
                data = null;
                return;
            }

            if (data == null)
            {
                MessageBox.Show("No data was loaded from Configuration File[" + configFileName + "] is not valid");
                return;
            }

            PicesFeatureVectorList examples = data.StratifyAmoungstClasses(numOfFolds);

            int imageCount       = examples.Count;
            int numImagesPerFold = (imageCount + numOfFolds - 1) / numOfFolds;

            int firstInGroup = 0;

            int foldNum;

            for (foldNum = 0; foldNum < numOfFolds; foldNum++)
            {
                int lastInGroup;

                // If We are doing the last Fold Make sure that we are including all the examples
                // that have not been tested.
                if (foldNum == (numOfFolds - 1))
                {
                    lastInGroup = imageCount;
                }
                else
                {
                    lastInGroup = firstInGroup + numImagesPerFold - 1;
                }

                PicesFeatureVectorList trainingImages = new PicesFeatureVectorList();
                PicesFeatureVectorList testImages     = new PicesFeatureVectorList();

                for (int x = 0; (x < imageCount) && (!cancelProcessing); x++)
                {
                    PicesFeatureVector newImage = new PicesFeatureVector(examples[x]);

                    if ((x >= firstInGroup) && (x <= lastInGroup))
                    {
                        testImages.Add(newImage);
                    }
                    else
                    {
                        trainingImages.Add(newImage);
                    }
                }

                if (cancelProcessing)
                {
                    break;
                }

                CrossValidate(testImages, trainingImages, foldNum);

                firstInGroup = firstInGroup + numImagesPerFold;
            }

            if (!cancelProcessing)
            {
                if (trainingModel != null)
                {
                    trainingModel = null;
                }

                msgQueue.AddMsg("\n" + "Building Final Classifier for User Query");
                trainingModel = new TrainingModel2(runLog, curSelModel);
                trainingModel.BuildTrainingModel(data);
            }

            if ((!cancelProcessing) && (trainingModel != null) && (!trainingModel.Valid))
            {
                MessageBox.Show("Error Loading Training Model;  refer to dialog box");
                cancelProcessing = true;
            }

            else if (!cancelProcessing)
            {
                confusionMatrix.SetTrainingModel(trainingModel);
            }

            // We no longer need the Training Model or Ground Truth loaded
            BlockStart();
            data = null;
            BlockEnd();
        } /* GradeAgainstCrossValidationRun */