halcon系列(1):高斯混合模型的一套算子
一、摘要
高斯混合模型,是EM算法之一种;该算法理论部分在另外的文章专门探讨,本篇专门对halcon的高斯分类的算子进行介绍,和基本使用方法。halcon的高斯模型其实很复杂,也很管用。希望多付出一些时间才能完全消化。
二、高斯混合模型Gaussian-Mixture-Models
2.1 产生高斯模型
create_class_gmm — Create a Gaussian Mixture Model for classification
create_class_gmm( : : NumDim, NumClasses, NumCenters, CovarType, Preprocessing, NumComponents, RandSeed : GMMHandle)
在特征向量为整数类型的特殊情况下,它们位于步长为 1.0 的网格中的特征空间中。例如,通常用于颜色分类的 RGB 特征向量是三元组,它们的每个分量具有 0 到 255 之间的整数值。事实上,甚至可能有几个特征向量代表同一个点。当使用此类数据训练 GMM 时,训练算法可能倾向于将建模的高斯分布沿与网格维度平行的线性相关数据线或平面对齐。如果 train_class_gmm 返回的中心数异常高,则表明算法存在这种行为。参数 Randomize 可用于处理此类不希望的效果。如果 Randomize > 0.0,则将均值为 0 和标准差 Randomize 的随机高斯噪声添加到训练数据向量的每个分量中,并将转换后的训练数据存储在 GMM 中。对于 Randomize 1.0 的值,随机数据将看起来像网格点周围的小云,这不会改善数据云的属性。对于 Randomize >> 2.0 的值,随机化可能会对生成的 GMM 产生过大的影响。对于整数特征向量,建议使用介于 1.5 和 2.0 之间的 Randomize 值,它将整数数据转换为均匀的云,而无需修改其在特征空间中的一般形式。如果数据是通过缩放从整数数据创建的,则可能会出现同样的问题。在这里,必须使用用于缩放原始数据的相同比例因子来缩放 Randomize。
在使用 train_class_gmm 训练 GMM 之前,必须使用 add_sample_class_gmm 将所有训练样本添加到 GMM。
当前存储的训练样本数可以通过get_sample_num_class_gmm查询。可以使用 get_sample_class_gmm 再次读取存储的训练样本。
通常,将训练样本保存在带有 write_samples_class_gmm 的文件中以方便重复使用样本是很有用的,并且如果需要,可以将新的训练样本添加到数据集中,从而促进新创建的 GMM 可以使用扩展数据集重新训练。
create_class_gmm 创建用于分类的高斯混合模型 (GMM)。 NumDim 指定特征空间的维数,NumClasses 指定类数。 GMM 由每个类的 NumCenters 高斯中心组成。 NumCenters 不仅可以是要使用的确切中心数,而且可以根据参数的数量指定中心数的上限和下限:
2.2 保存高斯模型数据
write_class_gmm( : : GMMHandle, FileName : )
只要将高斯模型的句柄,保存文件路径提供后,可以实现数据存储。
write_class_gmm 将高斯混合模型 (GMM) GMMHandle 写入 FileName 给定的文件。 GMM 分类器的默认 HALCON 文件扩展名是“ggc”。 write_class_gmm 通常在使用 train_class_gmm 训练 GMM 之后调用。可以使用 read_class_gmm 读取 GMM。 write_class_gmm 不写入任何可能已存储在 GMM 中的训练样本。为此,应使用 write_samples_class_gmm。
2.3 读入高斯模型
read_class_gmm( : : FileName : GMMHandle)
只要将保存文件路径提供后,可以生成高斯模型的句柄。
read_class_gmm 读取已使用 write_class_gmm 存储的高斯混合模型 (GMM)。由于 GMM 的训练会消耗相对较长的时间,因此 GMM 通常在离线过程中进行训练,并使用 write_class_gmm 写入文件。在在线过程中,使用 read_class_gmm 读取 GMM,随后使用evaluate_class_gmm 进行评估或使用classify_class_gmm 进行分类。 GMM 分类器的默认 HALCON 文件扩展名是“ggc”。
2.4 数据集合追加数据
add_sample_class_gmm( : : GMMHandle, Features, ClassID, Randomize : )
add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训练数据上。
- GMMHandle, 高斯混合模型句柄
- Features, 数据的属性数
- ClassID, 数据的类别数
- Randomizem,数据内追加的随机信号
2.5 将类级的图形区域添加到高斯混合模型分类器中(抓取训练图形)
例如:add_samples_image_classes_gmm(Image, Classes, GMMHandle, 0)
意指:将需要查找的类型在图形中的区域关联到高斯混合模型中,用于训练
2.6 计算一个特征向量属于哪个类
classify_class_gmm( : : GMMHandle, Features, Num : ClassID, ClassProb, Density, KSigmaProb)
classify_class_gmm 功能:通过一个高斯混合模型来计算一个特征向量的类。
- GMMHandle,高斯模型句柄
- Features, 特征维数
- Num : 要确定的最佳类别的数量。
- ClassID, 样本类别编号
- ClassProb, 是该类的概率
- Density, 特征向量的概率密度。
- KSigmaProb 特征向量的归一化 k-sigma-probability。
classify_class_gmm 使用高斯混合模型 (GMM) GMMHandle 计算特征向量 Features 的最佳 Num 类,并返回 ClassID 中的类和 ClassProb 中类的相应概率。在调用classify_class_gmm 之前,必须使用train_class_gmm 训练GMM。
classify_class_gmm 对应于对 evaluate_class_gmm 的调用和提取最佳 Num 类的附加步骤。如evaluate_class_gmm 所述,GMM 的输出值可以解释为各个类出现的概率。然而,这里的后验概率 ClassProb 被进一步归一化为 ClassProb = p(i|x)/p(x) ,其中 p(i|x) 和 p(x) 由 evaluate_class_gmm 指定。在大多数情况下,使用 Num = 1 就足以确定最佳类别的概率是否足够高。在某些应用程序中,考虑次优类(Num = 2)可能会很有趣,特别是如果可以预期这些类显示出显着程度的重叠。
密度和 KSigmaProb 用 evaluate_class_gmm 解释。
2.7 清除所有高斯混合模型
- clear_all_class_gmm 功能:清除所有高斯混合模型。
- clear_class_gmm 功能:清除一个高斯混合模型。
- clear_samples_class_gmm 功能:清除一个高斯混合模型的训练数据。
2.8 通过一个高斯混合模型评价一个特征向量
evaluate_class_gmm 功能:通过一个高斯混合模型评价一个特征向量。
evaluate_class_gmm( : : GMMHandle, Features : ClassProb, Density, KSigmaProb)
- GMMHandle,高斯模型句柄
- Features : 特征数量
- ClassProb, 类先验概率
- Density, 某向量的概率密度
- KSigmaProb,特征向量的归一化 k-sigma-probability。
2.9.返回一个高斯混合模型的参数
get_params_class_gmm 功能:返回一个高斯混合模型的参数。
get_params_class_gmm( : : GMMHandle : NumDim, NumClasses, MinCenters, MaxCenters, CovarType)
2.10 计算一个高斯混合模型的预处理特征向量的信息内容
get_prep_info_class_gmm 功能:计算一个高斯混合模型的预处理特征向量的信息内容。
get_prep_info_class_gmm( : : GMMHandle, Preprocessing : InformationCont, CumInformationCont)
get_prep_info_class_gmm 计算已通过 Preprocessing 给出的预处理转换的训练向量的信息内容。
预处理可以设置为“principal_components”或“canonical_variates”。
预处理方法用 create_class_mlp 描述。信息内容来自特征向量的变换分量的变化,即它仅基于训练数据计算,与训练数据上的任何错误率无关。为转换后的特征向量的所有相关分量计算信息内容('principal_components' 和 'canonical_variates' 的 NumComponents,请参阅 create_class_gmm),并在 InformationCont 中以 0 和 1 之间的数字形式返回。将信息内容转换为百分比,只需乘以 100。前 n 个分量的累积信息内容在 CumInformationCont 的第 n 个分量中返回,即 CumInformationCont 包含 InformationCont 的前 n 个元素的和。
要使用 get_prep_info_class_gmm,必须使用 add_sample_class_gmm 或 read_samples_class_gmm 将足够数量的样本添加到 GMMHandle 给出的 GMM。
InformationCont 和 CumInformationCont 可用于决定转换后的特征向量有多少分量包含相关信息。一个常用的标准是要求转换后的数据必须代表数据的 x%(例如,90%)。这可以从高于 x% 的 CumInformationCont 的第一个值轻松确定。如此获得的数字可以在对 create_class_gmm 的新调用中用作 NumComponents 的值。对 get_prep_info_class_gmm 的调用已经需要创建 GMM,因此将 create_class_gmm 中的 NumComponents 设置为初始值。但是,如果调用 get_prep_info_class_gmm,通常不知道有多少组件是相关的,因此不知道如何在此调用中设置 NumComponents。因此,通常应使用以下两步方法来选择 NumComponents: 在第一步中,创建一个具有最大 NumComponents 数量的 GMM(“principal_components”和“canonical_variates”的 NumComponents)。然后,将训练样本添加到 GMM 并使用 write_samples_class_gmm 保存在文件中。随后,get_prep_info_class_gmm被用来确定组件的信息内容,并用这个NumComponents。之后,创建具有所需组件数量的新 GMM,并使用 read_samples_class_gmm 读取训练样本。最后,使用 train_class_gmm 训练 GMM。
2.11 从一个高斯混合模型的训练数据返回训练样本
get_sample_class_gmm 功能:从一个高斯混合模型的训练数据返回训练样本。
2.12 返回存储在一个高斯混合模型的训练数据中的训练样本的数量
get_sample_num_class_gmm 功能:返回存储在一个高斯混合模型的训练数据中的训练样本的数量。
2.13. 从一个文件中读取一个高斯混合模型的训练数据
read_samples_class_gmm 功能:从一个文件中读取一个高斯混合模型的训练数据。
2.14 训练一个高斯混合模型
train_class_gmm 功能:训练一个高斯混合模型。
train_class_gmm( : : GMMHandle, MaxIter, Threshold, ClassPriors, Regularize : Centers, Iter)
例如:train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.001, Centers,Iter)
- MaxIter : 最多迭代次数
- Threshold:前后迭代error,迭代停止的阈值
- ClassPriors,种类先验概率
- Regularize:是否归一化
2.15 向文件中写入一个高斯混合模型的训练数据
write_samples_class_gmm 功能:向文件中写入一个高斯混合模型的训练数据。
2.16 创建一个查找表,关联高斯混合模型查找类级(创建定位对象)
例如:create_class_lut_gmm(GMMHandle, ['bit_depth','rejection_threshold'], [6,0.03], 、ClassLUTHandle)
三、实验代码【1】
这个示例程序向您展示了如何使用 GMM 分类器进行新颖性测以执行网络检查任务。为了执行新颖性检测,计算属于单个训练类的所有像素,然后从分类 ROI 中减去以提取错误像素。为了Web 检查任务,因此 GMM 可用于检测与训练好的对象的纹理不对应的纹理。
用于分类的纹理过滤器将返回图像处的伪影边界,因为要检查的塑料网的图像不包含整数个网格单元。因为这会导致错误在图像边界检测到错误,我们必须排除靠近来自训练和分类的图像边界。这是通过以下方式完成的长方形。请注意,图像稍后按比例缩小了两倍。
* This example program shows you how to use the GMM classifier for novelty
* detection to perform a web inspection task. To perform the novelty detection,
* all pixels belonging to the single trained class are computed, and are then
* subtracted from the classification ROI to extract the erroneous pixels. For
* the web inspection task, the GMM can consequently be used to detect
* textures that do not correspond to the texture of the trained good objects.
*
dev_update_off ()
*
ReadPretrainedClassifier := false
* Uncomment the following line to read the pretrained classifier from
* disk. The training may last up to half a minute.
* ReadPretrainedClassifier := true
SaveClassifier := false
* Uncomment the following line to write the GMM classifier to disk after training.
* SaveClassifier := true
*
read_image (Image, 'plastic_mesh/plastic_mesh_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('red')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_system ('example_dir', HalconExamples)
* The texture filters used for the classification will return artifacts at the image
* borders because the images of the plastic mesh to be inspected do not
* contain an integer number of mesh cells. Because this would lead to wrongly
* detected errors at the image borders, we must exclude the area close to the
* image border from the training and classification. This is done with the following
* rectangle. Note that the image is later scaled down by a factor of two.
gen_rectangle1 (Rectangle, 10, 10, Height / 2 - 11, Width / 2 - 11)
if (ReadPretrainedClassifier)* Read the pretrained classifier from disk.dev_display (Image)disp_message (WindowHandle, 'Reading classifier from disk...', 'window', 10, 10, 'black', 'true')read_class_gmm (HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmm', GMMHandle)wait_seconds (1.5)
else* Create the GMM classifier.create_class_gmm (5, 1, [1,5], 'spherical', 'normalization', 5, 42, GMMHandle)* The training is based on five images that contain no errors.for J := 1 to 5 by 1read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')* The images are zoomed down because the resolution of the mesh is very* high. This saves a large amount of processing time.zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant')dev_display (ImageZoomed)disp_message (WindowHandle, 'Adding training samples...', 'window', 10, 10, 'black', 'true')* Generate the texture image.gen_texture_image (ImageZoomed, ImageTexture)* Add the samples to the classifier.add_samples_image_class_gmm (ImageTexture, Rectangle, GMMHandle, 2.0)endfordev_display (ImageZoomed)disp_message (WindowHandle, 'Training GMM...', 'window', 10, 10, 'black', 'true')* Train the GMM.train_class_gmm (GMMHandle, 1000, 0.001, 'training', 1e-4, Centers, Iter)if (SaveClassifier)write_class_gmm (GMMHandle, HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmm')endif
endif
* Now detect errors in the plastic meshes.
for J := 1 to 14 by 1read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant')dev_display (ImageZoomed)gen_texture_image (ImageZoomed, ImageTexture)reduce_domain (ImageTexture, Rectangle, ImageTextureReduced)* Classify samples belonging to the trained class with the GMM.classify_image_class_gmm (ImageTextureReduced, Correct, GMMHandle, 0.001)* Subtract them from the ROI to obtain the texture errors.difference (Rectangle, Correct, Errors)* Postprocess the returned raw errors to remove insignificant parts of the* detected errors.opening_circle (Errors, ErrorsOpening, 3.5)closing_circle (ErrorsOpening, ErrorsClosing, 10.5)connection (ErrorsClosing, ErrorsConnected)select_shape (ErrorsConnected, FinalErrors, 'area', 'and', 300, 1000000)count_obj (FinalErrors, NumErrors)dev_set_color ('red')dev_set_draw ('margin')dev_set_line_width (3)dev_display (FinalErrors)if (NumErrors > 0)disp_message (WindowHandle, 'Mesh not OK', 'window', 10, 10, 'black', 'true')elsedisp_message (WindowHandle, 'Mesh OK', 'window', 10, 10, 'black', 'true')endifif (J < 14)disp_continue_message (WindowHandle, 'black', 'true')endifstop ()
endfor
clear_class_gmm (GMMHandle)
四、实验代码【2】
下列代码缺少数据集,但可以追加后调试成功。
* Create the initial GMM
* create_class_gmm(3, 5, 1, 'full', 'none', 3, 42, GMMHandle)NumDim:=3
NumClasses:=5
NumCenters:=1
NumComponents:=3create_class_gmm (NumDim, NumClasses, NumCenters, 'full', 'principal_components', NumComponents, 42, GMMHandle)
* Generate and add the training data
for J := 0 to NumData-1 by 1* Generate training features and classes* Data = [...]* ClassID = [...]add_sample_class_gmm (GMMHandle, Data, ClassID, Randomize)
endfor
write_samples_class_gmm (GMMHandle, 'samples.gtf')
* Compute the information content of the transformed features
get_prep_info_class_gmm (GMMHandle, 'principal_components',\InformationCont, CumInformationCont)
* Determine Comp by inspecting InformationCont and CumInformationCont
* NumComponents = [...]
clear_class_gmm (GMMHandle)
* Create the actual GMM
create_class_gmm (NumDim, NumClasses, NumCenters, 'full',\'principal_components', NumComponents, 42, GMMHandle)
* Train the GMM
read_samples_class_gmm (GMMHandle, 'samples.gtf')
train_class_gmm (GMMHandle, 200, 0.0001, 0.0001, Regularize, Centers, Iter)
write_class_gmm (GMMHandle, 'classifier.gmm')
clear_class_gmm (GMMHandle)
halcon系列(1):高斯混合模型的一套算子相关推荐
- halcon系列:高斯混合模型之create_class_gmm 算子
一.提要 混合高斯模型是比较难以理解的数学模型:需要数学原理,其中包含多维高斯分布.EM算法原理等.本篇针对Halcon的gmm算子中,较难理解的一个算子create_class_gmm进行分解.讲解 ...
- 【机器学习】高斯混合模型详解
目录 1 引言 2 高斯混合模型 2.1 高斯分布 2.2 高斯混合模型 3 高斯混合模型的求解 4 参考文献 1 引言 高斯混合模型(Gaussian Mixture Model, GMM)是单 ...
- halcon第十九讲:基于高斯混合模型的水果分类
高斯混合模型是将多个服从高斯分布的模型进行线性组合,几乎能拟合成任何一条曲线.比如在生活中男生身高普遍在175左右,女生身高普遍在165左右,并且都服从高斯分布,就可通过中间交叉点进行分类. 上面只用 ...
- 高斯混合模型Gaussian Mixture Model (GMM)——通过增加 Model 的个数,我们可以任意地逼近任何连续的概率密分布...
从几何上讲,单高斯分布模型在二维空间应该近似于椭圆,在三维空间上近似于椭球.遗憾的是在很多分类问题中,属于同一类别的样本点并不满足"椭圆"分布的特性.这就引入了高斯混合模型.--可 ...
- K-Means(K均值)、GMM(高斯混合模型),通俗易懂,先收藏了!
文章目录 1. 聚类算法都是无监督学习吗? 2. k-means(k均值)算法 2.1 算法过程 2.2 损失函数 2.3 k值的选择 2.4 KNN与K-means区别? 2.5 K-Means优缺 ...
- 【机器学习笔记11】高斯混合模型(GMM)【上篇】原理与推导
文章目录 推荐阅读 前言 高斯混合模型简介 GMM与K-mean 高斯混合模型的概率密度函数 几何角度 混合模型角度 可能会弄混的地方 隐变量的分布与隐变量的后验概率分布 极大似然估计 EM算法求近似 ...
- 高斯混合模型和期望最大化的完整解释
In the previous article, we described the Bayesian framework for linear regression and how we can us ...
- 其他算法-高斯混合模型
目录 高斯模型 单高斯模型 高斯混合模型GMM 参数估计 单高斯模型参数估计-极大似然 高斯混合模型参数估计-EM算法 GMM与k-means 高斯模型 单高斯模型 当样本数据 x∈Rx\in\mat ...
- 高斯混合模型聚类实战(Gaussian Mixtures)
高斯混合模型聚类实战(Gaussian Mixtures) 目录 高斯混合模型聚类实战(Gaussian Mixtures) 高斯混合模型构建
最新文章
- win10键盘全部没反应_Win10笔记本键盘失灵怎么办 Win10键盘失灵解决方法【详解】...
- AI领域我重点关注的几个今日头条号
- 看完王兴刷屏之后,你该学会建立属于自己的第一性原理
- java 1e6,java-GeoPoint getLatitudeE6()返回-80000000,但getLong...
- Mapper 接口无法注入或Invalid bound statement (not found)
- XHTML、HTML4,HTML5 之间的区别
- Cs代码写在html页面哪里,当用于在details.cshtml页面上查看时,我用什么代码来查看模型中的项目列表?...
- logging、hashlib、collections模块
- linux桌面2k分辨率,解决ubuntu下外接2k显示器却没法调2k分辨率问题
- html 前端传数据流,jquery – 使用Node.js流式传输数据
- php100视频教程html,PHP100视频教程48:Ajax+PHP快速上手及应用
- 计算机网络技术超星尔雅章节检测,计算机网络技术超星尔雅答案题库
- Ansys 15.0 x64 安装
- 使用node实现简单的增删改查功能的小demo
- 从eclipse官网下载eclipse
- js点击网页背景特效和js打字状态特效代码
- UCOS III 任务堆栈理解
- android 实现视频播放功能,Android项目实现视频播放器
- centos查询 硬盘序列号查询_CentOS 查看硬件信息
- 使用全加器实现补码的加减运算