Made the examples use the new simplified file serialization API.

pull/2/head
Davis King 11 years ago
parent eb2806f348
commit c6d778eaf9

@ -195,13 +195,10 @@ int main()
// Finally, the assigner can be serialized to disk just like most dlib objects. // Finally, the assigner can be serialized to disk just like most dlib objects.
ofstream fout("assigner.dat", ios::binary); serialize("assigner.dat") << assigner;
serialize(assigner, fout);
fout.close();
// recall from disk // recall from disk
ifstream fin("assigner.dat", ios::binary); deserialize("assigner.dat") >> assigner;
deserialize(assigner, fin);
} }
catch (std::exception& e) catch (std::exception& e)
{ {

@ -208,15 +208,12 @@ int main()
decision_function<radial_basis_kernel<sample_type> > // This is the output of the rbf_trainer decision_function<radial_basis_kernel<sample_type> > // This is the output of the rbf_trainer
> df2, df3; > df2, df3;
df2 = df; df2 = df;
ofstream fout("df.dat", ios::binary); // save to a file called df.dat
serialize(df2, fout); serialize("df.dat") << df2;
fout.close();
// load the function back in from disk and store it in df3. // load the function back in from disk and store it in df3.
ifstream fin("df.dat", ios::binary); deserialize("df.dat") >> df3;
deserialize(df3, fin);
// Test df3 to see that this worked. // Test df3 to see that this worked.

@ -180,14 +180,11 @@ int main(int argc, char** argv)
// Like everything in dlib, you can save your detector to disk using the // Like everything in dlib, you can save your detector to disk using the
// serialize() function. // serialize() function.
ofstream fout("face_detector.svm", ios::binary); serialize("face_detector.svm") << detector;
serialize(detector, fout);
fout.close();
// Then you can recall it using the deserialize() function. // Then you can recall it using the deserialize() function.
ifstream fin("face_detector.svm", ios::binary);
object_detector<image_scanner_type> detector2; object_detector<image_scanner_type> detector2;
deserialize(detector2, fin); deserialize("face_detector.svm") >> detector2;

@ -78,21 +78,17 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is serializable. // Another thing that is worth knowing is that just about everything in dlib is serializable.
// So for example, you can save the test object to disk and recall it later like so: // So for example, you can save the test object to disk and recall it later like so:
ofstream fout("saved_krls_object.dat",ios::binary); serialize("saved_krls_object.dat") << test;
serialize(test,fout);
fout.close();
// Now let's open that file back up and load the krls object it contains. // Now let's open that file back up and load the krls object it contains.
ifstream fin("saved_krls_object.dat",ios::binary); deserialize("saved_krls_object.dat") >> test;
deserialize(test, fin);
// If you don't want to save the whole krls object (it might be a bit large) // If you don't want to save the whole krls object (it might be a bit large)
// you can save just the decision function it has learned so far. You can get // you can save just the decision function it has learned so far. You can get
// the decision function out of it by calling test.get_decision_function() and // the decision function out of it by calling test.get_decision_function() and
// then you can serialize that object instead. E.g. // then you can serialize that object instead. E.g.
decision_function<kernel_type> funct = test.get_decision_function(); decision_function<kernel_type> funct = test.get_decision_function();
fout.open("saved_krls_function.dat",ios::binary); serialize("saved_krls_function.dat") << funct;
serialize(funct, fout);
} }

@ -196,14 +196,10 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is serializable. // Another thing that is worth knowing is that just about everything in dlib is serializable.
// So for example, you can save the learned_pfunct object to disk and recall it later like so: // So for example, you can save the learned_pfunct object to disk and recall it later like so:
ofstream fout("saved_function.dat",ios::binary); serialize("saved_function.dat") << learned_pfunct;
serialize(learned_pfunct,fout);
fout.close();
// Now let's open that file back up and load the function object it contains. // Now let's open that file back up and load the function object it contains.
ifstream fin("saved_function.dat",ios::binary); deserialize("saved_function.dat") >> learned_pfunct;
deserialize(learned_pfunct, fin);
} }

@ -94,14 +94,10 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is serializable. // Another thing that is worth knowing is that just about everything in dlib is serializable.
// So for example, you can save the test object to disk and recall it later like so: // So for example, you can save the test object to disk and recall it later like so:
ofstream fout("saved_function.dat",ios::binary); serialize("saved_function.dat") << test;
serialize(test,fout);
fout.close();
// Now let's open that file back up and load the function object it contains. // Now let's open that file back up and load the function object it contains.
ifstream fin("saved_function.dat",ios::binary); deserialize("saved_function.dat") >> test;
deserialize(test, fin);
} }

@ -344,13 +344,10 @@ int main()
// Finally, you can save your track_association_function to disk like so: // Finally, you can save your track_association_function to disk like so:
ofstream fout("track_assoc.svm", ios::binary); serialize("track_assoc.svm") << assoc;
serialize(assoc, fout);
fout.close();
// And recall it from disk later like so: // And recall it from disk later like so:
ifstream fin("track_assoc.svm", ios::binary); deserialize("track_assoc.svm") >> assoc;
deserialize(assoc, fin);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

