首页 > 解决方案 > 将 MLMultiArray (swift) 隐蔽到 C# (Xamarin)

问题描述

我需要将 MLMultiArray 转换为 C# 数组,以便实现以下目标

//TRYING TO ACHIEVE
heatmaps is a MLMultiArray with a shape of (1,19,19,32,32)
var keypointCount = (int)heatmaps.Shape[2] - 1;
var heatmapWidth = (int)heatmaps.Shape[3];
var heatmapHeight = (int)heatmaps.Shape[4];

for (int i = 0; i < keypointCount; i++)
{
    int positionX = 0;
    int positionY = 0;
    
    float confidence = 0.0f;
    
    for (int x = 0; x < heatmapWidth; x++)
    {
        for (int y = 0; y < heatmapHeight; y++)
        {
            int index = y + heatmapHeight * (x + heatmapWidth * i);
            
            if (heatmaps[index].FloatValue > confidence)
            {
                confidence = heatmaps[index].FloatValue;
                
                positionX = x;
                positionY = y;
            }
        }
    }
}
//POSSIBLE SOLUTION 
//This code below is something I found using a Float[] instead of MLMultiArray however I need it in C sharp

var a: [Float] = [ 1, 2, 3 ]
var m = try! MLMultiArray(a)

if let b = try? UnsafeBufferPointer<Float>(m) {
  let c = Array(b)
  print(c)
}



这是我目前正在尝试做的,mlmultiarray 的形状为 5,但有超过 10,000 个值,我如何用 float[] 反映这个?它是否必须是 float[5] ,但随后出现迭代错误,所以我使用 float[mlmultiarray.count] 但这似乎也不起作用,因为我认为它是一维数组,所以结果不是对

毫升多阵列

我想做的事

标签: c#iosxamarinxamarin.ios

解决方案


我没有MLMultiArray要测试的真实数据,但是这样的东西应该可以

// length of outer array
var length = heatmap.Shape.Length;

// linear counter
var linear = 0;

for (var outer = 0; outer < length; outer++)
{
  for (var inner = 0; inner < heatmap.Shape[outer]; inner++)
  {
     // this is the element at heatmap[outer,inner]
     var item = heatmap.Item[linear];

     // here add it to C# array
     
     // increment linear counter
     linear++;
  }
}

推荐阅读