37 #ifndef RERANKER_MODEL_READER_H_
38 #define RERANKER_MODEL_READER_H_
42 #include "../proto/model.pb.h"
48 using confusion_learning::ModelMessage;
59 shared_ptr<Model>
Read(
const string &filename,
60 bool compressed,
bool use_base64) {
61 ConfusionProtoIO proto_reader(filename, ConfusionProtoIO::READ,
62 compressed, use_base64);
63 ModelMessage model_message;
64 if (verbosity_ >= 1) {
65 cerr <<
"ModelReader: reading serialized model from \""
66 << filename <<
"\"...";
69 if (!proto_reader.Read(&model_message)) {
70 cerr <<
"ModelReader: unable to read serialized model from \""
71 << filename <<
"\"." << endl;
72 return shared_ptr<Model>();
74 shared_ptr<ModelProtoReader> model_reader =
75 GetModelProtoReader(model_message);
76 shared_ptr<Model> model =
77 model_factory_.CreateOrDie(model_message.model_spec(),
"model spec");
78 if (model.get() == NULL) {
81 model_reader->ReadFeatures(*(proto_reader.inputstream()), model.get());
83 if (verbosity_ >= 1) {
84 cerr <<
"done." << endl
85 <<
"Loaded model \"" << model->name() <<
"\"." << endl;
91 shared_ptr<Model>
Read(
const ModelMessage &model_message) {
92 shared_ptr<ModelProtoReader> model_proto_reader =
93 GetModelProtoReader(model_message);
94 shared_ptr<Model> model =
95 model_factory_.CreateOrDie(model_message.model_spec(),
"model spec");
96 model_proto_reader->Read(model_message, model.get());
100 shared_ptr<ModelProtoReader>
101 GetModelProtoReader(
const ModelMessage &model_message) {
102 if (!model_message.has_reader_spec()) {
103 cerr <<
"ModelReader: no reader_spec in ModelMessage" << endl;
104 return shared_ptr<ModelProtoReader>();
106 string model_reader_spec = model_message.reader_spec();
107 Factory<ModelProtoReader> model_proto_reader_factory;
109 model_proto_reader_factory_.CreateOrDie(model_reader_spec,
110 "model proto reader spec");
113 Factory<Model> model_factory_;
114 Factory<ModelProtoReader> model_proto_reader_factory_;
shared_ptr< Model > Read(const string &filename, bool compressed, bool use_base64)
ModelReader(int verbosity=0)
Interface for de-serializer for reranker::Model instances from ModelMessage instances.
shared_ptr< Model > Read(const ModelMessage &model_message)
Provides a generic dynamic object factory.
Knows how to create Model instances that have been serialized to a file.