public TensorShape(TensorShapeProto proto) { if (proto.UnknownRank) { return; } switch (proto.Dim.Count) { case 0: shape = new Shape(new int[0]); break; case 1: shape = Shape.Vector((int)proto.Dim[0].Size); break; case 2: shape = Shape.Matrix((int)proto.Dim[0].Size, (int)proto.Dim[1].Size); break; default: var protodims = proto.Dim; var len = protodims.Count; var dims = new int[len]; for (int i = 0; i < len; i++) { dims[i] = (int)protodims[i].Size; } shape = new Shape(dims); break; } }
public static TensorShapeProto as_shape <T>(T[] dims) { TensorShapeProto shape = new TensorShapeProto(); for (int i = 0; i < dims.Length; i++) { var dim = new TensorShapeProto.Types.Dim(); switch (dims[i]) { case int n: dim.Size = n; break; case long l: dim.Size = l; break; default: throw new NotImplementedException("as_shape Not Implemented"); } // dim.Name = $"dim_{i}"; shape.Dim.Add(dim); } return(shape); }
public TensorShape(TensorShapeProto proto) { if (proto.UnknownRank) { return; } Reshape(proto.Dim.Select(x => (int)x.Size).ToArray()); }
public static TensorShapeProto as_proto(this Shape tshape) { TensorShapeProto shape = new TensorShapeProto(); for (int i = 0; i < tshape.ndim; i++) { var dim = new TensorShapeProto.Types.Dim(); dim.Size = tshape.dims[i]; //dim.Name = $"dim_{i}"; shape.Dim.Add(dim); } return(shape); }
public static TensorShapeProto as_shape(long[] dims) { TensorShapeProto shape = new TensorShapeProto(); for (int i = 0; i < dims.Length; i++) { var dim = new TensorShapeProto.Types.Dim(); dim.Size = dims[i]; dim.Name = $"dim_{i}"; shape.Dim.Add(dim); } return(shape); }
public object get_attr(string name) { object ret = null; var fields = new string[] { "s", "i", "f", "b", "type", "shape", "tensor", "func" }; switch (name) { case "dtype": ret = _outputs[0]; break; case "shape": ret = new TensorShapeProto(); break; } return(ret); }
public void TestInception() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile <float>("grace_hopper.jpg", 224, 224, 128.0f, 1.0f)) using (Inception inceptionGraph = new Inception()) { bool processCompleted = false; inceptionGraph.OnDownloadCompleted += (sender, e) => { HashSet <string> opNames = new HashSet <string>(); HashSet <string> couldBeInputs = new HashSet <string>(); HashSet <string> couldBeOutputs = new HashSet <string>(); foreach (Operation op in inceptionGraph.Graph) { String name = op.Name; opNames.Add(name); if (op.NumInputs == 0 && op.OpType.Equals("Placeholder")) { couldBeInputs.Add(op.Name); AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata shapeMeta = op.GetAttrMetadata("shape"); DataType type = op.GetAttrType("dtype"); Int64[] shape = op.GetAttrShape("shape"); Buffer valueBuffer = op.GetAttrValueProto("shape"); Buffer shapeBuffer = op.GetAttrTensorShapeProto("shape"); Tensorflow.TensorShapeProto shapeProto = Tensorflow.TensorShapeProto.Parser.ParseFrom(shapeBuffer.Data); } if (op.OpType.Equals("Const")) { AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata valueMeta = op.GetAttrMetadata("value"); using (Tensor valueTensor = op.GetAttrTensor("value")) { var dim = valueTensor.Dim; } } if (op.OpType.Equals("Conv2D")) { AttrMetadata stridesMeta = op.GetAttrMetadata("strides"); AttrMetadata paddingMeta = op.GetAttrMetadata("padding"); AttrMetadata boolMeta = op.GetAttrMetadata("use_cudnn_on_gpu"); Int64[] strides = op.GetAttrIntList("strides"); bool useCudnn = op.GetAttrBool("use_cudnn_on_gpu"); String padding = op.GetAttrString("padding"); } foreach (Output output in op.Outputs) { int[] shape = inceptionGraph.Graph.GetTensorShape(output); if (output.NumConsumers == 0) { couldBeOutputs.Add(name); } } Buffer buffer = inceptionGraph.Graph.GetOpDef(op.OpType); Tensorflow.OpDef opDef = Tensorflow.OpDef.Parser.ParseFrom(buffer.Data); } using (Buffer versionDef = inceptionGraph.Graph.Versions()) { int l = versionDef.Length; } Inception.RecognitionResult[] results = inceptionGraph.Recognize(imageTensor); Trace.WriteLine(String.Format("Object is {0} with {1}% probability", results[0].Label, results[0].Probability * 100)); processCompleted = true; }; inceptionGraph.Init(); while (!processCompleted) { Thread.Sleep(1000); } } }
public async Task TestResnet() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg", 224, 224, 0, 1.0f / 255.0f)) using (Resnet resnet = new Resnet()) { await resnet.Init(); MetaGraphDef metaGraphDef = MetaGraphDef.Parser.ParseFrom(resnet.MetaGraphDefBuffer.Data); var signatureDef = metaGraphDef.SignatureDef["serving_default"]; var inputNode = signatureDef.Inputs; var outputNode = signatureDef.Outputs; HashSet <string> opNames = new HashSet <string>(); HashSet <string> couldBeInputs = new HashSet <string>(); HashSet <string> couldBeOutputs = new HashSet <string>(); foreach (Operation op in resnet.Graph) { String name = op.Name; opNames.Add(name); if (op.NumInputs == 0 && op.OpType.Equals("Placeholder")) { couldBeInputs.Add(op.Name); AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata shapeMeta = op.GetAttrMetadata("shape"); DataType type = op.GetAttrType("dtype"); Int64[] shape = op.GetAttrShape("shape"); Buffer valueBuffer = op.GetAttrValueProto("shape"); Buffer shapeBuffer = op.GetAttrTensorShapeProto("shape"); Tensorflow.TensorShapeProto shapeProto = Tensorflow.TensorShapeProto.Parser.ParseFrom(shapeBuffer.Data); } if (op.OpType.Equals("Const")) { AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata valueMeta = op.GetAttrMetadata("value"); using (Tensor valueTensor = op.GetAttrTensor("value")) { var dim = valueTensor.Dim; } } if (op.OpType.Equals("Conv2D")) { AttrMetadata stridesMeta = op.GetAttrMetadata("strides"); AttrMetadata paddingMeta = op.GetAttrMetadata("padding"); AttrMetadata boolMeta = op.GetAttrMetadata("use_cudnn_on_gpu"); Int64[] strides = op.GetAttrIntList("strides"); bool useCudnn = op.GetAttrBool("use_cudnn_on_gpu"); String padding = op.GetAttrString("padding"); } foreach (Output output in op.Outputs) { int[] shape = resnet.Graph.GetTensorShape(output); if (output.NumConsumers == 0) { couldBeOutputs.Add(name); } } Buffer buffer = resnet.Graph.GetOpDef(op.OpType); Tensorflow.OpDef opDef = Tensorflow.OpDef.Parser.ParseFrom(buffer.Data); } using (Buffer versionDef = resnet.Graph.Versions()) { int l = versionDef.Length; } Resnet.RecognitionResult[][] results = resnet.Recognize(imageTensor); } }
public TensorShapeProto as_proto() { TensorShapeProto dim = new TensorShapeProto(); return(new TensorShapeProto(dim)); }
public Shape(TensorShapeProto proto) { _dims = proto.Dim.Select(x => x.Size).ToArray(); }