Beispiel #1
0
        } /* BackGroundThread */

        private void  SaveSampleImages(PicesFeatureVectorList featureData)
        {
            PicesClassList classes = featureData.ExtractListOfClasses();

            if (classes == null)
            {
                return;
            }

            String sampleDir = OSservices.AddSlash(OSservices.GetPathPartOfFile(featureFileName)) +
                               OSservices.GetRootName(featureFileName) +
                               "_SampleImages";

            sampleDir = OSservices.AddSlash(sampleDir);
            OSservices.CreateDirectoryPath(sampleDir);

            foreach (PicesClass pc in classes)
            {
                String classSampleDir = OSservices.AddSlash(sampleDir + pc.Name);
                OSservices.CreateDirectoryPath(classSampleDir);
                PicesFeatureVectorList examplesThisClass = featureData.ExtractExamplesForAGivenClass(pc);
                examplesThisClass.RandomizeOrder();

                int numWritten = 0;
                foreach (PicesFeatureVector fv in examplesThisClass)
                {
                    String imageRootName = OSservices.GetRootName(fv.ExampleFileName);

                    PicesRaster i = dbConn.ImageFullSizeFind(imageRootName);
                    if (i != null)
                    {
                        String imageFileName = classSampleDir + imageRootName + ".bmp";
                        i.Save(imageFileName);
                        ++numWritten;
                    }
                    if (numWritten >= includeSampleImagesNumPerClass)
                    {
                        break;
                    }
                }
            }
        } /* SaveSampleImages */
        } /* 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 */