一、提要

混合高斯模型是比较难以理解的数学模型;需要数学原理,其中包含多维高斯分布、EM算法原理等。本篇针对Halcon的gmm算子中,较难理解的一个算子create_class_gmm进行分解、讲解;希望将最难的那个环节功克。

二、基本叙述

在特征向量为整数类型的特殊情况下,它们位于步长为 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。

2.1 产生高斯模型

create_class_gmm — Create a Gaussian Mixture Model for classification

create_class_gmm( : : NumDim, NumClasses, NumCenters, CovarType, Preprocessing, NumComponents, RandSeed : GMMHandle)

create_class_gmm 创建用于分类的高斯混合模型 (GMM)。 NumDim 指定特征空间的维数,NumClasses 指定类数。 GMM 由每个类的 NumCenters 高斯中心组成。 NumCenters 不仅可以是要使用的确切中心数,而且可以根据参数的数量指定中心数的上限和下限:

1 NumDim,

属性的维度

2 NumClasses,

指定类型的数量

3 NumCenters, 

GMM由每个类的NumCenters高斯中心组成。 NumCenters不仅可以是要使用的确切中心数目,而且可以根据参数的数目指定中心数目的上限和下限:

  • 正好一个参数:

该参数确定将用于所有类的中心的确切数量。

  • 正好两个参数:

第一个参数确定最少的中心数量,第二个参数确定所有类别的最大中心数量。

  • 确切的参数:

或者,每个第一个参数确定每个类的最小中心数,每个第二个参数确定每个类的最大中心数。

当指定了上限和下限时,将借助最小消息长度标准(MML)确定最佳中心数。通常,我们建议以(太多)最多的中心和最少的预期中心数开始培训。

4 CovarType,

每个中心都由参数center ,协方差矩阵和混合系数来描述。这些参数是通过期望最大化(EM)算法从训练数据中计算得出的。如果使用了足够的中心,则GMM可以近似任意概率密度。协方差矩阵 的尺寸为NumDim x NumDim(如果使用预处理,则为NumComponents x NumComponents),并且是对称的。进一步的约束可以由CovarType给出 : 

  • 对于CovarType = 'spherical',是单位矩阵的标量倍数。中心密度函数p(x | j)为

.

  • 对于CovarType = 'diag', 是对角矩阵。中心密度函数p(x | j)为 :

  • 对于CovarType = 'full', 为正定矩阵。中心密度函数p(x | j)为 :

计算的复杂度从CovarType = '球面'超过CovarType = 'diag' 到CovarType = '满'。同时,中心的灵活性增加了。因此,一般而言, “球形”对于NumCenter所需的值要 大于“完整”值

使用GMM的过程如下:首先,由create_class_gmm创建一个GMM 。然后,通过add_sample_class_gmm添加训练向量 ,然后可以使用write_samples_class_gmm将它们写入磁盘。使用train_class_gmm 确定分类器中心参数(如上定义)。此外,可以使用write_class_gmm保存它们,以供以后分类。

根据混合概率和中心密度函数p(x | j),可以通过以下公式计算概率密度函数p(x):

概率密度函数p(x)可以针对特征向量x用valuate_class_gmm进行评估。classify_class_gmm对p(x)进行排序,因此发现了特征向量中最可能的类。

参数Preprocessing和NumComponents可用于预处理训练数据并减小其尺寸。这些参数在操作符create_class_mlp的描述中进行了说明 。

create_class_gmm用随机数初始化中心的坐标。为确保使用train_class_gmm训练分类器的结果可再现,将随机数生成器的种子值传递 给RandSeed。

并行化

  • 多线程类型:可重入(与非排他运算符并行运行)。

  • 多线程作用域:全局(可以从任何线程调用)。

  • 未经并行处理。

该运算符返回一个句柄。请注意,此句柄类型的实例的状态可以由特定的运算符更改,即使这些运算符将句柄用作输入参数也是如此。

5 Preprocessing,

预处理(input_control)  字符串→ (字符串)

用于转换特征向量的预处理类型。

默认值: “规范化”

值列表:“ canonical_variates”,“ none”,“ normalization”,“ principal_components”

6 NumComponents,

NumComponents(input_control)  整数→ (整数)

预处理参数:变换后的特征数量(对于Preprocessing = 'none'和Preprocessing = 'normalization'忽略)。

默认值: 10

建议值: 1、2、3、4、5、8、10、15、20、30、40、50、60、70、80、90、100

限制:NumComponents> = 1

7 RandSeed 

RandSeed(input_control)  整数→ (整数)

随机数生成器的种子值,用于使用随机值初始化GMM。

默认值: 42

每个中心都由参数center ,协方差矩阵和混合系数来描述。这些参数是通过期望最大化(EM)算法从训练数据中计算得出的。如果使用了足够的中心,则GMM可以近似任意概率密度。协方差矩阵 的尺寸为NumDim x NumDim(如果使用预处理,则为NumComponents x NumComponents),并且是对称的。进一步的约束可以由CovarType给出 :

