asus4 / tf-lite-unity-sample

TensorFlow Lite Samples on Unity
834 stars 249 forks source link

a problem with Interpreter #281

Closed luanle-padua closed 1 year ago

luanle-padua commented 1 year ago

Environment (please complete the following information):

Describe the bug

Hi, My name is Luan, and I am very new to Unity. I am trying to make predictions by a deep-learning model in Unity. I train a model in GG colab using TF. I follow this tutorial on Youtube:

https://www.youtube.com/watch?v=s42RTgKSXis&list=PLlPnLTVCBE5VX_-1yze2ustaJwkAGcZo1&index=36&t=547s&ab_channel=Alkr

The code for prediction is as below:

`using System.Collections; using System.Collections.Generic; using UnityEngine; using TensorFlowLite; using UnityEngine.UI;

public class predict : MonoBehaviour { [SerializeField] string fileName = "model.tflite"; [SerializeField] Text outputTextWiew = null; [SerializeField] Button button = null; [SerializeField] InputField inputField = null;

Interpreter interpreter;

// Start is called before the first frame update
void Start()
{
    var options = new InterpreterOptions()
    {
        threads = 2,
    };

    interpreter = new Interpreter(FileUtil.LoadFile(fileName), options);

    interpreter.AllocateTensors();

    button.onClick.AddListener(OnbuttonClick);

}

void OnDestroy()
{
    interpreter?.Dispose();
}

public void OnbuttonClick()
{
    // doInference(inputField.text);
    Debug.Log(inputField.text);
}

// public void doInference(string val)
// {
//     var input = new float[1]; ////// dua input vao day
//     input[0] = float.Parse(val);
//     var output = new float[1];

//     interpreter.SetInputTensorData(0, input);
//     interpreter.Invoke();
//     interpreter.GetOutputTensorData(0, output);
//     for (int i = 0; i < output.Length; i++)
//     {
//         Debug.Log(output[i]);
//         outputTextWiew.text = output[i].ToString();

//     }
// }

// Update is called once per frame
// void Update()
// {

// }

} `

when I play in Unity, it comes with this error. Could you please help me to solve this problem?

NullReferenceException: Object reference not set to an instance of an object TensorFlowLite.Interpreter..ctor (System.Byte[] modelData, TensorFlowLite.InterpreterOptions options) (at Library/PackageCache/com.github.asus4.tflite@2.10.0-p1/Runtime/Interpreter.cs:66) predict.Start () (at Assets/predict.cs:26)

Ale1 commented 1 year ago

Hi Luan. I am not the developer, but I have seen this error show up before when I move the location of the model file and forget to update the path in the options. Check that your line FileUtil.LoadFile(filename) is pointing to the correct place.

luanle-padua commented 1 year ago

Thank you, I will try.

Ale1 commented 1 year ago

@luanle-padua . is your issue fixed?

luanle-padua commented 1 year ago

hi Ale1,

Thank you so much. I could not fix it properly, then I changed my workflow to ONNX. And it is okay with me now.

Best regards Thanh Luan

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.