MultiDataset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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] != "all")) )
  35. fthrow( Exception, "Syntax error " << examples_command );
  36. const std::string & mode = parts[0];
  37. const std::string & csel = parts[1];
  38. double parameter = (parts.size() == 3 ) ? atof(parts[2].c_str()) : 0.0;
  39. map<int, int> fpe;
  40. set<int> selection;
  41. cn.getSelection ( csel, selection );
  42. for ( set<int>::const_iterator j = selection.begin();
  43. j != selection.end();
  44. j++ )
  45. {
  46. int classno = *j;
  47. if ( processed_classes.find(classno) == processed_classes.end() )
  48. {
  49. #ifdef DEBUG_MultiDataset
  50. fprintf (stderr, "class %s: %s %d\n", cn.text(classno).c_str(),
  51. mode.c_str(), (int)parameter );
  52. #endif
  53. fpe[*j] = (int)parameter;
  54. processed_classes.insert(classno);
  55. } else {
  56. if ( csel != "*" ) {
  57. fthrow ( Exception, "Example selection method for class %s has multiple specifications" << cn.text(classno) );
  58. }
  59. }
  60. }
  61. if ( mode == "seq" ) {
  62. LabeledSetSelection<LabeledSet>::selectSequential (
  63. fpe, base, positives, negatives );
  64. #ifdef DEBUG_MultiDataset
  65. fprintf (stderr, "MultiDataset: after special seq selection: %d\n", positives.count() );
  66. #endif
  67. } else if ( mode == "step" ) {
  68. LabeledSetSelection<LabeledSet>::selectSequentialStep (
  69. fpe, base, positives, negatives );
  70. #ifdef DEBUG_MultiDataset
  71. fprintf (stderr, "MultiDataset: after special step selection: %d\n", positives.count() );
  72. #endif
  73. } else if ( mode == "random" ) {
  74. LabeledSetSelection<LabeledSet>::selectRandom (
  75. fpe, base, positives, negatives );
  76. #ifdef DEBUG_MultiDataset
  77. fprintf (stderr, "MultiDataset: after special random selection: %d\n", positives.count() );
  78. #endif
  79. } else if ( mode == "all" ) {
  80. if ( (int)selection.size() == cn.numClasses() )
  81. {
  82. // preserve permutation
  83. LabeledSet::Permutation permutation;
  84. base.getPermutation ( permutation );
  85. for ( LabeledSet::Permutation::iterator i = permutation.begin(); i != permutation.end(); i++ )
  86. {
  87. int classno = i->first;
  88. ImageInfo *element = const_cast< ImageInfo * > ( i->second );
  89. positives.add_reference ( classno, element );
  90. }
  91. } else {
  92. LabeledSetSelection<LabeledSet>::selectClasses ( selection, base, positives, negatives );
  93. }
  94. #ifdef DEBUG_MultiDataset
  95. fprintf (stderr, "MultiDataset: after special class selection: %d\n", positives.count() );
  96. #endif
  97. } else {
  98. fthrow ( Exception, "Wrong value for parameter example\n");
  99. }
  100. }
  101. #ifdef DEBUG_MultiDataset
  102. fprintf (stderr, "MultiDataset: after special selection operations: %d\n", positives.count() );
  103. #endif
  104. set<int> allclasses;
  105. cn.getSelection ( "*", allclasses );
  106. set<int> allnegative_classes;
  107. // add all examples from allclasses \setminus processed_classes
  108. set_difference(allclasses.begin(), allclasses.end(), processed_classes.begin(), processed_classes.end(),
  109. inserter(allnegative_classes, allnegative_classes.end()));
  110. LabeledSet dummy;
  111. LabeledSetSelection<LabeledSet>::selectClasses ( allnegative_classes,
  112. base, negatives, dummy );
  113. }
  114. /** MultiDataset ------- constructor */
  115. MultiDataset::MultiDataset( const Config *conf )
  116. {
  117. std::set<string> blocks;
  118. conf->getAllBlocks ( blocks );
  119. map<string, Config> dsconfs;
  120. map<string, string> dirs;
  121. for ( set<string>::iterator i = blocks.begin();
  122. i != blocks.end(); )
  123. {
  124. if ( conf->gB(*i, "disable", false) )
  125. {
  126. i++;
  127. continue;
  128. }
  129. std::string dataset = conf->gS( *i, "dataset", "unknown" );
  130. if ( dataset == "unknown" )
  131. blocks.erase(i++);
  132. else {
  133. #ifdef DEBUG_MultiDataset
  134. fprintf (stderr, "Reading dataset config for block [%s]\n", i->c_str() );
  135. #endif
  136. Config dsconf ( (dataset + "/dataset.conf").c_str() );
  137. dirs[*i] = dataset;
  138. dsconfs[*i] = dsconf;
  139. i++;
  140. }
  141. }
  142. if ( blocks.find("traintest") != blocks.end() )
  143. {
  144. LabeledSet ls_base;
  145. LabeledSet ls_train (true);
  146. LabeledSet ls_nontrain (true);
  147. LabeledSet ls_test (true);
  148. LabeledSet dummy (true);
  149. LabeledSet temp (true);
  150. bool localizationInfoDisabled = conf->gB("traintest", "disable_localization_info", false );
  151. std::string classselection_train = conf->gS("traintest", "classselection_train", "*");
  152. std::string classselection_test = conf->gS("traintest", "classselection_test", "*");
  153. classnames["traintest"] = ClassNames();
  154. std::string classNamesTxt = dirs["traintest"] + "/classnames.txt";
  155. if ( FileMgt::fileExists ( classNamesTxt ) )
  156. {
  157. classnames["traintest"].read ( classNamesTxt );
  158. } else {
  159. classnames["traintest"].readFromConfig ( dsconfs["traintest"], classselection_train );
  160. }
  161. lfl.get ( dirs["traintest"], dsconfs["traintest"], classnames["traintest"], ls_base,
  162. localizationInfoDisabled, conf->gB("traintest", "debug_dataset", false ) );
  163. std::string examples_train = conf->gS("traintest", "examples_train" );
  164. selectExamples ( examples_train, ls_base, ls_train, ls_nontrain, classnames["traintest"] );
  165. set<int> selection_test;
  166. classnames["traintest"].getSelection ( classselection_test, selection_test );
  167. std::string examples_test = conf->gS("traintest", "examples_test" );
  168. if ( examples_test == "reclassification" )
  169. {
  170. LabeledSetSelection<LabeledSet>::selectClasses
  171. ( selection_test, ls_train, ls_test, dummy );
  172. } else {
  173. selectExamples ( examples_test, ls_nontrain, temp, dummy, classnames["traintest"] );
  174. LabeledSetSelection<LabeledSet>::selectClasses
  175. ( selection_test, temp, ls_test, dummy );
  176. }
  177. classnames["train"] = classnames["traintest"];
  178. classnames["test"] = ClassNames ( classnames["traintest"], classselection_test );
  179. datasets["test"] = ls_test;
  180. datasets["train"] = ls_train;
  181. }
  182. for ( set<string>::const_iterator i = blocks.begin();
  183. i != blocks.end();
  184. i++ )
  185. {
  186. std::string name = *i;
  187. if ( classnames.find(name) != classnames.end() )
  188. continue;
  189. if ( conf->gB(name, "disable", false) == true )
  190. continue;
  191. if ( dsconfs.find(name) == dsconfs.end() )
  192. continue;
  193. LabeledSet ls_base;
  194. LabeledSet ls (true);
  195. LabeledSet dummy (true);
  196. LabeledSet temp (true);
  197. bool localizationInfoDisabled = conf->gB(name, "disable_localization_info", false );
  198. std::string classselection = conf->gS(name, "classselection", "*");
  199. classnames[name] = ClassNames();
  200. std::string classNamesTxt = dirs[name] + "/classnames.txt";
  201. if ( FileMgt::fileExists ( classNamesTxt ) )
  202. {
  203. #ifdef DEBUG_MultiDataset
  204. fprintf (stderr, "MultiDataset: reading class names from %s\n", classNamesTxt.c_str() );
  205. #endif
  206. classnames[name].read ( classNamesTxt );
  207. } else {
  208. #ifdef DEBUG_MultiDataset
  209. fprintf (stderr, "MultiDataset: reading class names from dataset config file\n" );
  210. #endif
  211. classnames[name].readFromConfig ( dsconfs[name], classselection );
  212. }
  213. lfl.get ( dirs[name], dsconfs[name], classnames[name], ls_base,
  214. localizationInfoDisabled, conf->gB(name, "debug_dataset", false ) );
  215. #ifdef DEBUG_MultiDataset
  216. fprintf (stderr, "MultiDataset: class names -->\n" );
  217. classnames[name].store ( cerr );
  218. fprintf (stderr, "MultiDataset: all information about %s set obtained ! (size %d)\n", name.c_str(), ls_base.count() );
  219. #endif
  220. std::string examples = conf->gS(name, "examples", "all *" );
  221. selectExamples ( examples, ls_base, ls, dummy, classnames[name] );
  222. #ifdef DEBUG_MultiDataset
  223. fprintf (stderr, "MultiDataset: size after selection %d\n", ls.count() );
  224. #endif
  225. datasets[name] = ls;
  226. }
  227. bool dumpSelections = conf->gB("datasets", "dump_selection", false);
  228. if ( dumpSelections )
  229. {
  230. for ( map<string, LabeledSet>::const_iterator i = datasets.begin();
  231. i != datasets.end(); i++ )
  232. {
  233. const std::string & name = i->first;
  234. const LabeledSet & ls = i->second;
  235. const ClassNames & classNames = classnames[name];
  236. mkdir ( name.c_str(), 0755 );
  237. std::string filelist = name + "/files.txt";
  238. ofstream olist ( filelist.c_str(), ios::out );
  239. if ( !olist.good() )
  240. fthrow (IOException, "Unable to dump selections to " << filelist );
  241. LOOP_ALL_S(ls)
  242. {
  243. EACH_S(classno, file);
  244. std::string classtext = classNames.code(classno);
  245. olist << classtext << " " << file << endl;
  246. }
  247. olist.close();
  248. std::string datasetconf = name + "/dataset.conf";
  249. ofstream oconf ( datasetconf.c_str(), ios::out );
  250. if ( !oconf.good() )
  251. fthrow (IOException, "Unable to dump selections to " << datasetconf );
  252. set<int> classnos;
  253. classNames.getSelection ( "*", classnos);
  254. oconf << "[main]" << endl;
  255. oconf << "filelist = \"files.txt\"" << endl << endl;
  256. oconf << "[classnames]" << endl;
  257. for ( set<int>::const_iterator i = classnos.begin();
  258. i != classnos.end(); i++ )
  259. {
  260. const std::string & code = classNames.code(*i);
  261. const std::string & text = classNames.text(*i);
  262. oconf << code << " = \"" << text << "\"" << endl;
  263. }
  264. oconf.close();
  265. classNames.save ( name + "/classnames.txt" );
  266. }
  267. }
  268. }
  269. MultiDataset::~MultiDataset()
  270. {
  271. }
  272. const ClassNames & MultiDataset::getClassNames ( const std::string & key ) const
  273. {
  274. map<string, ClassNames>::const_iterator i = classnames.find(key);
  275. if ( i == classnames.end() )
  276. {
  277. fprintf (stderr, "MultiDataSet::getClassNames() FATAL ERROR: dataset <%s> not found !\n", key.c_str() );
  278. exit(-1);
  279. }
  280. return (i->second);
  281. }
  282. const LabeledSet *MultiDataset::operator[] ( const std::string & key ) const
  283. {
  284. map<string, LabeledSet>::const_iterator i = datasets.find(key);
  285. if ( i == datasets.end() )
  286. {
  287. fprintf (stderr, "MultiDataSet: FATAL ERROR: dataset <%s> not found !\n", key.c_str() );
  288. exit(-1);
  289. }
  290. return &(i->second);
  291. }
  292. const LabeledSet *MultiDataset::at ( const std::string & key ) const
  293. {
  294. map<string, LabeledSet>::const_iterator i = datasets.find(key);
  295. if ( i == datasets.end() )
  296. return NULL;
  297. else
  298. return &(i->second);
  299. }