public void SetArrayTensor() { using (var tensor = new TFTensor(new [] { 123, 456 })) { tensor.SetValue(new [] { 234, 567 }); Assert.Equal((uint)sizeof(int) * 2, tensor.TensorByteSize.ToUInt32()); Assert.Equal(new [] { 234, 567 }, tensor.GetValue()); } }
public void SetByteTensor() { using (var tensor = new TFTensor((byte)123)) { tensor.SetValue((byte)234); Assert.Equal((uint)sizeof(byte), tensor.TensorByteSize.ToUInt32()); Assert.Equal((byte)234, tensor.GetValue()); } }
public void SetBoolTensor() { using (var tensor = new TFTensor(true)) { tensor.SetValue(false); Assert.Equal((uint)sizeof(bool), tensor.TensorByteSize.ToUInt32()); Assert.Equal(false, tensor.GetValue()); } }
public void CreateTenso() { // Some input matrices var inp = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 35, 55, 82, 115, 135, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 77, 91, 106, 122, 136, 138, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 103, 95, 107, 121, 131, 131, 111, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 112, 101, 91, 102, 121, 126, 126, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 107, 96, 88, 90, 108, 125, 130, 138, 173, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 98, 88, 91, 96, 101, 120, 138, 146, 185, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 96, 93, 91, 92, 98, 111, 135, 153, 172, 221, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 91, 93, 100, 107, 125, 153, 185, 210, 226, 237, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 126, 131, 140, 163, 188, 206, 216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 0 }; Debug.WriteLine("TEST"); for (int i = 0; i < inp.Length; i++) { if (inp[i] == 0) { inp[i] = 255; } // inp[i] = inp[i] / 255; //Debug.WriteLine(inp[i]); } // Debug.WriteLine("len : " + inp.Length); var tensor = TFTensor.FromBuffer(new TFShape(1, 30, 30, 1), inp, 0, inp.Length); // Debug.WriteLine("input: " + tensor); using (var graph = new TFGraph()) { // Load the model // graph.Import(File.ReadAllBytes(@"C:\Users\Public\TestFolder\my_model.pb")); string model_file = Path.Combine(Environment.CurrentDirectory, @"model\gesture_model.pb"); Debug.WriteLine("file path " + model_file); graph.Import(File.ReadAllBytes(model_file)); using (var session = new TFSession(graph)) { var runner = session.GetRunner(); //Debug.WriteLine(graph); runner.AddInput(graph[INPUT_NAME][0], tensor); runner.Fetch(graph[OUTPUT_NAME][0]); Debug.WriteLine(System.DateTime.Now); var output = runner.Run(); TFTensor result = output[0]; var re = result.GetValue() as float[, ]; foreach (float f in re) { Debug.WriteLine(f); } Debug.WriteLine("o: " + re[0, 1]); Debug.WriteLine(result); Debug.WriteLine(System.DateTime.Now); } } }
public double[] DetectObjects(string file) { sw.Restart(); TFTensor tensor = CreateTensorFromImageFile(file); Rectangle[] rect; Rectangle size = new Rectangle(10, 10, 10, 10); return(DetectObjectsFromTensor(tensor, size, out rect)); }
public double[] DetectObjects(Image img) { sw.Restart(); TFTensor tensor = CreateTensorFromBitmap(img); Rectangle[] rect; Rectangle size = new Rectangle(10, 10, 10, 10); return(DetectObjectsFromTensor(tensor, size, out rect)); }
public IEnumerable <TFTensor> GetFeaturesTensors() { byte[] sourceAsciiBytes = Encoding.ASCII.GetBytes(Source); byte[] targetAsciiBytes = Encoding.ASCII.GetBytes(Target); TFTensor sourceTensor = new TFTensor(sourceAsciiBytes); TFTensor targetTensor = new TFTensor(sourceAsciiBytes); return(new[] { sourceTensor, targetTensor }); }
public void SetArrayTensorWithWrongDimensions() { using (var tensor = new TFTensor(new [, ] { { 123 } })) { var exception = Assert.Throws <ArgumentException> (() => tensor.SetValue(new [] { 234 })); Assert.Equal("This tensor has 2 dimensions, the given array has 1", exception.Message); } }
public void LoadTFModel(Texture2D tex) { var shape = new TFShape(1, inputWidth, inputHeight, 3); var input = graph[inputName][0]; TFTensor inputTensor = null; int angle = 90; Flip flip = Flip.NONE; if (input.OutputType == TFDataType.Float) { float[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight, inputMean, inputStd, angle, flip); inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length); } else if (input.OutputType == TFDataType.UInt8) { byte[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight, angle, flip); inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length); } else { throw new Exception($"Input date type {input.OutputType} is not supported."); } var runner = session.GetRunner(); runner.AddInput(input, inputTensor).Fetch(graph[outputName][0]); var output = runner.Run()[0]; var outputs = output.GetValue() as float[, ]; inputTensor.Dispose(); output.Dispose(); var list = new List <KeyValuePair <string, float> >(); for (int i = 0; i < labels.Length; i++) { var confidence = outputs[0, i]; if (confidence < 0.05f) { continue; } list.Add(new KeyValuePair <string, float>(labels[i], confidence)); } var results = list.OrderByDescending(i => i.Value).Take(3).ToList(); foreach (KeyValuePair <string, float> value in results) { Debug.Log("my model: " + value.Key); } }
private TFTensor GenerateTensor(byte[] image) { #if UNITY_ANDROID TFShape tshape = new TFShape(1, 128, 128, 3); return(TFTensor.FromBuffer(tshape, image, 0, image.Length)); #endif #if UNITY_EDITOR_WIN // TODO: This dosen't work return(ImageUtil.CreateTensorFromImageFile(image)); #endif }
public unsafe static Tensor <float> ToNCHW(this TFTensor tensor) { var span = new Span <float>(tensor.Data.ToPointer(), (int)tensor.TensorByteSize / 4); Tensor <float> dense = new DenseTensor <float>(span.ToArray(), tensor.Shape.Select(x => (int)x).ToArray()); if (dense.Dimensions.Length == 4) { dense = dense.Transpose(new[] { 0, 3, 1, 2 }); } return(dense); }
public void SetArrayTensorWithWrongLength() { using (var tensor = new TFTensor(new [, ] { { 123 } })) { var exception = Assert.Throws <ArgumentException> (() => tensor.SetValue(new [, ] { { 234, 567 } })); Assert.Equal("This tensor has shape [1,1], the given array has shape [1,2]", exception.Message); } }
public float[,] GetValue(string inputNode, string outputNode, byte[] inputData) { if (!_bModelLoaded) return null; var runner = _session.GetRunner(); TFTensor tensor = TFUtils.CreateTensor(inputData, TFDataType.Float); runner.AddInput(_tfGraph[inputNode][0], tensor); runner.Fetch(_tfGraph[outputNode][0]); TFTensor[] output = runner.Run(); float[,] value = (float[,])output[0].GetValue(); return value; }
private static (TFTensor, TFTensor) CreateTestTensors() { var x = new int[] { 1061, 19693, 974, 980, 981, 811, 11474, 589, 39, 1058, 39, 57, 1057, 4934, 46, 1, 1, 1, 1, 1, 1, 1, 1 }; var l = new int[] { 15 }; var tensor = TFTensor.FromBuffer(new TFShape(1, x.Length), x, 0, x.Length); var lengths = TFTensor.FromBuffer(new TFShape(1), l, 0, l.Length); return(tensor, lengths); }
private IEnumerator GetCamPics() { if (search) { while (tempsearch) { tensor3 = TransformInput(camFeed.GetImage(), INPUT_SIZE, INPUT_SIZE); search = false; yield return(new WaitForSeconds(2.0f)); } } }
static void Main(string[] args) { var g = new TFGraph(); var hello = g.Const(TFTensor.CreateString(Encoding.UTF8.GetBytes("Hello,world!"))); var sess = new TFSession(g); var result = sess.GetRunner().Run(hello); Console.WriteLine(result.GetValue()); }
public void Export(string newModelDir) { if (newModelDir.Last() != '/' && newModelDir.Last() != '\\') { newModelDir += "/"; } TFOutput NodeSaver, NodeSaverPath; //if (!ForTraining) { NodeSaver = Graph["save_1/control_dependency"][0]; NodeSaverPath = Graph["save_1/Const"][0]; } //else //{ // NodeSaver = Graph["save_2/control_dependency"][0]; // NodeSaverPath = Graph["save_2/Const"][0]; //} Directory.CreateDirectory(newModelDir); if (Directory.Exists(newModelDir + "variables")) { Directory.Delete(newModelDir + "variables", true); } Directory.CreateDirectory(newModelDir + "variables"); foreach (var fileName in Directory.EnumerateFiles(ModelDir)) { File.Copy(fileName, newModelDir + Helper.PathToNameWithExtension(fileName), true); } TFTensor TensorPath = TFTensor.CreateString(Encoding.ASCII.GetBytes(newModelDir + "variables/variables")); var Runner = Session.GetRunner().AddInput(NodeSaverPath, TensorPath); Runner.Run(NodeSaver); if (Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").Count() > 0) { string TempName = Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").First(); foreach (var oldPath in Directory.EnumerateFiles(TempName)) { string OldName = Helper.PathToNameWithExtension(oldPath); string NewName = "variables" + OldName.Substring(OldName.IndexOf(".")); string NewPath = newModelDir + "variables/" + NewName; File.Move(oldPath, NewPath); } Directory.Delete(TempName, true); } TensorPath.Dispose(); }
public void TestSession() { var status = new TFStatus(); using (var graph = new TFGraph()) { var feed = Placeholder(graph, status); var two = ScalarConst(2, graph, status); var add = Add(feed, two, graph, status); Assert(status); // Create a session for this graph using (var session = new TFSession(graph, status)) { Assert(status); // Run the graph var inputs = new TFOutput [] { new TFOutput(feed, 0) }; var input_values = new TFTensor [] { 3 }; var add_output = new TFOutput(add, 0); var outputs = new TFOutput [] { add_output }; var results = session.Run(runOptions: null, inputs: inputs, inputValues: input_values, outputs: outputs, targetOpers: null, runMetadata: null, status: status); Assert(status); var res = results [0]; Assert(res.TensorType == TFDataType.Int32); Assert(res.NumDims == 0); // Scalar Assert(res.TensorByteSize == (UIntPtr)4); Assert(Marshal.ReadInt32(res.Data) == 3 + 2); // Use runner API var runner = session.GetRunner(); runner.AddInput(new TFOutput(feed, 0), 3); runner.Fetch(add_output); results = runner.Run(status: status); res = results [0]; Assert(res.TensorType == TFDataType.Int32); Assert(res.NumDims == 0); // Scalar Assert(res.TensorByteSize == (UIntPtr)4); Assert(Marshal.ReadInt32(res.Data) == 3 + 2); } } }
TFOperation ScalarConst(TFTensor v, TFGraph graph, TFStatus status, string name = null) { var desc = new TFOperationDesc(graph, "Const", name == null ? "scalar" : name); desc.SetAttr("value", v, status); if (status.StatusCode != TFCode.Ok) { return(null); } desc.SetAttrType("dtype", TFDataType.Int32); return(desc.FinishOperation()); }
private void ReadFrame(object sender, EventArgs arg)//捕获摄像头画面的事件 { DateTime TimeStart = DateTime.Now; // VideoCapture捕获一帧图像 try { capture.Retrieve(mat, 0); } catch { } // 创建Tensor作为网络输入 TFTensor tensor = Mat2Tensor(mat); // 前向推理 TFSession.Runner runner = session.GetRunner(); runner.AddInput(graph["image_tensor"][0], tensor); runner.Fetch(graph["num_detections"][0]); runner.Fetch(graph["detection_scores"][0]); runner.Fetch(graph["detection_boxes"][0]); runner.Fetch(graph["detection_classes"][0]); TFTensor[] outputs = runner.Run(); // 解析结果 float num = ((float[])outputs[0].GetValue(jagged: true))[0]; float[] scores = ((float[][])outputs[1].GetValue(jagged: true))[0]; float[][] boxes = ((float[][][])outputs[2].GetValue(jagged: true))[0]; float[] classes = ((float[][])outputs[3].GetValue(jagged: true))[0]; // 显示检测框和类别 for (int i = 0; i < (int)num; i++) { if (scores[i] > 0.8) { int left = (int)(boxes[i][1] * frameWidth); int top = (int)(boxes[i][0] * frameHeight); int right = (int)(boxes[i][3] * frameWidth); int bottom = (int)(boxes[i][2] * frameHeight); CvInvoke.PutText(mat, labels[(int)classes[i]] + ": " + scores[i].ToString("0.00"), new Point(left, top), FontFace.HersheyDuplex, (right - left) / 200, colorGreen); CvInvoke.Rectangle(mat, new Rectangle(left, top, right - left, bottom - top), colorRed, 2); } } // 在imageBox上显示经过缩放的图像 Mat dst = new Mat(); CvInvoke.Resize(mat, dst, new Size(imageBox.Width, imageBox.Height)); imageBox.Image = cameraFlag ? dst : null; TimeSpan TimeCount = DateTime.Now - TimeStart; textFPS.Text = (1000 / TimeCount.TotalMilliseconds).ToString("0.00"); }
private TFTensor[] ProcessImage(TFTensor input) { var runner = Session.GetRunner(); runner = runner.AddInput(Graph[InputParameter][0], input); foreach (string output in OutputParameters) { runner.Fetch(Graph[output][0]); } return(runner.Run()); }
private TFTensor Predict(TFTensor input) { var runner = _session.GetRunner(); var graph = _session.Graph; runner.AddInput(graph["Image"][0], input); runner.Fetch(graph["Predict"][0]); var output = runner.Run(); return(output[0]); }
public IList Classify(Texture2D texture, int numResults = 5, float threshold = 0.1f, int angle = 0, Flip flip = Flip.NONE) { var shape = new TFShape(1, inputWidth, inputHeight, 3); var input = graph[inputName][0]; TFTensor inputTensor = null; if (input.OutputType == TFDataType.Float) { float[] imgData = Utils.DecodeTexture(texture, inputWidth, inputHeight, inputMean, inputStd, angle, flip); inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length); } else if (input.OutputType == TFDataType.UInt8) { byte[] imgData = Utils.DecodeTexture(texture, inputWidth, inputHeight, angle, flip); inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length); } else { throw new Exception($"Input date type {input.OutputType} is not supported."); } var runner = session.GetRunner(); runner.AddInput(input, inputTensor).Fetch(graph[outputName][0]); var output = runner.Run()[0]; var outputs = output.GetValue() as float[, ]; inputTensor.Dispose(); output.Dispose(); var list = new List <KeyValuePair <string, float> >(); for (int i = 0; i < labels.Length; i++) { var confidence = outputs[0, i]; if (confidence < threshold) { continue; } list.Add(new KeyValuePair <string, float>(labels[i], confidence)); } var results = list.OrderByDescending(i => i.Value).Take(numResults).ToList(); //Utils.Log(results); return(results); }
public void ConstructDoubleTensor() { double value = 123.456; using (var tensor = new TFTensor(value)) { Assert.Equal(TFDataType.Double, tensor.TensorType); Assert.Equal(0, tensor.NumDims); Assert.Equal(new long [0], tensor.Shape); Assert.Equal((uint)sizeof(double), tensor.TensorByteSize.ToUInt32()); Assert.Equal(value, tensor.GetValue()); } }
public void ConstructUnsignedShortTensor() { ushort value = 123; using (var tensor = new TFTensor(value)) { Assert.Equal(TFDataType.UInt16, tensor.TensorType); Assert.Equal(0, tensor.NumDims); Assert.Equal(new long [0], tensor.Shape); Assert.Equal((uint)sizeof(ushort), tensor.TensorByteSize.ToUInt32()); Assert.Equal(value, tensor.GetValue()); } }
public void ConstructLongTensor() { long value = 123L; using (var tensor = new TFTensor(value)) { Assert.Equal(TFDataType.Int64, tensor.TensorType); Assert.Equal(0, tensor.NumDims); Assert.Equal(new long [0], tensor.Shape); Assert.Equal((uint)sizeof(long), tensor.TensorByteSize.ToUInt32()); Assert.Equal(value, tensor.GetValue()); } }
public void SetMultiDimArrayTensorWithJagged() { using (var tensor = new TFTensor(new [, ] { { 123, 456 } })) { tensor.SetValue(new [] { new [] { 234, 567 } }); Assert.Equal((uint)sizeof(int) * 2, tensor.TensorByteSize.ToUInt32()); Assert.Equal(new [, ] { { 234, 567 } }, tensor.GetValue()); } }
public void ConstructFloatTensor() { float value = 123.456f; using (var tensor = new TFTensor(value)) { Assert.Equal(TFDataType.Float, tensor.TensorType); Assert.Equal(0, tensor.NumDims); Assert.Equal(new long [0], tensor.Shape); Assert.Equal((uint)sizeof(float), tensor.TensorByteSize.ToUInt32()); Assert.Equal(value, tensor.GetValue()); } }
public void ConstructComplexTensor() { Complex value = new Complex(1, 2); using (var tensor = new TFTensor(value)) { Assert.Equal(TFDataType.Complex128, tensor.TensorType); Assert.Equal(0, tensor.NumDims); Assert.Equal(new long [0], tensor.Shape); Assert.Equal(16u, tensor.TensorByteSize.ToUInt32()); Assert.Equal(value, tensor.GetValue()); } }
public void GetArrayValueInPlace(Array array) { using (var tensor = new TFTensor(array)) { var type = array.GetType().GetElementType(); var value = Array.CreateInstance(type, tensor.Shape); Assert.NotEqual(array, value); tensor.GetValue(value); Assert.Equal(array, value); } }