halcon深度学习中的样本增强

news2025/2/1 14:43:49

一、问题描述

halcon的例程中,下面是最经典的语义分割例子。

但是,它并没做样本增强,因为 你看下图的代码,第90行,那两个参数都是[],空的。

二、解决方案

如下图所示,增加了从95到108行,并且在第110行,倒数第二和倒数第三个参数,不再是[]了,被我改了。

 

三、完整代码

* 
* Deep learning segmentation workflow:
* 
* This example shows the overall workflow for
* deep learning segmentation.
* 
* PLEASE NOTE:
* - This is a bare-bones example.
* - For this, default parameters are used as much as possible.
*   Hence, the results may differ from those obtained in other examples.
* - For more detailed steps, please refer to the respective examples from the series,
*   e.g. segment_pill_defects_deep_learning_1_preprocess.hdev etc.
* 
dev_close_window ()
dev_update_off ()
set_system ('seed_rand', 42)
* 
* ***   0) SET INPUT/OUTPUT PATHS AND DATASET PARAMETERS   ***
* 
ImageDir := './somepics'
SegmentationDir := './jiang_labels'
* 
OutputDir := 'segment_pill_defects_data'
* 
ClassNames := ['0', '1', '2','3']
ClassIDs := [0, 1, 2, 3]
* Set to true, if the results should be deleted after running this program.
RemoveResults := false
* 
* ***   1.) PREPARE   ***
* 
* Read and prepare the DLDataset.
read_dl_dataset_segmentation (ImageDir, SegmentationDir, ClassNames, ClassIDs, [], [], [], DLDataset)
split_dl_dataset (DLDataset, 60, 20, [])
create_dict (PreprocessSettings)
* Here, existing preprocessed data will be overwritten if necessary.
set_dict_tuple (PreprocessSettings, 'overwrite_files', 'auto')
create_dl_preprocess_param ('segmentation', 620, 100, 3, -127, 128, 'none', 'full_domain', [], [], [], [], DLPreprocessParam)
preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
* 
* Inspect 10 randomly selected preprocessed DLSamples visually.
create_dict (WindowDict)
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
find_dl_samples (DatasetSamples, 'split', 'train', 'match', TrainSampleIndices)
for Index := 0 to 9 by 1
    SampleIndex := TrainSampleIndices[round(rand(1) * (|TrainSampleIndices| - 1))]
    read_dl_samples (DLDataset, SampleIndex, DLSample)
    dev_display_dl_data (DLSample, [], DLDataset, ['segmentation_image_ground_truth', 'segmentation_weight_map'], [], WindowDict)
    dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
    *stop ()