三、示例代码

例如:生成高斯混合分类器.
    create_class_gmm (5, 1, [1,5], 'spherical', 'normalization', 5, 42, GMMHandle)
create_class_gmm创建用于分类的高斯混合模型(GMM)。 NumDim指定要素空间的维数, NumClasses指定类的数量。

* The training is based on five images that contain no errors.

* 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)

halcon系列:高斯混合模型之create_class_gmm 算子相关推荐

  1. halcon系列(1):高斯混合模型的一套算子

    一.摘要 高斯混合模型,是EM算法之一种:该算法理论部分在另外的文章专门探讨,本篇专门对halcon的高斯分类的算子进行介绍,和基本使用方法.halcon的高斯模型其实很复杂,也很管用.希望多付出一些 ...

  2. halcon第十九讲:基于高斯混合模型的水果分类

    高斯混合模型是将多个服从高斯分布的模型进行线性组合,几乎能拟合成任何一条曲线.比如在生活中男生身高普遍在175左右,女生身高普遍在165左右,并且都服从高斯分布,就可通过中间交叉点进行分类. 上面只用 ...

  3. 高斯混合模型Gaussian Mixture Model (GMM)——通过增加 Model 的个数,我们可以任意地逼近任何连续的概率密分布...

    从几何上讲,单高斯分布模型在二维空间应该近似于椭圆,在三维空间上近似于椭球.遗憾的是在很多分类问题中,属于同一类别的样本点并不满足"椭圆"分布的特性.这就引入了高斯混合模型.--可 ...

  4. 混合高斯模型_大数据小白入门高斯混合模型(GMM)聚类算法

    导读 高斯混合模型(Gaussian Mixture Model)通常简称GMM,是一种业界广泛使用的聚类算法,属于生成式模型,它假设所有的数据样本都是由某一个给定参数的 多元高斯分布 所生成的.从中 ...

  5. 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优缺 ...

  6. 【机器学习笔记11】高斯混合模型(GMM)【上篇】原理与推导

    文章目录 推荐阅读 前言 高斯混合模型简介 GMM与K-mean 高斯混合模型的概率密度函数 几何角度 混合模型角度 可能会弄混的地方 隐变量的分布与隐变量的后验概率分布 极大似然估计 EM算法求近似 ...

  7. 高斯混合模型(GMM)及其EM算法的理解

    一个例子 高斯混合模型(Gaussian Mixed Model)指的是多个高斯分布函数的线性组合,理论上GMM可以拟合出任意类型的分布,通常用于解决同一集合下的数据包含多个不同的分布的情况(或者是同 ...

  8. 高斯混合模型和期望最大化的完整解释

    In the previous article, we described the Bayesian framework for linear regression and how we can us ...

  9. 【机器学习】高斯混合模型详解

    目录 1 引言 2 高斯混合模型 2.1 高斯分布 2.2 高斯混合模型 3 高斯混合模型的求解 4 参考文献 1 引言   高斯混合模型(Gaussian Mixture Model, GMM)是单 ...

最新文章

  1. AttributeError: module ‘os‘ has no attribute ‘mknod‘
  2. 一.vtun源码学习笔记
  3. 俄罗斯方块之四 运动块的绘制实现
  4. fixture.detectChange开始单步调试,如何执行到Directive的ngAfterViewInit钩子
  5. 错误解决:常出现在iis搭建网站
  6. STM32那点事(5)_ADC(上)
  7. java类与对象(属性,方法)的使用
  8. linux 软件_RansomExx勒索软件现在也针对Linux系统
  9. 【实习之T100开发】T100 单档程序开发(1)开发流程
  10. ubuntu MySQL数据库输入中文乱码 解决方案
  11. Python100道经典练习题(附PDF)
  12. “仿QQ局域网聊天软件”项目-常用编程技巧总结
  13. choco install deno报错
  14. type c 网卡 外接网卡 禁用后 不见 消失 找不到 的解决办法
  15. 使用mprotect定位踩内存故障
  16. 关于不使用firefly补丁对系统进行美化的探讨(转)
  17. 今天为什么仍必须进行门级仿真(GLS)详细讲解
  18. 浏览器 DevTools使用
  19. echarts分段式的象形柱图
  20. Origin | origin 2021b 学生免费版 | 中英文切换

热门文章

  1. 公众号数据监测工具从零运营公众号策略
  2. 做好这两件事供应商整合的风险管理
  3. 关于 TeamViewer 客户端被黑客攻击的通报,但真相是?
  4. NodeMCU连接天猫精灵第一集——硬件构架(基于Blinker点灯科技)
  5. 通信系统模型以及三角函数基本概念复习
  6. mac 安装Homebrew提示brew command not found的解决方法
  7. Unity3d打包安卓时的字体格式设置
  8. 用C语言开发NES游戏(CC65)06、精灵
  9. jwt退出登录的解决方案
  10. 【Ubuntu】开启ssh服务/配置ftp内网穿透/自动连接校园网