halcon深度学习-图像检测

halcon深度学习-图像检测

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

一 训练

*使用深度学习检测功能步骤:
*1.把原始图像放到images目录下,使用标注工具deepLearningTool进行标注,标注完成导出成dataset.hdict文件,放到当前目录下;
*2.运行训练程序train.hdev进行训练,结果存为model_best.hdl,过程存图;

*影响训练效果和时长的参数
*网络类型
Model := 'pretrained_dl_classifier_compact.hdl'
*Model := 'pretrained_dl_classifier_enhanced.hdl'
*Model := 'pretrained_dl_classifier_resnet50.hdl'
*Model := 'model_best.hdl'
*训练迭代次数
NumEpochs := 100*3.0
*数据扩充比例
AugmentationPercentage := 100*0
*数据扩充是否支持水平、垂直镜像,可设值'r''c'、'rc'、'off'
AugmentationMirror := 'rc'
*数据扩充旋转角度范围,可设值如0,90,180
AugmentationRotateRange := 180
*最大图像宽度
MaxWidth := 640.0*3
*显卡序号
GpuId := 1
*初始学习率
InitialLearningRate := 0.001*1
*最多检测物体数量
MaxNumDetections := 100*1
*最大重叠度
*MaxOverlap := 0.2
IgnoreDirection := 'false'
ClassIdsNoOrientation := []
DatasetOverwrite := true
NumStages := 5
tuple_gen_sequence (1, NumStages-1, 1, Seq)
ChangeLearningRateEpochs := int(Seq * (NumEpochs/NumStages))
tuple_pow (0.5, Seq, Pow)
ChangeLearningRateValues := InitialLearningRate * Pow
Momentum := 0.99*1
ReCreateDataset := 1
TrainingPercent   := 80
ValidationPercent := 100-TrainingPercent
dev_update_off ()
dev_close_window ()
set_system ('seed_rand', 42)
read_dict('dataset.hdict',[], [], DLDataset)
split_dl_dataset (DLDataset, TrainingPercent, ValidationPercent, [])
create_dict (GenParam)
set_dict_tuple (GenParam, 'split', 'train')
set_dict_tuple (DLDataset, 'image_dir', 'images/')
get_dict_tuple (DLDataset, 'image_dir', ImageDir)
get_dict_tuple (DLDataset, 'samples', Samples)
get_dict_tuple (Samples[0], 'image_file_name', ImageName)
read_image (Image, ImageDir + ImageName)
*get_dict_tuple (DLDataset, 'class_ids', ClassIDs)
*get_dict_tuple (DLDataset, 'class_names', ClassNames)
if(Model == 'model_best.hdl')
    read_dl_model(Model, DLModelHandle)
else
    get_image_size(Image, Width, Height)
    if (Width > MaxWidth)
        tuple_min2(Width, MaxWidth*1.0, Min)
        Scale := Width / Min
    else
        Scale := 1.0
    endif
    Divisor := 64
    Width  := int(int((Width / Scale) / Divisor) * Divisor)
    Height := int(int((Height / Scale) / Divisor) * Divisor)
    create_dict (GenParam)
    set_dict_tuple (GenParam, 'split', 'train')

    determine_dl_model_detection_param (DLDataset,  Width,  Height, GenParam, DLModelDetectionParam)
    *set_dict_tuple (DLModelDetectionParam, 'min_level', MinLevel)
    set_dict_tuple (DLModelDetectionParam, 'max_num_detections', MaxNumDetections)
    set_dict_tuple (DLModelDetectionParam, 'capacity', 'high')
    get_dict_tuple (DLDataset, 'class_ids', ClassIDs)
    set_dict_tuple (DLModelDetectionParam, 'class_ids', ClassIDs)
    count_channels(Image, Channels)
    set_dict_tuple (DLModelDetectionParam, 'image_dimensions', [Width, Height, Channels])
    get_dict_param (Samples[0], 'key_exists', 'bbox_phi', Exist)
    if (Exist)
        set_dict_tuple (DLModelDetectionParam, 'instance_type', 'rectangle2')
        *set_dict_tuple (DLModelDetectionParam, 'anchor_angles', rad(AnchorAngles))
        set_dict_tuple (DLModelDetectionParam, 'ignore_direction', IgnoreDirection)
        set_dict_tuple (DLModelDetectionParam, 'class_ids_no_orientation', ClassIdsNoOrientation)
    endif
    create_dl_model_detection (Model, |ClassIDs|, DLModelDetectionParam, DLModelHandle)
