MultiDataset.cpp 13 KB

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