1 #include "caffe2/predictor/predictor_utils.h" 3 #include "caffe2/core/blob.h" 4 #include "caffe2/core/logging.h" 5 #include "caffe2/proto/caffe2_pb.h" 6 #include "caffe2/proto/predictor_consts.pb.h" 7 #include "caffe2/utils/proto_utils.h" 10 namespace predictor_utils {
12 CAFFE2_API
const NetDef& getNet(
13 const MetaNetDef& def,
14 const std::string& name) {
15 for (
const auto& n : def.nets()) {
16 if (n.key() == name) {
20 CAFFE_THROW(
"Net not found: ", name);
23 std::unique_ptr<MetaNetDef> extractMetaNetDef(
25 const std::string& key) {
26 CAFFE_ENFORCE(cursor);
27 if (cursor->SupportsSeek()) {
30 for (; cursor->Valid(); cursor->Next()) {
31 if (cursor->key() != key) {
36 CAFFE_ENFORCE(proto.ParseFromString(cursor->value()));
39 CAFFE_ENFORCE(blob.template IsType<string>());
40 auto def = caffe2::make_unique<MetaNetDef>();
41 CAFFE_ENFORCE(def->ParseFromString(blob.template Get<string>()));
44 CAFFE_THROW(
"Failed to find in db the key: ", key);
47 std::unique_ptr<MetaNetDef> runGlobalInitialization(
48 std::unique_ptr<db::DBReader> db,
50 CAFFE_ENFORCE(db.get());
51 auto* cursor = db->cursor();
53 auto metaNetDef = extractMetaNetDef(
54 cursor, PredictorConsts::default_instance().meta_net_def());
55 if (metaNetDef->has_modelinfo()) {
57 metaNetDef->modelinfo().predictortype() ==
58 PredictorConsts::default_instance().single_predictor(),
59 "Can only load single predictor");
61 VLOG(1) <<
"Extracted meta net def";
63 const auto globalInitNet = getNet(
64 *metaNetDef, PredictorConsts::default_instance().global_init_net_type());
65 VLOG(1) <<
"Global init net: " << ProtoDebugString(globalInitNet);
69 master->CreateBlob(PredictorConsts::default_instance().predictor_dbreader())
70 ->Reset(db.release());
74 master->RunNetOnce(globalInitNet),
75 "Failed running the globalInitNet: ",
76 ProtoDebugString(globalInitNet));
void DeserializeBlob(const string &content, Blob *result)
Deserializes from a string containing either BlobProto or TensorProto.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...