From 17565cfb5f347e49d971b278cef3f6675461c8d3 Mon Sep 17 00:00:00 2001 From: Davis King Date: Wed, 7 Mar 2012 22:38:29 -0500 Subject: [PATCH] Added serialization support for the rls, rls_filter, and kalman_filter objects. --- dlib/filtering/kalman_filter.h | 31 +++++++++++++++++++++++++ dlib/filtering/kalman_filter_abstract.h | 21 +++++++++++++++++ dlib/filtering/rls_filter.h | 25 ++++++++++++++++++++ dlib/filtering/rls_filter_abstract.h | 19 +++++++++++++++ dlib/svm/rls.h | 23 ++++++++++++++++++ dlib/svm/rls_abstract.h | 18 ++++++++++++++ 6 files changed, 137 insertions(+) diff --git a/dlib/filtering/kalman_filter.h b/dlib/filtering/kalman_filter.h index ba91b8263..5282cb44a 100644 --- a/dlib/filtering/kalman_filter.h +++ b/dlib/filtering/kalman_filter.h @@ -82,6 +82,37 @@ namespace dlib return xb; } + friend inline void serialize(const kalman_filter& item, std::ostream& out) + { + int version = 1; + serialize(version, out); + serialize(item.got_first_meas, out); + serialize(item.x, out); + serialize(item.xb, out); + serialize(item.P, out); + serialize(item.H, out); + serialize(item.A, out); + serialize(item.Q, out); + serialize(item.R, out); + } + + friend inline void deserialize(kalman_filter& item, std::istream& in) + { + int version = 0; + deserialize(version, in); + if (version != 1) + throw dlib::serialization_error("Unknown version number found while deserializing kalman_filter object."); + + deserialize(item.got_first_meas, in); + deserialize(item.x, in); + deserialize(item.xb, in); + deserialize(item.P, in); + deserialize(item.H, in); + deserialize(item.A, in); + deserialize(item.Q, in); + deserialize(item.R, in); + } + private: bool got_first_meas; diff --git a/dlib/filtering/kalman_filter_abstract.h b/dlib/filtering/kalman_filter_abstract.h index 52127acbd..7879590bd 100644 --- a/dlib/filtering/kalman_filter_abstract.h +++ b/dlib/filtering/kalman_filter_abstract.h @@ -6,6 +6,27 @@ namespace dlib { + +// ---------------------------------------------------------------------------------------- + + void serialize ( + const kalman_filter& item, + std::ostream& out + ); + /*! + provides serialization support + !*/ + + void deserialize ( + kalman_filter& item, + std::istream& in + ); + /*! + provides deserialization support + !*/ + +// ---------------------------------------------------------------------------------------- + } #endif // DLIB_KALMAN_FiLTER_ABSTRACT_H__ diff --git a/dlib/filtering/rls_filter.h b/dlib/filtering/rls_filter.h index 0c393f526..6128bcfcc 100644 --- a/dlib/filtering/rls_filter.h +++ b/dlib/filtering/rls_filter.h @@ -130,6 +130,31 @@ namespace dlib return next; } + friend inline void serialize(const rls_filter& item, std::ostream& out) + { + int version = 1; + serialize(version, out); + serialize(item.count, out); + serialize(item.size, out); + serialize(item.filter, out); + serialize(item.next, out); + serialize(item.data, out); + } + + friend inline void deserialize(rls_filter& item, std::istream& in) + { + int version = 0; + deserialize(version, in); + if (version != 1) + throw dlib::serialization_error("Unknown version number found while deserializing rls_filter object."); + + deserialize(item.count, in); + deserialize(item.size, in); + deserialize(item.filter, in); + deserialize(item.next, in); + deserialize(item.data, in); + } + private: unsigned long count; diff --git a/dlib/filtering/rls_filter_abstract.h b/dlib/filtering/rls_filter_abstract.h index ad64ddd2c..b8eb04e3e 100644 --- a/dlib/filtering/rls_filter_abstract.h +++ b/dlib/filtering/rls_filter_abstract.h @@ -70,6 +70,25 @@ namespace dlib // ---------------------------------------------------------------------------------------- + void serialize ( + const rls_filter& item, + std::ostream& out + ); + /*! + provides serialization support + !*/ + + void deserialize ( + rls_filter& item, + std::istream& in + ); + /*! + provides deserialization support + !*/ + +// ---------------------------------------------------------------------------------------- + + } #endif // DLIB_RLS_FiLTER_ABSTRACT_H__ diff --git a/dlib/svm/rls.h b/dlib/svm/rls.h index 9581d959a..d0260294b 100644 --- a/dlib/svm/rls.h +++ b/dlib/svm/rls.h @@ -143,6 +143,29 @@ namespace dlib return df; } + friend inline void serialize(const rls& item, std::ostream& out) + { + int version = 1; + serialize(version, out); + serialize(item.w, out); + serialize(item.R, out); + serialize(item.C, out); + serialize(item.forget_factor, out); + } + + friend inline void deserialize(rls& item, std::istream& in) + { + int version = 0; + deserialize(version, in); + if (version != 1) + throw dlib::serialization_error("Unknown version number found while deserializing rls object."); + + deserialize(item.w, in); + deserialize(item.R, in); + deserialize(item.C, in); + deserialize(item.forget_factor, in); + } + private: void add_eye_to_inv( diff --git a/dlib/svm/rls_abstract.h b/dlib/svm/rls_abstract.h index cea28e810..9183db6eb 100644 --- a/dlib/svm/rls_abstract.h +++ b/dlib/svm/rls_abstract.h @@ -133,6 +133,24 @@ namespace dlib }; +// ---------------------------------------------------------------------------------------- + + void serialize ( + const rls& item, + std::ostream& out + ); + /*! + provides serialization support + !*/ + + void deserialize ( + rls& item, + std::istream& in + ); + /*! + provides deserialization support + !*/ + // ---------------------------------------------------------------------------------------- }