|
| 1 | +using Google.Protobuf; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using static Tensorflow.OpDef.Types; |
| 7 | + |
| 8 | +namespace Tensorflow |
| 9 | +{ |
| 10 | + public class importer |
| 11 | + { |
| 12 | + public static ITensorOrOperation[] import_graph_def(GraphDef graph_def, |
| 13 | + Dictionary<string, Tensor> input_map = null, |
| 14 | + string[] return_elements = null, |
| 15 | + string name = "", |
| 16 | + OpList producer_op_list = null) |
| 17 | + { |
| 18 | + var op_dict = op_def_registry.get_registered_ops(); |
| 19 | + |
| 20 | + graph_def = _ProcessGraphDefParam(graph_def, op_dict); |
| 21 | + input_map = _ProcessInputMapParam(input_map); |
| 22 | + return_elements = _ProcessReturnElementsParam(return_elements); |
| 23 | + |
| 24 | + if (producer_op_list != null) |
| 25 | + _RemoveDefaultAttrs(op_dict, producer_op_list, graph_def); |
| 26 | + |
| 27 | + string prefix = ""; |
| 28 | + var graph = ops.get_default_graph(); |
| 29 | + Python.with<ops.name_scope>(new ops.name_scope(name, "import", input_map.Values), scope => |
| 30 | + { |
| 31 | + /*prefix = scope; |
| 32 | + if (!string.IsNullOrEmpty(prefix)) |
| 33 | + prefix = prefix.Substring(0, prefix.Length - 1); |
| 34 | + else |
| 35 | + prefix = "";*/ |
| 36 | + |
| 37 | + // Generate any input map tensors inside name scope |
| 38 | + input_map = _ConvertInputMapValues(name, input_map); |
| 39 | + }); |
| 40 | + |
| 41 | + var scoped_options = c_api_util.ScopedTFImportGraphDefOptions(); |
| 42 | + _PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements); |
| 43 | + |
| 44 | + var bytes = graph_def.ToByteString().ToArray(); |
| 45 | + |
| 46 | + var status = new Status(); |
| 47 | + c_api.TF_GraphImportGraphDefWithResults(graph, IntPtr.Zero, scoped_options, status); |
| 48 | + |
| 49 | + throw new NotImplementedException("importer.import_graph_def"); |
| 50 | + } |
| 51 | + |
| 52 | + public static void _PopulateTFImportGraphDefOptions(ImportGraphDefOptions options, |
| 53 | + string prefix, |
| 54 | + Dictionary<string, Tensor> input_map, |
| 55 | + string[] return_elements) |
| 56 | + { |
| 57 | + c_api.TF_ImportGraphDefOptionsSetPrefix(options, prefix); |
| 58 | + c_api.TF_ImportGraphDefOptionsSetUniquifyNames(options, (char)1); |
| 59 | + |
| 60 | + foreach(var input in input_map) |
| 61 | + { |
| 62 | + throw new NotImplementedException("_PopulateTFImportGraphDefOptions"); |
| 63 | + } |
| 64 | + |
| 65 | + if (return_elements == null) |
| 66 | + return_elements = new string[0]; |
| 67 | + |
| 68 | + foreach (var name in return_elements) |
| 69 | + { |
| 70 | + throw new NotImplementedException("_PopulateTFImportGraphDefOptions"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static Dictionary<string, Tensor> _ConvertInputMapValues(string name, Dictionary<string, Tensor> input_map) |
| 75 | + { |
| 76 | + return input_map; |
| 77 | + } |
| 78 | + |
| 79 | + public static GraphDef _ProcessGraphDefParam(GraphDef graph_def, Dictionary<string, OpDef> op_dict) |
| 80 | + { |
| 81 | + foreach(var node in graph_def.Node) |
| 82 | + { |
| 83 | + if (!op_dict.ContainsKey(node.Op)) |
| 84 | + continue; |
| 85 | + |
| 86 | + var op_def = op_dict[node.Op]; |
| 87 | + _SetDefaultAttrValues(node, op_def); |
| 88 | + } |
| 89 | + |
| 90 | + return graph_def; |
| 91 | + } |
| 92 | + |
| 93 | + private static void _SetDefaultAttrValues(NodeDef node_def, OpDef op_def) |
| 94 | + { |
| 95 | + foreach(var attr_def in op_def.Attr) |
| 96 | + { |
| 97 | + var key = attr_def.Name; |
| 98 | + if(attr_def.DefaultValue != null) |
| 99 | + { |
| 100 | + var value = node_def.Attr[key]; |
| 101 | + if (value == null) |
| 102 | + node_def.Attr[key] = attr_def.DefaultValue; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static Dictionary<string, Tensor> _ProcessInputMapParam(Dictionary<string, Tensor> input_map) |
| 108 | + { |
| 109 | + if (input_map == null) |
| 110 | + return new Dictionary<string, Tensor>(); |
| 111 | + |
| 112 | + return input_map; |
| 113 | + } |
| 114 | + |
| 115 | + private static string[] _ProcessReturnElementsParam(string[] return_elements) |
| 116 | + { |
| 117 | + if (return_elements == null) |
| 118 | + return null; |
| 119 | + |
| 120 | + return return_elements; |
| 121 | + } |
| 122 | + |
| 123 | + private static void _RemoveDefaultAttrs(Dictionary<string, OpDef> op_dict, OpList producer_op_list, GraphDef graph_def) |
| 124 | + { |
| 125 | + var producer_op_dict = new Dictionary<string, OpDef>(); |
| 126 | + producer_op_list.Op.Select(op => |
| 127 | + { |
| 128 | + producer_op_dict[op.Name] = op; |
| 129 | + return op; |
| 130 | + }).ToArray(); |
| 131 | + |
| 132 | + foreach(var node in graph_def.Node) |
| 133 | + { |
| 134 | + // Remove any default attr values that aren't in op_def. |
| 135 | + if (producer_op_dict.ContainsKey(node.Op)) |
| 136 | + { |
| 137 | + var op_def = op_dict[node.Op]; |
| 138 | + var producer_op_def = producer_op_dict[node.Op]; |
| 139 | + foreach(var key in node.Attr) |
| 140 | + { |
| 141 | + if(_FindAttrInOpDef(key.Key, op_def) == null) |
| 142 | + { |
| 143 | + var attr_def = _FindAttrInOpDef(key.Key, producer_op_def); |
| 144 | + if (attr_def != null && attr_def.DefaultValue != null && |
| 145 | + node.Attr[key.Key] == attr_def.DefaultValue) |
| 146 | + node.Attr[key.Key].ClearValue(); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static AttrDef _FindAttrInOpDef(string name, OpDef op_def) |
| 154 | + { |
| 155 | + return op_def.Attr.FirstOrDefault(x => x.Name == name); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments