void OurNeighbors::RemoveExcludedClasses (ImageFeaturesListPtr& examples) { bool keepClass = true; MLClassPtr oldClass = NULL; examples->SortByClass (); ImageFeaturesListPtr examplesToKeep = new ImageFeaturesList (examples->FileDesc (), true // true = We will own images, ); examples->Owner (false); ImageFeaturesList::iterator idx; for (idx = examples->begin (); idx != examples->end (); idx++) { ImageFeaturesPtr i = *idx; if (oldClass != i->MLClass ()) { oldClass = i->MLClass (); keepClass = (excludedClasses->LookUpByName (oldClass->Name ()) == NULL); } if (keepClass) { examplesToKeep->PushOnBack (i); } else { delete i; } } delete examples; examples = examplesToKeep; } /* RemoveExcludedClasses */
void GradeClassification::GradeExamplesAgainstGroundTruth (FeatureVectorListPtr examplesToGrade, FeatureVectorListPtr groundTruth ) { log.Level (10) << "GradeClassification::GradeExamplesAgainstGroundTruth" << endl; groundTruth->SortByRootName (); MLClassConstPtr unknownClass = mlClasses->GetUnKnownClass (); MLClassConstListPtr classes = NULL; { MLClassConstListPtr examplesToGradeClasses = examplesToGrade->ExtractMLClassConstList (); MLClassConstListPtr groundTruthClasses = groundTruth->ExtractMLClassConstList (); classes = MLClassConstList::MergeClassList (*examplesToGradeClasses, *groundTruthClasses); delete examplesToGradeClasses; delete groundTruthClasses; } uint16 maxHierarchialLevel = 0; { MLClassConstList::iterator idx; for (idx = classes->begin (); idx != classes->end (); idx++) { MLClassConstPtr c = *idx; maxHierarchialLevel = Max (maxHierarchialLevel, c->NumHierarchialLevels ()); } } // Create ConfusionMatrix objects for each posible level of Hierarchy. The 'resultsSummary' vector will // end up owning the instances of 'ConfusionMatrix2' and th edestructr will be responable for deleting them. uint curLevel = 0; vector<ConfusionMatrix2Ptr> cmByLevel; for (curLevel = 0; curLevel < maxHierarchialLevel; curLevel++) { MLClassConstListPtr classesThisLevel = classes->ExtractListOfClassesForAGivenHierarchialLevel (curLevel); ConfusionMatrix2Ptr cm = new ConfusionMatrix2 (*classesThisLevel); cmByLevel.push_back (cm); } ConfusionMatrix2 cm (*classes); ImageFeaturesList::iterator idx; for (idx = examplesToGrade->begin (); idx != examplesToGrade->end (); idx++) { ImageFeaturesPtr exampleToGrade = *idx; MLClassConstPtr predictedClass = exampleToGrade->MLClass (); float origSize = exampleToGrade->OrigSize (); float probability = exampleToGrade->Probability (); KKStr rootName = osGetRootName (exampleToGrade->ImageFileName ()); FeatureVectorPtr groundTruthExample = groundTruth->LookUpByRootName (rootName); MLClassConstPtr groundTruthClass = unknownClass; if (groundTruthExample) groundTruthClass = groundTruthExample->MLClass (); cm.Increment (groundTruthClass, predictedClass, (int)origSize, probability, log); for (curLevel = 0; curLevel < maxHierarchialLevel; curLevel++) { MLClassConstPtr groundTruthClasssThisLevel = groundTruthClass->MLClassForGivenHierarchialLevel (curLevel); MLClassConstPtr predictedClassThisLevel = predictedClass->MLClassForGivenHierarchialLevel (curLevel); cmByLevel[curLevel]->Increment (groundTruthClasssThisLevel, predictedClassThisLevel, (int)origSize, probability, log); } } //cm.PrintTrueFalsePositivesTabDelimited (*report); { // report Hierarchial results for (curLevel = 0; curLevel < maxHierarchialLevel; curLevel++) { log.Level (10) << "GradeClassification::GradeExamplesAgainstGroundTruth Printing Level[" << curLevel << "]" << endl; *report << endl << endl << endl << "Confusion Matrix Training Level[" << maxHierarchialLevel << "] Preduction Level[" << (curLevel + 1) << "]" << endl << endl; cmByLevel[curLevel]->PrintConfusionMatrixTabDelimited (*report); resultsSummary.push_back (SummaryRec (maxHierarchialLevel, curLevel + 1, cmByLevel[curLevel])); } *report << endl << endl << endl; } log.Level (10) << "GradeClassification::GradeExamplesAgainstGroundTruth Exiting" << endl; } /* GradeExamplesAgainstGroundTruth */
void OurNeighbors::LookForNeighbors () { ImageFeaturesListPtr currentImageFeatures = NULL; KKStr relativeDir; log.Level (10) << "OurNeighbors::LookForNeighbors" << endl; /* * create an image feature list from the source directory that corresponds to the * current locations of the actual image files. Where possible, the feature data * file will be used. However, if an image has been moved it's features will have * to be recalculated (which is handled by the function call) and we'll have to * look in the origImageFeatures list for the original predicted class. We must do * this since the predicted class for an image file should NEVER change between * classification runs. */ FeatureFileIOPices::Driver ()->LoadInSubDirectoryTree (PicesFVProducerFactory::Factory (&log), sourceRootDirPath, *mlClasses, false, // useDirectoryNameForClassName, DB (), cancelFlag, false, // rewiteRootFeatureFile log ); currentImageFeatures->FixSipperFileScanLineAndColFields (); lastScanLine = LastScanLine (*currentImageFeatures); { // Make sure Class name matches subdirectory that Example was found in. ImageFeaturesList::iterator idx; for (idx = currentImageFeatures->begin (); idx != currentImageFeatures->end (); idx++) { ImageFeaturesPtr image = *idx; MLClassPtr mlClass = DetermineClassFromFileName (image->ExampleFileName ()); if (mlClass) image->MLClass (mlClass); } } if (excludedClasses) { if (excludedClasses->QueueSize () > 0) RemoveExcludedClasses (currentImageFeatures); } //if (randomizeLocations) // RandomizeLocations (*currentImageFeatures); if (!fromPlanktonName.Empty ()) { fromPlankton = mlClasses->LookUpByName (fromPlanktonName); if (!fromPlankton) { log.Level (-1) << endl << endl << endl << "LookForNeighbors ****** ERROR *******" << endl << endl << "No images that are of PlanktonName[" << fromPlanktonName << "]" << endl << endl; osWaitForEnter (); exit (-1); } } // We will now build Neighbor List NeighborList neighbors (*currentImageFeatures, log); neighbors.FindNearestNeighbors (neighborType, fromPlankton); double allClassesMeanNND = 0.0f; double allClassesMeanStdDev = 0.0f; double allClassesMinDist = 0.0f; double allClassesMaxDist = 0.0f; neighbors.CalcStatistics (allClassesMeanNND, allClassesMeanStdDev, allClassesMinDist, allClassesMaxDist ); if (fromPlankton) neighbors.ReportClassRowRestricted (mlClasses, *report, fromPlankton); else neighbors.ReportClassRow (mlClasses, *report); neighbors.ReportClassNeighbor (mlClasses, *report); if (randomizeLocations) RandomReport (*currentImageFeatures); log.Level (10) << "OurNeighbors::LookForNeighbors Exiting" << endl; } /* LookForNeighbors */