初学,记录学习过程
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)
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.16e′MetaData.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)