TensorFlowLite Error : ‘Interpreter.GetOutputTensor(int)’ is inaccessible due to its protection level

37 Views Asked by At

I have a tflite model that takes image as a input and predict its class. I want it to use in my unity Project. When I used the code given by chatgpt, following error occurs.can anyone help, i dont know much about unity and c#

Assets\Samples\Detection\Scripts\PythonBridge.cs(72,9): error CS0246: The type or namespace name ‘Tensor’ could not be found (are you missing a using directive or an assembly reference?)

Assets\Samples\Detection\Scripts\PythonBridge.cs(72,43): error CS0122: ‘Interpreter.GetOutputTensor(int)’ is inaccessible due to its protection level

using UnityEngine;
using TensorFlowLite;
using System.IO;
using System.Collections.Generic;

public class ObjectDetection : MonoBehaviour
{
    [SerializeField]
    [FilePopup("*.tflite")]
    public string modelPath = "model.tflite";


    [SerializeField]
    private TextAsset labelFile;

    [SerializeField]
    private Texture2D inputImage;

    private Interpreter interpreter;
    private List<string> labels;

    private const int IMAGE_SIZE = 224;
    private const int CHANNELS = 3;

    private void Start()
    {
        LoadModel();
        LoadLabels();
        PreprocessImage();
        RunInference();
    }

    private void LoadModel()
    {
        interpreter = new Interpreter(File.ReadAllBytes(modelPath));
    }

    private void LoadLabels()
    {
        labels = new List<string>();
        using (StringReader reader = new StringReader(labelFile.text))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                labels.Add(line.Trim());
            }
        }
    }

    private void PreprocessImage()
    {
        Texture2D resizedImage = ResizeImage(inputImage, IMAGE_SIZE, IMAGE_SIZE);
        Color32[] pixels = resizedImage.GetPixels32();

        float[] imgArray = new float[IMAGE_SIZE * IMAGE_SIZE * CHANNELS];
        for (int i = 0; i < pixels.Length; i++)
        {
            imgArray[i * 3] = pixels[i].r / 255.0f;
            imgArray[i * 3 + 1] = pixels[i].g / 255.0f;
            imgArray[i * 3 + 2] = pixels[i].b / 255.0f;
        }

        interpreter.SetInputTensorData(0, imgArray);
    }

    private void RunInference()
    {
        interpreter.Invoke();

        // Retrieve output and process predictions
        Tensor outputTensor = interpreter.GetOutputTensor(0);
        float[] results = outputTensor.Data<float>();

        // Find class with highest probability
        int maxIndex = 0;
        float maxProbability = 0f;
        for (int i = 0; i < results.Length; i++)
        {
            if (results[i] > maxProbability)
            {
                maxProbability = results[i];
                maxIndex = i;
            }
        }

        string predictedLabel = labels[maxIndex];
        Debug.Log("Predicted object: " + predictedLabel);
    }

    private Texture2D ResizeImage(Texture2D source, int width, int height)
    {
        RenderTexture rt = RenderTexture.GetTemporary(width, height, 24);
        RenderTexture.active = rt;
        Graphics.Blit(source, rt);
        Texture2D result = new Texture2D(width, height);
        result.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        result.Apply();
        RenderTexture.active = null;
        RenderTexture.ReleaseTemporary(rt);
        return result;
    }
}

I have tried to solve in chatgpt but it is not updated. I have use .h5 and python in c# and i got the output. but it is not working when exported as apk. So when searched, I saw that tensorflowlite will solve the issue

1

There are 1 best solutions below

0
Shaun Wilson On

The error The type or namespace name ‘Tensor’ could not be found (are you missing a using directive or an assembly reference?) means that you are missing a nuget package or assembly reference.

Unfortunately I cannot determine from your question alone which nuget package is missing, but you might try adding a nuget package reference to:

https://www.nuget.org/packages/TensorFlowLite.iOS

However, I can only find iOS versions of this library on nuget.org and you may want to avoid this package unless you know for certain you will only need to support iOS (seems unusual.) This package also does not list any dependencies which I find suspect, but, I'm not familiar enough with it to know if that is true or not. If it has dependencies and they are not listed you will have the struggle of figuring out which dependencies need to be added yourself.

Not sure if Tensorflow.NET nuget package would be usable from Unity, or compatible with the model you are trying to use, I lack experience with these packages to know.

The error ‘Interpreter.GetOutputTensor(int)’ is inaccessible due to its protection level may be a symptom of incorrect or missing packages being used. When I look at the current reference docs for tensorflow lite this particular method is decorated with a public accessor, and should not be giving this error.