123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /**
- * @file SemSegTools.cpp
- * @brief tools for semantic segmentation
- * @author Erik Rodner, Sven Sickert
- * @date 03/19/2009
- */
- #include <iostream>
- #include <iomanip>
- #include "SemSegTools.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- #undef DEBUG_LOCALIZATION
- #undef DEBUG
- void SemSegTools::segmentToOverlay (
- const NICE::Image *orig,
- const NICE::ColorImage & segment,
- NICE::ColorImage & result )
- {
- int xsize = orig->width();
- int ysize = orig->height();
- result.resize( xsize, ysize );
- std::vector< NICE::MatrixT<double> > channelMat;
- double alpha = .3;
- for (int c = 0; c < 3; c++)
- {
- NICE::MatrixT<double> chan ( xsize, ysize );
- channelMat.push_back( chan );
- }
- for (int y = 0; y < ysize; y++)
- for (int x = 0; x < xsize; x++)
- {
- uchar val = orig->getPixelQuick(x,y);
- for (int c = 0; c < 3; c++)
- channelMat[c](x,y) = alpha*(double)val
- + (1.0-alpha)*(double)segment.getPixel( x, y, c );
- }
- for (int y = 0; y < ysize; y++)
- for (int x = 0; x < xsize; x++)
- for (int c = 0; c < 3; c++)
- {
- int val = channelMat[c](x,y);
- result.setPixel( x, y, c, (uchar)val);
- }
- }
- void SemSegTools::updateConfusionMatrix(
- const ImageT<int> &img,
- const ImageT<int> >,
- Matrix &M,
- const std::set<int> &forbiddenClasses,
- map<int,int> & classMapping )
- {
- double subsamplex = gt.width() / ( double ) img.width();
- double subsampley = gt.height() / ( double ) img.height();
- for ( int y = 0 ; y < gt.height() ; y++ )
- for ( int x = 0 ; x < gt.width() ; x++ )
- {
- int xx = ( int ) ( x / subsamplex );
- int yy = ( int ) ( y / subsampley );
- if ( xx < 0 ) xx = 0;
- if ( yy < 0 ) yy = 0;
- if ( xx > img.width() - 1 ) xx = img.width() - 1;
- if ( yy > img.height() - 1 ) yy = img.height() - 1;
- int cimg = img.getPixel ( xx, yy );
- int gimg = gt.getPixel ( x, y );
- if ( forbiddenClasses.find ( gimg ) == forbiddenClasses.end() )
- {
- M ( classMapping[gimg], classMapping[cimg] ) ++;
- }
- }
- }
- void SemSegTools::computeClassificationStatistics(
- Matrix &confMat,
- const ClassNames &classNames,
- const std::set<int> &forbiddenClasses,
- map<int,int> & classMappingInv )
- {
- std::cout << "\nPERFORMANCE" << std::endl;
- std::cout << "###########\n" << std::endl;
- double overallTrue = confMat.trace();
- double sumAll = 0.0;
- // overall recognition rate
- for ( int r = 0; r < (int) confMat.rows(); r++ )
- for ( int c = 0; c < (int) confMat.cols(); c++ )
- sumAll += confMat( r, c );
- overallTrue /= sumAll;
- double truePos = (double)confMat(1,1);
- //double trueNeg = (double)confMat(0,0);
- double falsePos = (double)confMat(0,1);
- double falseNeg = (double)confMat(1,0);
- // binary classification metrics
- if ( classMappingInv.size() == 2 )
- {
- double precision = truePos / (truePos+falsePos);
- double recall = truePos / (truePos+falseNeg);
- double f1score = 2.0*(precision*recall)/(precision+recall);
- std::cout << "Precision: " << precision;
- std::cout << "\nRecall: " << recall;
- std::cout << "\nF1Score: " << f1score << "\n\n";
- }
- // normalizing confMat using rows
- for ( int r = 0 ; r < (int) confMat.rows() ; r++ )
- {
- double sum = 0.0;
- for ( int c = 0 ; c < (int) confMat.cols() ; c++ )
- sum += confMat ( r, c );
- if ( std::fabs ( sum ) > 1e-4 )
- for ( int c = 0 ; c < (int) confMat.cols() ; c++ )
- confMat ( r, c ) /= sum;
- }
- // printing confusion matrix
- short int printWidth = 16;
- std::cout.precision(6);
- std::cout << std::setw(printWidth) << "";
- for (int r = 0; r < (int) confMat.rows(); r++)
- {
- int cl = classMappingInv[r];
- if ( classNames.existsClassno ( cl )
- && ( forbiddenClasses.find ( cl ) == forbiddenClasses.end() ) )
- {
- std::string cname = classNames.text ( cl );
- std::cout << std::setw(printWidth) << cname.c_str();
- }
- }
- std::cout << std::endl;
- for (int r = 0; r < (int) confMat.rows(); r++)
- {
- int cl = classMappingInv[r];
- if ( classNames.existsClassno ( cl )
- && ( forbiddenClasses.find ( cl ) == forbiddenClasses.end() ) )
- {
- std::string cname = classNames.text ( cl );
- std::cout << std::setw(printWidth) << cname.c_str();
- for (int c = 0; c < (int) confMat.cols(); c++)
- std::cout << std::setw(printWidth) << std::fixed << confMat (r, c);
- std::cout << std::endl;
- }
- }
- // print classification statistics
- std::cout << "\nOverall Recognition Rate: " << overallTrue;
- std::cout << "\nAverage Recognition Rate: " << confMat.trace() / (double)classMappingInv.size();
- std::cout << "\nLower Bound: " << 1.0 /(double)classMappingInv.size();
- std::cout << std::endl;
- }
- void SemSegTools::computeResourceStatistics (
- NICE::ResourceStatistics &rs )
- {
- std::cout << "\nSTATISTICS" << std::endl;
- std::cout << "##########\n" << std::endl;
- long maxMemory;
- double userCPUTime, sysCPUTime;
- rs.getStatistics ( maxMemory, userCPUTime, sysCPUTime );
- std::cout << "Memory (max): " << maxMemory << " KB" << std::endl;
- std::cout << "CPU Time (user): " << userCPUTime << " seconds" << std::endl;
- std::cout << "CPU Time (sys): " << sysCPUTime << " seconds" << std::endl;
- }
- void SemSegTools::saveResultsToImageFile(
- const Config *conf,
- const string §ion,
- const ColorImage &orig,
- const ColorImage >ruth,
- const ColorImage &segment,
- const string &file,
- string & outStr )
- {
- std::string resultDir = conf->gS ( section, "resultdir", "." );
- std::string outputType = conf->gS ( section, "output_type", "ppm" );
- std::string outputPostfix = conf->gS ( section, "output_postfix", "" );
- NICE::ColorImage overlaySegment, overlayGTruth;
- NICE::Image* origGrey = orig.getChannel(1);
- segmentToOverlay( origGrey, segment, overlaySegment );
- segmentToOverlay( origGrey, gtruth, overlayGTruth );
- std::stringstream out;
- out << resultDir << "/" << file << outputPostfix;
- #ifdef DEBUG
- std::cout << "Writing to file " << out.str() << "_*." << outputType << std::endl;
- #endif
- orig.write ( out.str() + "_orig." + outputType );
- segment.write ( out.str() + "_result." + outputType );
- gtruth.write ( out.str() + "_groundtruth." + outputType );
- overlaySegment.write ( out.str() + "_overlay_res." + outputType );
- overlayGTruth.write ( out.str() + "_overlay_gt." + outputType );
- outStr = out.str();
- }
- void SemSegTools::collectTrainingExamples (
- const Config * conf,
- const std::string & section,
- const LabeledSet & train,
- const ClassNames & cn,
- Examples & examples,
- vector<CachedExample *> & imgexamples )
- {
- assert ( train.count() > 0 );
- examples.clear();
- imgexamples.clear();
- int grid_size_x = conf->gI(section, "grid_size_x", 5 );
- int grid_size_y = conf->gI(section, "grid_size_y", 5 );
- int grid_border_x = conf->gI(section, "grid_border_x", 20 );
- int grid_border_y = conf->gI(section, "grid_border_y", 20 );
- std::string selection = conf->gS(section, "train_selection" );
- set<int> classnoSelection;
- cn.getSelection ( selection, classnoSelection );
-
- bool useExcludedAsBG = conf->gB(section, "use_excluded_as_background", false );
- int backgroundClassNo = 0;
-
- if ( useExcludedAsBG )
- {
- backgroundClassNo = cn.classno("various");
- assert ( backgroundClassNo >= 0 );
- }
- LOOP_ALL_S (train)
- {
- EACH_INFO(image_classno,imgInfo);
- std::string imgfn = imgInfo.img();
- if ( ! imgInfo.hasLocalizationInfo() ) {
- std::cerr << "WARNING: NO localization info found for "
- << imgfn << " !" << std::endl;
- continue;
- }
- int xsize, ysize;
- CachedExample *ce = new CachedExample ( imgfn );
- ce->getImageSize ( xsize, ysize );
- imgexamples.push_back ( ce );
- const LocalizationResult *locResult = imgInfo.localization();
- if ( locResult->size() <= 0 ) {
- std::cerr << "WARNING: NO ground truth polygons found for "
- << imgfn << " !" << std::endl;
- continue;
- }
- std::cerr << "SemSegTools: Collecting pixel examples from localization info: "
- << imgfn << std::endl;
- NICE::Image pixelLabels (xsize, ysize);
- pixelLabels.set(0);
- locResult->calcLabeledImage ( pixelLabels, cn.getBackgroundClass() );
- #ifdef DEBUG_LOCALIZATION
- NICE::Image img (imgfn);
- showImage(img);
- showImage(pixelLabels);
- #endif
- Example pce ( ce, 0, 0 );
- for ( int x = 0 ; x < xsize ; x += grid_size_x )
- for ( int y = 0 ; y < ysize ; y += grid_size_y )
- {
- if ( (x >= grid_border_x) &&
- ( y >= grid_border_y ) && ( x < xsize - grid_border_x ) &&
- ( y < ysize - grid_border_x ) )
- {
- pce.x = x; pce.y = y;
- int classno = pixelLabels.getPixel(x,y);
- if ( classnoSelection.find(classno) != classnoSelection.end() ) {
- examples.push_back ( pair<int, Example> (
- classno,
- pce // FIXME: offset handling
- ) );
- } else if ( useExcludedAsBG ) {
- examples.push_back ( pair<int, Example> (
- backgroundClassNo,
- pce // FIXME: offset handling
- ) );
- }
- }
- }
- }
- std::cerr << "total number of examples: " << (int)examples.size() << std::endl;
- }
|