Guest User

Untitled

a guest
Jul 15th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.XR;
  4. using System.IO;
  5. using System;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using UnityEngine.Networking;
  10. using UnityEngine.UI;
  11. using System.Threading;
  12.  
  13. using TensorFlow;
  14.  
  15.  
  16. public class AccelerometerInputCNN : MonoBehaviour
  17. {
  18. // set by trained CNN model
  19. public int inputWidth = 90;
  20.  
  21. // third value corresponds to inputWidth
  22. public TextAsset graphModel;
  23. // the dimenstions of 'inputTensor' correspond to the dimensions of the input for my trained graph
  24. private float[,,,] inputTensor = new float[1, 1, 90, 1];
  25.  
  26. // determine if person is walking from cnn returned value
  27. private int standIndex = 0;
  28.  
  29. // how many options of activities we have - standing, walking, jogging
  30. private int activityIndexChoices = 2;
  31.  
  32. // list to collect acceleration values
  33. private List<float> accelL;
  34.  
  35. float confidence = 0;
  36. float sum = 0f;
  37. float test = 0f;
  38. int activity = 0;
  39. int index = 0;
  40.  
  41. void Start ()
  42. {
  43. #if UNITY_ANDROID
  44. TensorFlowSharp.Android.NativeBinding.Init ();
  45. #endif
  46.  
  47. // initialize the oculus go display
  48. display = new OVRDisplay ();
  49.  
  50. accelL = new List<float> ();
  51. }
  52.  
  53. void FixedUpdate ()
  54. {
  55. collectValues();
  56. }
  57.  
  58. void collectValues()
  59. {
  60. float curr = display.acceleration.y;
  61.  
  62. if (accelL.Count < inputWidth) {
  63. accelL.Add (curr);
  64. }
  65. if (accelL.Count == inputWidth) {
  66. index = evaluate ();
  67. accelL.RemoveAt (0);
  68. accelL.Add (curr);
  69. }
  70. }
  71.  
  72. void evaluate ()
  73. {
  74. // convert from list to array
  75. for (int i = 0; i < accelL.Count; i++) {
  76. inputTensor [0, 0, i, 0] = accelL [i];
  77. test = inputTensor [0, 0, i, 0];
  78. }
  79.  
  80. // declare a list for outputs
  81. float[,] recurrentTensor;
  82.  
  83. // set up and run a graph session
  84. using (var graph = new TFGraph())
  85. {
  86. graph.Import(graphModel.bytes);
  87. var session = new TFSession(graph);
  88.  
  89. var runner = session.GetRunner();
  90.  
  91. // implicitally convert a C# array to a tensor
  92. TFTensor input = inputTensor;
  93.  
  94. // set up input tensor and input
  95. // KEY: I am telling my session to go find the placeholder named "input_placeholder_x" and user my input TENSOR instead
  96. runner.AddInput(graph["input_placeholder_x"][0], input);
  97.  
  98. // set up output tensor
  99. runner.Fetch(graph["output_node"][0]);
  100.  
  101. // run model - recurrentTensor now holds the probabilities for each outcome
  102. recurrentTensor = runner.Run()[0].GetValue() as float[,];
  103.  
  104. // frees up resources - very important if you are running graph > 400 or so times
  105. session.Dispose();
  106. graph.Dispose();
  107. }
  108.  
  109. // get the option with the highest probability
  110. float highVal = 0;
  111. int highInd = -1;
  112. sum = 0f;
  113.  
  114. for (int j = 0; j < activityIndexChoices; j++) {
  115. confidence = recurrentTensor [0, j];
  116. if (highInd > -1) {
  117. if (recurrentTensor [0, j] > highVal) {
  118. highVal = confidence;
  119. highInd = j;
  120. }
  121. } else {
  122. highVal = confidence;
  123. highInd = j;
  124. }
  125.  
  126. // debugging - sum should = 1 at the end
  127. sum += confidence;
  128. }
  129.  
  130. // save the index of the highest value for later use!
  131. index = highInd;
  132. }
Add Comment
Please, Sign In to add comment