endif
get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
dev_open_window(0, 0, 600, ImageDimensions[1] * 600 / ImageDimensions[0], 'black', WindowHandle)
set_font(WindowHandle, 'default-20')
create_dict (PreprocessSettings)
set_dict_tuple (PreprocessSettings, 'overwrite_files', DatasetOverwrite)
create_dl_preprocess_param_from_model (DLModelHandle, 'false', 'full_domain', [], [], [], DLPreprocessParam)
if (ReCreateDataset)
    file_exists ('data', FileExists) 
    if (FileExists)
        remove_dir_recursively('data')
    endif
    preprocess_dl_dataset (DLDataset, 'data', DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
else
    set_dict_tuple (DLDataset, 'preprocess_param', DLPreprocessParam)
    set_dict_tuple (DLDataset, 'dlsample_dir', 'data/samples')
endif
create_dict (WindowDict)
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
for Index := 0 to 9 by 1
    SampleIndex := round(rand(1) * (|DatasetSamples| - 1))
    read_dl_samples (DLDataset, SampleIndex, DLSample)
    dev_display_dl_data (DLSample, [], DLDataset, 'bbox_ground_truth', [], WindowDict)
    wait_seconds(1)
endfor
get_dict_tuple (WindowDict, 'bbox_ground_truth', WindowHandles)
dump_window(WindowHandles[0], 'png', 'label')
dev_close_window_dict (WindowDict)
*2.) TRAIN   ***
GenParamName := []
GenParamValue := []
create_dict (AugmentationParam)
set_dict_tuple (AugmentationParam, 'augmentation_percentage', AugmentationPercentage)
set_dict_tuple (AugmentationParam, 'rotate', AugmentationRotateRange)
set_dict_tuple (AugmentationParam, 'mirror', AugmentationMirror)
GenParamName := [GenParamName,'augment']
GenParamValue := [GenParamValue,AugmentationParam]
if (|ChangeLearningRateEpochs| > 0)
    create_dict (ChangeStrategy)
    set_dict_tuple (ChangeStrategy, 'model_param', 'learning_rate')
    set_dict_tuple (ChangeStrategy, 'initial_value', InitialLearningRate)
    set_dict_tuple (ChangeStrategy, 'epochs', ChangeLearningRateEpochs)
    set_dict_tuple (ChangeStrategy, 'values', ChangeLearningRateValues)
    *set_dict_tuple (ChangeStrategy, 'scale_momentum_threshold', 1)
    GenParamName := [GenParamName,'change']
    GenParamValue := [GenParamValue,ChangeStrategy]
