Halcon深度学习常用算子及参数解释,完整流程(一)

前言

HALCON 提供了深度学习网络算子供开发人员使用,但是网上资料很多,对于一些基础性概念和解释以及流程并没有很清楚,本专栏着重讲述halcon深度学习的基本算子和概念,基础概念和算子讲完以后附上完整的halcon代码,在专栏的最后提供了一个完整的halcon深度学习项目供大家下载学习。

Halcon深度学习的应用场景分类

  1. 分类:对图像进行分类,并没有划分目标框
  2. 目标检测:图片中的物体提取并分类
  3. 语义分割:为图像的每个像素分配一个类

Halcon中模型处理

模型处理:数据及数据的传输方法!
字典:输入时,包含每个图像的字典,字典包括图像,信息,标签!
输出时,网络返回一个 包含结果的字典。
DLDataset:
字典存储有关数据集的一般信息并收集各个示例的字典
samples:
该键获取字典元组作为值
DLSamples:
字典用于网络模型的输入

Halcon中深度学习的一般流程

  1. 准备一个神经网络或创建一个准备数据
    1.1 预先培训的网络中读取或新建一个
    1.2 标签图像
    1.3 预先处理图像(图像尺寸,灰度值范围等…)
    1.4 拆分数据及(训练,验证和测试)

  2. 训练网络并评估进度

  3. 应用及预测!(应用时与预先处理图像一致大小,尺寸等)

Halcon深度学习中的一些术语(持续更新)

锚:
锚点时固定边界框,它们用作参考框

锚点(anchor)的生成:
anchor是将训练集的目标框选出来的框。anchor指的是一类框(一类box)

confidence:置信概率
confusion matrix:混淆矩阵

动量:momentum
momentum范围在0到1之间,用于优化损失函数参数

正则化:weight_prior
防止神经网络过拟合

Halcon做目标检测深度学习的算子

首先,目标检测使用的网络有三部分:
在这里插入图片描述
第一部分:The backbone
使用的是预处理网络,是Halcon原先提供的
第二部分:特征金字塔
第三部分:是自己标定的部分,还包括减少重叠的预测边界框

get_model_dl_param  //返回目标检测网络的参数
set_dl_model_param  //设置目标检测神经网络参数

目标检测流程

一.创建网络(先创建第一个Halcon文件)

  1. 创建模型和数据预处理
    create_dl_model_detection 创建用于目标检测的深度学习网络
create_dict(DLModelDetectionParam)
create_dl_model_detection(Backone,NumClasses,DLModelDetectionParam,DLModelHandle)
//Backone是预处理网络
//NumClasses每幅图最大检测数
//DLModelDetectionParam这里的数据都是字典数据,包括标签图像大小,通道等

这里Halcon提供的预处理网络有3种:

1.1. pretrained_dl_classifier_compact.hdl (适合快速且高效的网络)

此网络可以从get_dl_model_param中读取相关参数,这里的默认参数有
image_width:224
image_height:224
image_num_channels:3
image_range_min:-127
image_range_max:128

1.2. pretrained_dl_classifier_enhanced.hdl (网路更适合复杂的任务,但是要花费更多的时间和内存要求)
1.3. pretrained_dl_classifier_resnet50.hdl (采用ResNet50网络作为预处理网络,网络最复杂,但训练更加稳健,检测结果更好)

这里需要对create_dl_model_detection中的DLModeDetectionParam的元素更改。
像素金字塔:max_level 影响最小输入图像 的大小,默认为6
min_level 至少是2
anchor_num_subscales:锚点比。不同级别的锚点不同大小的数量。默认为3
anchor_aspect_ratios:锚点的纵横比。默认为[1.0 2.0 3.0]
capacity:大致确认对象的可能值。在检测性能和速度之间权衡。
可能性有’medium’,‘high’和’low’

特别的也有已经训练好的网络在halcon中,read_dl_model(一般是分割神经网络),之后再更改网络就是set_dl_model_param

  1. 读取训练集的标签数据(以字典的形式读取数据)
    这里特别注意的是,要使用MVTec提供的标记软件。
    在这里插入图片描述
    具体的标记方法以后详细介绍。其实操作并不复杂,这里并不叙述了。
    生成的数据集是.dict格式,也可以是.json格式
read_dict(数据集,string,string,句柄DictHandle)

从训练集的字典数据中的class_ids去设置网络的class_ids

若是第三方数据集读取,需要使用COCO读取

read_dl_dataset_from_coco(数据集,string,string,DLDataset)

之后再

write_dl_mode(DLmodeHandle,预训练路径)

