Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using TensorFlow;
  6. using UnityEngine;
  7.  
  8.  
  9. namespace TFClassify
  10. {
  11. public class Segmentation
  12. {
  13.  
  14. private TFGraph graph;
  15. private TFSession session;
  16. private int inputSize;
  17. private static string INPUT_NAME = "in_node"; //ImageTensor
  18. private static string OUTPUT_NAME = "module1/Sigmoid"; // SemanticPredictions
  19.  
  20.  
  21. public Segmentation(byte[] model, int inputSize)
  22. {
  23. #if (UNITY_ANDROID && !UNITY_EDITOR)
  24. TensorFlowSharp.Android.NativeBinding.Init();
  25. #endif
  26. this.inputSize = inputSize;
  27. this.graph = new TFGraph();
  28. this.graph.Import(new TFBuffer(model));
  29. this.session = new TFSession(this.graph);
  30. }
  31.  
  32.  
  33. public Task<float[,,,]> SegmentAsync(Color32[] data, int width, int height)
  34. {
  35. return Task.Run(() =>
  36. {
  37. //Debug.Log("Start : " + System.DateTime.Now.ToString());
  38.  
  39. var pixel = new float[1,height,width,1];
  40.  
  41. using (var tensor = TransformInput(data, width, height))
  42. {
  43.  
  44. var runner = this.session.GetRunner();
  45. runner.AddInput(this.graph[INPUT_NAME][0], tensor)
  46. .Fetch(this.graph[OUTPUT_NAME][0]);
  47. var t = System.DateTime.Now.Ticks;
  48. var output = runner.Run();
  49.  
  50. // Fetch the results from output:
  51. TFTensor result = output[0];
  52. var shape = result.Shape;
  53. //Debug.Log((float[][][][]) result.GetValue(jagged: true));
  54.  
  55. try
  56. {
  57. pixel = (float[,,,])result.GetValue(jagged: false); // int[][]
  58. Debug.Log("Finish : " + (System.DateTime.Now.Ticks - t));
  59. }
  60. catch (Exception e)
  61. {
  62. Debug.Log(e.ToString());
  63. }
  64.  
  65. }
  66. return pixel;
  67. });
  68.  
  69. }
  70.  
  71.  
  72.  
  73. public static TFTensor TransformInput(Color32[] data, int width, int height)
  74. {
  75. //byte[] mFlatIntValues = new byte[0];
  76.  
  77. //try {
  78. // mFlatIntValues = new byte[width * height * 3];
  79.  
  80. //}catch(Exception e)
  81. //{
  82. // Debug.Log(e.ToString());
  83. //}
  84.  
  85. //for (int i = 0; i < data.Length; ++i) // data.Length = 513*513 = 263169
  86. //{
  87. // var color = data[i];
  88.  
  89. // mFlatIntValues[i * 3 + 0] = (byte)color.r;
  90. // mFlatIntValues[i * 3 + 1] = (byte)color.g;
  91. // mFlatIntValues[i * 3 + 2] = (byte)color.b;
  92. //}
  93.  
  94. float[] mFlatIntValues = new float[0];
  95.  
  96. try
  97. {
  98. mFlatIntValues = new float[width * height * 3];
  99.  
  100. }
  101. catch (Exception e)
  102. {
  103. Debug.Log(e.ToString());
  104. }
  105.  
  106. for (int i = 0; i < data.Length; ++i) // data.Length = 513*513 = 263169
  107. {
  108. var color = data[i];
  109.  
  110. mFlatIntValues[i * 3 + 0] = (float) color.r;
  111. mFlatIntValues[i * 3 + 1] = (float) color.g;
  112. mFlatIntValues[i * 3 + 2] = (float) color.b;
  113. }
  114.  
  115.  
  116. TFShape shape = new TFShape(1, height, width, 3);
  117.  
  118. //Debug.Log("height " + height);
  119. //Debug.Log("width " + width);
  120.  
  121. return TFTensor.FromBuffer(shape, mFlatIntValues, 0, mFlatIntValues.Length);
  122. //TFShape, Byte[] data, int start, int count
  123. }
  124.  
  125.  
  126.  
  127.  
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement