323 {
324
325 std::string custom_weightfile = weightfile.generateFileName();
326 weightfile.getFile("FastBDT_Weightfile", custom_weightfile);
327 std::fstream file(custom_weightfile, std::ios_base::in);
328
329 int version = weightfile.getElement<int>("FastBDT_version", 0);
330 B2DEBUG(100, "FastBDT Weightfile Version " << version);
331 if (version < 2) {
332#if FastBDT_VERSION_MAJOR >= 3
333 std::stringstream s;
334 {
335 std::string t;
336 std::fstream file2(custom_weightfile, std::ios_base::in);
337 getline(file2, t);
338 s << t;
339 }
340 int dummy;
341
342 if (!(s >> dummy >> dummy)) {
343 B2DEBUG(100, "FastBDT: I read a new weightfile of FastBDT using the new FastBDT version 3. Everything fine!");
344
346 } else {
347 B2INFO("FastBDT: I read an old weightfile of FastBDT using the new FastBDT version 3."
348 "I will convert your FastBDT on-the-fly to the new version."
349 "Retrain the classifier to get rid of this message");
350
351
352 std::vector<FastBDT::FeatureBinning<float>> feature_binnings;
353 file >> feature_binnings;
354 double F0;
355 file >> F0;
356 double shrinkage;
357 file >> shrinkage;
358
359 bool transform2probability = true;
360 FastBDT::Forest<unsigned int> temp_forest(shrinkage, F0, transform2probability);
361 unsigned int size;
362 file >> size;
363 for (unsigned int i = 0; i < size; ++i) {
364 temp_forest.AddTree(FastBDT::readTreeFromStream<unsigned int>(file));
365 }
366
367 FastBDT::Forest<float> cleaned_forest(temp_forest.GetShrinkage(), temp_forest.GetF0(), temp_forest.GetTransform2Probability());
368 for (auto& tree : temp_forest.GetForest()) {
369 cleaned_forest.AddTree(FastBDT::removeFeatureBinningTransformationFromTree(tree, feature_binnings));
370 }
372 }
373#else
374 B2INFO("FastBDT: I read an old weightfile of FastBDT using the old FastBDT version."
375 "I try to fix the weightfile first to avoid problems due to NaN and inf values."
376 "Consider to switch to the newer version of FastBDT (newer externals)");
377
378 std::stringstream s;
379 std::string t;
380 while (getline(file, t)) {
381 size_t f = 0;
382
383 while ((f = t.find("inf", f)) != std::string::npos) {
384 t.replace(f, std::string("inf").length(), std::string("0.0"));
385 f += std::string("0.0").length();
386 B2WARNING("Found infinity in FastBDT weightfile, I replace it with 0 to prevent horrible crashes, this is fixed in the newer version");
387 }
388 f = 0;
389 while ((f = t.find("nan", f)) != std::string::npos) {
390 t.replace(f, std::string("nan").length(), std::string("0.0"));
391 f += std::string("0.0").length();
392 B2WARNING("Found nan in FastBDT weightfile, I replace it with 0 to prevent horrible crashes, this is fixed in the newer version");
393 }
394 s << t + '\n';
395 }
398#endif
399 }
400#if FastBDT_VERSION_MAJOR >= 5
401 else {
402 m_use_simplified_interface = true;
403 m_classifier = FastBDT::Classifier(file);
404 }
405#else
406 else {
407 B2ERROR("Unknown Version 2 of Weightfile, please use a more recent FastBDT version");
408 }
409#endif
410 file.close();
411
413 }
FastBDTOptions m_specific_options
Method specific options.