C# 使用 ONNX 格式的模型,完成 AI 识别(机器学习笔记)
模型查看
模型输入(图片转张量)
/// <summary>
/// 计算张量(RRRR GGGG BBBB)
/// </summary>
/// <param name="source">图像</param>
/// <returns></returns>
public static Tensor<float> FastToOnnxTensor(SKBitmap source)
{
var tensorShape = new int[] { 1, 3, source.Height, source.Width };
var tensor = new DenseTensor<float>(tensorShape);
for (int y = 0; y < source.Height; y++)
{
for (int x = 0; x < source.Width; x++)
{
var pixel = source.GetPixel(x, y);
tensor[0, 0, y, x] = pixel.Red / 255f; // R
tensor[0, 1, y, x] = pixel.Green / 255f; // G
tensor[0, 2, y, x] = pixel.Blue / 255f; // B
}
}
return tensor;
}
模型输出(张量计算坐标)
/// <summary>
/// 解析结果
/// </summary>
/// <param name="results">计算后的张量</param>
/// <returns></returns>
private List<Prediction> ParseResults(float[] results)
{
//边界框长度
int confidenceIndex = 4;
//置信度长度
int labelStartIndex = 5;
//标签数量
int labelLength = _YoloLabes.Length;
//每个分区长度
int dimensions = labelLength + 5;
//分区数量
int rows = results.Length / dimensions;
//
var detections = new List<Prediction>();
for (int i = 0; i < rows; ++i)
{
var index = i * dimensions;
//不要置信度低的
if (results[index + confidenceIndex] <= 0.4f) continue;
//对每个预测类别的置信度进行缩放
for (int j = labelStartIndex; j < dimensions; ++j)
{
results[index + j] = results[index + j] * results[index + confidenceIndex];
}
//分析每个预测类别
for (int k = labelStartIndex; k < dimensions; ++k)
{
//不要过低的预测类别
if (results[index + k] <= 0.5f) continue;
var value_0 = results[index];
var value_1 = results[index + 1];
var value_2 = results[index + 2];
var value_3 = results[index + 3];
var bbox = new BBox(
(value_0 - value_2 / 2) / _YoloWidth,
(value_1 - value_3 / 2) / _YoloWidth,
(value_0 + value_2 / 2) / _YoloHeight,
(value_1 + value_3 / 2) / _YoloHeight);
var l_index = k - labelStartIndex;
detections.Add(new Prediction()
{
Box = bbox,
Confidence = results[index + k],
LabelIndex = l_index,
LabelName = _YoloLabes[l_index]
});
}
}
return Prediction.NMS(detections);
}
示例项目
- 源码:AiYoloV5Onnx.7z
内容已隐藏,需要评论并且审核通过后,才能阅读隐藏内容
版权属于:zgcwkj
本文链接:https://blog.zgcwkj.cn/archives/213.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