Separate random forest examples

This commit is contained in:
Simon Giraudot
2018-01-12 14:15:02 +01:00
parent d78ff28d53
commit ce4c967f12
7 changed files with 223 additions and 81 deletions
@@ -10,10 +10,8 @@ cmake_minimum_required(VERSION 2.8.11)
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
@@ -24,11 +22,8 @@ include( ${CGAL_USE_FILE} )
find_package( Boost REQUIRED COMPONENTS serialization iostreams)
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
find_package( TBB )
@@ -55,17 +50,21 @@ endif()
create_single_source_cgal_program( "example_generation_and_training.cpp" CXX_FEATURES ${needed_cxx_features} )
create_single_source_cgal_program( "example_feature.cpp" CXX_FEATURES ${needed_cxx_features} )
if (Boost_SERIALIZATION_FOUND AND Boost_IOSTREAMS_FOUND)
if( OpenCV_FOUND )
message(STATUS "Found OpenCV ${OpenCV_VERSION}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
create_single_source_cgal_program( "example_random_forest.cpp" CXX_FEATURES ${needed_cxx_features} )
target_link_libraries( example_random_forest ${OpenCV_LIBS} ${Boost_SERIALIZATION_LIBRARY} ${Boost_IOSTREAMS_LIBRARY})
target_compile_definitions(example_random_forest PUBLIC "-DCGAL_LINKED_WITH_OPENCV")
else()
create_single_source_cgal_program( "example_random_forest.cpp" CXX_FEATURES ${needed_cxx_features} )
target_link_libraries( example_random_forest ${Boost_SERIALIZATION_LIBRARY} ${Boost_IOSTREAMS_LIBRARY})
message(STATUS "OpenCV not found, random forest example won't have OpenCV classifier.")
endif()
if( OpenCV_FOUND )
message(STATUS "Found OpenCV ${OpenCV_VERSION}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
create_single_source_cgal_program( "example_opencv_random_forest.cpp" CXX_FEATURES ${needed_cxx_features} )
target_link_libraries( example_opencv_random_forest ${OpenCV_LIBS} )
target_compile_definitions(example_opencv_random_forest PUBLIC "-DCGAL_LINKED_WITH_OPENCV")
else()
message(STATUS "OpenCV not found, OpenCV random forest example won't be compiled.")
endif()
if (Boost_SERIALIZATION_FOUND AND Boost_IOSTREAMS_FOUND)
create_single_source_cgal_program( "example_ethz_random_forest.cpp" CXX_FEATURES ${needed_cxx_features} )
target_link_libraries( example_ethz_random_forest ${Boost_SERIALIZATION_LIBRARY} ${Boost_IOSTREAMS_LIBRARY})
target_compile_definitions(example_ethz_random_forest PUBLIC "-DCGAL_LINKED_WITH_BOOST_SERIALIZATION")
else()
message(STATUS "Boost serialization and IO streams not found, ETHZ random forest example won't be compiled.")
endif()
@@ -0,0 +1,156 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Classification.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/Point_set_3/IO.h>
#include <CGAL/Real_timer.h>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_set_3<Point> Point_set;
typedef Kernel::Iso_cuboid_3 Iso_cuboid_3;
typedef Point_set::Point_map Pmap;
typedef Point_set::Property_map<int> Imap;
typedef Point_set::Property_map<unsigned char> UCmap;
namespace Classification = CGAL::Classification;
typedef Classification::Label_handle Label_handle;
typedef Classification::Feature_handle Feature_handle;
typedef Classification::Label_set Label_set;
typedef Classification::Feature_set Feature_set;
typedef Classification::Point_set_feature_generator<Kernel, Point_set, Pmap> Feature_generator;
int main (int argc, char** argv)
{
std::string filename = "data/b9_training.ply";
bool use_opencv = false;
if (argc > 1)
{
if (std::string(argv[1]) == "-cv")
{
use_opencv = true;
if (argc > 2)
filename = argv[2];
}
else
filename = argv[1];
}
std::ifstream in (filename.c_str(), std::ios::binary);
Point_set pts;
std::cerr << "Reading input" << std::endl;
in >> pts;
Imap label_map;
bool lm_found = false;
boost::tie (label_map, lm_found) = pts.property_map<int> ("label");
if (!lm_found)
{
std::cerr << "Error: \"label\" property not found in input file." << std::endl;
return EXIT_FAILURE;
}
std::vector<int> ground_truth;
ground_truth.reserve (pts.size());
std::copy (pts.range(label_map).begin(), pts.range(label_map).end(),
std::back_inserter (ground_truth));
Feature_set features;
std::cerr << "Generating features" << std::endl;
CGAL::Real_timer t;
t.start();
Feature_generator generator (features, pts, pts.point_map(),
5); // using 5 scales
t.stop();
std::cerr << "Done in " << t.time() << " second(s)" << std::endl;
// Add types
Label_set labels;
Label_handle ground = labels.add ("ground");
Label_handle vegetation = labels.add ("vegetation");
Label_handle roof = labels.add ("roof");
std::vector<int> label_indices(pts.size(), -1);
std::cerr << "Using ETHZ Random Forest Classifier" << std::endl;
Classification::ETHZ_random_forest_classifier classifier (labels, features);
std::cerr << "Training" << std::endl;
t.reset();
t.start();
classifier.train (ground_truth);
t.stop();
std::cerr << "Done in " << t.time() << " second(s)" << std::endl;
t.reset();
t.start();
Classification::classify_with_graphcut<CGAL::Sequential_tag>
(pts, pts.point_map(), labels, classifier,
generator.neighborhood().k_neighbor_query(12),
0.2f, 1, label_indices);
t.stop();
std::cerr << "Classification with graphcut done in " << t.time() << " second(s)" << std::endl;
std::cerr << "Precision, recall, F1 scores and IoU:" << std::endl;
Classification::Evaluation evaluation (labels, ground_truth, label_indices);
for (std::size_t i = 0; i < labels.size(); ++ i)
{
std::cerr << " * " << labels[i]->name() << ": "
<< evaluation.precision(labels[i]) << " ; "
<< evaluation.recall(labels[i]) << " ; "
<< evaluation.f1_score(labels[i]) << " ; "
<< evaluation.intersection_over_union(labels[i]) << std::endl;
}
std::cerr << "Accuracy = " << evaluation.accuracy() << std::endl
<< "Mean F1 score = " << evaluation.mean_f1_score() << std::endl
<< "Mean IoU = " << evaluation.mean_intersection_over_union() << std::endl;
// Color point set according to class
UCmap red = pts.add_property_map<unsigned char>("red", 0).first;
UCmap green = pts.add_property_map<unsigned char>("green", 0).first;
UCmap blue = pts.add_property_map<unsigned char>("blue", 0).first;
for (std::size_t i = 0; i < label_indices.size(); ++ i)
{
label_map[i] = label_indices[i]; // update label map with computed classification
Label_handle label = labels[label_indices[i]];
if (label == ground)
{
red[i] = 245; green[i] = 180; blue[i] = 0;
}
else if (label == vegetation)
{
red[i] = 0; green[i] = 255; blue[i] = 27;
}
else if (label == roof)
{
red[i] = 255; green[i] = 0; blue[i] = 170;
}
}
// Write result
std::ofstream f ("classification.ply");
f.precision(18);
f << pts;
std::cerr << "All done" << std::endl;
return EXIT_SUCCESS;
}
@@ -1,8 +1,3 @@
#if defined (_MSC_VER) && !defined (_WIN64)
#pragma warning(disable:4244) // boost::number_distance::distance()
// converts 64 to 32 bits integers
#endif
#include <cstdlib>
#include <fstream>
#include <iostream>
@@ -51,14 +46,6 @@ int main (int argc, char** argv)
filename = argv[1];
}
#ifndef CGAL_LINKED_WITH_OPENCV
if (use_opencv)
{
std::cerr << "OpenCV not available, exiting." << std::endl;
return EXIT_SUCCESS;
}
#endif
std::ifstream in (filename.c_str(), std::ios::binary);
Point_set pts;
@@ -97,48 +84,23 @@ int main (int argc, char** argv)
std::vector<int> label_indices(pts.size(), -1);
#ifdef CGAL_LINKED_WITH_OPENCV
if (use_opencv)
{
std::cerr << "Using OpenCV Random Forest Classifier" << std::endl;
Classification::OpenCV_random_forest_classifier classifier (labels, features);
std::cerr << "Using OpenCV Random Forest Classifier" << std::endl;
Classification::OpenCV_random_forest_classifier classifier (labels, features);
std::cerr << "Training" << std::endl;
t.reset();
t.start();
classifier.train (ground_truth);
t.stop();
std::cerr << "Done in " << t.time() << " second(s)" << std::endl;
std::cerr << "Training" << std::endl;
t.reset();
t.start();
classifier.train (ground_truth);
t.stop();
std::cerr << "Done in " << t.time() << " second(s)" << std::endl;
t.reset();
t.start();
Classification::classify_with_graphcut<CGAL::Sequential_tag>
(pts, pts.point_map(), labels, classifier,
generator.neighborhood().k_neighbor_query(12),
0.2f, 1, label_indices);
t.stop();
}
else
#endif
{
std::cerr << "Using ETHZ Random Forest Classifier" << std::endl;
Classification::ETHZ_random_forest_classifier classifier (labels, features);
std::cerr << "Training" << std::endl;
t.reset();
t.start();
classifier.train (ground_truth);
t.stop();
std::cerr << "Done in " << t.time() << " second(s)" << std::endl;
t.reset();
t.start();
Classification::classify_with_graphcut<CGAL::Sequential_tag>
(pts, pts.point_map(), labels, classifier,
generator.neighborhood().k_neighbor_query(12),
0.2f, 1, label_indices);
t.stop();
}
t.reset();
t.start();
Classification::classify_with_graphcut<CGAL::Sequential_tag>
(pts, pts.point_map(), labels, classifier,
generator.neighborhood().k_neighbor_query(12),
0.2f, 1, label_indices);
t.stop();
std::cerr << "Classification with graphcut done in " << t.time() << " second(s)" << std::endl;
@@ -1,2 +0,0 @@
data/b9_training.ply
-cv data/b9_training.ply