Halcon 深度学习之语义分割 预处理 案例解析

语义分割 预处理


前言

声明:本篇是个人针对于语义分割预处理案例的理解,有理解不到位或者错误的地方,还望各位能够给予指正,在此表示感谢!

例子名称: segment_pill_defects_deep_learning_1_preprocess


一、预处理的目的是什么?

1、设置图像预处理的参数,此部分参数后续会写入到训练模型当中,具体参数信息如下图

预处理文件的信息

2、得到语义分割的文件,用于后续的训练当中,语义分割可通过多种方式生成

①:导入原图文件夹,标注文件夹,类别信息等
②:通过Deep Learning Tool 工具创建的语义分割文件【内部的信息也跟①的一样】
②:通过Deep Learning Tool工具创建的语义分割分拣,所导出的数据集文件.hdict【内部的信息也跟①方式生成的一样】
语义分割文件参数具体如下图
语义分割文件的信息

二、案例分块解析

1.案例说明部分,可跳过

* 一 :显示该例子介绍信息 
ShowExampleScreens := false
dev_example_init (ShowExampleScreens, ExampleInternals)
* 
if (ShowExampleScreens)
    * 
    * Introduction text of example series.
    dev_display_screen_introduction_part_1 (ExampleInternals)
    stop ()
    dev_display_screen_introduction_part_2 (ExampleInternals)
    stop ()
    * 
    * Explain semantic segmentation data.
    dev_display_screen_segmentation_data (ExampleInternals)
    stop ()
    * 
    * Explain splitting the dataset.
    dev_display_screen_split_dataset (ExampleInternals)
    stop ()
    * 
    * Explain preprocessing parameters.
    dev_display_screen_preprocessing_params (ExampleInternals)
    stop ()
    dev_display_screen_weight_images (ExampleInternals)
    stop ()
    * 
    * Explain the next steps.
    dev_display_screen_next_steps (ExampleInternals)
    stop ()
endif

2.设置深度学习模型文件的路径,其为相对路径的方式

* 原图像目录
ImageDir := 'pill'
* 分割图像存放文件夹,即标注的缺陷区域
SegmentationDir := 'labels/pill'
* 生成文件的根目录
ExampleDataDir := 'segment_pill_defects_data'
DataDirectoryBaseName := ExampleDataDir + '/dldataset_pill_'
* 模型预处理参数的文件名
PreprocessParamFileBaseName := '/dl_preprocess_param.hdict'

注:后续读取的算子
*读取分割模型数据集 (原图像目录,标注的缺陷目录,  标注类别),类别对应ID)         
* DLDataset 实际上Deep Lerning Tool 创建的标注文件.hdict
read_dl_dataset_segmentation (ImageDir, SegmentationDir, ClassNames, ClassIDs, [], [], [], DLDataset)

3.预处理参数的设置

* 缺陷标签类别:好的,脏污,断裂
ClassNames := ['good','contamination','crack']
* 相应的标注序号,即灰度值索引
ClassIDs := [0,1,2]
* 拆分数据集的百分比 训练百分之70  确认百分之15【即每学习一段时间后,从此部分抽出一些进行识别验证,判断学习的准确程度】
TrainingPercent := 70
ValidationPercent := 15
*设置预处理图片的相应信息, 图片尺寸,图片彩色—— 
*①、此处可对图片进行压缩,
*②、但后续图像进行推理的时候,也需要对原图进行相应的压缩,不然推理的缺陷区域会与原图缺陷区域对应不上。
*③、图片越大相应的硬件要求也高一些
ImageWidth := 400
ImageHeight := 400
ImageNumChannels := 3
* 设置图片灰度值的范围,范围是-127128,即为byte,有疑问
ImageRangeMin := -127
ImageRangeMax := 128
*设置进一步预处理的相应参数
*正常化参数,暂不明其意
NormalizationType := 'none'
*域处理参数,full_domain 即所操作的范围为全图
DomainHandling := 'full_domain'
IgnoreClassIDs := []
SetBackgroundID := []
ClassIDsBackground := []

