ModelConfig.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <boost/property_tree/ptree.hpp>
7 
8 /*
9  * # Model Configuration File
10  *
11  * Model configuration file is a json dictionary, containing names of
12  * input/output nodes of a tensorflow graph. For each input node, it also
13  * contains a list of variable names whose values will be used to construct
14  * an input tensor for that node.
15  *
16  * Here is an example of the config file:
17  * ```
18  * {
19  * "input_node" : "input_1",
20  * "input_vars" : [ "var1", "var2", "var3" ],
21  * "output_node" : "target/BiasAdd"
22  * }
23  * ```
24  *
25  * This config file describes a tensorflow graph has one input node "input_1"
26  * and one output node "target/BiasAdd". The names "input_1" and
27  * "target/BiasAdd" are node names in the tensorflow graph. The input tensor
28  * for the "input_1" node should be constructed from values of three variables:
29  * [ "var1", "var2", "var3" ].
30  *
31  *
32  * # `ModelConfig`
33  *
34  * Model configuration file can be parsed with a help of `ModelConfig` class.
35  * Constructor of `ModelConfig` receives names of config file keys that specify
36  * input/output configuration.
37  *
38  * The code below can be used to parse an example configuration file above:
39  * ```
40  * // Keys in config file that specify "input_1" node configuration:
41  * const InputConfigKeys configKeys1("input_node", "input_vars");
42  *
43  * const std::string savedir; // Directory where config file is stored
44  * const std::vector<InputConfigKeys> scalarInputKeys({ configKeys1 });
45  * const std::vector<InputConfigKeys> vectorInputKeys({ });
46  * const std::vector<std::string> outputKeys ({ "output_node" });
47  *
48  * ModelConfig config(savedir, scalarInputKeys, vectorInputKeys, outputKeys);
49  * config.load(); // Parse config file
50  * ```
51  */
52 
53 namespace pt = boost::property_tree;
54 
55 const std::string CONFIG_NAME = "config.json";
56 const std::string MODEL_NAME = "model.pb";
57 
58 /*
59  * `InputConfig` -- Input configuration for a single tensorflow graph node.
60  *
61  * `nodeName` -- name of the tensorflow graph node.
62  * `varNames` -- list of variable names.
63  * Input tensor for `nodeName` will be constructed from variables
64  * listed in `varNames`.
65  */
67 {
69  std::vector<std::string> varNames;
70 };
71 
72 /*
73  * `InputConfigKeys` -- names of the config file keys specifying input
74  * configuration for a single tensorflow graph node:
75  *
76  * `nodeName` -- name of the key in a config file which value specifies
77  * name of the tensorflow graph node.
78  * `varNames` -- name of the key in a config file which value is a list
79  * of variable names.
80  *
81  */
83 {
86 };
87 
89 {
90 public:
92  const std::string &savedir,
93  const std::vector<InputConfigKeys> &scalarInputKeys,
94  const std::vector<InputConfigKeys> &vectorInputKeys,
95  const std::vector<std::string> &outputKeys
96  );
97 
98  // Expand environment variables in the path
99  static std::string expandPath(const std::string &path);
100 
101  void load();
102  bool isLoaded() const;
103 
104  std::string getConfigPath() const;
105  std::string getModelPath() const;
106  std::string getSavedir() const;
107 
108  // Get list of configurations of scalar input nodes
109  const std::vector<InputConfig>& getScalarInputs() const;
110 
111  // Get list of configurations of vector input nodes
112  const std::vector<InputConfig>& getVectorInputs() const;
113 
114  // Get list of output node names
115  const std::vector<std::string>& getOutputNodes() const;
116 
117 private:
119  bool loaded;
120 
121  void parseConfig(const std::string &fname);
122 
123  static InputConfig parseInput(
124  pt::ptree &tree, const InputConfigKeys &keys
125  );
126 
127  std::vector<InputConfigKeys> scalarInputKeys;
128  std::vector<InputConfigKeys> vectorInputKeys;
129  std::vector<std::string> outputKeys;
130 
131  std::vector<InputConfig> scalarInputs;
132  std::vector<InputConfig> vectorInputs;
133  std::vector<std::string> outputNodes;
134 };
135 
std::vector< std::string > outputKeys
Definition: ModelConfig.h:129
std::vector< InputConfigKeys > scalarInputKeys
Definition: ModelConfig.h:127
std::string nodeName
Definition: ModelConfig.h:84
std::vector< std::string > outputNodes
Definition: ModelConfig.h:133
std::vector< InputConfigKeys > vectorInputKeys
Definition: ModelConfig.h:128
std::string string
Definition: nybbler.cc:12
std::string varNames
Definition: ModelConfig.h:85
std::string savedir
Definition: ModelConfig.h:118
std::vector< InputConfig > vectorInputs
Definition: ModelConfig.h:132
bool parseConfig(const QString &fileName, const QHash< QString, Input * > &options)
std::string nodeName
Definition: ModelConfig.h:68
std::vector< InputConfig > scalarInputs
Definition: ModelConfig.h:131
void parseInput()
Definition: doxygen.cpp:10880
const std::string CONFIG_NAME
Definition: ModelConfig.h:55
const std::string MODEL_NAME
Definition: ModelConfig.h:56
std::vector< std::string > varNames
Definition: ModelConfig.h:69
def load(filename, jpath="depos")
Definition: depos.py:34