Exemple #1
0
        } /* ImageCount*/

        private void  LoadClassList()
        {
            ClassList.Items.Clear();

            if (config == null)
            {
                return;
            }

            if (!config.Valid())
            {
                return;
            }

            PicesTrainingClassList trainingClasses = config.TrainingClasses();

            if (trainingClasses != null)
            {
                int idx = 0;
                foreach (PicesTrainingClass tc  in  trainingClasses)
                {
                    String expDir = tc.ExpandedDirectory(config.RootDir());
                    ClassList.Items.Add(tc.MLClass.Name);
                    ClassList.Items[idx].SubItems.Add(tc.Directory);
                    ClassList.Items[idx].SubItems.Add(ImageCount(expDir).ToString("#,###,##0"));
                    idx++;
                }
            }
        } /* LoadClassList */
        } /* InitializeTrainAndPredLevelFields */

        private void  ValidateTrainingModel()
        {
            if (string.IsNullOrEmpty(TrainingModelSelected.Text))
            {
                return;
            }

            trainingModelName  = TrainingModelSelected.Text;
            curSelModel        = new PicesTrainingConfigManaged(trainingModelName, runLog);
            trainingModelValid = curSelModel.Valid();
            if (!trainingModelValid)
            {
                String alarmMsg = "Error Loading Configuration File";
                alarmMsg += "\n\n";
                alarmMsg += curSelModel.LoadLogStream();

                MessageBox.Show(this, alarmMsg, "Could not load Config file", MessageBoxButtons.OK);
            }
            else
            {
                otherClass = curSelModel.OtherClass();
                if (crossValidation)
                {
                    GroundTruth.Text = "F:\\Pices\\SavedImages\\Cruise-WB0812_Station-DWH";
                }
                //GroundTruth.Text = curSelModel.RootDir ();

                origClassifierClasses = curSelModel.ExtractClassList();
                InitializeTrainAndPredLevelFields();
            }
        } /* ValidateTrainingModel */
        private void  ValidateModelName()
        {
            errorProvider1.SetError(ModelName, null);
            if (String.IsNullOrEmpty(ModelName.Text))
            {
                MessageBox.Show(this, "Model Name is blank");
                errorsFound    = true;
                ModelName.Text = modelName;
                return;
            }

            if (ModelName.Text == modelName)
            {
                return;
            }

            if (runLog == null)
            {
                runLog = new PicesRunLog();
            }
            PicesTrainingConfigManaged tc = new PicesTrainingConfigManaged(ModelName.Text, runLog);

            if (!tc.Valid())
            {
                errorsFound = true;
                errorProvider1.SetError(ModelName, "Model[" + ModelName.Text + "] is not a valid model");
                ModelName.Text = modelName;
            }
            else
            {
                modelName = ModelName.Text;
            }
        } /* ValidateModelName*/
        } /* 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 */
        } /* DialogTimer_Tick */

        private void  CreateFeatureDataFileThread()
        {
            savingThreadRunning = true;

            PicesDataBase.ThreadInit();

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

            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.");
                savingThreadFailed  = true;
                savingThreadDone    = true;
                savingThreadRunning = false;
                return;
            }


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

            if (data == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  Count not load Feature Data.");
                savingThreadFailed  = true;
                savingThreadDone    = true;
                savingThreadRunning = false;
                return;
            }

            if (savingThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Process Canceled.");
                savingThreadDone    = true;
                savingThreadRunning = 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.");

            if (!savingThreadCancelReq)
            {
                data.Save(destinationFileName, FileFormat.Text, runLog);
            }

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

            PicesDataBase.ThreadEnd();

            savingThreadDone    = true;
            savingThreadRunning = false;
        } /* CreateFeatureDataFileThread */