注:后续设置的算子
*创建预处理模型参数(语义分割,图像宽,高,通道数,最低灰度值,最高灰度值,正常化参数,操作区域范围,图像背景信息[],[],[],[],out 预处理参数句柄)
create_dl_preprocess_param ('segmentation', ImageWidth, ImageHeight, ImageNumChannels, ImageRangeMin, ImageRangeMax, NormalizationType, DomainHandling, IgnoreClassIDs, SetBackgroundID, ClassIDsBackground, [], DLPreprocessParam)
* 

4.导出语义分割文件与保存预处理文件

*创建一个空字典
create_dict (GenParam)
*是否覆盖文件
set_dict_tuple (GenParam, 'overwrite_files', true)

*预处理模型数据集 导出语义分割文件 即DLDatasetFilename 
*将每张图片结合标注的信息,以及缺陷的权重,以hdict文件的格式记录,每张图片对应一个hdict【可不需要】,。。。。。。
preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFilename)

PreprocessParamFile := DataDirectory + PreprocessParamFileBaseName
*将当前的句柄保存下来,即保存当前预处理的相关参数
write_dict (DLPreprocessParam, PreprocessParamFile, [], [])

5.从训练集当中,随机抽取十张查看预处理效果

get_dict_tuple (DLDataset, 'samples', DatasetSamples)
*找出训练的数据集
find_dl_samples (DatasetSamples, 'split', 'train', 'match', SampleIndices)
*将数组打散随机,然后将打散后的数组取前十个
tuple_shuffle (SampleIndices, ShuffledIndices)
read_dl_samples (DLDataset, ShuffledIndices[0:9], DLSampleBatchDisplay)
* 
create_dict (WindowHandleDict)
for Index := 0 to |DLSampleBatchDisplay| - 1 by 1
    * Loop over samples in DLSampleBatchDisplay.
    dev_display_dl_data (DLSampleBatchDisplay[Index], [], DLDataset, ['image','segmentation_image_ground_truth'], [], WindowHandleDict)
    get_dict_tuple (WindowHandleDict, 'segmentation_image_ground_truth', WindowHandleImage)
    dev_set_window (WindowHandleImage[1])
    Text := 'Press Run (F5) to continue'
    dev_disp_text (Text, 'window', 400, 40, 'black', [], [])
    stop ()
endfor

6.附个人注释的代码

* 
* This example is part of a series of examples, which summarizes
* the workflow for DL segmentation. It uses the MVTec pill dataset.
* 
* The four parts are:
* 1. Dataset preprocessing.    预处理:数据集预处理
* 2. Training of the model.    训练:模型的训练
* 3. Evaluation of the trained model.   评估:对训练模型的评估
* 4. Inference on new images.         推理:对新图像的推断
* 
* 第一部分:数据预处理
* 
dev_update_off ()
* 一 :显示该例子介绍信息 
ShowExampleScreens := false
dev_example_init (ShowExampleScreens, ExampleInternals)
* 
if (ShowExampleScreens)
    * 
    * Introduction text of example series.
    dev_display_screen_introduction_part_1 (ExampleInternals)
    stop ()
    dev_display_screen_introduction_part_2 (ExampleInternals)
    stop ()
    * 
    * Explain semantic segmentation data.
    dev_display_screen_segmentation_data (ExampleInternals)
    stop ()
    * 
    * Explain splitting the dataset.
    dev_display_screen_split_dataset (ExampleInternals)
    stop ()
    * 
    * Explain preprocessing parameters.
    dev_display_screen_preprocessing_params (ExampleInternals)
    stop ()
    dev_display_screen_weight_images (ExampleInternals)
    stop ()
    * 
    * Explain the next steps.
    dev_display_screen_next_steps (ExampleInternals)
    stop ()
endif