这里的意思是往.hdl里面加入新的.hdl

  1. 拆分数据集
split_dl_dataset()   //按照百分比拆分数据集为训练集,测试集,验证集等....
  1. 从预训练网络中获取预处理参数
create_dl_preprocess_param_from_model(DLModeHand,....,DLPreprocessParam)
//DLPreprocessParam输出的是进程参数

完整的模型建立流程的Halcon代码是:

dev_update_off ()
Backbone:='pretrained_dl_classifier_compact.hdl'
NumClasses:=1
ImageWidth:=512
ImageHeight:=320
*压缩后的图像通道数
ImageNumChannels:=1
*设置最小值,最大值,锚数值和锚纵横比等超参数
MinLevel:=2
MaxLevel:=4
AnchorNumSubscales:=3
AnchorAspectRatios:=[1.0,0.5,2.0]
Capacity:='medium'
******创建对象检测模型************
*为泛型参数创建字典并创建对象检测模型
create_dict (DLModelDetectionParam)
set_dict_tuple (DLModelDetectionParam, 'image_width', ImageWidth)
set_dict_tuple (DLModelDetectionParam, 'image_height', ImageHeight)
set_dict_tuple (DLModelDetectionParam, 'image_num_channels', ImageNumChannels)
set_dict_tuple (DLModelDetectionParam, 'min_level', MinLevel)
set_dict_tuple (DLModelDetectionParam, 'max_level', MaxLevel)
set_dict_tuple (DLModelDetectionParam, 'anchor_num_subscales', AnchorNumSubscales)
set_dict_tuple (DLModelDetectionParam, 'anchor_aspect_ratios', AnchorAspectRatios)
set_dict_tuple (DLModelDetectionParam, 'capacity', Capacity)
*创建模型
create_dl_model_detection (Backbone, NumClasses, DLModelDetectionParam, DLModelHandle)
*************数据预处理***************
*输出文件夹
ExampleDataDir:='D:/middle report'
DLModeFileName:=ExampleDataDir+'/pretrained_dl_model_detection.hdl'
DataDirectory:=ExampleDataDir+'/output_data_'+ImageWidth+'x'+ImageHeight
PreprocessParamFileName:=DataDirectory+'/dl_preprocess_param_'+ImageWidth+'x'+ImageHeight+'.hdict'
*用于拆分的数据集,训练集70%15%验证集,15%测试集
TrainingPercent:=70
ValidationPercent:=15
*设置随机种子
SeedRand:=42
file_exists (ExampleDataDir, FileExists)
if(not FileExists)
    make_dir (ExampleDataDir)
endif
*读取数据库字典
read_dict ('D:/middle report/jiance.hdict', [], [], DictHandle)
*从模型中的数据集中设置类ID
get_dict_tuple (DictHandle, 'class_ids', ClassIDs)
set_dl_model_param (DLModelHandle, 'class_ids', ClassIDs)
*编写初始化的DL对象检测模型 
write_dl_model (DLModelHandle, DLModeFileName)
set_system ('seed_rand', SeedRand)
split_dl_dataset (DictHandle, TrainingPercent, ValidationPercent, [])
*从模型中获取预处理参数
create_dl_preprocess_param_from_model (DLModelHandle, 'false', 'full_domain', [], [], [], DLPreprocessParam)
create_dict (GenParam)
set_dict_tuple (GenParam, 'overwrite_files', true)
*数据预处理部分
preprocess_dl_dataset (DictHandle, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFileName)
write_dict (DLPreprocessParam, PreprocessParamFileName, [], [])
get_dict_tuple (DictHandle, 'samples', DatasetSamples)
find_dl_samples (DatasetSamples, 'split', 'train', 'match', SampleIndices)
tuple_shuffle (SampleIndices, ShuffledIndices)
read_dl_samples (DictHandle, ShuffledIndices[0:9], DLSampleBatchDisplay)
*设置dev_display_dl_数据的参数
create_dict (WindowHandleDict)
create_dict (GenParam)
set_dict_tuple (GenParam, 'scale_windows', 1.2)
*在DLSampleBatchDisplay中显示示例
for Index := 0 to |DLSampleBatchDisplay| - 1 by 1
   *在DLSampleBatchDisplay中循环采样
    dev_display_dl_data (DLSampleBatchDisplay[Index], [], DictHandle, 'bbox_ground_truth', GenParam, WindowHandleDict)
    get_dict_tuple (WindowHandleDict, 'bbox_ground_truth', WindowHandles)
    * 添加解释性文本
    dev_set_window (WindowHandles[0])
    get_dict_object (Image, DLSampleBatchDisplay[Index], 'image')
    get_image_size (Image, ImageWidth, ImageHeight)
    dev_disp_text ('New image size after preprocessing: ' + ImageWidth + ' x ' + ImageHeight, 'window', 'bottom', 'right', 'black', [], [])
    dev_set_window (WindowHandles[1])
    dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
