Commit d1b579f0 authored by Davis King's avatar Davis King

Added try/catch block to main().

parent 8c8c5bf3
...@@ -170,79 +170,87 @@ void make_training_examples( ...@@ -170,79 +170,87 @@ void make_training_examples(
int main() int main()
{ {
// Get the training samples we defined above. try
dlib::array<graph_type> samples; {
std::vector<std::vector<bool> > labels; // Get the training samples we defined above.
make_training_examples(samples, labels); dlib::array<graph_type> samples;
std::vector<std::vector<bool> > labels;
make_training_examples(samples, labels);
// Create a structural SVM trainer for graph labeling problems. The vector_type
// needs to be set to a type capable of holding node or edge vectors.
typedef matrix<double,0,1> vector_type; // Create a structural SVM trainer for graph labeling problems. The vector_type
structural_graph_labeling_trainer<vector_type> trainer; // needs to be set to a type capable of holding node or edge vectors.
// This is the usual SVM C parameter. Larger values make the trainer try typedef matrix<double,0,1> vector_type;
// harder to fit the training data but might result in overfitting. You structural_graph_labeling_trainer<vector_type> trainer;
// should set this value to whatever gives the best cross-validation results. // This is the usual SVM C parameter. Larger values make the trainer try
trainer.set_c(10); // harder to fit the training data but might result in overfitting. You
// should set this value to whatever gives the best cross-validation results.
// Do 3-fold cross-validation and print the results. In this case it will trainer.set_c(10);
// indicate that all nodes were correctly classified.
cout << "3-fold cross-validation: " << cross_validate_graph_labeling_trainer(trainer, samples, labels, 3) << endl; // Do 3-fold cross-validation and print the results. In this case it will
// indicate that all nodes were correctly classified.
// Since the trainer is working well. Lets have it make a graph_labeler cout << "3-fold cross-validation: " << cross_validate_graph_labeling_trainer(trainer, samples, labels, 3) << endl;
// based on the training data.
graph_labeler<vector_type> labeler = trainer.train(samples, labels); // Since the trainer is working well. Lets have it make a graph_labeler
// based on the training data.
graph_labeler<vector_type> labeler = trainer.train(samples, labels);
/*
Lets try the graph_labeler on a new test graph. In particular, lets
use one with 5 nodes as shown below: /*
Lets try the graph_labeler on a new test graph. In particular, lets
(0 F)-----(1 T) use one with 5 nodes as shown below:
| |
| | (0 F)-----(1 T)
| | | |
(3 T)-----(2 T)------(4 T) | |
| |
I have annotated each node with either T or F to indicate the correct (3 T)-----(2 T)------(4 T)
output (true or false).
*/ I have annotated each node with either T or F to indicate the correct
graph_type g; output (true or false).
g.set_number_of_nodes(5); */
g.node(0).data = 1, 0; // Node data indicates a false node. graph_type g;
g.node(1).data = 0, 1; // Node data indicates a true node. g.set_number_of_nodes(5);
g.node(2).data = 0, 0; // Node data is ambiguous. g.node(0).data = 1, 0; // Node data indicates a false node.
g.node(3).data = 0, 0; // Node data is ambiguous. g.node(1).data = 0, 1; // Node data indicates a true node.
g.node(4).data = 0.1, 0; // Node data slightly indicates a false node. g.node(2).data = 0, 0; // Node data is ambiguous.
g.node(3).data = 0, 0; // Node data is ambiguous.
g.add_edge(0,1); g.node(4).data = 0.1, 0; // Node data slightly indicates a false node.
g.add_edge(1,2);
g.add_edge(2,3); g.add_edge(0,1);
g.add_edge(3,0); g.add_edge(1,2);
g.add_edge(2,4); g.add_edge(2,3);
g.add_edge(3,0);
// Set the edges up so nodes 1, 2, 3, and 4 are all strongly connected. g.add_edge(2,4);
edge(g,0,1) = 0;
edge(g,1,2) = 1; // Set the edges up so nodes 1, 2, 3, and 4 are all strongly connected.
edge(g,2,3) = 1; edge(g,0,1) = 0;
edge(g,3,0) = 0; edge(g,1,2) = 1;
edge(g,2,4) = 1; edge(g,2,3) = 1;
edge(g,3,0) = 0;
// The output of this shows all the nodes are correctly labeled. edge(g,2,4) = 1;
cout << "Predicted labels: " << endl;
std::vector<bool> temp = labeler(g); // The output of this shows all the nodes are correctly labeled.
for (unsigned long i = 0; i < temp.size(); ++i) cout << "Predicted labels: " << endl;
cout << " " << i << ": " << temp[i] << endl; std::vector<bool> temp = labeler(g);
for (unsigned long i = 0; i < temp.size(); ++i)
cout << " " << i << ": " << temp[i] << endl;
// Breaking the strong labeling consistency link between node 1 and 2 causes
// nodes 2, 3, and 4 to flip to false. This is because of their connection
// to node 4 which has a small preference for false. // Breaking the strong labeling consistency link between node 1 and 2 causes
edge(g,1,2) = 0; // nodes 2, 3, and 4 to flip to false. This is because of their connection
cout << "Predicted labels: " << endl; // to node 4 which has a small preference for false.
temp = labeler(g); edge(g,1,2) = 0;
for (unsigned long i = 0; i < temp.size(); ++i) cout << "Predicted labels: " << endl;
cout << " " << i << ": " << temp[i] << endl; temp = labeler(g);
for (unsigned long i = 0; i < temp.size(); ++i)
cout << " " << i << ": " << temp[i] << endl;
}
catch (std::exception& e)
{
cout << "Error, an exception was thrown!" << endl;
cout << e.what() << endl;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment