|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using Tensorflow; |
| 7 | +using static Tensorflow.OpDef.Types; |
| 8 | + |
| 9 | +namespace TensorFlowNET.Core |
| 10 | +{ |
| 11 | + public class OpDefLibrary |
| 12 | + { |
| 13 | + public Dictionary<string, OpDef> _ops = new Dictionary<string, OpDef>(); |
| 14 | + |
| 15 | + public void add_op_list(OpList op_list) |
| 16 | + { |
| 17 | + foreach(var op_def in op_list.Op) |
| 18 | + { |
| 19 | + add_op(op_def); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public void add_op(OpDef op_def) |
| 24 | + { |
| 25 | + _ops[op_def.Name] = op_def; |
| 26 | + } |
| 27 | + |
| 28 | + public unsafe Operation _apply_op_helper(string op_type_name, string name = "", DataType? dtype = null, TensorShape shape = null) |
| 29 | + { |
| 30 | + var op_def = _ops[op_type_name]; |
| 31 | + |
| 32 | + var status = new Status(); |
| 33 | + var buffer = new Buffer(); |
| 34 | + |
| 35 | + var g = ops.get_default_graph(); |
| 36 | + |
| 37 | + if (String.IsNullOrEmpty(name)) |
| 38 | + { |
| 39 | + name = op_type_name; |
| 40 | + } |
| 41 | + |
| 42 | + foreach(var attr_def in op_def.Attr) |
| 43 | + { |
| 44 | + if (attr_def.Type != "type") continue; |
| 45 | + var key = attr_def.Name; |
| 46 | + } |
| 47 | + |
| 48 | + foreach(var input_arg in op_def.InputArg) |
| 49 | + { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + var attr_protos = new Dictionary<string, AttrValue>(); |
| 54 | + foreach (var attr_def in op_def.Attr) |
| 55 | + { |
| 56 | + var key = attr_def.Name; |
| 57 | + var attr_value = new AttrValue(); |
| 58 | + |
| 59 | + switch (attr_def.Type) |
| 60 | + { |
| 61 | + case "type": |
| 62 | + attr_value.Type = dtype.Value; |
| 63 | + break; |
| 64 | + case "shape": |
| 65 | + attr_value.Shape = new TensorShapeProto(); |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + attr_protos[key] = attr_value; |
| 70 | + } |
| 71 | + |
| 72 | + var output_types = new List<DataType>(); |
| 73 | + |
| 74 | + foreach (var arg in op_def.OutputArg) |
| 75 | + { |
| 76 | + if (!String.IsNullOrEmpty(arg.NumberAttr)) |
| 77 | + { |
| 78 | + |
| 79 | + } |
| 80 | + else if (!String.IsNullOrEmpty(arg.TypeAttr)) |
| 81 | + { |
| 82 | + output_types.Add(attr_protos[arg.TypeAttr].Type); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + var op = g.create_op(op_type_name, null, output_types.ToArray(), |
| 87 | + name: "Placeholder_1/", |
| 88 | + input_types: new DataType[] { }, |
| 89 | + attrs: null, |
| 90 | + op_def: null); |
| 91 | + |
| 92 | + return op; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments