CMS 3D CMS Logo

Predictor.h
Go to the documentation of this file.
1 /*
2  * MXNetCppPredictor.h
3  *
4  * Created on: Jul 19, 2018
5  * Author: hqu
6  */
7 
8 #ifndef PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_
9 #define PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_
10 
11 #include <map>
12 #include <vector>
13 #include <memory>
14 #include <mutex>
15 
16 #include "mxnet-cpp/MxNetCpp.h"
17 
18 namespace mxnet {
19 
20  namespace cpp {
21 
22  // note: Most of the objects in mxnet::cpp are effective just shared_ptr's
23 
24  // Simple class to hold MXNet model (symbol + params)
25  // designed to be sharable by multiple threads
26  class Block {
27  public:
28  Block();
29  Block(const std::string& symbol_file, const std::string& param_file);
30  virtual ~Block();
31 
32  const Symbol& symbol() const { return sym_; }
33  Symbol symbol(const std::string& output_node) const { return sym_.GetInternals()[output_node]; }
34  const std::map<std::string, NDArray>& arg_map() const { return arg_map_; }
35  const std::map<std::string, NDArray>& aux_map() const { return aux_map_; }
36 
37  private:
38  void load_parameters(const std::string& param_file);
39 
40  // symbol
41  Symbol sym_;
42  // argument arrays
43  std::map<std::string, NDArray> arg_map_;
44  // auxiliary arrays
45  std::map<std::string, NDArray> aux_map_;
46  };
47 
48  // Simple helper class to run prediction
49  // this cannot be shared between threads
50  class Predictor {
51  public:
52  Predictor();
53  Predictor(const Block& block);
54  Predictor(const Block& block, const std::string& output_node);
55  virtual ~Predictor();
56 
57  // set input array shapes
58  void set_input_shapes(const std::vector<std::string>& input_names,
59  const std::vector<std::vector<mx_uint>>& input_shapes);
60 
61  // run prediction
62  const std::vector<float>& predict(const std::vector<std::vector<mx_float>>& input_data);
63 
64  private:
66 
67  void bind_executor();
68 
69  // context
70  static const Context context_;
71  // executor
72  std::unique_ptr<Executor> exec_;
73  // symbol
74  Symbol sym_;
75  // argument arrays
76  std::map<std::string, NDArray> arg_map_;
77  // auxiliary arrays
78  std::map<std::string, NDArray> aux_map_;
79  // output of the prediction
80  std::vector<float> pred_;
81  // names of the input nodes
82  std::vector<std::string> input_names_;
83  };
84 
85  } /* namespace cpp */
86 } /* namespace mxnet */
87 
88 #endif /* PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ */
mxnet
Definition: Predictor.h:18
mxnet::cpp::Block::arg_map
const std::map< std::string, NDArray > & arg_map() const
Definition: Predictor.h:34
mxnet::cpp::Predictor::Predictor
Predictor()
Definition: Predictor.cc:46
mxnet::cpp::Predictor::mutex_
static std::mutex mutex_
Definition: Predictor.h:65
mxnet::cpp::Block::Block
Block()
Definition: Predictor.cc:17
mxnet::cpp::Block::aux_map
const std::map< std::string, NDArray > & aux_map() const
Definition: Predictor.h:35
mxnet::cpp::Predictor::~Predictor
virtual ~Predictor()
Definition: Predictor.cc:54
Context
mxnet::cpp::Block
Definition: Predictor.h:26
mxnet::cpp::Predictor::bind_executor
void bind_executor()
Definition: Predictor.cc:91
mxnet::cpp::Predictor::context_
static const Context context_
Definition: Predictor.h:70
mxnet::cpp::Predictor::set_input_shapes
void set_input_shapes(const std::vector< std::string > &input_names, const std::vector< std::vector< mx_uint >> &input_shapes)
Definition: Predictor.cc:56
mxnet::cpp::Predictor::exec_
std::unique_ptr< Executor > exec_
Definition: Predictor.h:72
mutex
static boost::mutex mutex
Definition: Proxy.cc:9
mxnet::cpp::Block::symbol
const Symbol & symbol() const
Definition: Predictor.h:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
mxnet::cpp::Block::load_parameters
void load_parameters(const std::string &param_file)
Definition: Predictor.cc:28
mxnet::cpp::Predictor::input_names_
std::vector< std::string > input_names_
Definition: Predictor.h:82
mxnet::cpp::Block::~Block
virtual ~Block()
Definition: Predictor.cc:26
groupFilesInBlocks.block
block
Definition: groupFilesInBlocks.py:150
mxnet::cpp::Predictor::predict
const std::vector< float > & predict(const std::vector< std::vector< mx_float >> &input_data)
Definition: Predictor.cc:67
mxnet::cpp::Block::arg_map_
std::map< std::string, NDArray > arg_map_
Definition: Predictor.h:43
hltPfDeepFlavourJetTags_cfi.input_names
input_names
Definition: hltPfDeepFlavourJetTags_cfi.py:12
mxnet::cpp::Block::symbol
Symbol symbol(const std::string &output_node) const
Definition: Predictor.h:33
mxnet::cpp::Block::sym_
Symbol sym_
Definition: Predictor.h:41
mxnet::cpp::Block::aux_map_
std::map< std::string, NDArray > aux_map_
Definition: Predictor.h:45
mxnet::cpp::Predictor::pred_
std::vector< float > pred_
Definition: Predictor.h:80
mxnet::cpp::Predictor::arg_map_
std::map< std::string, NDArray > arg_map_
Definition: Predictor.h:76
mxnet::cpp::Predictor::aux_map_
std::map< std::string, NDArray > aux_map_
Definition: Predictor.h:78
mxnet::cpp::Predictor
Definition: Predictor.h:50
mxnet::cpp::Predictor::sym_
Symbol sym_
Definition: Predictor.h:74