Halcon深度学习图像分类也是深度方向一大领域,可以用于超市商品分类识别,餐馆食品分类计价,垃圾分类等等。下面有我个人总结颜色分类代码,注释详细,需要或者想要学习halcon深度学习分类的朋友可做参考。
*分类网络
dev_update_off ()
dev_close_window ()
WindowWidth := 800
WindowHeight := 600
dev_open_window_fit_size (0, 0, WindowWidth, WindowHeight, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*
* Some procedures use a random number generator. Set the seed for reproducibility.
set_system ('seed_rand', 42)
*
* ** TRAINING **
*
* 读取一个预先训练的网络
read_dl_classifier ('pretrained_dl_classifier_compact.hdl', DLClassifierHandle)
*获取模型需要的图片大小
get_dl_classifier_param (DLClassifierHandle, 'image_width', DlImageWidth)
get_dl_classifier_param (DLClassifierHandle, 'image_height', DlImageHeight)
*获取模型需要的图片通道数
get_dl_classifier_param (DLClassifierHandle, 'image_num_channels', DlNumChannels)
*获取模型需要的图片灰度值范围
get_dl_classifier_param (DLClassifierHandle, 'image_range_min', DlRangeMin)
get_dl_classifier_param (DLClassifierHandle, 'image_range_max', DlRangeMax)
*总路径
DataDir := 'D:/DL_PVC_PinShan/DL_ColorClassify_PVC_PinShan/'
* 带图像的目录路径(0黑色,1红色 ,2黄色,3蓝色,4其他)
RawDataFolder := DataDir + 'Image/' + ['0','1','2','3','4']
* 根据原始数据对应打上标签,参数:输入路径,方法,所有图片路径,所有图片标签,所有标签索引,标签类型
read_dl_classifier_data_set (RawDataFolder, 'last_folder', RawImageFiles, Labels, LabelIndices, Classes)
* 处理后图片存放文件夹的名称
PreprocessedFolder := DataDir + '分类检测hobj'
* false不覆盖处理后的图片
OverwritePreprocessingFolder := false
*true执行完后删除保存的图片
RemovePreprocessingAfterExample := true
*
*将打标签的图像,并进行处理
*判断文件是否存在.
file_exists (PreprocessedFolder, FileExists)
if (not FileExists or OverwritePreprocessingFolder)
*如果覆盖时,文件夹存在,则删除
if (FileExists)
remove_dir_recursively (PreprocessedFolder)
endif
*如果覆盖时,文件夹存在,则删除
make_dir (PreprocessedFolder)
for I := 0 to |Classes| - 1 by 1
make_dir (PreprocessedFolder + '/' + Classes[I])
endfor
*定义待存放文件路径和名称
*将文件名解析为目录,基本文件名和扩展名
parse_filename (RawImageFiles, BaseNames, Extensions, Directories)
ObjectFilesOut := PreprocessedFolder + '/' + Labels + '/' + BaseNames + '.hobj'
* 处理输入图像并保存为.hobj格式文件
*每个样本图才处理成224*224分辨率(224*224是当前模型中获取到的参数,也可以设置成别的分辨率)、3通道、-127到128亮度级图像
DlImageWidth := 224
DlImageHeight := 224
DlNumChannels := 3
DlRangeMin := -127.0
DlRangeMax := 128.0
for I := 0 to |RawImageFiles| - 1 by 1
*读取样本文件
read_image (Image,RawImageFiles[I])
*将图片缩放到网络模型需求的大小
zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant')
*将图片的灰度缩放成网络模型需求范围
*类型转换
convert_image_type (Image, Image, 'real')
RescaleRange := (DlRangeMax-DlRangeMin)/255.0
*缩放图像的灰度值
scale_image (Image, Image, RescaleRange, DlRangeMin)
*合成三通道图片
*获取图片通道个数
count_obj (Image, Number)
for j := 1 to Number by 1
select_obj (Image, ObjectSelected, j)
count_channels (ObjectSelected, Channel)
*如果图片不是三通道图,将图片合成为三通道图
if (Channel != DlNumChannels)
*转换为3通道图像
compose3 (ObjectSelected, ObjectSelected, ObjectSelected, ThreeChannel)
*替换图元数组
replace_obj (Image, ThreeChannel, Image, 1)
endif
endfor
*并写入文件
write_object (Image, ObjectFilesOut[I])
dev_disp_preprocessing_progress (I, RawImageFiles, PreprocessedFolder, WindowHandle)
endfor
dev_clear_window ()
dev_disp_text ('Preprocessing done.', 'window', 'top', 'left', 'black', [], [])
endif
*
*将数据分为 训练集,验证集,测试集
*读取处理后的数据
read_dl_classifier_data_set (PreprocessedFolder, 'last_folder', ImageFiles, Labels, LabelsIndices, Classes)
*
*将数据分为三个子集,训练集占:70%、验证集占:15%、测试集占:15%
TrainingPercent := 70
ValidationPercent := 15
*将数据拆分 参数:输入图像路径、输入的标签、训练%,验证%、返回用于训练的图像、返会用于训练图像的标签、返回用于验证的图像、返会用于验证图像的标签、返回用于测试的图像、返会用于测试图像的标签
split_dl_classifier_data_set (ImageFiles, Labels, TrainingPercent, ValidationPercent, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels)
*
*设置分类模型训练参数
*设置分类模型类名
set_dl_classifier_param (DLClassifierHandle, 'classes', Classes)
*设置分类模型训练批处理数
BatchSize := 25
set_dl_classifier_param (DLClassifierHandle, 'batch_size', BatchSize)
Momentum := 0.9
set_dl_classifier_param (DLClassifierHandle, 'momentum', Momentum)
WeightPrior := 0.0005
set_dl_classifier_param (DLClassifierHandle, 'weight_prior', WeightPrior)
*设置运行环境(有可能halcon18训练需要gpu,运行可以cpu)
try
set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately')
catch (Exception)
dev_disp_error_text (Exception)
if (RemovePreprocessingAfterExample and Exception[0] != 4104)
remove_dir_recursively (PreprocessedFolder)
dev_disp_text ('Preprocessed data in folder "' + PreprocessedFolder + '" have been deleted.', 'window', 'bottom', 'left', 'black', [], [])
endif
stop ()
endtry
*对于该数据集,初始学习率为0.001
*已被证明产生了良好的效果。
InitialLearningRate := 0.001
set_dl_classifier_param (DLClassifierHandle, 'learning_rate', InitialLearningRate)
*每30次根据下降因子更新学习率
LearningRateStepEveryNthEpoch := 30
LearningRateStepRatio := 0.1
*迭代次数
NumEpochs := 120
*
* 训练模型
*
dev_clear_window ()
dev_disp_text ('Training has started...', 'window', 'top', 'left', 'black', [], [])
*每迭代4次绘制一下图
PlotEveryNthEpoch := 1
*训练好的分类网络模型名称和保存路径
FileName := DataDir + 'classifier_color_PVCPinShan.hdl'
*训练
train_fruit_classifier (DLClassifierHandle, FileName, NumEpochs, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, LearningRateStepEveryNthEpoch, LearningRateStepRatio, PlotEveryNthEpoch, WindowHandle)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*验证
*加载训练好的网络模型
read_dl_classifier (FileName, DLClassifierHandle)
*
* 计算验证数据集的混淆矩阵
get_predicted_classes (ValidationImages, DLClassifierHandle, PredictedClassesValidation)
*生成混淆矩阵模型
gen_confusion_matrix (ValidationLabels, PredictedClassesValidation, [], [], WindowHandle, ConfusionMatrix)
dev_disp_text ('Validation data', 'window', 'top', 'left', 'gray', 'box', 'false')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
dev_clear_window ()
*
* ** INFERENCE **
*
*测试
*加载训练好的网络模型
read_dl_classifier (FileName, DLClassifierHandle)
*设置单张图片测试
set_dl_classifier_param (DLClassifierHandle, 'batch_size', 1)
*测试是否可以在CPU环境下运行
try
set_dl_classifier_param (DLClassifierHandle, 'runtime', 'cpu')
Runtime := 'cpu'
catch (Exception)
* Keep the 'gpu' runtime if switching to 'cpu' failed.
Runtime := 'gpu'
endtry
*立即初始化运行时环境
set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately')
*
dev_resize_window_fit_size (0, 0, WindowWidth, WindowHeight, -1, -1)
dev_disp_inference_text (Runtime)
stop ()
*每个样本图才处理成224*224分辨率(也可以设置成别的分辨率)、3通道、-127到128亮度级图像
DlImageWidth := 224
DlImageHeight := 224
DlNumChannels := 3
DlRangeMin := -127.0
DlRangeMax := 128.0
for Index := 0 to 20 by 1
ImageFile := RawImageFiles[floor(rand(1) * |RawImageFiles|)]
read_image (Image, ImageFile)
*将图片缩放到网络模型需求的大小
zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant')
dev_resize_window_fit_image (Image, 0, 0, -1, -1)
*将图片的灰度缩放成网络模型需求范围
*类型转换
convert_image_type (Image, Image, 'real')
RescaleRange := (DlRangeMax-DlRangeMin)/255.0
*缩放图像的灰度值
scale_image (Image, Image, RescaleRange, DlRangeMin)
*合成三通道图片
*获取图片通道个数
count_channels (Image, Channel)
*如果图片不是三通道图,将图片合成为三通道图
if (Channel != DlNumChannels)
*转换为3通道图像
compose3 (Image, Image, Image, ThreeChannel)
*替换图元数组
replace_obj (Image, ThreeChannel, Image, 1)
endif
*使用已经训练好的深度学习网络识别图像 参数:输入图像、分类器的句柄、分类的结果
apply_dl_classifier (Image, DLClassifierHandle, DLClassifierResultHandle)
*获取识别结果 参数:分类的结果,批处理中图像的索引,通用参数的名称,通用参数的值
get_dl_classifier_result (DLClassifierResultHandle, 'all', 'predicted_classes', PredictedClass)
get_dl_classifier_result (DLClassifierResultHandle, 'all', 'confidences', Confidences)
get_dl_classifier_result (DLClassifierResultHandle, 'all', 'predicted_class_indices', PredictedClassIndices)
*
dev_display (Image)
Text := '预测类为: ' + PredictedClass + ' 置信度:' + Confidences
dev_disp_text (Text, 'window', 'top', 'left', 'white', 'box', 'false')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endfor
stop ()
if (RemovePreprocessingAfterExample)
remove_dir_recursively (PreprocessedFolder)
dev_disp_text ('End of program.\nPreprocessed data have been deleted.', 'window', 'bottom', 'right', 'black', [], [])
else
dev_disp_text (' End of program ', 'window', 'bottom', 'right', 'black', [], [])
endif