Halcon深度学习分类代码
代码整体预览:
备注:Halcon19.11版本;
dev_update_off ()
*读数据
read_dict ('2020.hdict', [], [], DLDataset)
*数据预处理
Data_preprocessing_classify (500, 374, 1, DLDataset, 4, 'png', 255)
*训练
Data_Train_classify (4, 0.0005, 0.9, 10, 0.0001, 'compact', 1, false, [], [])
*模型评估
Data_Evaluate_classify (4, true)
*推理初始化
Data_Infer_Init_classify (true, 1, DLModelHandle, DLPreprocessParam)
dev_get_window (WindowHandle)
set_display_font (WindowHandle, 32, 'mono', 'true', 'false')
**************************************************************
list_files ('test_images', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*在线部署运行
Data_Infer_OnLine_classify (Image, DLPreprocessParam, DLModelHandle, bbox_class_id)
stop ()
endfor
1、数据预处理:(Data_preprocessing_classify)
dev_update_off ()
*分类的数据增强
if (Enhanced > 0)
data_enhanced_classify (DLDataset, Enhanced, ImageFormat, ImageFill, DLDataset)
endif
*预处理输出文件夹
ExampleDataDir := 'defects_data'
DataDirectoryBaseName := ExampleDataDir + '/dldataset'
*预处理前删除上次数据
file_exists (ExampleDataDir, FileExists)
if (FileExists)
remove_dir_recursively (ExampleDataDir)
endif
*模型存放文件
file_exists ('New_Model', FileExists)
if (not FileExists)
make_dir ('New_Model')
endif
*设置相关参数(默认即可)
Capacity := 'medium'
TrainingPercent := 75
ValidationPercent := 15
*默认参数
NormalizationType := 'none'
DomainHandling := 'full_domain'
*随机种子
SeedRand := 42
set_system ('seed_rand', SeedRand)
*分割数据集
split_dl_dataset_RS_TECH (DLDataset, TrainingPercent, ValidationPercent, [])
*如果输出目录还不存在,请创建它。
file_exists (ExampleDataDir, FileExists)
if (not FileExists)
make_dir (ExampleDataDir)
endif
*创建可训练的参数
create_dl_preprocess_param ('classification', ImageWidth, ImageHeight, ImageNumChannels, -127, 128, NormalizationType, DomainHandling, [], [], [], [], DLPreprocessParam)
* Dataset directory for any outputs written by preprocess_dl_dataset.
DataDirectory := DataDirectoryBaseName
* Preprocess the dataset. This might take a few seconds.
create_dict (GenParam)
set_dict_tuple (GenParam, 'overwrite_files', true)
*halcon数据预处理
preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFileName)
*数据写入本地
PreprocessParamFileBaseName := 'New_Model/dl_preprocess_param.hdict'
write_dict (DLPreprocessParam, PreprocessParamFileBaseName, [], [])
*数据预处理已经结束
return ()
2、训练:(Data_Train_classify )
dev_update_off ()
*模型存放文件
file_exists ('New_Model', FileExists)
if (not FileExists)
make_dir ('New_Model')
endif
*model_type
*alexnet -> 'pretrained_dl_classifier_alexnet.hdl'
*compact -> 'pretrained_dl_classifier_compact.hdl'
*enhanced -> 'pretrained_dl_classifier_enhanced.hdl'
*resnet50 -> 'pretrained_dl_classifier_resnet50.hdl'
*alexnet,compact,enhanced,resnet50
if(Model_type == 'alexnet')
Backbone := 'pretrained_dl_classifier_alexnet.hdl'
endif
if (Model_type == 'compact')
Backbone := 'pretrained_dl_classifier_compact.hdl'
endif
if (Model_type == 'enhanced')
Backbone := 'pretrained_dl_classifier_enhanced.hdl'
endif
if (Model_type == 'resnet50')
Backbone := 'pretrained_dl_classifier_resnet50.hdl'
endif
*总输出文件夹
ExampleDataDir := 'defects_data'
*选择是否累加训练方案
ifelse (OldModel)
InitialModelFileName :='New_Model/best_dl_model_classification.hdl'
else
ModelFileName := Backbone
endif
*预处理文件存放路径
DataDirectory := ExampleDataDir + '/dldataset'
DLDatasetFileName := DataDirectory + '/dl_dataset.hdict'
DLPreprocessParamFileName := 'New_Model/dl_preprocess_param.hdict'
*best模型(实际部署的时候使用)
BestModelBaseName := 'New_Model/best_dl_model_classification'
*final(避免训练过程中意外停止)
FinalModelBaseName := 'New_Model/final_dl_model_classification'
*改变学习率规则
ChangeLearningRateValues := InitialLearningRate * ChangeLearningRateEpochs
*显示以及相关超参数
DisplayEvaluation := true
RandomSeed := 41
GenParamName := []
GenParamValue := []
* Halcon迷你版的数据增强
create_dict (AugmentationParam)
* Percentage of samples to be augmented.
set_dict_tuple (AugmentationParam, 'augmentation_percentage', 10)
* Mirror images along row and column.
set_dict_tuple (AugmentationParam, 'mirror', 'rc')
GenParamName := [GenParamName,'augment']
GenParamValue := [GenParamValue,AugmentationParam]
* 改变学习率
if (|ChangeLearningRateEpochs| > 0)
create_dict (ChangeStrategy)
* Specify the model parameter to be changed, here the learning rate.
set_dict_tuple (ChangeStrategy, 'model_param', 'learning_rate')
* Start the parameter value at 'initial_value'.
set_dict_tuple (ChangeStrategy, 'initial_value', InitialLearningRate)
* Reduce the learning rate in the following epochs.
set_dict_tuple (ChangeStrategy, 'epochs', ChangeLearningRateEpochs)
* Reduce the learning rate to the following values.
set_dict_tuple (ChangeStrategy, 'values', ChangeLearningRateValues)
* Collect all change strategies as input.
GenParamName := [GenParamName,'change']
GenParamValue := [GenParamValue,ChangeStrategy]
endif
* 'best'模型
create_dict (SerializationStrategy)
set_dict_tuple (SerializationStrategy, 'type', 'best')
set_dict_tuple (SerializationStrategy, 'basename', BestModelBaseName)
GenParamName := [GenParamName,'serialize']
GenParamValue := [GenParamValue,SerializationStrategy]
*'final'模型
create_dict (SerializationStrategy)
set_dict_tuple (SerializationStrategy, 'type', 'final')
set_dict_tuple (SerializationStrategy, 'basename', FinalModelBaseName)
GenParamName := [GenParamName,'serialize']
GenParamValue := [GenParamValue,SerializationStrategy]
*显示参数
*在本例中,选择20%的训练分割来显示
*训练过程中减少训练分裂的评价措施。较低的百分比
*有助于加快评估/培训,如果培训部门的评估措施
*不显示,将该值设置为0(默认值)
SelectedPercentageTrainSamples := 20
create_dict (DisplayParam)
set_dict_tuple (DisplayParam, 'selected_percentage_train_samples', SelectedPercentageTrainSamples)
GenParamName := [GenParamName,'display']
GenParamValue := [GenParamValue,DisplayParam]
*检查是否存在所有必要的文件。
check_data_availability(ExampleDataDir, DLDatasetFileName, DLPreprocessParamFileName)
*读入在预处理期间初始化的模型。
read_dl_model (ModelFileName, DLModelHandle)
*读入预处理的DLDataset文件。
read_dict (DLDatasetFileName, [], [], DLDataset)
* Set model hyper-parameters as specified in the settings above.
set_dl_model_param (DLModelHandle, 'learning_rate', InitialLearningRate)
set_dl_model_param (DLModelHandle, 'momentum', Momentum)
* Set the class names for the model.
get_dict_tuple (DLDataset, 'class_names', ClassNames)
set_dl_model_param (DLModelHandle, 'class_names', ClassNames)
* Get image dimensions from preprocess parameters and set them for the model.
read_dict (DLPreprocessParamFileName, [], [], DLPreprocessParam)
get_dict_tuple (DLPreprocessParam, 'image_width', ImageWidth)
get_dict_tuple (DLPreprocessParam, 'image_height', ImageHeight)
get_dict_tuple (DLPreprocessParam, 'image_num_channels', ImageNumChannels)
set_dl_model_param (DLModelHandle, 'image_dimensions', [ImageWidth,ImageHeight,ImageNumChannels])
*设置BatchSize
if (BatchSize == 'maximum')
set_dl_model_param_max_gpu_batch_size (DLModelHandle, 100)
else
set_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
endif
*加载权重
if (|WeightPrior| > 0)
set_dl_model_param (DLModelHandle, 'weight_prior', WeightPrior)
endif
*设置'runtime_init'框架,'immediately'默认为GPU运行
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
*创建可训练参数
create_dl_train_param (DLModelHandle, NumEpochs, EvaluationIntervalEpochs, DisplayEvaluation, RandomSeed, GenParamName, GenParamValue, TrainParam)
*halcon训练
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
return ()
3、模型评估:(Data_Evaluate_classify )
dev_update_off ()
*总输出文件夹
ExampleDataDir := 'defects_data'
DataDirectory := ExampleDataDir + '/dldataset'
DLDatasetFileName := DataDirectory + '/dl_dataset.hdict'
* 最终部署需要的
RetrainedModelFileName := 'New_Model/best_dl_model_classification.hdl'
ClassificationMeasures := ['top1_error','precision','recall','f_score','absolute_confusion_matrix','relative_confusion_matrix']
* Check availability of GPU mode.
if (UseGPU)
get_system ('cuda_loaded', CudaLoaded)
get_system ('cudnn_loaded', CuDNNLoaded)
get_system ('cublas_loaded', CuBlasLoaded)
if (not (CudaLoaded == 'true' and CuDNNLoaded == 'true' and CuBlasLoaded == 'true'))
UseGPU := false
endif
endif
* Check if all necessary files exist.
check_data_availability(ExampleDataDir, DLDatasetFileName, RetrainedModelFileName)
* Read the retrained model.
read_dl_model (RetrainedModelFileName, DLModelHandle)
* Initialize the model.
if (not UseGPU)
set_dl_model_param (DLModelHandle, 'runtime', 'cpu')
endif
set_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
* Read the preprocessed DLDataset file.
read_dict (DLDatasetFileName, [], [], DLDataset)
* Set parameters for evaluation.
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'measures', ClassificationMeasures)
set_dict_tuple (GenParamEval, 'show_progress', 'true')
stop ()
*模型评估
evaluate_dl_model(DLDataset, DLModelHandle, 'split', 'test', GenParamEval, EvaluationResult, EvalParams)
* Display measures.
create_dict (WindowHandleDict)
create_dict (GenParamEvalDisplay)
set_dict_tuple (GenParamEvalDisplay, 'display_mode', ['measures','pie_charts_precision','pie_charts_recall','absolute_confusion_matrix'])
*模型评估结果显示
dev_display_classification_evaluation (EvaluationResult, EvalParams, GenParamEvalDisplay, WindowHandleDict)
dev_close_window_dict (WindowHandleDict)
stop ()
* Call interactive confusion matrix.
dev_display_dl_interactive_confusion_matrix (DLDataset, EvaluationResult, [])
* Close window handles.
dev_close_window_dict (WindowHandleDict)
SelectedHeatmapGTClassName := 'contamination'
SelectedHeatmapInfClassName := 'contamination'
* Get information from DLDataset and EvaluationResult.
get_dict_tuple (EvaluationResult, 'evaluated_samples', EvaluatedSamples)
get_dict_tuple (EvaluatedSamples, 'image_ids', ImageIDs)
get_dict_tuple (EvaluatedSamples, 'image_label_ids', ImageLabelIDs)
get_dict_tuple (EvaluatedSamples, 'top1_predictions', Predictions)
get_dict_tuple (DLDataset, 'class_names', ClassNames)
get_dict_tuple (DLDataset, 'class_ids', ClassIDs)
*默认值报错:情况未知
* Get class IDs for selected classes.
* PredictedClassID := ClassIDs[find(ClassNames,SelectedHeatmapInfClassName)]
* GroundTruthClassID := ClassIDs[find(ClassNames,SelectedHeatmapGTClassName)]
PredictedClassID := 0
GroundTruthClassID := 0
* Get tuple position of selected classes.
GTIndices := find(ImageLabelIDs [==] GroundTruthClassID,1)
PredictionIndices := find(Predictions [==] PredictedClassID,1)
* Get image IDs for selected combination.
ImageIDsSelected := []
if (PredictionIndices != -1 and PredictionIndices != [])
ImageIDsSelected := ImageIDs[intersection(GTIndices,PredictionIndices)]
endif
HeatmapMethod := 'heatmap_grad_cam'
*
if (HeatmapMethod != 'heatmap_grad_cam' and HeatmapMethod != 'heatmap_confidence_based')
throw ('Unsupported heatmap option.')
elseif (HeatmapMethod == 'heatmap_grad_cam' and not UseGPU)
throw ('The heatmap option "heatmap_grad_cam" is only supported for GPU runtime')
endif
TargetClassID := []
create_dict (HeatmapParam)
if (HeatmapMethod == 'heatmap_grad_cam')
set_dict_tuple (HeatmapParam, 'use_conv_only', 'false')
set_dict_tuple (HeatmapParam, 'scaling', 'scale_after_relu')
else
set_dict_tuple (HeatmapParam, 'target_class_id', TargetClassID)
FeatureSize := 30
SamplingSize := 10
set_dict_tuple (HeatmapParam, 'feature_size', FeatureSize)
set_dict_tuple (HeatmapParam, 'sampling_size', SamplingSize)
endif
set_dl_model_param (DLModelHandle, 'batch_size', 1)
create_dict (WindowHandleDict)
get_dict_tuple (DLDataset, 'samples', DLSamples)
for Index := 0 to min([|ImageIDsSelected| - 1,10]) by 1
find_dl_samples (DLSamples, 'image_id', ImageIDsSelected[Index], 'match', DLSampleIndex)
read_dl_samples (DLDataset, DLSampleIndex, DLSample)
if (HeatmapMethod == 'heatmap_grad_cam')
gen_dl_model_heatmap (DLModelHandle, DLSample, 'grad_cam', TargetClassID, HeatmapParam, DLResult)
else
create_dict (DLResult)
gen_dl_model_classification_heatmap (DLModelHandle, DLSample, DLResult, HeatmapParam)
endif
dev_display_dl_data (DLSample, DLResult, DLDataset, HeatmapMethod, [], WindowHandleDict)
dev_disp_text ('Press F5 to continue.', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endfor
dev_close_window_dict (WindowHandleDict)
return ()
4、推理初始化:(Data_Infer_Init_classify )
dev_update_off ()
*预处理前删除上次数据
* file_exists ('detect_data', FileExists)
* if (FileExists)
* remove_dir_recursively ('detect_data')
* endif
PreprocessParamFileName := 'New_Model/dl_preprocess_param.hdict'
RetrainedModelFileName := 'New_Model/best_dl_model_classification.hdl'
if (UseGPU)
get_system ('cuda_loaded', CudaLoaded)
get_system ('cudnn_loaded', CuDNNLoaded)
get_system ('cublas_loaded', CuBlasLoaded)
if (not (CudaLoaded == 'true' and CuDNNLoaded == 'true' and CuBlasLoaded == 'true'))
UseGPU := false
endif
endif
read_dl_model (RetrainedModelFileName, DLModelHandle)
get_dl_model_param (DLModelHandle, 'class_names', ClassNames)
get_dl_model_param (DLModelHandle, 'class_ids', ClassIDs)
set_dl_model_param (DLModelHandle, 'batch_size', BatchSizeInference)
if (not UseGPU)
set_dl_model_param (DLModelHandle, 'runtime', 'cpu')
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
read_dict (PreprocessParamFileName, [], [], DLPreprocessParam)
return ()
5、在线部署运行:(Data_Infer_OnLine_classify )
get_image_size (Image, Width, Height)
*开始推理检测
gen_dl_samples_from_images (Image, DLSampleBatch)
preprocess_dl_samples (DLSampleBatch, DLPreprocessParam)
apply_dl_model (DLModelHandle, DLSampleBatch, [], DLResultBatch)
*读dict
get_dict_tuple (DLResultBatch, 'classification_class_names', classification_class_names)
result := classification_class_names[0]
dev_clear_window ()
dev_display (Image)
*显示问题(自行显示)
dev_disp_text ( classification_class_names[0], 'image', Width/2, Height/2, 'black', 'box', 'true')
return ()