halcon深度学习-图像分割

halcon深度学习-图像分割

通过网盘分享的文件:segmentation.zip
链接: https://pan.baidu.com/s/1IjAFVa-MUb5yvdoch4FQCw?pwd=1234 提取码: 1234

一 训练

*使用深度学习分割功能步骤:
*1.把原始图像放到images目录下,使用标注工具labeltool.exe进行标注,标注文件放到labels目录下;
*2.运行训练程序train.hdev进行训练,结果存为model_best.hdl,过程存图;
*影响训练的参数
*网络类型
Model := 'pretrained_dl_segmentation_compact.hdl'
*Model := 'pretrained_dl_segmentation_enhanced.hdl'
*Model := 'model_best.hdl'
*训练迭代次数
NumEpochs := 100*1
*最大图像宽度
MaxWidth := 1024*1.0
*显卡序号
GpuId := 0
*初始学习率
LearningRate := 0.0001*10
dev_update_off ()
dev_close_window ()
TrainingPercent := 80
ReCreateDataset := 1
dlt_read_classnames ('classes.txt', ClassNames)
ClassNames := ['background', ClassNames]
tuple_gen_sequence (0, |ClassNames|-1, 1, ClassIDs)
*1.) prepare the DLDataset
read_dl_dataset_segmentation ('images', 'labels', ClassNames, ClassIDs, [], [], [], DLDataset)
split_dl_dataset (DLDataset, TrainingPercent, 100-TrainingPercent, [])
create_dict (PreprocessSettings)
set_dict_tuple (PreprocessSettings, 'overwrite_files', true)
ImageDir := 'images/'
read_dl_model (Model, DLModelHandle)
get_dict_tuple (DLDataset, 'samples', Samples)
if(Model == 'model_best.hdl')
    get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
    Width   := ImageDimensions[0]
    Height  := ImageDimensions[1]
    Channels:= ImageDimensions[2]    
else
    get_dict_tuple (Samples[0], 'image_file_name', ImageFileName)
    read_image (Image, ImageDir + '/' + ImageFileName)
    get_image_size(Image, Width, Height)
    tuple_min2(Width, MaxWidth*1.0, Min)
    Scale := Width / Min
    Width  := round(Width / Scale)
    Height := round(Height / Scale) 
    count_channels(Image, Channels) 
endif
create_dl_preprocess_param ('segmentation', Width, Height, Channels, -127, 128, 'false', 'full_domain', [], [], [], [], DLPreprocessParam)
preprocess_dl_dataset (DLDataset, 'data', DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
* Inspect several randomly selected preprocessed DLSamples visually.
create_dict (WindowDict)
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
find_dl_samples (DatasetSamples, 'split', 'train', 'match', TrainSampleIndices)
for Index := 0 to 9 by 1
    SampleIndex := TrainSampleIndices[round(rand(1) * (|TrainSampleIndices| - 1))]
    read_dl_samples (DLDataset, SampleIndex, DLSample)
    dev_display_dl_data (DLSample, [], DLDataset, ['segmentation_image_ground_truth','segmentation_weight_map'], [], WindowDict)
    wait_seconds(1)
endfor
get_dict_tuple (WindowDict, 'segmentation_image_ground_truth', WindowHandles)
dump_window(WindowHandles[0], 'png', 'label')
dev_close_window_dict (WindowDict)
*2.) TRAIN
set_dl_model_param (DLModelHandle, 'image_dimensions', [Width, Height, Channels])
set_dl_model_param_based_on_preprocessing (DLModelHandle, DLPreprocessParam, ClassIDs)
if (GpuId > 0)
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
tuple_min2(round(|Samples|*TrainingPercent/100/2), 16, BatchSize)
set_dl_model_param_max_gpu_batch_size (DLModelHandle, BatchSize)
set_dl_model_param (DLModelHandle, 'learning_rate', LearningRate)
*set_dl_model_param (DLModelHandle, 'momentum', Momentum)
*set_dl_model_param (DLModelHandle, 'weight_prior', WeightPrior)
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
create_dl_train_param (DLModelHandle, NumEpochs, 1, 'true', 42, [], [], TrainParam)
*验证样本数量太少不足以支持一行四列显示
if (|Samples| < 20)
    get_dict_tuple(TrainParam, 'display_param', DisplayParam)
    set_dict_tuple(DisplayParam, 'num_images', 1)
    set_dict_tuple(TrainParam, 'display_param', DisplayParam)