@ -138,13 +138,10 @@ int main()
// Put df into df2 and then save df2 to disk. Note that we could have also said // Put df into df2 and then save df2 to disk. Note that we could have also said
// df2 = trainer.train(samples, labels); But doing it this way avoids retraining. // df2 = trainer.train(samples, labels); But doing it this way avoids retraining.
df2 = df; df2 = df;
ofstream fout("df.dat", ios::binary); serialize("df.dat") << df2;
serialize(df2, fout);
fout.close();
// load the function back in from disk and store it in df3. // load the function back in from disk and store it in df3.
ifstream fin("df.dat", ios::binary); deserialize("df.dat") >> df3;
deserialize(df3, fin);
// Test df3 to see that this worked. // Test df3 to see that this worked.

@ -247,13 +247,10 @@ int main()
// Finally, note that the detector can be serialized to disk just like other dlib objects. // Finally, note that the detector can be serialized to disk just like other dlib objects.
ofstream fout("object_detector.dat", ios::binary); serialize("object_detector.dat") << detector;
serialize(detector, fout);
fout.close();
// Recall from disk. // Recall from disk.
ifstream fin("object_detector.dat", ios::binary); deserialize("object_detector.dat") >> detector;
deserialize(detector, fin);
} }
catch (exception& e) catch (exception& e)
{ {

@ -208,14 +208,10 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is serializable. // Another thing that is worth knowing is that just about everything in dlib is serializable.
// So for example, you can save the learned_pfunct object to disk and recall it later like so: // So for example, you can save the learned_pfunct object to disk and recall it later like so:
ofstream fout("saved_function.dat",ios::binary); serialize("saved_function.dat") << learned_pfunct;
serialize(learned_pfunct,fout);
fout.close();
// Now let's open that file back up and load the function object it contains. // Now let's open that file back up and load the function object it contains.
ifstream fin("saved_function.dat",ios::binary); deserialize("saved_function.dat") >> learned_pfunct;
deserialize(learned_pfunct, fin);
} }

@ -91,14 +91,10 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is serializable. // Another thing that is worth knowing is that just about everything in dlib is serializable.
// So for example, you can save the test object to disk and recall it later like so: // So for example, you can save the test object to disk and recall it later like so:
ofstream fout("saved_function.dat",ios::binary); serialize("saved_function.dat") << test;
serialize(test,fout);
fout.close();
// Now let's open that file back up and load the function object it contains. // Now let's open that file back up and load the function object it contains.
ifstream fin("saved_function.dat",ios::binary); deserialize("saved_function.dat") >> test;
deserialize(test, fin);
} }

@ -292,13 +292,10 @@ int main()
// Finally, the labeler can be serialized to disk just like most dlib objects. // Finally, the labeler can be serialized to disk just like most dlib objects.
ofstream fout("labeler.dat", ios::binary); serialize("labeler.dat") << labeler;
serialize(labeler, fout);
fout.close();
// recall from disk // recall from disk
ifstream fin("labeler.dat", ios::binary); deserialize("labeler.dat") >> labeler;
deserialize(labeler, fin);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

@ -228,13 +228,10 @@ int main()
// Finally, the segmenter can be serialized to disk just like most dlib objects. // Finally, the segmenter can be serialized to disk just like most dlib objects.
ofstream fout("segmenter.dat", ios::binary); serialize("segmenter.dat") << segmenter;
serialize(segmenter, fout);
fout.close();
// recall from disk // recall from disk
ifstream fin("segmenter.dat", ios::binary); deserialize("segmenter.dat") >> segmenter;
deserialize(segmenter, fin);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

@ -210,13 +210,10 @@ int main()
// Another thing that is worth knowing is that just about everything in dlib is // Another thing that is worth knowing is that just about everything in dlib is
// serializable. So for example, you can save the learned_pfunct object to disk and // serializable. So for example, you can save the learned_pfunct object to disk and
// recall it later like so: // recall it later like so:
ofstream fout("saved_function.dat",ios::binary); serialize("saved_function.dat") << learned_pfunct;
serialize(learned_pfunct,fout);
fout.close();
// Now let's open that file back up and load the function object it contains. // Now let's open that file back up and load the function object it contains.
ifstream fin("saved_function.dat",ios::binary); deserialize("saved_function.dat") >> learned_pfunct;
deserialize(learned_pfunct, fin);
// Note that there is also an example program that comes with dlib called the // Note that there is also an example program that comes with dlib called the
// file_to_code_ex.cpp example. It is a simple program that takes a file and outputs a // file_to_code_ex.cpp example. It is a simple program that takes a file and outputs a

@ -291,9 +291,7 @@ int main(int argc, char** argv)
object_detector<image_scanner_type> detector = trainer.train(images, object_locations, ignore); object_detector<image_scanner_type> detector = trainer.train(images, object_locations, ignore);
cout << "Saving trained detector to object_detector.svm" << endl; cout << "Saving trained detector to object_detector.svm" << endl;
ofstream fout("object_detector.svm", ios::binary); serialize("object_detector.svm") << detector;
serialize(detector, fout);
fout.close();
cout << "Testing detector on training data..." << endl; cout << "Testing detector on training data..." << endl;
cout << "Test detector (precision,recall,AP): " << test_object_detection_function(detector, images, object_locations) << endl; cout << "Test detector (precision,recall,AP): " << test_object_detection_function(detector, images, object_locations) << endl;

Loading…
Cancel
Save