设为首页 加入收藏

TOP

机器学习框架ML.NET学习笔记【8】目标检测(采用YOLO2模型)(二)
2019-09-17 17:49:01 】 浏览:33
Tags:机器 学习 框架 ML.NET 笔记 目标 检测 采用 YOLO2 模型
nfo testdir
= new DirectoryInfo(testimagesFolder); foreach (var jpgfile in testdir.GetFiles("*.jpg")) { ImageNetData image = new ImageNetData { ImagePath = jpgfile.FullName };
var Predicted = predictionEngine.Predict(image); PredictImage(image.ImagePath, Predicted); } }
代码遍历一个文件夹下面的JPG文件。对每一个文件进行转换,获得预测结果。
ImageNetPrediction类定义如下:
    public class ImageNetPrediction
    {
        [ColumnName(TinyYoloModelSettings.ModelOutput)]
        public float[] PredictedLabels;       
    }

 输出的“grid”列数据是一个float数组,不能直接理解其含义,所以需要通过代码将其数据转换为便于理解的格式。

     YoloWinMlParser _parser = new YoloWinMlParser();
     IList<YoloBoundingBox> boundingBoxes = _parser.ParseOutputs(Predicted.PredictedLabels, 0.4f);            

YoloWinMlParser.ParseOutputs方法将float数组转为YoloBoundingBox对象的列表,第二个参数是可信度阙值,只输出大于该可信度的数据。

YoloBoundingBox类定义如下:

    class YoloBoundingBox
    {    
        public string Label { get; set; }
        public float Confidence { get; set; }

        public float X { get; set; }
        public float Y { get; set; }
        public float Height { get; set; }
        public float Width { get; set; }
        public RectangleF Rect
        {
            get { return new RectangleF(X, Y, Width, Height); }
        }
    }

 其中:Label为目标类型,Confidence为可行程度。

由于YOLO的特点导致对同一个目标会输出多个同样的检测结果,所以还需要对检测结果进行过滤,去掉那些高度重合的结果。

     YoloWinMlParser _parser = new YoloWinMlParser();
     IList<YoloBoundingBox> boundingBoxes = _parser.ParseOutputs(Predicted.PredictedLabels, 0.4f); 
     var filteredBoxes = _parser.NonMaxSuppress(boundingBoxes, 5, 0.6F);

 YoloWinMlParser.NonMaxSuppress第二个参数表示最多保留多少个结果,第三个参数表示重合率阙值,将去掉重合率大于该值的记录。

 

四、资源获取 

源码下载地址:https://github.com/seabluescn/Study_ML.NET

工程名称:YOLO_ObjectDetection

资源获取:https://gitee.com/seabluescn/ML_Assets (ObjectDetection)

点击查看机器学习框架ML.NET学习笔记系列文章目录

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ocelot(四)- 认证与授权 下一篇C# Queue与RabbitMQ的爱恨情仇(..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目