endfor
dev_display_dl_data_close_windows (WindowHandleDict)
  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Halcon中关于intersection的算子有: 1. IntersectionLineLine:计算两条直线的交点坐标。 2. IntersectionLineSegment:计算一条直线与线段的交点坐标。 3. IntersectionLines:计算多条直线的交点坐标。 4. IntersectionRaySegment:计算一条射线与线段的交点坐标。 5. IntersectionSegments:计算多条线段的交点坐标。 6. IntersectionCircleCircle:计算两个圆的交点坐标。 7. IntersectionLineCircle:计算一条直线与圆的交点坐标。 8. IntersectionRayCircle:计算一条射线与圆的交点坐标。 9. IntersectionSegmentCircle:计算一条线段与圆的交点坐标。 10. IntersectionCircles:计算多个圆的交点坐标。 每个算子的详细解释如下: 1. IntersectionLineLine:计算两条直线的交点坐标。 输入参数:Line1StartRow,Line1StartCol,Line1EndRow,Line1EndCol,Line2StartRow,Line2StartCol,Line2EndRow,Line2EndCol。 输出参数:IntersectionRow,IntersectionCol。 解释:计算两条直线的交点坐标。 2. IntersectionLineSegment:计算一条直线与线段的交点坐标。 输入参数:LineStartRow,LineStartCol,LineEndRow,LineEndCol,SegmentStartRow,SegmentStartCol,SegmentEndRow,SegmentEndCol。 输出参数:IntersectionRow,IntersectionCol。 解释:计算一条直线与线段的交点坐标。 3. IntersectionLines:计算多条直线的交点坐标。 输入参数:LineStartRow,LineStartCol,LineEndRow,LineEndCol。 输出参数:IntersectionRow,IntersectionCol。 解释:计算多条直线的交点坐标。 4. IntersectionRaySegment:计算一条射线与线段的交点坐标。 输入参数:RayStartRow,RayStartCol,RayEndRow,RayEndCol,SegmentStartRow,SegmentStartCol,SegmentEndRow,SegmentEndCol。 输出参数:IntersectionRow,IntersectionCol。 解释:计算一条射线与线段的交点坐标。 5. IntersectionSegments:计算多条线段的交点坐标。 输入参数:SegmentStartRow,SegmentStartCol,SegmentEndRow,SegmentEndCol。 输出参数:IntersectionRow,IntersectionCol。 解释:计算多条线段的交点坐标。 6. IntersectionCircleCircle:计算两个圆的交点坐标。 输入参数:Circle1Row,Circle1Col,Circle1Radius,Circle2Row,Circle2Col,Circle2Radius。 输出参数:Intersection1Row,Intersection1Col,Intersection2Row,Intersection2Col。 解释:计算两个圆的交点坐标。 7. IntersectionLineCircle:计算一条直线与圆的交点坐标。 输入参数:LineStartRow,LineStartCol,LineEndRow,LineEndCol,CircleRow,CircleCol,CircleRadius。 输出参数:Intersection1Row,Intersection1Col,Intersection2Row,Intersection2Col。 解释:计算一条直线与圆的交点坐标。 8. IntersectionRayCircle:计算一条射线与圆的交点坐标。 输入参数:RayStartRow,RayStartCol,RayEndRow,RayEndCol,CircleRow,CircleCol,CircleRadius。 输出参数:Intersection1Row,Intersection1Col,Intersection2Row,Intersection2Col。 解释:计算一条射线与圆的交点坐标。 9. IntersectionSegmentCircle:计算一条线段与圆的交点坐标。 输入参数:SegmentStartRow,SegmentStartCol,SegmentEndRow,SegmentEndCol,CircleRow,CircleCol,CircleRadius。 输出参数:Intersection1Row,Intersection1Col,Intersection2Row,Intersection2Col。 解释:计算一条线段与圆的交点坐标。 10. IntersectionCircles:计算多个圆的交点坐标。 输入参数:CircleRow,CircleCol,CircleRadius。 输出参数:Intersection1Row,Intersection1Col,Intersection2Row,Intersection2Col。 解释:计算多个圆的交点坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手写不期而遇

感谢你的打赏,也欢迎一起学习

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值