Пример #1
0
        /// <summary>
        /// Load param and model from a model directory
        /// </summary>
        /// <param name="model_dir">directory containing param and model (graphdef) file</param>
        /// <returns>true if successful, false if failed</returns>
        public virtual bool Load(string model_dir)
        {
            bool result = false;

            try
            {
                string param_file = Path.Combine(model_dir, "params.json");
                // looks for params.json file, load model
                string params_json = File.ReadAllText(param_file);
                _params = JsonConvert.DeserializeObject <Dictionary <string, object> >(params_json);

                _prefix = (string)(_params["model_name"]);
                string graphdef_filepath = Path.Combine(model_dir, _prefix + ".graphdef");

                _graph = new TFGraph();
                TFImportGraphDefOptions opts = new TFImportGraphDefOptions();
                TFBuffer buff = new TFBuffer(File.ReadAllBytes(graphdef_filepath));
                opts.SetPrefix(_prefix);
                _graph.Import(buff, opts);
                _session = new TFSession(_graph);
                result   = true;
            } catch (Exception)
            {
                // somethign went wrong
            }

            return(result);
        }
Пример #2
0
        public override void Initialize()
        {
            string vocab_json = File.ReadAllText("vocab.json");

            _vocab = JsonConvert.DeserializeObject <Dictionary <string, int> >(vocab_json);
            _graph = new TFGraph();
            TFImportGraphDefOptions opts = new TFImportGraphDefOptions();
            TFBuffer buff = new TFBuffer(File.ReadAllBytes("commaV5.graphdef"));

            opts.SetPrefix(_prefix);
            _graph.Import(buff, opts);
            _session = new TFSession(_graph);
        }
Пример #3
0
        public void TestImportGraphDef()
        {
            var      status = new TFStatus();
            TFBuffer graphDef;

            // Create graph with two nodes, "x" and "3"
            using (var graph = new TFGraph())
            {
                Assert(status);
                Placeholder(graph, status);
                Assert(graph["feed"] != null);

                ScalarConst(3, graph, status);
                Assert(graph["scalar"] != null);

                // Export to GraphDef
                graphDef = new TFBuffer();
                graph.ToGraphDef(graphDef, status);
                Assert(status);
            }

            // Import it again, with a prefix, in a fresh graph
            using (var graph = new TFGraph())
            {
                using (var options = new TFImportGraphDefOptions())
                {
                    options.SetPrefix("imported");
                    graph.Import(graphDef, options, status);
                    Assert(status);
                }
                graphDef.Dispose();

                var scalar = graph["imported/scalar"];
                var feed   = graph["imported/feed"];
                Assert(scalar != null);

                Assert(feed != null);

                // Can add nodes to the imported graph without trouble
                Add(feed, scalar, graph, status);
                Assert(status);
            }
        }