*二:设置深度学习模型文件的路径,其为相对路径的方式
* 原图像目录
ImageDir := 'pill'
* 分割图像存放文件夹,即标注的缺陷区域
SegmentationDir := 'labels/pill'
* 生成文件的根目录
ExampleDataDir := 'segment_pill_defects_data'
DataDirectoryBaseName := ExampleDataDir + '/dldataset_pill_'
* 模型预处理参数的文件名
PreprocessParamFileBaseName := '/dl_preprocess_param.hdict'


* 三:设置训练的相应参数
* 缺陷标签类别:好的,脏污,断裂
ClassNames := ['good','contamination','crack']
* 相应的标注序号,即灰度值索引
ClassIDs := [0,1,2]
* 拆分数据集的百分比 训练百分之70  确认百分之15【即每学习一段时间后,从此部分抽出一些进行识别验证,判断学习的准确程度】
TrainingPercent := 70
ValidationPercent := 15
*设置预处理图片的相应信息, 图片尺寸,图片彩色—— 
*①、此处可对图片进行压缩,
*②、但后续图像进行推理的时候,也需要对原图进行相应的压缩,不然推理的缺陷区域会与原图缺陷区域对应不上。
*③、图片越大相应的硬件要求也高一些
ImageWidth := 400
ImageHeight := 400
ImageNumChannels := 3
* 设置图片灰度值的范围,范围是-127128,即为byte,有疑问
ImageRangeMin := -127
ImageRangeMax := 128
*设置进一步预处理的相应参数
*正常化参数,暂不明其意
NormalizationType := 'none'
*域处理参数,full_domain 即所操作的范围为全图
DomainHandling := 'full_domain'
IgnoreClassIDs := []
SetBackgroundID := []
ClassIDsBackground := []
* 
*设置一个随机种子,不设置的话会已当前时间段生成一个随机种子,具体干嘛的暂无感触
SeedRand := 42
set_system ('seed_rand', SeedRand)


*四:读取分割数据集,并设置相应好坏种类的比例 
* Read the dataset.   
*读取分割模型数据集          (原图像目录,标注的缺陷目录,  标注类别),类别对应ID),          DLDataset 分割数据集的句柄,记录着传进来的各种参数信息
* DLDataset 实际上Deep Lerning Tool 创建的标注文件.hdict
read_dl_dataset_segmentation (ImageDir, SegmentationDir, ClassNames, ClassIDs, [], [], [], DLDataset)
*生成拆分数据集, 训练的70   确认的15,要额外设置
split_dl_dataset (DLDataset, TrainingPercent, ValidationPercent, [])


*判断一下相应路径是否存在,如果不存在的话,则新建路径
file_exists (ExampleDataDir, FileExists)
if (not FileExists)
    *后续会在此目录下生成每张图片的hdict文件,里面记录着图片的尺寸信息,分割区域,推断区域等,详情见图
    make_dir (ExampleDataDir)
endif

*创建预处理模型参数(语义分割,图像宽,高,通道数,最低灰度值,最高灰度值,正常化参数,操作区域范围,图像背景信息[],[],[],[],out 预处理参数句柄)
create_dl_preprocess_param ('segmentation', ImageWidth, ImageHeight, ImageNumChannels, ImageRangeMin, ImageRangeMax, NormalizationType, DomainHandling, IgnoreClassIDs, SetBackgroundID, ClassIDsBackground, [], DLPreprocessParam)
* 
* Dataset directory for any outputs written by preprocess_dl_dataset.
DataDirectory := DataDirectoryBaseName + ImageWidth + 'x' + ImageHeight
* 
*创建一个空字典
create_dict (GenParam)
*是否覆盖文件
set_dict_tuple (GenParam, 'overwrite_files', true)
*预处理模型数据集 生成语义分割文件 即DLDatasetFilename 
*将每张图片结合标注的信息,以及缺陷的权重,以hdict文件的格式记录,每张图片对应一个hdict【可不需要】,。。。。。。
preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFilename)
* 
* Store preprocess params separately in order to use it e.g. during inference.
PreprocessParamFile := DataDirectory + PreprocessParamFileBaseName
*将当前的句柄保存下来,即保存当前预处理的相关参数
write_dict (DLPreprocessParam, PreprocessParamFile, [], [])

