TensorFlowLite 错误:“Interpreter.GetOutputTensor(int)”由于其保护级别而无法访问

问题描述 投票:0回答:1

我有一个 tflite 模型,它以图像作为输入并预测其类别。我希望它在我的统一项目中使用。当我使用chatgpt给出的代码时,出现以下错误。任何人都可以帮忙吗,我对unity和c#不太了解

Assets\Samples\Detection\Scripts\PythonBridge.cs(72,9):错误 CS0246:找不到类型或命名空间名称“Tensor”(您是否缺少 using 指令或程序集引用?)

Assets\Samples\Detection\Scripts\PythonBridge.cs(72,43):错误 CS0122:“Interpreter.GetOutputTensor(int)”由于其保护级别而无法访问

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;
    }
}

我尝试在chatgpt中解决,但没有更新。我在 c# 中使用了 .h5 和 python,并得到了输出。但导出为 apk 时不起作用。所以在搜索时,我看到tensorflowlite可以解决这个问题

c# tensorflow unity-game-engine machine-learning tensorflow-lite
1个回答
0
投票

ChatGPT 不能替代编程技能。如果您不熟悉某种编程语言,那么要么不要使用它,要么学习它。

因为我不确定如何在不引用任何 C# 内容的情况下描述问题的修复方法,因为从错误的内容来看,您的代码遇到的问题似乎非常简单(缺少插件或类似的问题)

也不知道在 C# 中使用 .h5 和 python 是什么意思。听起来你在做一些巫术,但并没有真正理解你在做什么。

如果 GetOutputTensor 不可访问,您可以使其更易于访问,或者,如果它来自您无法更改的插件,请找到没有该功能的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.