MultiDataset.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * @file MultiDataset.cpp
  3. * @brief multiple datasets
  4. * @author Erik Rodner
  5. * @date 02/08/2008
  6. */
  7. #include <iostream>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include "vislearning/cbaselib/ClassNames.h"
  11. #include "core/basics/StringTools.h"
  12. #include "core/basics/FileMgt.h"
  13. #include "vislearning/cbaselib/MultiDataset.h"
  14. using namespace OBJREC;
  15. using namespace std;
  16. using namespace NICE;
  17. #undef DEBUG_MultiDataset
  18. void MultiDataset::selectExamples ( const std::string & examples_command,
  19. const LabeledSet & base,
  20. LabeledSet & positives,
  21. LabeledSet & negatives,
  22. const ClassNames & cn ) const
  23. {
  24. vector<string> examples;
  25. StringTools::split ( examples_command, ';', examples );
  26. set<int> processed_classes;
  27. for ( vector<string>::const_iterator i = examples.begin();
  28. i != examples.end();
  29. i++ )
  30. {
  31. const std::string & cmd = *i;
  32. vector<string> parts;
  33. StringTools::split ( cmd, ' ', parts );
  34. if ( (parts.size() != 3) && ((parts.size() != 2) || (parts[0].compare("all") != 0)) )
  35. {
  36. std::cerr << "parts.size(): "<<parts.size()<<std::endl;
  37. std::cerr << " parts[0]: " << parts[0] << std::endl;
  38. fthrow( Exception, "Syntax error " << examples_command );
  39. }
  40. const std::string & mode = parts[0];
  41. const std::string & csel = parts[1];
  42. double parameter = (parts.size() == 3 ) ? atof(parts[2].c_str()) : 0.0;
  43. map<int, int> fpe;
  44. set<int> selection;
  45. cn.getSelection ( csel, selection );
  46. for ( set<int>::const_iterator j = selection.begin();
  47. j != selection.end();
  48. j++ )
  49. {
  50. int classno = *j;
  51. if ( processed_classes.find(classno) == processed_classes.end() )
  52. {
  53. #ifdef DEBUG_MultiDataset
  54. fprintf (stderr, "class %s: %s %d\n", cn.text(classno).c_str(),
  55. mode.c_str(), (int)parameter );
  56. #endif
  57. fpe[*j] = (int)parameter;
  58. processed_classes.insert(classno);
  59. } else {
  60. if ( csel != "*" ) {
  61. fthrow ( Exception, "Example selection method for class %s has multiple specifications" << cn.text(classno) );
  62. }
  63. }
  64. }
  65. if ( mode == "seq" ) {
  66. LabeledSetSelection<LabeledSet>::selectSequential (
  67. fpe, base, positives, negatives );
  68. #ifdef DEBUG_MultiDataset
  69. fprintf (stderr, "MultiDataset: after special seq selection: %d\n", positives.count() );
  70. #endif
  71. } else if ( mode == "step" ) {
  72. LabeledSetSelection<LabeledSet>::selectSequentialStep (
  73. fpe, base, positives, negatives );
  74. #ifdef DEBUG_MultiDataset
  75. fprintf (stderr, "MultiDataset: after special step selection: %d\n", positives.count() );
  76. #endif
  77. } else if ( mode == "random" ) {
  78. LabeledSetSelection<LabeledSet>::selectRandom (
  79. fpe, base, positives, negatives );
  80. #ifdef DEBUG_MultiDataset
  81. fprintf (stderr, "MultiDataset: after special random selection: %d\n", positives.count() );
  82. #endif
  83. } else if ( mode == "all" ) {
  84. if ( (int)selection.size() == cn.numClasses() )
  85. {
  86. // preserve permutation
  87. LabeledSet::Permutation permutation;
  88. base.getPermutation ( permutation );
  89. for ( LabeledSet::Permutation::iterator i = permutation.begin(); i != permutation.end(); i++ )
  90. {
  91. int classno = i->first;
  92. ImageInfo *element = const_cast< ImageInfo * > ( i->second );
  93. positives.add_reference ( classno, element );
  94. }
  95. } else {
  96. LabeledSetSelection<LabeledSet>::selectClasses ( selection, base, positives, negatives );
  97. }
  98. #ifdef DEBUG_MultiDataset
  99. fprintf (stderr, "MultiDataset: after special class selection: %d\n", positives.count() );
  100. #endif
  101. } else {
  102. fthrow ( Exception, "Wrong value for parameter example\n");
  103. }
  104. }
  105. #ifdef DEBUG_MultiDataset
  106. fprintf (stderr, "MultiDataset: after special selection operations: %d\n", positives.count() );
  107. #endif
  108. set<int> allclasses;
  109. cn.getSelection ( "*", allclasses );
  110. set<int> allnegative_classes;
  111. // add all examples from allclasses \setminus processed_classes
  112. set_difference(allclasses.begin(), allclasses.end(), processed_classes.begin(), processed_classes.end(),
  113. inserter(allnegative_classes, allnegative_classes.end()));
  114. LabeledSet dummy;
  115. LabeledSetSelection<LabeledSet>::selectClasses ( allnegative_classes,
  116. base, negatives, dummy );
  117. }
  118. /** MultiDataset ------- constructor */
  119. MultiDataset::MultiDataset( const Config *conf , LabeledSetFactory *pSetFactory)
  120. {
  121. //read all blocks from our config file
  122. std::set<string> blocks;
  123. conf->getAllBlocks ( blocks );
  124. #ifdef DEBUG_MultiDataset
  125. std::cerr << "found the following config blocks: " << std::endl;
  126. for ( std::set<string>::const_iterator blockIt = blocks.begin(); blockIt != blocks.end(); blockIt++)
  127. {
  128. std::cerr << *blockIt << " ";
  129. }
  130. std::cerr << std::endl;
  131. #endif
  132. lfl.setFactory( pSetFactory );
  133. //for every dataset (e.g., train and test), we store a single confog file
  134. map<string, Config> dsconfs;
  135. //for every dataset (e.g., train and test), we store the position of the file directory
  136. map<string, string> dirs;
  137. //first of all, remove all blocks which do correspond to specified datasets, i.e., that do not contain a "dataset" entry
  138. for ( set<string>::iterator i = blocks.begin();
  139. i != blocks.end(); )
  140. {
  141. if ( conf->gB(*i, "disable", false) )
  142. {
  143. i++;
  144. continue;
  145. }
  146. std::string dataset = conf->gS( *i, "dataset", "unknown" );
  147. if ( dataset == "unknown" )
  148. blocks.erase(i++);
  149. else {
  150. #ifdef DEBUG_MultiDataset
  151. fprintf (stderr, "Reading dataset config for block [%s]\n", i->c_str() );
  152. #endif
  153. Config dsconf ( (dataset + "/dataset.conf").c_str() );
  154. dirs[*i] = dataset;
  155. dsconfs[*i] = dsconf;
  156. i++;
  157. }
  158. }
  159. #ifdef DEBUG_MultiDataset
  160. std::cerr << "found the following datasets within all config blocks: " << std::endl;
  161. for ( std::set<string>::const_iterator blockIt = blocks.begin(); blockIt != blocks.end(); blockIt++)
  162. {
  163. std::cerr << *blockIt << " ";
  164. }
  165. std::cerr << std::endl;
  166. #endif
  167. //is there a dataset specified that contains images for both, training and testing?
  168. if ( blocks.find("traintest") != blocks.end() )
  169. {
  170. LabeledSet ls_base;
  171. LabeledSet ls_train (true);
  172. LabeledSet ls_nontrain (true);
  173. LabeledSet ls_test (true);
  174. LabeledSet dummy (true);
  175. LabeledSet temp (true);
  176. bool localizationInfoDisabled = conf->gB("traintest", "disable_localization_info", false );
  177. std::string classselection_train = conf->gS("traintest", "classselection_train", "*");
  178. std::string classselection_test = conf->gS("traintest", "classselection_test", "*");
  179. classnames["traintest"] = ClassNames();
  180. std::string classNamesTxt = dirs["traintest"] + "/classnames.txt";
  181. if ( FileMgt::fileExists ( classNamesTxt ) )
  182. {
  183. classnames["traintest"].read ( classNamesTxt );
  184. } else {
  185. classnames["traintest"].readFromConfig ( dsconfs["traintest"], classselection_train );
  186. }
  187. lfl.get ( dirs["traintest"], dsconfs["traintest"], classnames["traintest"], ls_base,
  188. localizationInfoDisabled, conf->gB("traintest", "debug_dataset", false ) );
  189. std::string examples_train = conf->gS("traintest", "examples_train" );
  190. selectExamples ( examples_train, ls_base, ls_train, ls_nontrain, classnames["traintest"] );
  191. set<int> selection_test;
  192. classnames["traintest"].getSelection ( classselection_test, selection_test );
  193. std::string examples_test = conf->gS("traintest", "examples_test" );
  194. if ( examples_test == "reclassification" )
  195. {
  196. LabeledSetSelection<LabeledSet>::selectClasses
  197. ( selection_test, ls_train, ls_test, dummy );
  198. } else {
  199. selectExamples ( examples_test, ls_nontrain, temp, dummy, classnames["traintest"] );
  200. LabeledSetSelection<LabeledSet>::selectClasses
  201. ( selection_test, temp, ls_test, dummy );
  202. }
  203. classnames["train"] = classnames["traintest"];
  204. classnames["test"] = ClassNames ( classnames["traintest"], classselection_test );
  205. datasets["test"] = ls_test;
  206. datasets["train"] = ls_train;
  207. }
  208. //now read files for every specified dataset (e.g., train and test)
  209. for ( set<string>::const_iterator i = blocks.begin();
  210. i != blocks.end();
  211. i++ )
  212. {
  213. std::string name = *i;
  214. std::cerr << "read: " << name << std::endl;
  215. if ( classnames.find(name) != classnames.end() )
  216. continue;
  217. if ( conf->gB(name, "disable", false) == true )
  218. continue;
  219. if ( dsconfs.find(name) == dsconfs.end() )
  220. continue;
  221. LabeledSet ls_base;
  222. LabeledSet ls (true);
  223. LabeledSet dummy (true);
  224. LabeledSet temp (true);
  225. bool localizationInfoDisabled = conf->gB(name, "disable_localization_info", false );
  226. std::string classselection = conf->gS(name, "classselection", "*");
  227. classnames[name] = ClassNames();
  228. std::string classNamesTxt = dirs[name] + "/classnames.txt";
  229. if ( FileMgt::fileExists ( classNamesTxt ) )
  230. {
  231. #ifdef DEBUG_MultiDataset
  232. fprintf (stderr, "MultiDataset: reading class names from %s\n", classNamesTxt.c_str() );
  233. #endif
  234. classnames[name].read ( classNamesTxt );
  235. } else {
  236. #ifdef DEBUG_MultiDataset
  237. fprintf (stderr, "MultiDataset: reading class names from dataset config file\n" );
  238. #endif
  239. classnames[name].readFromConfig ( dsconfs[name], classselection );
  240. }
  241. #ifdef DEBUG_MultiDataset
  242. std::cerr << "we set up everything to read this dataset - so now call lfl.get" << std::endl;
  243. #endif
  244. lfl.get ( dirs[name],
  245. dsconfs[name],
  246. classnames[name],
  247. ls_base,
  248. localizationInfoDisabled,
  249. conf->gB(name, "debug_dataset", false ) );
  250. #ifdef DEBUG_MultiDataset
  251. fprintf (stderr, "MultiDataset: class names -->\n" );
  252. classnames[name].store ( cerr );
  253. fprintf (stderr, "MultiDataset: all information about %s set obtained ! (size %d)\n", name.c_str(), ls_base.count() );
  254. #endif
  255. #ifdef DEBUG_MultiDataset
  256. std::cerr << "we now call selectExamples to pick only a subset if desired" << std::endl;
  257. #endif
  258. std::string examples = conf->gS(name, "examples", "all *" );
  259. selectExamples ( examples, ls_base, ls, dummy, classnames[name] );
  260. #ifdef DEBUG_MultiDataset
  261. fprintf (stderr, "MultiDataset: size after selection %d\n", ls.count() );
  262. #endif
  263. datasets[name] = ls;
  264. }
  265. bool dumpSelections = conf->gB("datasets", "dump_selection", false);
  266. if ( dumpSelections )
  267. {
  268. for ( map<string, LabeledSet>::const_iterator i = datasets.begin();
  269. i != datasets.end(); i++ )
  270. {
  271. const std::string & name = i->first;
  272. const LabeledSet & ls = i->second;
  273. const ClassNames & classNames = classnames[name];
  274. mkdir ( name.c_str(), 0755 );
  275. std::string filelist = name + "/files.txt";
  276. ofstream olist ( filelist.c_str(), ios::out );
  277. if ( !olist.good() )
  278. fthrow (IOException, "Unable to dump selections to " << filelist );
  279. LOOP_ALL_S(ls)
  280. {
  281. EACH_S(classno, file);
  282. std::string classtext = classNames.code(classno);
  283. olist << classtext << " " << file << endl;
  284. }
  285. olist.close();
  286. std::string datasetconf = name + "/dataset.conf";
  287. ofstream oconf ( datasetconf.c_str(), ios::out );
  288. if ( !oconf.good() )
  289. fthrow (IOException, "Unable to dump selections to " << datasetconf );
  290. set<int> classnos;
  291. classNames.getSelection ( "*", classnos);
  292. oconf << "[main]" << endl;
  293. oconf << "filelist = \"files.txt\"" << endl << endl;
  294. oconf << "[classnames]" << endl;
  295. for ( set<int>::const_iterator i = classnos.begin();
  296. i != classnos.end(); i++ )
  297. {
  298. const std::string & code = classNames.code(*i);
  299. const std::string & text = classNames.text(*i);
  300. oconf << code << " = \"" << text << "\"" << endl;
  301. }
  302. oconf.close();
  303. classNames.save ( name + "/classnames.txt" );
  304. }
  305. }
  306. }
  307. MultiDataset::~MultiDataset()
  308. {
  309. }
  310. const ClassNames & MultiDataset::getClassNames ( const std::string & key ) const
  311. {
  312. map<string, ClassNames>::const_iterator i = classnames.find(key);
  313. if ( i == classnames.end() )
  314. {
  315. fprintf (stderr, "MultiDataSet::getClassNames() FATAL ERROR: dataset <%s> not found !\n", key.c_str() );
  316. exit(-1);
  317. }
  318. return (i->second);
  319. }
  320. const LabeledSet *MultiDataset::operator[] ( const std::string & key ) const
  321. {
  322. map<string, LabeledSet>::const_iterator i = datasets.find(key);
  323. if ( i == datasets.end() )
  324. {
  325. fprintf (stderr, "MultiDataSet: FATAL ERROR: dataset <%s> not found !\n", key.c_str() );
  326. exit(-1);
  327. }
  328. return &(i->second);
  329. }
  330. const LabeledSet *MultiDataset::at ( const std::string & key ) const
  331. {
  332. map<string, LabeledSet>::const_iterator i = datasets.find(key);
  333. if ( i == datasets.end() )
  334. return NULL;
  335. else
  336. return &(i->second);
  337. }