123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // Beispielhafter Aufruf: BUILD_x86_64/progs/testSemanticSegmentation3D -config <CONFIGFILE>
- /**
- * @file testSemanticSegmentation3D.cpp
- * @brief test semantic segmentation routines for 3d images and 2d images
- * @author Erik Rodner, Björn Fröhlich, Sven Sickert
- * @date 03/20/2008
- */
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #endif
- #include "core/basics/Config.h"
- #include "core/basics/StringTools.h"
- #include "core/image/Morph.h"
- #include "core/image/MultiChannelImage3DT.h"
- #include "vislearning/baselib/ICETools.h"
- #include "semseg/semseg/SemSegContextTree3D.h"
- #include "semseg/semseg/SemSegTools.h"
- #include <fstream>
- #include <vector>
- #undef DEBUG
- using namespace OBJREC;
- using namespace NICE;
- using namespace std;
- void startClassification (SemanticSegmentation *semseg,
- std::vector< NICE::Matrix > & M_vec,
- const Config & conf,
- const LabeledSet* testFiles,
- const ClassNames & classNames,
- const set<int> & forbidden_classes,
- std::map<int,int> & classMapping,
- const unsigned short cvRuns)
- {
- bool write_results = conf.gB ( "debug", "write_results", false );
- bool writeProbMaps = conf.gB ( "debug", "write_prob_maps", false );
- if (cvRuns > 1)
- write_results = false;
- bool run_3Dseg = conf.gB( "SSContextTree", "run_3dseg", false);
- string output_type = conf.gS ( "debug", "output_type", "ppm" );
- vector< int > zsizeVec;
- SemSegTools::getDepthVector ( testFiles, zsizeVec, run_3Dseg );
- int depthCount = 0, idx = 0;
- vector< string > filelist;
- NICE::MultiChannelImageT<int> segresult;
- NICE::MultiChannelImageT<int> gt;
- for (LabeledSet::const_iterator it = testFiles->begin(); it != testFiles->end(); it++)
- {
- for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
- jt != it->second.end(); jt++)
- {
- ImageInfo & info = *(*jt);
- std::string file = info.img();
- filelist.push_back ( file );
- depthCount++;
- NICE::ImageT<int> lm, lm_gt;
- if ( info.hasLocalizationInfo() )
- {
- const LocalizationResult *l_gt = info.localization();
- lm_gt.resize ( l_gt->xsize, l_gt->ysize );
- #ifdef DEBUG
- cout << "testSemanticSegmentation3D: Generating Labeled NICE::Image (Ground-Truth)" << endl;
- #endif
- l_gt->calcLabeledImage ( lm_gt, classNames.getBackgroundClass() );
- }
- segresult.addChannel ( lm_gt );
- gt.addChannel ( lm_gt );
- int depthBoundary = 0;
- if ( run_3Dseg )
- {
- depthBoundary = zsizeVec[idx];
- }
- if ( depthCount < depthBoundary ) continue;
- NICE::MultiChannelImage3DT<double> probabilities;
- semseg->classify ( filelist, segresult, probabilities );
- // save to file
- lm = lm_gt;
- for ( int z = 0; z < segresult.channels(); z++ )
- {
- NICE::ColorImage orig ( filelist[z] );
- NICE::ColorImage rgb;
- NICE::ColorImage rgb_gt;
- for ( int y = 0 ; y < orig.height(); y++ )
- for ( int x = 0 ; x < orig.width(); x++ )
- {
- lm.setPixel ( x, y, segresult.get ( x, y, ( uint ) z ) );
- // get current <slice> of ground truth
- if ( run_3Dseg )
- lm_gt.setPixel ( x, y, gt.get ( x, y, ( uint ) z ) );
- }
- // confusion matrix
- NICE::Matrix M ( classMapping.size(), classMapping.size() );
- M.set ( 0 );
- SemSegTools::updateConfusionMatrix (
- lm, lm_gt, M, forbidden_classes, classMapping );
- M_vec.push_back ( M );
- classNames.labelToRGB ( lm, rgb );
- classNames.labelToRGB ( lm_gt, rgb_gt );
- if ( write_results )
- {
- std::string fname = StringTools::baseName ( filelist[z], false );
- std::string outStr;
- SemSegTools::saveResultsToImageFile ( &conf, "debug",
- orig, rgb_gt, rgb, fname, outStr );
- // write Probability maps
- if (writeProbMaps)
- {
- NICE::ColorImage prob_map( probabilities.width(), probabilities.height() );
- prob_map.set(0,0,0);
- int iNumChannels = probabilities.channels();
- for ( int idxProbMap = 0; idxProbMap < iNumChannels; idxProbMap++)
- {
- for ( int y = 0 ; y < probabilities.height(); y++ )
- {
- for ( int x = 0 ; x < probabilities.width(); x++ )
- {
- double probVal = probabilities.get( x, y, z, idxProbMap ) * 255.0;
- for ( int c = 0 ; c < 3 ; c++ )
- prob_map.setPixel( x, y, c, round(probVal) );
- }
- }
- std::stringstream ssFileProbMap;
- ssFileProbMap << outStr << "_probs." << "c-" << classNames.code( idxProbMap ) << "." << output_type;
- //classNames
- prob_map.write ( ssFileProbMap.str() );
- }
- }
- }
- }
- // prepare for new 3d image
- filelist.clear();
- segresult.reInit(0,0,0);
- gt.reInit(0,0,0);
- depthCount = 0;
- idx++;
- }
- }
- segresult.freeData();
- }
- /**
- test semantic segmentation routines
- */
- int main ( int argc, char **argv )
- {
- std::set_terminate ( __gnu_cxx::__verbose_terminate_handler );
- Config conf ( argc, argv );
- ResourceStatistics rs;
- /*---------------CONFIGURATION---------------*/
- unsigned short crossValRuns = conf.gI ( "debug", "cross_val_runs", 1 );
- /*-------------------------------------------*/
- #ifdef DEBUG
- cerr << "Writing Results to " << resultdir << endl;
- #endif
- std::vector< NICE::Matrix > M_vec;
- MultiDataset md ( &conf );
- const ClassNames & classNames = md.getClassNames ( "train" );
- set<int> forbidden_classes;
- classNames.getSelection ( conf.gS ( "analysis", "forbidden_classes", "" ),
- forbidden_classes );
- vector<bool> usedClasses ( classNames.numClasses(), true );
- for ( set<int>::const_iterator it = forbidden_classes.begin();
- it != forbidden_classes.end(); ++it)
- {
- usedClasses [ *it ] = false;
- }
- map<int,int> classMapping, classMappingInv;
- int j = 0;
- for ( int i = 0; i < usedClasses.size(); i++ )
- if (usedClasses[i])
- {
- classMapping[i] = j;
- classMappingInv[j] = i;
- j++;
- }
- // initialize semantic segmentation method
- SemanticSegmentation *semseg = NULL;
- // TRAINING AND TESTING
- if ( crossValRuns == 1 )
- {
- semseg = new SemSegContextTree3D ( &conf, &classNames );
- // STANDARD EVALUATION
- cout << "\nTRAINING" << endl;
- cout << "########\n" << endl;
- semseg->train( &md );
- cout << "\nCLASSIFICATION" << endl;
- cout << "##############\n" << endl;
- const LabeledSet *testFiles = md["test"];
- startClassification (semseg, M_vec, conf, testFiles, classNames,
- forbidden_classes, classMapping, crossValRuns );
- delete semseg;
- }
- else
- {
- // CROSS-VALIDATION
- for (int cval = 1; cval <= crossValRuns; cval++)
- {
- semseg = new SemSegContextTree3D ( &conf, &classNames );
- stringstream ss;
- ss << cval;
- string cvaltrain = "train_cv" + ss.str();
- string cvaltest = "test_cv" + ss.str();
- cout << "\nTRAINING " << cval << endl;
- cout << "###########\n" << endl;
- const LabeledSet *trainFiles = md[cvaltrain];
- semseg->train( trainFiles );
- cout << "\nCLASSIFICATION " << cval << endl;
- cout << "#################\n" << endl;
- const LabeledSet *testFiles = md[cvaltest];
- startClassification (semseg, M_vec, conf, testFiles, classNames,
- forbidden_classes, classMapping, crossValRuns );
- delete semseg;
- }
- }
- // resource statistics
- SemSegTools::computeResourceStatistics ( rs );
- NICE::Matrix M ( classMapping.size(), classMapping.size() );
- M.set ( 0 );
- for ( int s = 0; s < ( int ) M_vec.size(); s++ )
- {
- NICE::Matrix M_tmp = M_vec[s];
- for ( int r = 0; r < ( int ) M_tmp.rows(); r++ )
- for ( int c = 0; c < ( int ) M_tmp.cols(); c++ )
- M ( r, c ) += M_tmp ( r, c );
- }
- // evaluation & analysis
- SemSegTools::computeClassificationStatistics(
- M, classNames, forbidden_classes, classMappingInv );
- return 0;
- }
|