halcon深度学习异常检测

初学,记录学习过程
halcon深度学习例程dl_anomaly_detection_workflow,算法流程共分为5部分:
(0)准备阶段
准备输入、输出路径。
dev_update_off ()
dev_close_window ()
*设置随机种子
set_system (‘seed_rand’, 73)

HalconImages := ‘C:/Users/Administrator/Desktop’

  • 指定图像类别所在的文件夹
    ImageDir := HalconImages+‘/leather’

  • 限制读取的子文件夹
    ImageSubDirs := [‘good’,‘fold’,‘cut’,‘poke’,‘color’,‘glue’]
    *存放标签
    AnomalyLabelsDir := HalconImages+‘/labels/leather’
    OutputDir := ‘./anomaly_leather_data’
    *数据集预处理
    ExampleSpecificPreprocessing := true
    *图像缩放之后的大小,是32的倍数
    ImageWidth := 320
    ImageHeight := 320
    *复杂度,越大准确率越高,训练时间越长,代表模型处理更复杂缺陷的能力
    Complexity := 15
    (1)第一阶段:准备阶段
    GenParamDataset:=dict{image_sub_dirs:ImageSubDirs}
    *读取异常检测的深度学习数据集
    read_dl_dataset_anomaly (ImageDir, AnomalyLabelsDir, [], [], GenParamDataset, DLDataset)
    *拆分样本集为训练集(50%),验证集(20%),测试集(剩余的30%)
    split_dl_dataset (DLDataset, 50, 10, [])
    *读取MVTec提供的初始网络之一

  • 加载预训练的模型、设置参数
    read_dl_model (‘initial_dl_anomaly_medium.hdl’, DLModelHandle)
    *设置之前定义的模型参数
    set_dl_model_param (DLModelHandle, ‘image_width’, ImageWidth)
    set_dl_model_param (DLModelHandle, ‘image_height’, ImageHeight)
    set_dl_model_param (DLModelHandle, ‘complexity’, Complexity)

  • In this example, the anomaly detection model is trained on the CPU.
    query_available_dl_devices ([‘runtime’, ‘id’], [‘cpu’, 0], DLDevice)
    set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
    *设置预处理参数,并进行预处理
    create_dl_preprocess_param_from_model (DLModelHandle, ‘constant_values’, ‘full_domain’, [], [], [], DLPreprocessParam)
    PreprocessSetting:=dict{overwrite_files:‘true’}
    preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSetting, DLDatasetFileName)
    *获得样本集DLDataset中的样本
    if(ExampleSpecificPreprocessing)
    read_dl_samples (DLDataset, [0:|DLDataset.samples|-1], DLSampleBatch)
    *对图像背景进行处理
    preprocess_dl_samples_bottle(DLSampleBatch)
    write_dl_samples (DLDataset, [0:|DLDataset.samples|-1], DLSampleBatch, [], [])
    endif
    展示10个随机预处理后的DLSample
    WindowDict:=dict{}
    for Index := 0 to 9 by 1
    SampleIndex:=int(rand(1)
    |DLDataset.samples|)
    *读取深度学习样本
    read_dl_samples (DLDataset, SampleIndex, DLSample)
    *显示所有数据
    dev_display_dl_data (DLSample, [], DLSample, ‘anomaly_ground_truth’, [], WindowDict)

    dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
    dev_set_window (WindowDict.anomaly_ground_truth[0])
    dev_disp_text (‘Preprocessed image’, ‘window’, ‘top’, ‘left’, ‘black’, [], [])
    stop()
    endfor

dev_close_window_dict (WindowDict)
stop()
(2)第二阶段:训练阶段
EnableDisplay:=true
*创建字典,并存储键-值对
TrainPramaAnomaly:=dict{}
TrainPramaAnomaly.error_threshold:=0.001
MaxNumEpochs:=30

TrainPramaAnomaly.domain_ratio:=0.25
TrainPramaAnomaly.regularization_noise:=0.01
*创建训练参数
create_dl_train_param (DLModelHandle, MaxNumEpochs, [], EnableDisplay, 73, ‘anomaly’, TrainPramaAnomaly, TrainParam)

train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop()
*
dev_close_window()

(3)第三阶段:评估阶段
*标准差因子(如果缺陷很小,推荐较大值)
StandardDeviationFactor:=1.0
set_dl_model_param (DLModelHandle, ‘standard_deviation_factor’, StandardDeviationFactor)

*计算阈值
GenParamThresholds:=dict{enable_display:true}
compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, GenParamThresholds, AnomalySegmentationThreshold, AnomalyClassificationThresholds)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop()

