Exemple #1
0
        } /* SaveSampleImages */

        private PicesFeatureVectorList  FilterFeatureVectorList(PicesFeatureVectorList data,
                                                                ref uint numFilteredOut
                                                                )
        {
            numFilteredOut = 0;

            PicesFeatureVectorList keepers = new PicesFeatureVectorList();

            foreach (PicesFeatureVector fv in data)
            {
                bool keep = true;

                if ((probMin > 0.0f) && (fv.Probability < probMin))
                {
                    keep = false;
                }

                else if ((probMax < 1.0f) && (fv.Probability > probMax))
                {
                    keep = false;
                }

                else if ((sizeMin > 0) && (fv.OrigSize < sizeMin))
                {
                    keep = false;
                }

                else if ((sizeMax > 0) && (fv.OrigSize > sizeMax))
                {
                    keep = false;
                }

                else if ((depthMin > 0) && (fv.Depth < depthMin))
                {
                    keep = false;
                }

                else if ((depthMax > 0) && (fv.Depth > depthMax))
                {
                    keep = false;
                }

                if (keep)
                {
                    keepers.Add(fv);
                }
                else
                {
                    numFilteredOut++;
                }
            }

            return(keepers);
        } /* FilterFeatureVectorList */
Exemple #2
0
        } /* CountImagesMissingFeatureData */

        private PicesFeatureVectorList  SaveFeatureDataBySipperFiles()
        {
            PicesFeatureVectorList data = new PicesFeatureVectorList();

            String[] sipperFileNames = null;
            if (String.IsNullOrEmpty(sipperFileName))
            {
                sipperFileNames = dbConn.SipperFileGetList(cruise, station, deployment);
            }
            else
            {
                sipperFileNames    = new String[1];
                sipperFileNames[0] = sipperFileName;
            }

            if (sipperFileNames != null)
            {
                for (int idx = 0; (idx < sipperFileNames.Length) && (!cancelRequested); idx++)
                {
                    String curSipperFileName = sipperFileNames[idx];
                    backGroundStatus = "Loading[" + (idx + 1).ToString() + " of " + sipperFileNames.Length.ToString() + "]  File[" + curSipperFileName + "]";

                    PicesFeatureVectorList dataThisSipperFile = dbConn.FeatureDataGetOneSipperFile(curSipperFileName, mlClass, classKeyToUse, false);
                    if (dataThisSipperFile != null)
                    {
                        imagesLoaded += (uint)dataThisSipperFile.Count;
                        uint numFilteredOut = 0;
                        dataThisSipperFile        = FilterFeatureVectorList(dataThisSipperFile, ref numFilteredOut);
                        imagesFilteredOut        += numFilteredOut;
                        imagesMissingFeatureData += CountImagesMissingFeatureData(dataThisSipperFile);

                        foreach (PicesFeatureVector fv in dataThisSipperFile)
                        {
                            data.Add(fv);
                        }
                    }
                }
            }

            return(data);
        } /* SaveFeatureDataBySipperFiles */
        } /* 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 */
        } /* DialogTimer_Tick */

        private void  CreateTuningFeatureFiles()
        {
            tuningThreadRunning = true;

            PicesDataBase.ThreadInit();


            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Started");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Creating Destination Directory");
            String destinationDirectory = OSservices.AddSlash(DestinationDirectory.Text) + OSservices.GetRootName(modelName);

            OSservices.CreateDirectoryPath(destinationDirectory);

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Loading feature data for Model[" + modelName + "]");
            config = new PicesTrainingConfigManaged(modelName, runLog);
            if (!config.Valid())
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  is invalid.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }


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

            if (data == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  Could not load Feature Data.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }

            if (tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Process Canceled.");
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Feature Data Loaded.");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Checking that Depth Data Exists.");
            MakeSureDepthFieldIsIncluded(data);

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Splitting into separate files.");

            PicesFeatureVectorList trainData      = new PicesFeatureVectorList();
            PicesFeatureVectorList testData       = new PicesFeatureVectorList();
            PicesFeatureVectorList validationData = new PicesFeatureVectorList();

            PicesClassList classes = data.ExtractListOfClasses();

            if (classes == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Could not extract list of classes from data.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }


            foreach (PicesClass c in classes)
            {
                int trainCount      = 0;
                int testCount       = 0;
                int validationCount = 0;

                PicesFeatureVectorList examplesForClass = data.ExtractExamplesForAGivenClass(c);
                int n = examplesForClass.Count;

                AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Processing Class[" + c.Name + "]  Count[" + n + "]");

                trainCount = (int)((float)n * trainingDataPercentage / 100.0f + 0.5f);
                if (trainingDataMaxImagesPerClass > 0)
                {
                    if (trainCount > trainingDataMaxImagesPerClass)
                    {
                        trainCount = trainingDataMaxImagesPerClass;
                    }
                }

                if (validationDataPercentage == 0.0f)
                {
                    testCount = n - trainCount;
                }
                else
                {
                    testCount = (int)((float)n * testDataPercentage / 100.0f + 0.5f);
                }

                validationCount = n - (trainCount + testCount);

                examplesForClass.RandomizeOrder();

                int validationStart = trainCount + testCount;
                int testStart       = trainCount;

                for (int idx = 0; idx < n; idx++)
                {
                    PicesFeatureVector fv = examplesForClass[idx];
                    if (idx >= validationStart)
                    {
                        validationData.Add(fv);
                    }

                    else if (idx >= testStart)
                    {
                        testData.Add(fv);
                    }

                    else
                    {
                        trainData.Add(fv);
                    }
                }

                if (tuningThreadCancelReq)
                {
                    AddMsgToDialogMsgQueue("\n\n");
                    AddMsgToDialogMsgQueue("Process Canceled.");
                    break;
                }
            }

            if (!tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n" + DateTime.Now.ToShortTimeString() + " Stratifying Data");

                PicesFeatureVectorList trainDataStratified      = trainData.StratifyAmoungstClasses(numOfFolds);
                PicesFeatureVectorList testDataStratified       = testData.StratifyAmoungstClasses(numOfFolds);
                PicesFeatureVectorList validationDataStratified = validationData.StratifyAmoungstClasses(numOfFolds);

                String rootName = OSservices.AddSlash(destinationDirectory) + OSservices.GetRootName(modelName);


                if ((trainDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    trainDataFileName = rootName + "_Train.data";
                    AddMsgToDialogMsgQueue("Saving Training Data[" + trainDataFileName + "]");
                    trainDataStratified.Save(trainDataFileName, "", runLog);
                }

                if ((testDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    testDataFileName = rootName + "_Test.data";
                    AddMsgToDialogMsgQueue("Saving Test Data[" + testDataFileName + "]");
                    testDataStratified.Save(testDataFileName, "", runLog);
                }

                if ((validationDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    validationDataFileName = rootName + "_Validation.data";
                    AddMsgToDialogMsgQueue("Saving Validation Data[" + validationDataFileName + "]");
                    validationDataStratified.Save(validationDataFileName, "", runLog);
                }
            }

            AddMsgToDialogMsgQueue("\n\n");
            if (tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("Process    *** CANCELED ***.");
            }
            else
            {
                AddMsgToDialogMsgQueue("Process Completed.");
            }

            PicesDataBase.ThreadEnd();

            tuningThreadDone    = true;
            tuningThreadRunning = false;
        } /* CreateTuningFeatureFiles */