From c6d778eaf9c0e39a5dfaa97a9b002cb89cf135fc Mon Sep 17 00:00:00 2001 From: Davis King Date: Thu, 8 May 2014 21:07:56 -0400 Subject: [PATCH] Made the examples use the new simplified file serialization API. --- examples/assignment_learning_ex.cpp | 7 ++----- examples/custom_trainer_ex.cpp | 9 +++------ examples/fhog_object_detector_ex.cpp | 7 ++----- examples/krls_ex.cpp | 10 +++------- examples/krr_classification_ex.cpp | 8 ++------ examples/krr_regression_ex.cpp | 8 ++------ examples/learning_to_track_ex.cpp | 7 ++----- examples/multiclass_classification_ex.cpp | 7 ++----- examples/object_detector_ex.cpp | 7 ++----- examples/rvm_ex.cpp | 8 ++------ examples/rvm_regression_ex.cpp | 8 ++------ examples/sequence_labeler_ex.cpp | 7 ++----- examples/sequence_segmenter_ex.cpp | 7 ++----- examples/svm_ex.cpp | 7 ++----- examples/train_object_detector.cpp | 4 +--- 15 files changed, 31 insertions(+), 80 deletions(-) diff --git a/examples/assignment_learning_ex.cpp b/examples/assignment_learning_ex.cpp index a7103c67a..bafc5a098 100644 --- a/examples/assignment_learning_ex.cpp +++ b/examples/assignment_learning_ex.cpp @@ -195,13 +195,10 @@ int main() // Finally, the assigner can be serialized to disk just like most dlib objects. - ofstream fout("assigner.dat", ios::binary); - serialize(assigner, fout); - fout.close(); + serialize("assigner.dat") << assigner; // recall from disk - ifstream fin("assigner.dat", ios::binary); - deserialize(assigner, fin); + deserialize("assigner.dat") >> assigner; } catch (std::exception& e) { diff --git a/examples/custom_trainer_ex.cpp b/examples/custom_trainer_ex.cpp index ec3fc550b..39af53f39 100644 --- a/examples/custom_trainer_ex.cpp +++ b/examples/custom_trainer_ex.cpp @@ -208,15 +208,12 @@ int main() decision_function > // This is the output of the rbf_trainer > df2, df3; - df2 = df; - ofstream fout("df.dat", ios::binary); - serialize(df2, fout); - fout.close(); + // save to a file called df.dat + serialize("df.dat") << df2; // load the function back in from disk and store it in df3. - ifstream fin("df.dat", ios::binary); - deserialize(df3, fin); + deserialize("df.dat") >> df3; // Test df3 to see that this worked. diff --git a/examples/fhog_object_detector_ex.cpp b/examples/fhog_object_detector_ex.cpp index d2d246b96..6ddb02aa3 100644 --- a/examples/fhog_object_detector_ex.cpp +++ b/examples/fhog_object_detector_ex.cpp @@ -180,14 +180,11 @@ int main(int argc, char** argv) // Like everything in dlib, you can save your detector to disk using the // serialize() function. - ofstream fout("face_detector.svm", ios::binary); - serialize(detector, fout); - fout.close(); + serialize("face_detector.svm") << detector; // Then you can recall it using the deserialize() function. - ifstream fin("face_detector.svm", ios::binary); object_detector detector2; - deserialize(detector2, fin); + deserialize("face_detector.svm") >> detector2; diff --git a/examples/krls_ex.cpp b/examples/krls_ex.cpp index d20b90777..968f1a6dd 100644 --- a/examples/krls_ex.cpp +++ b/examples/krls_ex.cpp @@ -78,21 +78,17 @@ int main() // 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: - ofstream fout("saved_krls_object.dat",ios::binary); - serialize(test,fout); - fout.close(); + serialize("saved_krls_object.dat") << test; // Now let's open that file back up and load the krls object it contains. - ifstream fin("saved_krls_object.dat",ios::binary); - deserialize(test, fin); + deserialize("saved_krls_object.dat") >> test; // 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 // the decision function out of it by calling test.get_decision_function() and // then you can serialize that object instead. E.g. decision_function funct = test.get_decision_function(); - fout.open("saved_krls_function.dat",ios::binary); - serialize(funct, fout); + serialize("saved_krls_function.dat") << funct; } diff --git a/examples/krr_classification_ex.cpp b/examples/krr_classification_ex.cpp index 4eff004c0..42648351f 100644 --- a/examples/krr_classification_ex.cpp +++ b/examples/krr_classification_ex.cpp @@ -196,14 +196,10 @@ int main() // 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: - ofstream fout("saved_function.dat",ios::binary); - serialize(learned_pfunct,fout); - fout.close(); + serialize("saved_function.dat") << learned_pfunct; // Now let's open that file back up and load the function object it contains. - ifstream fin("saved_function.dat",ios::binary); - deserialize(learned_pfunct, fin); - + deserialize("saved_function.dat") >> learned_pfunct; } diff --git a/examples/krr_regression_ex.cpp b/examples/krr_regression_ex.cpp index f1bb23694..26c1412d7 100644 --- a/examples/krr_regression_ex.cpp +++ b/examples/krr_regression_ex.cpp @@ -94,14 +94,10 @@ int main() // 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: - ofstream fout("saved_function.dat",ios::binary); - serialize(test,fout); - fout.close(); + serialize("saved_function.dat") << test; // Now let's open that file back up and load the function object it contains. - ifstream fin("saved_function.dat",ios::binary); - deserialize(test, fin); - + deserialize("saved_function.dat") >> test; } diff --git a/examples/learning_to_track_ex.cpp b/examples/learning_to_track_ex.cpp index 55e79cc16..2f9f3947f 100644 --- a/examples/learning_to_track_ex.cpp +++ b/examples/learning_to_track_ex.cpp @@ -344,13 +344,10 @@ int main() // Finally, you can save your track_association_function to disk like so: - ofstream fout("track_assoc.svm", ios::binary); - serialize(assoc, fout); - fout.close(); + serialize("track_assoc.svm") << assoc; // And recall it from disk later like so: - ifstream fin("track_assoc.svm", ios::binary); - deserialize(assoc, fin); + deserialize("track_assoc.svm") >> assoc; } // ---------------------------------------------------------------------------------------- diff --git a/examples/multiclass_classification_ex.cpp b/examples/multiclass_classification_ex.cpp index cb7c46abd..782511ca3 100644 --- a/examples/multiclass_classification_ex.cpp +++ b/examples/multiclass_classification_ex.cpp @@ -138,13 +138,10 @@ int main() // 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 = df; - ofstream fout("df.dat", ios::binary); - serialize(df2, fout); - fout.close(); + serialize("df.dat") << df2; // load the function back in from disk and store it in df3. - ifstream fin("df.dat", ios::binary); - deserialize(df3, fin); + deserialize("df.dat") >> df3; // Test df3 to see that this worked. diff --git a/examples/object_detector_ex.cpp b/examples/object_detector_ex.cpp index f7168fe62..cda71eb5a 100644 --- a/examples/object_detector_ex.cpp +++ b/examples/object_detector_ex.cpp @@ -247,13 +247,10 @@ int main() // Finally, note that the detector can be serialized to disk just like other dlib objects. - ofstream fout("object_detector.dat", ios::binary); - serialize(detector, fout); - fout.close(); + serialize("object_detector.dat") << detector; // Recall from disk. - ifstream fin("object_detector.dat", ios::binary); - deserialize(detector, fin); + deserialize("object_detector.dat") >> detector; } catch (exception& e) { diff --git a/examples/rvm_ex.cpp b/examples/rvm_ex.cpp index 7ed04b724..d1d5935e7 100644 --- a/examples/rvm_ex.cpp +++ b/examples/rvm_ex.cpp @@ -208,14 +208,10 @@ int main() // 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: - ofstream fout("saved_function.dat",ios::binary); - serialize(learned_pfunct,fout); - fout.close(); + serialize("saved_function.dat") << learned_pfunct; // Now let's open that file back up and load the function object it contains. - ifstream fin("saved_function.dat",ios::binary); - deserialize(learned_pfunct, fin); - + deserialize("saved_function.dat") >> learned_pfunct; } diff --git a/examples/rvm_regression_ex.cpp b/examples/rvm_regression_ex.cpp index c2604b583..d65cb5203 100644 --- a/examples/rvm_regression_ex.cpp +++ b/examples/rvm_regression_ex.cpp @@ -91,14 +91,10 @@ int main() // 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: - ofstream fout("saved_function.dat",ios::binary); - serialize(test,fout); - fout.close(); + serialize("saved_function.dat") << test; // Now let's open that file back up and load the function object it contains. - ifstream fin("saved_function.dat",ios::binary); - deserialize(test, fin); - + deserialize("saved_function.dat") >> test; } diff --git a/examples/sequence_labeler_ex.cpp b/examples/sequence_labeler_ex.cpp index eee78ab57..bdb666a7e 100644 --- a/examples/sequence_labeler_ex.cpp +++ b/examples/sequence_labeler_ex.cpp @@ -292,13 +292,10 @@ int main() // Finally, the labeler can be serialized to disk just like most dlib objects. - ofstream fout("labeler.dat", ios::binary); - serialize(labeler, fout); - fout.close(); + serialize("labeler.dat") << labeler; // recall from disk - ifstream fin("labeler.dat", ios::binary); - deserialize(labeler, fin); + deserialize("labeler.dat") >> labeler; } // ---------------------------------------------------------------------------------------- diff --git a/examples/sequence_segmenter_ex.cpp b/examples/sequence_segmenter_ex.cpp index ba0897d48..3b0eb8cde 100644 --- a/examples/sequence_segmenter_ex.cpp +++ b/examples/sequence_segmenter_ex.cpp @@ -228,13 +228,10 @@ int main() // Finally, the segmenter can be serialized to disk just like most dlib objects. - ofstream fout("segmenter.dat", ios::binary); - serialize(segmenter, fout); - fout.close(); + serialize("segmenter.dat") << segmenter; // recall from disk - ifstream fin("segmenter.dat", ios::binary); - deserialize(segmenter, fin); + deserialize("segmenter.dat") >> segmenter; } // ---------------------------------------------------------------------------------------- diff --git a/examples/svm_ex.cpp b/examples/svm_ex.cpp index 7d598536f..3d5d0bb84 100644 --- a/examples/svm_ex.cpp +++ b/examples/svm_ex.cpp @@ -210,13 +210,10 @@ int main() // 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: - ofstream fout("saved_function.dat",ios::binary); - serialize(learned_pfunct,fout); - fout.close(); + serialize("saved_function.dat") << learned_pfunct; // Now let's open that file back up and load the function object it contains. - ifstream fin("saved_function.dat",ios::binary); - deserialize(learned_pfunct, fin); + deserialize("saved_function.dat") >> learned_pfunct; // 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 diff --git a/examples/train_object_detector.cpp b/examples/train_object_detector.cpp index b861ad20f..6ba478490 100644 --- a/examples/train_object_detector.cpp +++ b/examples/train_object_detector.cpp @@ -291,9 +291,7 @@ int main(int argc, char** argv) object_detector detector = trainer.train(images, object_locations, ignore); cout << "Saving trained detector to object_detector.svm" << endl; - ofstream fout("object_detector.svm", ios::binary); - serialize(detector, fout); - fout.close(); + serialize("object_detector.svm") << detector; cout << "Testing detector on training data..." << endl; cout << "Test detector (precision,recall,AP): " << test_object_detection_function(detector, images, object_locations) << endl;