dev_close_window ()
*设置评估参数,在test集上进行评估
GenParamEvaluation:=dict{}
GenParamEvaluation.measures:=‘all’
GenParamEvaluation.anomaly_classification_thresholds:=AnomalyClassificationThresholds
evaluate_dl_model (DLDataset, DLModelHandle, ‘split’, ‘test’, GenParamEvaluation, EvaluationResult, EvalParams)
*要展示的参数
GenParamDisplay:=dict{}
*直方图、图例
GenParamDisplay.display_mode:=[‘score_histogram’,‘score_legend’]
WindowDict:=dict{}
*生成了一张直方图
dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop()
*
dev_close_window_dict (WindowDict)

  • 可视化precision,recall召回率,和confusion_matrix
    GenParamDisplay.display_mode:=[‘pie_charts_precision’,‘pie_charts_recall’,‘absolute_confusion_matrix’]
    ClassificationThresholdIndex:=|AnomalyClassificationThresholds|-1
    GenParamDisplay.classification_threshold_index:=ClassificationThresholdIndex
    WindowDict:=dict{}
    dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict)
    dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
    stop()

dev_close_window_dict (WindowDict)
get_dl_model_param (DLModelHandle, ‘meta_data’, MetaData)
MetaData.anomaly_segmentation_threshold:=AnomalySegmentationThreshold ′ 1.16 e ′ M e t a D a t a . a n o m a l y c l a s s i f i c a t i o n t h r e s h o l d : = A n o m a l y C l a s s i f i c a t i o n T h r e s h o l d s [ C l a s s i f i c a t i o n T h r e s h o l d I n d e x ] '1.16e' MetaData.anomaly_classification_threshold:=AnomalyClassificationThresholds[ClassificationThresholdIndex] 1.16eMetaData.anomalyclassificationthreshold:=AnomalyClassificationThresholds[ClassificationThresholdIndex]‘1.16e’
set_dl_model_param (DLModelHandle, ‘meta_data’, MetaData)

write_dl_model (DLModelHandle, ‘model_final.hdl’)
(4)第四阶段:推理阶段
list_image_files (ImageDir+‘/’+ImageSubDirs, ‘default’, ‘recursive’, ImageFiles)
*打乱数据集
tuple_shuffle (ImageFiles, ImageFilesShuffled)
*设置阈值,模型训练后得到
get_dl_model_param (DLModelHandle, ‘meta_data’, MetaData)
InferenceClassificationThreshold:=number(MetaData.anomaly_classification_threshold)
InferenceSegmentationThreshold:=number(MetaData.anomaly_segmentation_threshold)

  • 创建类别标签字典
    DLDatasetInfo := dict{class_names:[‘OK’,‘nok’],class_ids:[0,1]}
    WindowDict:=dict{}
    for IndexInference := 0 to min2(|ImageFilesShuffled|,20)-1 by 1
    read_image (Image, ImageFilesShuffled[IndexInference])
    gen_dl_samples_from_images (Image, DLSample)
    preprocess_dl_samples (DLSample, DLPreprocessParam)
    if(ExampleSpecificPreprocessing)
    preprocess_dl_samples_bottle (DLSample)
    endif
    apply_dl_model (DLModelHandle, DLSample, [], DLResult)
    threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult)

    dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, [‘anomaly_result’,‘anomaly_image’], [], WindowDict)
    dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
    stop()
    endfor
    dev_close_window_dict (WindowDict)

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Halcon深度学习缺陷检测是一种基于深度学习技术的自动化缺陷检测方法。该方法通过训练神经网络模型来识别产品中的缺陷,如裂纹、划痕、变形等,从而实现自动化的检测和分类。 Halcon是一款常用的机器视觉软件平台,支持多种图像处理和分析功能。通过集成深度学习技术,Halcon可以提高缺陷检测的准确度和效率,同时减少人力和时间成本。 Halcon深度学习缺陷检测的实现过程包括以下步骤: 1. 数据准备:收集产品图片和标注数据,构建训练数据集。 2. 模型选择:根据缺陷检测任务的特点,选择合适的深度学习模型。 3. 模型训练:使用训练数据集对模型进行训练,不断调整参数直至达到最佳效果。 4. 模型测试:使用测试数据集对模型进行测试,评估其准确度和泛化能力。 5. 部署应用:将训练好的模型部署到实际应用中,实现自动化缺陷检测Halcon深度学习缺陷检测具有以下优点: 1. 高准确度:深度学习模型可以学习和识别复杂的缺陷形态和纹理特征,从而提高检测准确度。 2. 高效性:深度学习模型可以快速处理大规模的数据,实现高效的自动化检测。 3. 可扩展性:可以根据不同的应用场景和产品类型,自定义训练模型,实现个性化的缺陷检测。 总之,Halcon深度学习缺陷检测是一种先进的自动化检测技术,可以大大提高产品质量和生产效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值