endif
set_dl_model_param (DLModelHandle, 'momentum', Momentum)
set_dl_model_param (DLModelHandle, 'weight_prior', 0.00001)
get_system ('cuda_devices', Gpus)
if ((|Gpus| > 1) and (GpuId > 0))
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
tuple_min2(int(floor(|Samples|*TrainingPercent/100/2)), 16, BatchSize)
set_dl_model_param (DLModelHandle, 'batch_size', 1)
set_dl_model_param (DLModelHandle, 'batch_size_multiplier', 1)
set_dl_model_param_max_gpu_batch_size (DLModelHandle, BatchSize)
get_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
tuple_max2 (8 / BatchSize, 1, BatchSizeMultiplier)
set_dl_model_param (DLModelHandle, 'batch_size_multiplier', BatchSizeMultiplier)
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
create_dl_train_param (DLModelHandle, NumEpochs, 1, 'true', 42, GenParamName, GenParamValue, TrainParam)
get_dl_model_param (DLModelHandle, 'image_dimensions', ImageDimensions)
zoom_image_size (Image, ImagePreprocessed, ImageDimensions[0], ImageDimensions[1], 'constant')
dev_display (ImagePreprocessed)
Text := Model
Text[|Text|] := |Samples| + ', ' + TrainingPercent + ':' + ValidationPercent + ', ' + ImageDimensions[0] + '×' + ImageDimensions[1] + '×' + ImageDimensions[2]
Text[|Text|] := 'gpu' + GpuId + ': ' + BatchSize + ', ' + BatchSizeMultiplier + ', ' + NumEpochs + ', ' + InitialLearningRate + ', ' + Momentum
dev_disp_text (Text, 'window', 'top', 'left', 'green', 'box', 'false')
dev_get_window (WindowHandle)  
dump_window(WindowHandle, 'png', 'train_1')
dev_close_window ()
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_2')
dev_close_window ()
dev_get_window(WindowHandle)
dump_window(WindowHandle, 'png', 'train_3')
*3.) EVALUATE   ***
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'detailed_evaluation', true)
set_dict_tuple (GenParamEval, 'show_progress', true)
set_dict_tuple (GenParamEval, 'measures', 'all')
set_dict_tuple (GenParamEval, 'iou_threshold', [0.5,0.7])
set_dl_model_param (DLModelHandle, 'min_confidence', 0.5)
set_dl_model_param (DLModelHandle, 'max_overlap', 0.2)
set_dl_model_param (DLModelHandle, 'max_overlap_class_agnostic', 0.3)
evaluate_dl_model (DLDataset, DLModelHandle, 'split', ['validation','test'], GenParamEval, EvaluationResult, EvalParams)
create_dict (DisplayMode)
set_dict_tuple (DisplayMode, 'display_mode', ['pie_charts_precision','pie_charts_recall','absolute_confusion_matrix'])
create_dict (WindowDict)
dev_display_detection_detailed_evaluation (EvaluationResult, EvalParams, DisplayMode, WindowDict)
get_dict_tuple (WindowDict, 'window_pie_chart_precision', WindowHandle)
dump_window(WindowHandle, 'png', 'precision')
get_dict_tuple (WindowDict, 'window_pie_chart_recall', WindowHandle)
dump_window(WindowHandle, 'png', 'recall')
get_dict_tuple (WindowDict, 'window_absolute_confusion_matrix', WindowHandle)
dump_window(WindowHandle, 'png', 'matrix')

二 推理

*推理程序:调用model_best.hdl推理test目录下测试图像,结果放在result目录;
TestImageDir := 'test/'
dev_update_off()
dev_close_window ()
read_dl_model ('model_best.hdl', DLModelHandle)
*设置置信度阈值
set_dl_model_param (DLModelHandle, 'min_confidence', 0.2)
*设置单幅图中物体最大数量
set_dl_model_param (DLModelHandle, 'max_num_detections', 10*50)
*设置重叠程度
set_dl_model_param (DLModelHandle, 'max_overlap', 0.5)
*设置不同类物体间的重叠程度
set_dl_model_param (DLModelHandle, 'max_overlap_class_agnostic', 0.5)
set_dl_model_param (DLModelHandle, 'batch_size', 1)
set_dl_model_param (DLModelHandle, 'batch_size_multiplier', 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)
get_dl_model_param(DLModelHandle, 'instance_type', InstanceType)
Dict  := 'dataset.hdict'
file_exists(Dict, FileExists)
if(FileExists)
    read_dict(Dict,[], [], DLDataset)
    get_dict_tuple (DLDataset, 'class_names', ClassNames)
else
    ClassNames := ClassIDs
endif
ResultDir := 'result/'
file_exists(ResultDir, FileExists)
if (FileExists)
    remove_dir_recursively(ResultDir)