*五: 验证,实时反馈
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
*找出训练的数据集
find_dl_samples (DatasetSamples, 'split', 'train', 'match', SampleIndices)
*将数组打散随机,然后将打散后的数组取前十个
tuple_shuffle (SampleIndices, ShuffledIndices)
read_dl_samples (DLDataset, ShuffledIndices[0:9], DLSampleBatchDisplay)
* 
create_dict (WindowHandleDict)
for Index := 0 to |DLSampleBatchDisplay| - 1 by 1
    * Loop over samples in DLSampleBatchDisplay.
    dev_display_dl_data (DLSampleBatchDisplay[Index], [], DLDataset, ['image','segmentation_image_ground_truth'], [], WindowHandleDict)
    get_dict_tuple (WindowHandleDict, 'segmentation_image_ground_truth', WindowHandleImage)
    dev_set_window (WindowHandleImage[1])
    Text := 'Press Run (F5) to continue'
    dev_disp_text (Text, 'window', 400, 40, 'black', [], [])
    stop ()
endfor
* 
* Close windows that have been used for visualization.
dev_close_window_dict (WindowHandleDict)
* 
if (ShowExampleScreens)
    * Hint to the DL segmentation training process example.
    dev_display_screen_next_example (ExampleInternals)
    stop ()
    * Close example windows.
    dev_close_example_windows (ExampleInternals)
endif

个人总结

预处理这块的代码量虽然很多,但大多数是用于介绍等,实际上,当有标注好的文件,在设置上图像的一些信息
如:图像宽度,高度,通道,训练的模式【CPU || GPU】 训练集与验证集的比例,缺陷放大的权重参数,即可完成图像预处理
个人也是刚刚接触的深度学习这块,总结不到位或者理解错误的地方,希望各位能够给予指正,再次感谢大家!

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Halcon深度学习语义分割是指使用Halcon软件进行图像处理和分析,通过深度学习算法对图像进行语义分割,即将图像中的每个像素分配到不同的类别中。语义分割可以用于目标检测、图像分割、医学影像分析等领域。 在Halcon中进行深度学习语义分割,需要进行一系列的预处理操作。预处理的目的是设置图像预处理的参数,并生成语义分割的文件,用于后续的训练。预处理的参数包括图像的宽度、高度、通道数,训练模式(CPU或GPU),训练集与验证集的比例等。预处理的代码量可能较多,但大部分是用于介绍和设置图像信息的,实际上只需要有标注好的文件和设置好的图像信息,就可以完成图像预处理。 具体操作步骤如下: 1. 使用Everything软件或全盘搜索,找到文件segment_pill_defects_deep_learning_1_preprocess.hdev,该文件路径应该在HALCON安装目录下的examples\hdevelop\Deep-Learning\Segmentation文件夹中。 2. 返回到该目录下找到images文件夹,并将生成的test1_images文件夹拷贝到images文件夹下。确保test1_images文件夹与pill在同级目录下。 3. 在images文件夹中找到labels文件夹,并将生成的test1_labels文件夹放入其中。同样,确保test1_labels文件夹与pill在同级目录下。 4. 修改代码中相应的名称,并根据需要注释部分代码。 5. 完成以上步骤后,即可进行Halcon深度学习语义分割操作。 请注意,以上是一般的操作步骤,具体的操作可能会因个人需求和实际情况而有所不同。如果有任何问题或错误,请及时指正。 #### 引用[.reference_title] - *1* *2* [Halcon 深度学习语义分割 预处理 案例解析](https://blog.csdn.net/zhuifengyx/article/details/127538927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Halcon深度学习---语义分割(1)---数据集预处理](https://blog.csdn.net/WDX4092410/article/details/131213087)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值