Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.XR;
- using System.IO;
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- using System.Threading;
- using TensorFlow;
- public class AccelerometerInputCNN : MonoBehaviour
- {
- // set by trained CNN model
- public int inputWidth = 90;
- // third value corresponds to inputWidth
- public TextAsset graphModel;
- // the dimenstions of 'inputTensor' correspond to the dimensions of the input for my trained graph
- private float[,,,] inputTensor = new float[1, 1, 90, 1];
- // determine if person is walking from cnn returned value
- private int standIndex = 0;
- // how many options of activities we have - standing, walking, jogging
- private int activityIndexChoices = 2;
- // list to collect acceleration values
- private List<float> accelL;
- float confidence = 0;
- float sum = 0f;
- float test = 0f;
- int activity = 0;
- int index = 0;
- void Start ()
- {
- #if UNITY_ANDROID
- TensorFlowSharp.Android.NativeBinding.Init ();
- #endif
- // initialize the oculus go display
- display = new OVRDisplay ();
- accelL = new List<float> ();
- }
- void FixedUpdate ()
- {
- collectValues();
- }
- void collectValues()
- {
- float curr = display.acceleration.y;
- if (accelL.Count < inputWidth) {
- accelL.Add (curr);
- }
- if (accelL.Count == inputWidth) {
- index = evaluate ();
- accelL.RemoveAt (0);
- accelL.Add (curr);
- }
- }
- void evaluate ()
- {
- // convert from list to array
- for (int i = 0; i < accelL.Count; i++) {
- inputTensor [0, 0, i, 0] = accelL [i];
- test = inputTensor [0, 0, i, 0];
- }
- // declare a list for outputs
- float[,] recurrentTensor;
- // set up and run a graph session
- using (var graph = new TFGraph())
- {
- graph.Import(graphModel.bytes);
- var session = new TFSession(graph);
- var runner = session.GetRunner();
- // implicitally convert a C# array to a tensor
- TFTensor input = inputTensor;
- // set up input tensor and input
- // KEY: I am telling my session to go find the placeholder named "input_placeholder_x" and user my input TENSOR instead
- runner.AddInput(graph["input_placeholder_x"][0], input);
- // set up output tensor
- runner.Fetch(graph["output_node"][0]);
- // run model - recurrentTensor now holds the probabilities for each outcome
- recurrentTensor = runner.Run()[0].GetValue() as float[,];
- // frees up resources - very important if you are running graph > 400 or so times
- session.Dispose();
- graph.Dispose();
- }
- // get the option with the highest probability
- float highVal = 0;
- int highInd = -1;
- sum = 0f;
- for (int j = 0; j < activityIndexChoices; j++) {
- confidence = recurrentTensor [0, j];
- if (highInd > -1) {
- if (recurrentTensor [0, j] > highVal) {
- highVal = confidence;
- highInd = j;
- }
- } else {
- highVal = confidence;
- highInd = j;
- }
- // debugging - sum should = 1 at the end
- sum += confidence;
- }
- // save the index of the highest value for later use!
- index = highInd;
- }
Add Comment
Please, Sign In to add comment