@takureepers

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[Unity6 × Sentis 2.1.2] ToReadOnlyArray() / DownloadToArray() 使用時に発生するエラーについて

解決したいこと

Unity6でSentis 2.1.2を利用して、感情を認識して遊べるゲームを開発しています。
開発の過程で、ToReadOnlyArray()メソッドを使ったコードを実装したところ、コンパイルエラーが発生しました。

エラーを解消するために、インターネットの記事を参考にしてDownloadToArray()DownloadToNativeArray()に書き換えてみましたが、それでも同じようなエラーが発生してしまいました。

この問題について、Google検索やChatGPTなどの生成AIにも質問しましたが、具体的な解決策を見つけることができませんでした。
そのため、こちらに相談させていただきました。

環境

  • MacBook Pro 2020 13"
  • macOS Sequoia 15
  • Unity 6
  • Unity Sentis 2.1.3

発生している問題・エラー

Assets/Script/InitCam.cs(110,18): error CS1061: 'Tensor' does not contain a definition for 'DownloadToArray' and no accessible extension method 'DownloadToArray' accepting a first argument of type 'Tensor' could be found (are you missing a using directive or an assembly reference?)
Assets/Script/InitCam.cs(112,39): error CS1061: 'Tensor' does not contain a definition for 'DownloadToArray' and no accessible extension method 'DownloadToArray' accepting a first argument of type 'Tensor' could be found (are you missing a using directive or an assembly reference?)

該当するソースコード

    void Update()
    {
        if (!isCameraInit)
            return;

        Graphics.Blit(webcamTexture, preRenderTexture);
        RenderTexture.active = preRenderTexture;
        preRenderTexture2D.ReadPixels(new Rect(0, 0, preRenderTexture.width, preRenderTexture.height), 0, 0);
        preRenderTexture2D.Apply();
        RenderTexture.active = null;

        var inputTensor = TextureConverter.ToTensor(preRenderTexture2D);

        worker.Schedule(inputTensor);
        Tensor outputTensor = worker.PeekOutput() as Tensor<float>;

        if (outputTensor != null)
        {
            int maxIndex = outputTensor
                .DownloadToNativeArray() // ここ(110行目)
                .ToList()
                .IndexOf(outputTensor.DownloadToArray().Max()); // ここ(112行目)
            predictedEmotion = emotionLabels[maxIndex];
            Debug.Log($"Predicted Emotion: {predictedEmotion}");
        }

        outputTensor?.Dispose();
    }
0 likes

2Answer

Unityのリファレンスを参照してみると、TensorクラスにDownloadToArray()DownloadToNativeArray()というメソッドは見当たらないので、ご提示のエラーは当然かと思います。
で、ToReadOnlyArray()はあるので使えるはずですが、それをどのように使ったコードで、どのようなエラーが出たのかを見せていただけますか?
Class Tensor | Barracuda | 3.0.0

1Like

Comments

  1. @takureepers

    Questioner

    ToReadOnlyArray()あたりはエラーが出てませんが、

    Assets/Script/InitCam.cs(157,48): error CS1061: 'Tensor' does not contain a definition for 'DownloadToArray' and no accessible extension method 'DownloadToArray' accepting a first argument of type 'Tensor' could be found (are you missing a using directive or an assembly reference?)
    

    が出っぱなしですね...
    また、リファレンスにあったメソッドは違うものなのでしょうか?

  2. var outputTensor = worker.PeekOutput();
    

    ここが原因ですね
    varが型推論でTensor型を選択しているので、このクラスに存在しないメソッドを呼び出しています

    Tensorクラス

    DownloadToArrayは子クラスであるTensor<T>のメソッドなので、使用にはダウンキャストが必要です
    以前指定されていた型はTensor<float>なので、それを例に取ると以下のようになります

    float[] outputArray = ((Tensor<float>)outputArray).DownloadToArray();
    

    TensorはUnity内に最低二件存在します

    Unity.Barracuda.Tensor
    Unity.Sentis.Tensor

    この内、ToReadOnlyArrayUnity.Barracuda.Tensorに存在します
    GitHub側のソースコードでも、該当の名前空間の宣言がありますね

    using System.Barracuda
    

    ですがSystem.Barracuda.Tensor<T>は存在しないので、こちらの名前空間ではDownloadToArrayを呼べません
    よってUnity.Sentisを使用します

    varは基底クラスに準拠して当たりをつけるので、宣言したい型によっては継承関係と名前の競合に注意する必要があります

Tensor outputTensor = worker.PeekOutput() as Tensor<float>;

ここでnullが返っているはずです
型を明示しましょう

Tensor<float> outputTensor = worker.PeekOutput() as Tensor<float>;
1Like

Comments

  1. @takureepers

    Questioner

    ありがとうございます!これにしてみたらそこの部分のエラーは解消しました!

Your answer might help someone💌