endif
make_dir (ResultDir)
create_dict (DLSample)
list_image_files (TestImageDir, 'default', ['recursive'], ImageFiles)
read_image (Image, ImageFiles[0])
get_image_size(Image, Width, Height)
count_channels(Image, Channels)
tuple_max2(Width, Height, Max)
if (Height > Height)
    WndWidth := Width * 600 / Max
    WndHeight := Height * 600 / Max
else
    WndWidth := Width
    WndHeight := Height
endif
dev_open_window(0, 0, WndWidth, WndHeight, 'black', WindowHandle)
set_font(WindowHandle, 'Consolas-32')
dev_set_draw ('margin')
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']
tics := []
for Index := 0 to |ImageFiles|-1 by 1
    read_image (Image, ImageFiles[Index])
    count_seconds(Start)
    zoom_image_size (Image, Image, ImageDimensions[0], ImageDimensions[1], 'constant')
    convert_image_type (Image, ImagePreprocessed, 'real')
    scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
    set_dict_object (ImagePreprocessed, DLSample, 'image')
    apply_dl_model (DLModelHandle, DLSample, [], DLResult)
    count_seconds(End)
    tics := [tics, (End - Start)*1000]
    get_dict_tuple (DLResult, 'bbox_class_id', Bbox_ids)
    get_dict_tuple (DLResult, 'bbox_confidence', Bbox_scores)
    if(|Bbox_ids|)
        if (InstanceType == 'rectangle2')
            get_dict_tuple (DLResult, 'bbox_row', Bbox_rows)
            get_dict_tuple (DLResult, 'bbox_col', Bbox_cols)
            get_dict_tuple (DLResult, 'bbox_phi', Bbox_phis)
            get_dict_tuple (DLResult, 'bbox_length1', Bbox_len1s)
            get_dict_tuple (DLResult, 'bbox_length2', Bbox_len2s)
            gen_rectangle2 (Rectangle, Bbox_rows, Bbox_cols, Bbox_phis, Bbox_len1s, Bbox_len2s)
            Bbox_row1s := Bbox_rows
            Bbox_col1s := Bbox_cols
        else
            get_dict_tuple (DLResult, 'bbox_row1', Bbox_row1s)
            get_dict_tuple (DLResult, 'bbox_col1', Bbox_col1s)
            get_dict_tuple (DLResult, 'bbox_row2', Bbox_row2s)
            get_dict_tuple (DLResult, 'bbox_col2', Bbox_col2s)
            gen_rectangle1(Rectangle, Bbox_row1s, Bbox_col1s, Bbox_row2s, Bbox_col2s)
        endif
        dev_display(Image)
        Text := []
        tuple_max (Bbox_ids, Max)
        for IndexID := 0 to Max by 1
            tuple_find (Bbox_ids, IndexID, Indices)
            if (Indices >= 0)
                dev_set_color(Colors[IndexID])
                for IndexObj := 0 to |Indices|-1 by 1
                    select_obj (Rectangle, ObjectSelected, Indices[IndexObj]+1)
                    dev_display(ObjectSelected)
                    set_tposition (WindowHandle, Bbox_row1s[Indices[IndexObj]]-20, Bbox_col1s[Indices[IndexObj]])
                    write_string(WindowHandle, ClassNames[Bbox_ids[Indices[IndexObj]]-ClassIDs[0]] + ' ' + Bbox_scores[Indices[IndexObj]]$'.2f')
                endfor
                Text[|Text|] := ClassNames[Bbox_ids[Indices[0]]-ClassIDs[0]] + ': ' + |Indices|
            endif            
        endfor
        dev_disp_text (Text, 'window', 'top', 'left', 'blue', 'box', 'true') 
        parse_filename (ImageFiles[Index], BaseName, Extension, Directory)      
        *write_dict(DLResult, ResultDir + BaseName, [], [])
        dump_window(WindowHandle, 'png', ResultDir + BaseName)
    endif
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、付费专栏及课程。

余额充值