endfor
dev_close_window_dict (WindowDict)
* 
* ***   2.) TRAIN   ***
* 
* Read a pretrained model and adapt its parameters
* according to the dataset.
*read_dl_model ('pretrained_dl_segmentation_compact.hdl', DLModelHandle)
read_dl_model ('pretrained_dl_segmentation_enhanced.hdl', DLModelHandle)
set_dl_model_param_based_on_preprocessing (DLModelHandle, DLPreprocessParam, ClassIDs)
set_dl_model_param (DLModelHandle, 'class_names', ClassNames)
* Set training related model parameters.
* Training can be performed on a GPU or CPU.
* See the respective system requirements in the Installation Guide.
* If possible a GPU is used in this example.
* In case you explicitely wish to run this example on the CPU,
* choose the CPU device instead.
query_available_dl_devices (['runtime', 'runtime'], ['gpu', 'cpu'], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
    throw ('No supported device found to continue this example.')
endif
* Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
get_dl_device_param (DLDevice, 'type', DLDeviceType)
if (DLDeviceType == 'cpu')
    * The number of used threads may have an impact
    * on the training duration.
    NumThreadsTraining := 4
    set_system ('thread_num', NumThreadsTraining)
endif
* 
* For details see the documentation of set_dl_model_param () and get_dl_model_param ().
if (DLDeviceType == 'gpu')
    set_dl_model_param_max_gpu_batch_size (DLModelHandle, 30)
endif
set_dl_model_param (DLModelHandle, 'learning_rate', 0.0001)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
* 
* Here, we run a short training of 10 epochs.
* For better model performance increase the number of epochs
* and train as long as your compute budget allows,
* e.g. for 100, 1000 or 3000 epochs.
* Set generic parameters of create_dl_train_param.
* Please see the documentation of create_dl_train_param for an overview on all available parameters.
GenParamName := []
GenParamValue := []
* 
* Augmentation parameters.
* If samples should be augmented during training, create the dict required by augment_dl_samples.
* Here, we set the augmentation percentage and method.
AugmentationParam := dict{}
* Percentage of samples to be augmented.
AugmentationParam.augmentation_percentage := 90
AugmentationParam.brightness_variation := 20
AugmentationParam.contrast_variation :=0.2
AugmentationParam.saturation_variation :=0.2
GenParamName := [GenParamName,'augment']
GenParamValue := [GenParamValue,AugmentationParam]

create_dl_train_param (DLModelHandle, 3000, 1, 'true', 42, GenParamName, GenParamValue, TrainParam)
* The training and thus the call of train_dl_model_batch ()
* is done using the following procedure.
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
* 
* Read the best model, which is written to file by train_dl_model.
read_dl_model ('model_best.hdl', DLModelHandle)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'left', 'black', [], [])
stop ()
* 
dev_close_window ()
dev_close_window ()
* 
* ***   3.) EVALUATE   ***
* 
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'show_progress', true)
set_dict_tuple (GenParamEval, 'measures', ['mean_iou', 'pixel_accuracy', 'class_pixel_accuracy', 'pixel_confusion_matrix'])
* 
set_dl_model_param (DLModelHandle, 'device', DLDevice)
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEval, EvaluationResult, EvalParams)
* 
create_dict (GenParamEvalDisplay)
set_dict_tuple (GenParamEvalDisplay, 'display_mode', ['measures', 'absolute_confusion_matrix'])
dev_display_segmentation_evaluation (EvaluationResult, EvalParams, GenParamEvalDisplay, WindowDict)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
dev_close_window_dict (WindowDict)
* 
* Optimize the model for inference,
* meaning, reduce its memory consumption.
set_dl_model_param (DLModelHandle, 'optimize_for_inference', 'true')
set_dl_model_param (DLModelHandle, 'batch_size', 1)
* Save the model in this optimized state.
write_dl_model (DLModelHandle, 'model_best.hdl')
* 
* ***   4.) INFER   ***
* 
* To demonstrate the inference steps, we apply the
* trained model to some randomly chosen example images.
list_image_files (ImageDir, 'default', 'recursive', ImageFiles)
tuple_shuffle (ImageFiles, ImageFilesShuffled)
* 
* Create dictionaries used in visualization.
create_dict (WindowDict)
get_dl_model_param (DLModelHandle, 'class_ids', ClassIds)
get_dl_model_param (DLModelHandle, 'class_names', ClassNames)
create_dict (DLDatasetInfo)
set_dict_tuple (DLDatasetInfo, 'class_ids', ClassIds)
set_dict_tuple (DLDatasetInfo, 'class_names', ClassNames)
for IndexInference := 0 to 9 by 1
    read_image (Image, ImageFilesShuffled[IndexInference])
    gen_dl_samples_from_images (Image, DLSample)
    preprocess_dl_samples (DLSample, DLPreprocessParam)
    apply_dl_model (DLModelHandle, DLSample, [], DLResult)
    * 
    dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['segmentation_image_result', 'segmentation_confidence_map'], [], WindowDict)
    dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
    stop ()
endfor
dev_close_window_dict (WindowDict)
* 
* ***   5.) REMOVE FILES   ***
* 
clean_up_output (OutputDir, RemoveResults)

 

四、注意事项

我增加的代码中,依据的是halcon的帮助文件,建议各位使用的时候,认真研读,如下图

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1093789.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

启航kp OpenHarmony环境搭建

前提 启航kp OpenHarmony环境搭建 搭建好OpenHarmony环境 未搭建好可以参考OpenHarmony docker环境搭建 安装vscode 下载好启航kp所需的开发包和样例 下载地址 搭建过程 进入正确文件夹 首先要进入 /home/openharmony 目录下,如果没有打开在vsc左上角找到文…

DELL R710硬盘格式化方法备忘录

DELL R710硬盘格式化方法备忘录 本方法是全部格式化硬盘,不能格式化其中的某一块 开机出现带CTRLR字样后 按CTRLR 按下CTRLP三次 以上是目前硬盘的状态,使用中的是ONLINE状态, 按CTRLP,回到第一个界面,然后按F2 选择…

【线上Java项目部署Bug记录】天翼云80端口不能使用,即使暴露了也不行!!!

项目场景: 项目背景:使用Docker部署SpringBoot项目,前端是 Vue 项目是:https://gitee.com/JavaLionLi/RuoYi-Vue-Plus 服务器:天翼云服务器 端口: 问题描述 所有端口都开的好好的,docker的…

山西电力市场日前价格预测【2023-10-15】

日前价格预测 预测说明: 如上图所示,预测明日(2023-10-15)山西电力市场全天平均日前电价为409.82元/MWh。其中,最高日前价格为722.95元/MWh,预计出现在19: 00。最低日前电价为255.87元/MWh,预计…

tkinter自定义组件:文件选择按钮和颜色选择按钮

文章目录 文件对话框按钮代码实现颜色对话框 tkinter系列: GUI初步💎布局💎绑定变量💎绑定事件💎消息框💎文件对话框Frame控件💎PanedWindow和notebook控件扫雷小游戏💎强行表白神器…

人工智能就业前景越来越严峻了,你还在坚持吗?

点击上方关注 “终端研发部” 设为“星标”,和你一起掌握更多数据库知识 从最近最火的chatGpt来看,AI时长不但没有低迷下去,而且还越来越好了!去年毕业的一个朋友,硕士毕业,目前在字节做机器学习工程师&…

【Spring框架】Spring监听器的简介和基本使用

