From ad3ede2d1282c45fc4f5f25fccc4809ca5c9fead Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 5 May 2012 14:30:22 -0400 Subject: [PATCH] Added unit tests for the graph labeling stuff. --- dlib/test/CMakeLists.txt | 1 + dlib/test/graph_labeler.cpp | 434 ++++++++++++++++++++++++++++++++++++ dlib/test/makefile | 1 + 3 files changed, 436 insertions(+) create mode 100644 dlib/test/graph_labeler.cpp diff --git a/dlib/test/CMakeLists.txt b/dlib/test/CMakeLists.txt index 003c747df..0cd8a77e8 100644 --- a/dlib/test/CMakeLists.txt +++ b/dlib/test/CMakeLists.txt @@ -46,6 +46,7 @@ set (tests geometry.cpp graph.cpp graph_cuts.cpp + graph_labeler.cpp hash.cpp hash_map.cpp hash_set.cpp diff --git a/dlib/test/graph_labeler.cpp b/dlib/test/graph_labeler.cpp new file mode 100644 index 000000000..0a5aae900 --- /dev/null +++ b/dlib/test/graph_labeler.cpp @@ -0,0 +1,434 @@ +// Copyright (C) 2012 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#include +#include +#include +#include +#include +#include + +#include "tester.h" + +namespace +{ + + using namespace test; + using namespace dlib; + using namespace std; + + + + logger dlog("test.graph_cuts"); + + + template < + typename graph_type, + typename samples_type, + typename labels_type + > + void make_data( + samples_type& samples, + labels_type& labels + ) + { + //samples.clear(); + //labels.clear(); + + std::vector label; + graph_type g; + + // --------------------------- + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data = 0, 0, 1; label[0] = 1; + g.node(1).data = 0, 0, 1; label[1] = 1; + g.node(2).data = 0, 1, 0; label[2] = 0; + g.node(3).data = 0, 1, 0; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1) = 1, 1; + edge(g,1,2) = 1, 1; + edge(g,2,3) = 1, 1; + edge(g,3,0) = 1, 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + + g.clear(); + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data = 0, 0, 1; label[0] = 1; + g.node(1).data = 0, 0, 0; label[1] = 1; + g.node(2).data = 0, 1, 0; label[2] = 0; + g.node(3).data = 0, 0, 0; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1) = 1, 0; + edge(g,1,2) = 0, 1; + edge(g,2,3) = 1, 0; + edge(g,3,0) = 0, 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + + g.clear(); + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data = 0, 1, 0; label[0] = 0; + g.node(1).data = 0, 1, 0; label[1] = 0; + g.node(2).data = 0, 1, 0; label[2] = 0; + g.node(3).data = 0, 0, 0; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1) = 1, 0; + edge(g,1,2) = 0, 1; + edge(g,2,3) = 1, 0; + edge(g,3,0) = 0, 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + } + + + + + template < + typename graph_type, + typename samples_type, + typename labels_type + > + void make_data_sparse( + samples_type& samples, + labels_type& labels + ) + { + //samples.clear(); + //labels.clear(); + + std::vector label; + graph_type g; + typename graph_type::edge_type v; + + // --------------------------- + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data[2] = 1; label[0] = 1; + g.node(1).data[2] = 1; label[1] = 1; + g.node(2).data[1] = 1; label[2] = 0; + g.node(3).data[1] = 1; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + g.add_edge(3,1); + + v[0] = 1; v[1] = 1; + edge(g,0,1) = v; + edge(g,1,2) = v; + edge(g,2,3) = v; + edge(g,3,0) = v; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + + g.clear(); + g.set_number_of_nodes(5); + label.resize(g.number_of_nodes()); + g.node(0).data[2] = 1; label[0] = 1; + g.node(1).data[0] = 0; label[1] = 1; + g.node(2).data[1] = 1; label[2] = 0; + g.node(3).data[0] = 0; label[3] = 0; + label[4] = 1; + + g.add_edge(0,1); + g.add_edge(1,4); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1)[0] = 1; + edge(g,1,4)[0] = 1; + edge(g,1,2)[1] = 1; + edge(g,2,3)[0] = 1; + edge(g,3,0)[1] = 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + + g.clear(); + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data[1] = 1; label[0] = 0; + g.node(1).data[1] = 1; label[1] = 0; + g.node(2).data[1] = 1; label[2] = 0; + g.node(3).data[1] = 0; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1)[0] = 1; + edge(g,1,2)[1] = 1; + edge(g,2,3)[0] = 1; + edge(g,3,0)[1] = 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + } + + + + + + + template < + typename graph_type, + typename samples_type, + typename labels_type + > + void make_data2( + samples_type& samples, + labels_type& labels + ) + { + //samples.clear(); + //labels.clear(); + + std::vector label; + graph_type g; + + // --------------------------- + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data = 0, 0, 1; label[0] = 1; + g.node(1).data = 0, 0, 1; label[1] = 1; + g.node(2).data = 0, 1, 0; label[2] = 0; + g.node(3).data = 0, 1, 0; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + edge(g,0,1) = 1, 1; + edge(g,1,2) = 1, 1; + edge(g,2,3) = 1, 1; + edge(g,3,0) = 1, 1; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + } + + + + + template < + typename graph_type, + typename samples_type, + typename labels_type + > + void make_data2_sparse( + samples_type& samples, + labels_type& labels + ) + { + //samples.clear(); + //labels.clear(); + + std::vector label; + graph_type g; + typename graph_type::edge_type v; + + // --------------------------- + g.set_number_of_nodes(4); + label.resize(g.number_of_nodes()); + g.node(0).data[2] = 1; label[0] = 1; + g.node(1).data[2] = 1; label[1] = 1; + g.node(2).data[1] = 1; label[2] = 0; + g.node(3).data[1] = 1; label[3] = 0; + + g.add_edge(0,1); + g.add_edge(1,2); + g.add_edge(2,3); + g.add_edge(3,0); + + v[0] = 1; v[1] = 1; + edge(g,0,1) = v; + edge(g,1,2) = v; + edge(g,2,3) = v; + edge(g,3,0) = v; + samples.push_back(g); + labels.push_back(label); + // --------------------------- + + } + + + + + + template < + typename node_vector_type, + typename edge_vector_type, + typename vector_type, + typename graph_type + > + void test1( + const dlib::array& samples, + const std::vector >& labels + ) + { + dlog << LINFO << "begin test1()"; + + structural_graph_labeling_trainer trainer; + //trainer.be_verbose(); + trainer.set_epsilon(1e-12); + graph_labeler labeler = trainer.train(samples, labels); + + + // test serialization code for the labeler. + std::ostringstream sout; + serialize(labeler, sout); + std::istringstream sin(sout.str()); + labeler = graph_labeler(); + deserialize(labeler, sin); + + std::vector temp; + for (unsigned long k = 0; k < samples.size(); ++k) + { + temp = labeler(samples[k]); + for (unsigned long i = 0; i < temp.size(); ++i) + { + const bool true_label = (labels[k][i] != 0); + const bool pred_label = (temp[i] != 0); + DLIB_TEST(true_label == pred_label); + } + } + + matrix cv; + + cv = test_graph_labeling_function(labeler, samples, labels); + DLIB_TEST(sum(cv) == 2); + cv = cross_validate_graph_labeling_trainer(trainer, samples, labels, 4); + DLIB_TEST(sum(cv) == 2); + + dlog << LINFO << "edge weights: " << trans(sparse_to_dense(labeler.get_edge_weights())); + dlog << LINFO << "node weights: " << trans(sparse_to_dense(labeler.get_node_weights())); + } + + + + class graph_labeling_tester : public tester + { + public: + graph_labeling_tester ( + ) : + tester ("test_graph_labeling", + "Runs tests on the graph labeling component.") + {} + + void perform_test ( + ) + { + print_spinner(); + // test with dense vectors + { + typedef matrix node_vector_type; + typedef matrix edge_vector_type; + typedef matrix vector_type; + typedef dlib::graph::kernel_1a_c graph_type; + + dlib::array samples; + std::vector > labels; + + make_data(samples, labels); + make_data(samples, labels); + make_data(samples, labels); + make_data(samples, labels); + + + test1(samples, labels); + } + print_spinner(); + // test with sparse vectors + { + typedef std::vector > vector_type; + typedef std::map edge_vector_type; + typedef std::map node_vector_type; + typedef dlib::graph::kernel_1a_c graph_type; + + dlib::array samples; + std::vector > labels; + + make_data_sparse(samples, labels); + make_data_sparse(samples, labels); + make_data_sparse(samples, labels); + make_data_sparse(samples, labels); + + + test1(samples, labels); + } + + + + print_spinner(); + // test with dense vectors + { + typedef matrix node_vector_type; + typedef matrix edge_vector_type; + typedef matrix vector_type; + typedef dlib::graph::kernel_1a_c graph_type; + + dlib::array samples; + std::vector > labels; + + make_data2(samples, labels); + make_data2(samples, labels); + make_data2(samples, labels); + make_data2(samples, labels); + + + test1(samples, labels); + } + print_spinner(); + // test with sparse vectors + { + typedef std::vector > vector_type; + typedef std::map edge_vector_type; + typedef std::map node_vector_type; + typedef dlib::graph::kernel_1a_c graph_type; + + dlib::array samples; + std::vector > labels; + + make_data2_sparse(samples, labels); + make_data2_sparse(samples, labels); + make_data2_sparse(samples, labels); + make_data2_sparse(samples, labels); + + + test1(samples, labels); + } + } + } a; + + +} + + + + diff --git a/dlib/test/makefile b/dlib/test/makefile index 6ff885f21..42b325597 100644 --- a/dlib/test/makefile +++ b/dlib/test/makefile @@ -61,6 +61,7 @@ SRC += find_max_factor_graph_viterbi.cpp SRC += geometry.cpp SRC += graph.cpp SRC += graph_cuts.cpp +SRC += graph_labeler.cpp SRC += hash.cpp SRC += hash_map.cpp SRC += hash_set.cpp