endif
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
dev_get_window(WindowHandle)
dump_window(WindowHandle, 'png', 'train_1')
dev_close_window ()
dev_get_window(WindowHandle)
dump_window(WindowHandle, 'png', 'train_2')
*3.) EVALUATE
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'show_progress', true)
set_dict_tuple (GenParamEval, 'measures', ['mean_iou','pixel_accuracy','class_pixel_accuracy','pixel_confusion_matrix'])
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'validation', GenParamEval, EvaluationResult, EvalParams)
create_dict (GenParamEvalDisplay)
set_dict_tuple (GenParamEvalDisplay, 'display_mode', ['measures','absolute_confusion_matrix'])
dev_display_segmentation_evaluation (EvaluationResult, EvalParams, GenParamEvalDisplay, WindowDict)
get_dict_tuple (WindowDict, 'window_measures', WindowHandle)
dump_window(WindowHandle, 'png', 'measures')
get_dict_tuple (WindowDict, 'window_confusion_absolute', WindowHandle)
dump_window(WindowHandle, 'png', 'matrix')

二 推理

*推理程序:调用model_best.hdl推理test目录下测试图像,结果放在result目录;
TestImageDir := 'test/'
ConfidenceThreshold := 0.9
dev_update_off()
dev_close_window ()
read_dl_model ('model_best.hdl', DLModelHandle)
set_dl_model_param (DLModelHandle, 'batch_size', 1)
GpuId := 0
if (GpuId > 0)
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
get_dl_model_param(DLModelHandle, 'class_ids', ClassIDs)
file_exists('classes.txt', FileExists)
if(FileExists)
    dlt_read_classnames ('classes.txt', ClassNames)
    ClassNames := ['background', ClassNames]
endif
ResultDir := 'result/'
file_exists(ResultDir, FileExists)
if(FileExists)
    remove_dir_recursively (ResultDir)
    *remove_dir_recursively (ResultDir + 'labels/')
endif
make_dir (ResultDir)
*make_dir (ResultDir + 'labels/')
list_image_files (TestImageDir, 'default', ['recursive'], ImageFiles)
read_image (Image, ImageFiles[0])
get_image_size(Image, Width, Height)
tuple_min2(Width, Width, Min)
Scale := Width / Min
dev_open_window(0, 0, Width  / Scale, Height  / Scale, 'black', WindowHandle)
dev_set_draw ('margin')
set_font(WindowHandle, 'Consolas-32')
dev_set_line_width(1)
Colors := ['red', 'green', 'blue', 'cyan', 'magenta', 'blue violet', 'firebrick', 'navy', 'yellow green', 'orange', 'forest green', 'cornflower blue', 'plum', 'tan', 'yellow', 'cadet blue', 'light blue', 'khaki']
create_dict (DLSample)
MinArea := 10
tics := []
for Index := 0 to |ImageFiles|-1 by 1
    read_image (Image, ImageFiles[Index])
    count_seconds(Start)
    zoom_image_size (Image, ImagePreprocessed, ImageDimensions[0], ImageDimensions[1], 'constant')
    convert_image_type (ImagePreprocessed, ImagePreprocessed, 'real')
    scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
    set_dict_object (ImagePreprocessed, DLSample, 'image') 
    apply_dl_model (DLModelHandle, DLSample, [], DLResult)
    get_dict_object (SegImage, DLResult, 'segmentation_image')
    get_dict_object (Confidence, DLResult, 'segmentation_confidence')
    count_seconds(End)
    tics := [tics, (End - Start)*1000]
    dev_clear_window()
    dev_display (ImagePreprocessed)
    Text := []
    for I := 1 to |ClassIDs|-1 by 1
        gen_empty_region (Region)
        threshold (SegImage, Region, I, I)
        connection (Region, ConnectedRegions)
        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', MinArea, 99999)
        union1 (SelectedRegions, Region)
        area_center (Region, Area, Row, Column)
        if (Area > 0)
            if (|ClassNames|)
                Text[|Text|] := ClassNames[I] + ': ' + Area$'6d'
            else
                Text[|Text|] := 'Class' + (I) + ': ' + Area$'6d'
            endif   
        endif
        dev_set_color(Colors[I-1])
        dev_display (Region)        
    endfor
    dev_disp_text (Text, 'window', 'top', 'left', 'black', [], [])
    parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
    dump_window (WindowHandle, 'png', ResultDir + BaseName)
    *convert_image_type (SegImage, SegImage, 'byte')
    *write_image(SegImage, 'png', 0, ResultDir + 'labels/' + BaseName)
endfor
tuple_mean(tics[1:(|tics|-1)], ImageProcTimeMs)
Text := 'infer: ' + ImageDimensions[0] + '×' + ImageDimensions[1] + '×' + ImageDimensions[2] + '×' + |ImageFiles|
Text[|Text|] := 'speed: ' + ImageProcTimeMs$'.1f' + ' ms/image' 
dev_disp_text (Text, 'window', 'center', 'left', 'blue', 'box', 'true')  
dump_window (WindowHandle, 'png', 'speed')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值