目录 一、观察者模式 1.1 模型介绍 1.2 观察者模式Demo 1.2.1 观察者实体 1.2.2 主题实体 1.2.3 测试代码 二、Spring监听器的介绍 2.1 事件(ApplicationEvent) 2.1.1 Spring内置事件 2.1.2 Spring内置事件 2.2 事件监听器(Applic…

阿里云安全中心需要购买吗?功能及价格告诉你值不值!

阿里云云安全中心有必要购买吗?云安全中心经常提示云服务器高危漏洞,需要购买云安全中心吗?无论是云服务器上是网站还是其他应用,难免会存在漏洞,有漏洞是一定要修复的,云安全中心不仅可以修复漏洞还可以防…

【C++STL基础入门】list的增、删

文章目录 前言一、list迭代器1.1 list迭代器的定义 二、list增2.1 头添加2.2 尾添加2.3 中间添加 三、list删3.1 尾删除3.2 头删除3.3 删除指定元素3.4 clear()函数3.5 remove()函数3.6 unique()函数 总结 前言 在C中,STL(Standard Template Library&am…

ALBERT-更小更少但并不快

BERT模型的压缩大致可以分为:1. 参数剪枝;2. 知识蒸馏;3. 参数共享;4. 低秩分解。 其中,对于剪枝,比较简单,但是容易误操作降低精读; 对于知识蒸馏,之前我写个一系列的…

【完美世界】战王之殇特别篇定档,11月3日播,云曦受辱石昊杀红眼了

Hello,小伙伴们,我是小郑继续为大家深度解析完美世界国漫资讯。 完美世界第132集已经播出了,相信很多人都去看了。但是不知道大家注意到这一集的片尾没有。如果没有快进或者直接跳过的话,那么应该知道,官方给大家送惊喜了。 这个…

高校教务系统登录页面JS分析——四川大学

高校教务系统密码加密逻辑及JS逆向 本文将介绍高校教务系统的密码加密逻辑以及使用JavaScript进行逆向分析的过程。通过本文,你将了解到密码加密的基本概念、常用加密算法以及如何通过逆向分析来破解密码。 本文仅供交流学习,勿用于非法用途。 一、密码加…

进程同步互斥问题

互斥-临界区前后分别PV操作;同步-前V后P 一、生产者费者问题 1.过程: 生产者生产产品——>缓冲区(供两者共享使用)——>消费者取出产品使用;但缓冲区有容量要求,会导致等待与唤醒。缓冲区是临界资源,各进程必…

Jetpack:007-各种各样的Button

文章目录 1. 概念介绍2. 使用方法2.1 Button2.2 IconButton2.3 ElevatedButton2.4 OutlinedButton2.5 TextButton2.6 FloatingActionButton 3. 示例代码4. 内容总结 我们在上一章回中介绍了Jetpack中输入框相关的内容,本章回中将要介绍 Button。闲话休提&#xff0…

Linux系统编程02

makefile的原理 问题需求 之前我们讲过C语言代码主要是经过编译和链接两个步骤生成目标文件,但是在编译的时候我们可能需要进行多条指令的输入,要对main函数所在的文件进行汇编,再将我们定义的函数文件进行汇编,分别形成*.o文件&a…

网课、会议投屏自动截屏软件推荐 —— 使用定时自动截屏软件,自动截屏网课、会议投屏,方便回顾、整理

在当前的远程学习和工作环境下,我们经常需要参加网课和会议,而这些内容通常都是通过投屏的方式呈现给我们的。为了更好地记录和回顾这些内容,我们可以使用定时截屏软件来保存这些投屏截屏。 定时截屏软件功能 定时截屏软件可以帮助我们定时…

ModSecurity开源WAF防火墙和控制面板安装教程

ModSecurity开源WAF防火墙和控制面板安装教程 CyberPanel带有两个版本,一个是CyberPanel,另一个是CyberPanel Ent。CyberPanel附带OpenLiteSpeed,不限数量域名完全免费。CyberPanel Ent附带LiteSpeed Web Server Enterprise,只免…

2023年中国门把手产量、销量及市场规模分析[图]

门把手行业是指专门从事门把手的设计、制造、销售和安装等相关业务的行业。门把手是门窗装饰硬件的一种,用于开启和关闭门窗,同时也具有装饰和美化门窗的作用。 门把手行业分类 资料来源:共研产业咨询(共研网) 随着消…

leetCode 72. 编辑距离 动态规划 + 滚动数组 + 优化空间

72. 编辑距离 - 力扣(LeetCode) 给你两个单词 word1 和 word2, 请返回将 word1 转换成 word2 所使用的最少操作数 。你可以对一个单词进行如下三种操作: 插入一个字符删除一个字符替换一个字符 编辑距离的应用场景:…

200多万开发者才开发91款应用,国产手机操作系统离开安卓活不了?

随着某国产手机操作系统强调将不再兼容安卓,他们沸腾了,表示将真正独立自主发展,然而业界人士却指出号称拥有220万开发者的该款操作系统至今仅开发了91款应用,彻底撕下了它的遮羞布。 应用对一个智能操作系统有多重要,…