问题描述
我需要将 MLMultiArray 转换为 C# 数组,以便我可以实现以下内容
//TRYING TO ACHIEVE
heatmaps is a MLMultiArray with a shape of (1,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] 但这似乎也不起作用,因为我认为它是一个一维数组,所以结果不是'对了
解决方法
我没有要测试的真实 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++;
}
}