• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7   http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 using System;
16 using System.Collections;
17 using System.Collections.Generic;
18 using System.Linq;
19 using TensorFlowLite;
20 using UnityEngine;
21 using UnityEngine.UI;
22 
23 /// <summary>
24 /// Simple example demonstrating use of the experimental C# bindings for TensorFlowLite.
25 /// </summary>
26 public class HelloTFLite : MonoBehaviour {
27 
28   [Tooltip("Configurable TFLite model.")]
29   public TextAsset model;
30 
31   [Tooltip("Configurable TFLite input tensor data.")]
32   public float[] inputs;
33 
34   [Tooltip("Target Text widget for display of inference execution.")]
35   public Text inferenceText;
36 
37   private Interpreter interpreter;
38   private float[] outputs;
39 
Awake()40   void Awake() {
41     // As the demo is extremely simple, there's no need to run at full frame-rate.
42     QualitySettings.vSyncCount = 0;
43     Application.targetFrameRate = 5;
44   }
45 
Start()46   void Start () {
47     interpreter = new Interpreter(model.bytes);
48     Debug.LogFormat(
49         "InputCount: {0}, OutputCount: {1}",
50         interpreter.GetInputTensorCount(),
51         interpreter.GetOutputTensorCount());
52   }
53 
Update()54   void Update () {
55     if (inputs == null) {
56       return;
57     }
58 
59     if (outputs == null || outputs.Length != inputs.Length) {
60       interpreter.ResizeInputTensor(0, new int[]{inputs.Length});
61       interpreter.AllocateTensors();
62       outputs = new float[inputs.Length];
63     }
64 
65     float startTimeSeconds = Time.realtimeSinceStartup;
66     interpreter.SetInputTensorData(0, inputs);
67     interpreter.Invoke();
68     interpreter.GetOutputTensorData(0, outputs);
69     float inferenceTimeSeconds = Time.realtimeSinceStartup - startTimeSeconds;
70 
71     inferenceText.text = string.Format(
72         "Inference took {0:0.0000} ms\nInput(s): {1}\nOutput(s): {2}",
73         inferenceTimeSeconds * 1000.0,
74         ArrayToString(inputs),
75         ArrayToString(outputs));
76   }
77 
OnDestroy()78   void OnDestroy() {
79     interpreter.Dispose();
80   }
81 
ArrayToString(float[] values)82    private static string ArrayToString(float[] values) {
83     return string.Join(",", values.Select(x => x.ToString()).ToArray());
84   }
85 }
86