| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2579afc commit c7cf8b6
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow | ||
| 6 | + { | ||
| 7 | + public class RuntimeError : Exception | ||
| 8 | + { | ||
| 9 | + public RuntimeError() : base() | ||
| 10 | + { | ||
| 11 | + | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + public RuntimeError(string message) : base(message) | ||
| 15 | + { | ||
| 16 | + | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,135 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Linq; | ||
| 4 | + using System.Text; | ||
| 5 | + using static Tensorflow.MetaGraphDef.Types; | ||
| 6 | + | ||
| 7 | + namespace Tensorflow | ||
| 8 | + { | ||
| 9 | + public class meta_graph | ||
| 10 | + { | ||
| 11 | + /// <summary> | ||
| 12 | + /// Returns `MetaGraphDef` proto. Optionally writes it to filename. | ||
| 13 | + /// </summary> | ||
| 14 | + /// <param name="filename"></param> | ||
| 15 | + /// <param name="graph_def"></param> | ||
| 16 | + /// <param name="as_text"></param> | ||
| 17 | + /// <param name="unbound_inputs_col_name"></param> | ||
| 18 | + /// <param name="clear_devices"></param> | ||
| 19 | + /// <param name="saver_def"></param> | ||
| 20 | + /// <param name="clear_extraneous_savers"></param> | ||
| 21 | + /// <param name="strip_default_attrs"></param> | ||
| 22 | + /// <param name="meta_info_def"></param> | ||
| 23 | + /// <returns></returns> | ||
| 24 | + public static MetaGraphDef export_scoped_meta_graph(string filename = "", | ||
| 25 | + GraphDef graph_def = null, | ||
| 26 | + bool as_text = false, | ||
| 27 | + string unbound_inputs_col_name = "unbound_inputs", | ||
| 28 | + bool clear_devices = false, | ||
| 29 | + SaverDef saver_def = null, | ||
| 30 | + bool clear_extraneous_savers= false, | ||
| 31 | + bool strip_default_attrs= false, | ||
| 32 | + byte[] meta_info_def = null) | ||
| 33 | + { | ||
| 34 | + var graph = ops.get_default_graph(); | ||
| 35 | + | ||
| 36 | + var var_list = new Dictionary<string, RefVariable>(); | ||
| 37 | + var variables = graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES); | ||
| 38 | + | ||
| 39 | + foreach(var v in variables as RefVariable[]) | ||
| 40 | + { | ||
| 41 | + var_list[v.name] = v; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + var scoped_meta_graph_def = create_meta_graph_def( | ||
| 45 | + graph_def: graph_def, | ||
| 46 | + export_scope: "", | ||
| 47 | + exclude_nodes: "", | ||
| 48 | + clear_extraneous_savers: clear_extraneous_savers, | ||
| 49 | + saver_def: saver_def, | ||
| 50 | + strip_default_attrs: strip_default_attrs); | ||
| 51 | + | ||
| 52 | + throw new NotImplementedException("meta_graph.export_scoped_meta_graph"); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + private static bool _should_include_node() | ||
| 56 | + { | ||
| 57 | + return true; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + private static byte[] create_meta_graph_def(MetaInfoDef meta_info_def = null, | ||
| 61 | + GraphDef graph_def = null, | ||
| 62 | + string export_scope = "", | ||
| 63 | + string exclude_nodes = "", | ||
| 64 | + SaverDef saver_def = null, | ||
| 65 | + bool clear_extraneous_savers = false, | ||
| 66 | + bool strip_default_attrs = false) | ||
| 67 | + { | ||
| 68 | + // Sets graph to default graph if it's not passed in. | ||
| 69 | + var graph = ops.get_default_graph(); | ||
| 70 | + // Creates a MetaGraphDef proto. | ||
| 71 | + var meta_graph_def = new MetaGraphDef(); | ||
| 72 | + if (meta_info_def == null) | ||
| 73 | + meta_info_def = new MetaInfoDef(); | ||
| 74 | + | ||
| 75 | + // Set the tf version strings to the current tf build. | ||
| 76 | + meta_info_def.TensorflowVersion = tf.VERSION; | ||
| 77 | + meta_info_def.TensorflowGitVersion = "unknown"; | ||
| 78 | + meta_graph_def.MetaInfoDef = meta_info_def; | ||
| 79 | + | ||
| 80 | + // Adds graph_def or the default. | ||
| 81 | + if (graph_def == null) | ||
| 82 | + meta_graph_def.GraphDef = graph._as_graph_def(add_shapes: true); | ||
| 83 | + else | ||
| 84 | + meta_graph_def.GraphDef = graph_def; | ||
| 85 | + | ||
| 86 | + // Fills in meta_info_def.stripped_op_list using the ops from graph_def. | ||
| 87 | + if (meta_graph_def.MetaInfoDef.StrippedOpList.Op.Count == 0) | ||
| 88 | + meta_graph_def.MetaInfoDef.StrippedOpList = stripped_op_list_for_graph(meta_graph_def.GraphDef); | ||
| 89 | + | ||
| 90 | + throw new NotImplementedException("create_meta_graph_def"); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + private static OpList stripped_op_list_for_graph(GraphDef graph_def) | ||
| 94 | + { | ||
| 95 | + var used_ops = ops_used_by_graph_def(graph_def); | ||
| 96 | + | ||
| 97 | + // Verify that all used ops are registered. | ||
| 98 | + // var registered_ops = op_def_registry.get_registered_ops(); | ||
| 99 | + | ||
| 100 | + var op_list = new OpList(); | ||
| 101 | + /*used_ops.OrderBy(x => x).Select(x => { | ||
| 102 | + | ||
| 103 | + }).ToArray();*/ | ||
| 104 | + | ||
| 105 | + return op_list; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + /// <summary> | ||
| 109 | + /// Collect the list of ops used by a graph. | ||
| 110 | + /// </summary> | ||
| 111 | + /// <param name="graph_def"></param> | ||
| 112 | + /// <returns></returns> | ||
| 113 | + private static string[] ops_used_by_graph_def(GraphDef graph_def) | ||
| 114 | + { | ||
| 115 | + var used_ops = new List<string>(); | ||
| 116 | + | ||
| 117 | + Action<string> mark_op_as_used = (op) => | ||
| 118 | + { | ||
| 119 | + if (!used_ops.Contains(op)) | ||
| 120 | + { | ||
| 121 | + | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + used_ops.Add(op); | ||
| 125 | + }; | ||
| 126 | + | ||
| 127 | + foreach (var node in graph_def.Node) | ||
| 128 | + { | ||
| 129 | + mark_op_as_used(node.Op); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + return used_ops.ToArray(); | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow | ||
| 6 | + { | ||
| 7 | + public class op_def_registry | ||
| 8 | + { | ||
| 9 | + private static Dictionary<string, OpDef> _registered_ops; | ||
| 10 | + | ||
| 11 | + public static Dictionary<string, OpDef> get_registered_ops() | ||
| 12 | + { | ||
| 13 | + if(_registered_ops == null) | ||
| 14 | + { | ||
| 15 | + _registered_ops = new Dictionary<string, OpDef>(); | ||
| 16 | + var handle = c_api.TF_GetAllOpList(); | ||
| 17 | + var buffer = new Buffer(handle); | ||
| 18 | + var op_list = OpList.Parser.ParseFrom(buffer); | ||
| 19 | + | ||
| 20 | + foreach (var op_def in op_list.Op) | ||
| 21 | + _registered_ops[op_def.Name] = op_def; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + return _registered_ops; | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ public Buffer ToGraphDef(Status s) | |||
| 18 | 18 | return buffer; | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | - public GraphDef _as_graph_def() | ||
| 21 | + public GraphDef _as_graph_def(bool add_shapes = false) | ||
| 22 | 22 | { | |
| 23 | 23 | var buffer = ToGraphDef(Status); | |
| 24 | 24 | Status.Check(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments