目标检测系统下载地址:https://download.csdn.net/download/jbossjf/80419315
一、准备1、准备目标检测所需图片images文件夹:训练模型的图片
2、用工具将训练图片打上标签(MVTec Deep Learning Tool)
下载地址: https://www.mvtec.com/products/deep-learning-tool/https://www.mvtec.com/products/deep-learning-tool/
2.2、打上标签
以分类中的pretrained_dl_classifier_compact.hdl的模型为主体
dev_update_off ()
*** 创建检测模型 ***
**设置参数
*必须的参数
Backbone := 'pretrained_dl_classifier_compact.hdl'
*class_ids的数量,标签的总类别数量
NumClasses := 6
*模型所需图片的规格参数
ImageWidth := 512
ImageHeight := 320
ImageNumChannels := 3
*
MinLevel := 2
MaxLevel := 4
NumSubscales := 3
AspectRatios := [1.0,0.5,2.0]
*容量参数
Capacity := 'medium'
*创建模型参数字典
create_dict (DLModelDetectionParam)
set_dict_tuple (DLModelDetectionParam, 'image_width', ImageWidth)
set_dict_tuple (DLModelDetectionParam, 'image_height', ImageHeight)
set_dict_tuple (DLModelDetectionParam, 'image_num_channels', ImageNumChannels)
set_dict_tuple (DLModelDetectionParam, 'min_level', MinLevel)
set_dict_tuple (DLModelDetectionParam, 'max_level', MaxLevel)
set_dict_tuple (DLModelDetectionParam, 'num_subscales', NumSubscales)
set_dict_tuple (DLModelDetectionParam, 'aspect_ratios', AspectRatios)
set_dict_tuple (DLModelDetectionParam, 'capacity', Capacity)
* 创建模型
create_dl_model_detection (Backbone, NumClasses, DLModelDetectionParam, DLModelHandle)
2、预处理数据
*** 预处理数据 ***
**设置路径
*总路径
DataDir := 'E:/视觉/halcon_深度学习/DeepLearning/目标检测'
*图片路径
HalconImageDir:= DataDir + '/testImages/'
*数据集路径
PillBagHdictFile := DataDir + '/images.hdict'
*输出的模型路径
DLModelFileName := DataDir + '/pretrained_dl_model_detection.hdl'
* 用于预处理数据集的输出
DataDirectory := DataDir + '/dataset'
* 分别存储预处理参数以便在推理过程中使用。
PreprocessParamFileName := DataDirectory + '/dl_preprocess_param_' + ImageWidth + 'x' + ImageHeight
**设置参数
TrainingPercent := 80
ValidationPercent := 10
SeedRand := 42
*判断总路径是否存在
file_exists (DataDir, FileExists)
if (not FileExists)
make_dir (DataDir)
endif
*读取数据集
read_dict (PillBagHdictFile, [], [], DLDataset)
*获取数据集ID给到模型参数
get_dict_tuple (DLDataset, 'class_ids', ClassIDs)
set_dl_model_param (DLModelHandle, 'class_ids', ClassIDs)
*生成新的模型
write_dl_model (DLModelHandle, DLModelFileName)
set_system ('seed_rand', SeedRand)
*分割数据集
split_dl_dataset (DLDataset, TrainingPercent, ValidationPercent, [])
*从模型中获取预处理参数
create_dict (GenParam)
set_dict_tuple (GenParam, 'overwrite_files', true)
create_dl_preprocess_param_from_model (DLModelHandle, 'false', 'full_domain', [], [], [], DLPreprocessParam)
*预处理
preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFilename)
3、训练
*** 训练 ***
* 设置训练相关模型参数.
set_dl_model_param (DLModelHandle, 'batch_size', 1)
set_dl_model_param (DLModelHandle, 'learning_rate', 0.001)
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
* 创建在模型中使用的训练参数字典
*参数:模型、将训练模型的时期数(越大训练效果越好)、定义将多少时间传递给下一个模型评估
* 是否显示训练进度、设置随机种子、输出参数的名称、输出参数的字典、输出句柄
create_dl_train_param (DLModelHandle, 20, 1, 'true', 42, [], [], TrainParam)
*在数据集上训练一个基于深度学习的模型
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
*
* 读取最佳模型,该模型由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 ()
4、评估
*** 评估 ***
create_dict (WindowDict)
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'detailed_evaluation', true)
set_dict_tuple (GenParamEval, 'show_progress', true)
*对选定的DLDataset样本评估DLModelHandle给出的模型。
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEval, EvaluationResult, EvalParams)
*
create_dict (DisplayMode)
set_dict_tuple (DisplayMode, 'display_mode', ['pie_charts_precision','pie_charts_recall'])
*可视化类型检测模型的详细评估结果
dev_display_detection_detailed_evaluation (EvaluationResult, EvalParams, DisplayMode, WindowDict)
stop ()
*关闭字典中包含句柄的所有窗口。
dev_display_dl_data_close_windows (WindowDict)
5、测试
*** 测试 ***
*获取该路径下的所有图片
list_image_files (HalconImageDir, 'default', 'recursive', ImageFiles)
*对图片文件进行随机排序
tuple_shuffle (ImageFiles, ImageFilesShuffled)
*每次读取一张
set_dl_model_param (DLModelHandle, 'batch_size', 1)
for i := 0 to |ImageFiles|-1 by 1
read_image (Image, ImageFilesShuffled[i])
gen_dl_samples_from_images (Image, DLSampleInference)
preprocess_dl_samples (DLSampleInference, DLPreprocessParam)
apply_dl_model (DLModelHandle, DLSampleInference, [], DLResult)
*为一个示例可视化不同的图像、注释和推理结果。
dev_display_dl_data (DLSampleInference, DLResult, DLDataset, 'bbox_result', [], WindowDict)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endfor
*关闭可视化窗体
dev_display_dl_data_close_windows (WindowDict)