Search Unity

Question How to acces tensor data with IWorker Interface

Discussion in 'Barracuda' started by WoWPerroDev, Mar 21, 2021.

  1. WoWPerroDev

    WoWPerroDev

    Joined:
    Jan 26, 2019
    Posts:
    5
    Hello, I´m trying to understand how to use the IWorker interface, I can actually run my .onnx model in unity, but when I want to get the result from my output tensor, it returns the shape of the tensor, anyone knows how to get the value? I think I´m not understanding the documentation, bc I thought worker.PeekOutput(string name) was actually going to give me my result, but I keep getting my tensor shape, anyone knows why this is happening? this is my first time using .onnx models in unity, srry if my question is really novice, I can provide my .onnx trained models for debbuging, thx in advance

    Code (CSharp):
    1. public Texture2D texture;
    2. public NNModel modelAsset;
    3. private Model runtimeModel;
    4. private IWorker worker;
    5.  
    6. void ParagraphsVsDrawings()
    7.     {
    8.         runtimeModel = ModelLoader.Load(modelAsset);
    9.         worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, runtimeModel);
    10.  
    11.         var channelCount = 1; // you can treat input pixels as 1 (grayscale), 3 (color) or 4 (color with alpha) channels
    12.         Tensor input = new Tensor(texture, channelCount);
    13.         worker.Execute(input);
    14.         Tensor Output = worker.PeekOutput("dense_4");
    15.         Output.Print();
    16.         Output.Dispose();
    17.     }
    I call my function from pressing a ui button
     
  2. alexandreribard_unity

    alexandreribard_unity

    Unity Technologies

    Joined:
    Sep 18, 2019
    Posts:
    53
    Hey! No worries at all.
    Your code is correct actually.
    You are missing the flowing
    Code (CSharp):
    1.  
    2. float[] outputBuffer = Output.ToReadOnlyArray();
    3. // or slimply access
    4. float a0 = Output[0]; float a1 = Output[1];
    5.  
    :
    You see the Tensor class is a wrapper on the data. It abstracts the fact that the data can be on the GPU.
    So to access the data we need to potentially download it.
    So either call ToReadOnlyArray() which copies the data to a buffer.
    Or simply access the Tensor with exposed accessors.
    You can read more about this here:
    https://github.com/Unity-Technologi...elease/1.0.0/Documentation~/TensorHandling.md