c# - 将 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] 但这似乎也不起作用,因为我认为它是一维数组,所以结果不是对
解决方案
我没有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++;
}
}
推荐阅读
- kubernetes - 使用 helm 设置 Kubernetes kerberos 和 kafka 秘密
- google-sheets - 如何在谷歌表格中使用自定义创建的字体
- javascript - 我在从 JS 上的 API 下载 xlsx 文件时遇到问题
- objective-c - 如何在普通 Textfield 自定义类中添加占位符颜色
- node.js - 删除 html 标签 - JSDOM
- javascript - JAVASCRIPT - 在数组展平后从子对象内部的父对象数组中获取值
- javascript - 使用 Jquery 将 YouTube 链接从手表更改为动态嵌入
- javascript - 如何在 txt 文件中加载 html 内容以进行电子邮件确认,有什么建议吗?
- django - 如何第二次运行 django 服务器我已经成功安装了 django
- css - CSS样式在mac的Safari浏览器中不起作用