halcon深度学习-异常检测

halcon深度学习-异常检测

一 训练

*使用深度学习异常检测功能步骤:
*1.图像分文件夹:在images目录下ok文件夹里放入20-100张正样本图像,如果有负样本图像,放入nok文件夹;
*  测试图像放test目录下;推荐png或bmp格式,不建议使用jpg格式;
*2.训练所得模型存为model_best.hdl;
*说明:
*1.异常检测使用CPU进行训练和推理,训练时长可能需要几十分钟到几小时不等,请耐心等待;
*如有问题,联系lidp@daheng-imaging.com
*影响训练效果和时长的参数
*网络类型
Model := 'initial_dl_anomaly_medium.hdl'
Model := 'initial_dl_anomaly_large.hdl'
*Model := 'model_best.hdl'
GpuId := 0
*训练迭代次数
NumEpochs := int(10*10)
*图像最大宽度
MaxWidth := 1024*1
*训练停止的误差阈值
ErrorThreshold := 0.01*0.1
*每幅图的权重,范围[0,1]
DomainRatio := 0.25
RegularizationNoise := 0.1
Complexity := 15*2
dev_update_off ()
dev_close_window ()
set_system ('seed_rand', 73)
DatasetOverwrite := true
ImageDir := 'images'
read_dl_dataset_anomaly (ImageDir, [], [], [], [], DLDataset)
split_dl_dataset (DLDataset, 70, 15, [])
get_dict_tuple (DLDataset, 'samples', Samples)
get_dict_tuple (Samples[0], 'image_file_name', ImageFileName)
read_image (Image, ImageDir + '/' + ImageFileName)
get_image_size (Image, Width, Height)
count_channels (Image, Channels)
tuple_min2(Width, MaxWidth*1.0, Min)
Zoom := Min / Width
Divisor := 32
Width  := int(int((Width * Zoom) / Divisor) * Divisor)
Height := int(int((Height * Zoom) / Divisor) * Divisor)
create_dict (PreprocessSettings)
set_dict_tuple (PreprocessSettings, 'overwrite_files', DatasetOverwrite)
create_dl_preprocess_param ('anomaly_detection', Width, Height, Channels, [], [], 'constant_values', 'full_domain', [], [], [], [], DLPreprocessParam)
preprocess_dl_dataset (DLDataset, 'data', DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
* ***   2.) TRAIN   ***
read_dl_model (Model, DLModelHandle)
if (Model != 'model_best.hdl')
    set_dl_model_param (DLModelHandle, 'image_dimensions', [Width, Height, Channels])
endif
get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
set_dl_model_param (DLModelHandle, 'standard_deviation_factor', 1.0)
set_dl_model_param (DLModelHandle, 'complexity', Complexity)

get_system ('cuda_devices', Gpus)
if (|Gpus| == 0)
    set_dl_model_param (DLModelHandle, 'runtime', 'cpu')
elseif ((|Gpus| > 1) and (GpuId > 0))
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
    set_dl_model_param (DLModelHandle, 'runtime', 'gpu')
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
EnableDisplay := true
create_dict (TrainParamAnomaly)
set_dict_tuple (TrainParamAnomaly, 'regularization_noise', RegularizationNoise)
set_dict_tuple (TrainParamAnomaly, 'error_threshold', ErrorThreshold)
set_dict_tuple (TrainParamAnomaly, 'domain_ratio', DomainRatio)
create_dl_train_param (DLModelHandle, NumEpochs, [], EnableDisplay, 73, 'anomaly', TrainParamAnomaly, TrainParam)
count_seconds(Start)
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
count_seconds(End)
write_dl_model (DLModelHandle, 'model_best.hdl')

create_dict (GenParamThreshold)
set_dict_tuple (GenParamThreshold, 'enable_display', 'true')
compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, GenParamThreshold, AnomalySegmentationThreshold, AnomalyClassificationThresholds)

*compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, [], AnomalySegmentationThreshold, AnomalyClassificationThresholds)
Text := Model
Text[|Text|] := ImageDimensions[0] + '×' + ImageDimensions[1] + '×' + ImageDimensions[2] + '×' + |Samples| + '×' + NumEpochs + ', '  + ((End - Start)/3600.0)$'.1f' + 'h'
Text[|Text|] := 'ClassThd: ' + AnomalyClassificationThresholds[0]$'.3f' + ', SegThd: ' + AnomalySegmentationThreshold$'.3f'
dev_disp_text (Text, 'window', 'bottom', 'left', 'blue', 'box', 'true')
dev_get_window (WindowHandle)
dump_window(WindowHandle, 'png', 'info')
* ***   3.) EVALUATE   ***
create_dict (GenParamEvaluation)
set_dict_tuple (GenParamEvaluation, 'measures', 'all')
set_dict_tuple (GenParamEvaluation, 'anomaly_classification_thresholds', AnomalyClassificationThresholds)
evaluate_dl_model (DLDataset, DLModelHandle, 'split', ['validation','test'], GenParamEvaluation, EvaluationResult, EvalParams)
create_dict (GenParamDisplay)
create_dict (WindowDict)
set_dict_tuple (GenParamDisplay, 'display_mode', [])
set_dict_tuple (GenParamDisplay, 'display_mode', ['score_histogram', 'pie_charts_precision','pie_charts_recall','absolute_confusion_matrix'])
set_dict_tuple (GenParamDisplay, 'classification_threshold_index', 2)
dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict)
get_dict_tuple (WindowDict, 'window_score_histogram', WindowHandle)
dump_window(WindowHandle, 'png', 'histogram')  
get_dict_tuple (WindowDict, 'window_pie_charts_precision', WindowHandle)
dump_window(WindowHandle, 'png', 'precision')  
get_dict_tuple (WindowDict, 'window_pie_charts_recall', WindowHandle)
dump_window(WindowHandle, 'png', 'recall') 
get_dict_tuple (WindowDict, 'window_absolute_confusion_matrix', WindowHandle)
dump_window(WindowHandle, 'png', 'matrix') 

推理

*推理程序:推理test目录下图像,结果放在result目录;
*影响推理结果和速度的参数
*异常分类阈值
AnomalyClassificationThreshold := 0.182
*异常区域阈值
AnomalySegmentationThreshold   := 0.286
MinAnomalyRegionArea := 100
GpuId := 0
Runtime := 'gpu'
dev_update_off ()
dev_close_window ()
read_dl_model ('model_best.hdl', DLModelHandle)
get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
set_dl_model_param (DLModelHandle, 'runtime', Runtime)
if((Runtime == 'gpu') and (GpuId > 0))
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
ResultDir := 'result/'
file_exists(ResultDir, FileExists)
if (FileExists)
    remove_dir_recursively(ResultDir)
endif
make_dir (ResultDir)
tuple_max2(600.0/ImageDimensions[0], 600.0/ImageDimensions[1], Zoom)
dev_open_window(0, 0, ImageDimensions[0]*Zoom, ImageDimensions[1]*Zoom, 'black', WindowHandle)
dev_set_draw ('margin')
set_font(WindowHandle, 'default-24')
create_dict (DLSample)
MeanValues := [123.675,116.28,103.53]
DeviationValues := [58.395,57.12,57.375]
list_image_files ('test', 'default', 'recursive', ImageFiles)
tics := []
for Index := 0 to |ImageFiles|-1 by 1
    parse_filename (ImageFiles[Index], BaseName, Extension, Directory) 
    read_image (Image, ImageFiles[Index])
    count_seconds(Start)
    zoom_image_size (Image, Image, ImageDimensions[0], ImageDimensions[1], 'constant')
    convert_image_type (Image, ImagePreprocessed, 'real')
    gen_empty_obj (ImageNormChannels)
    for IndexChannel := 1 to ImageDimensions[2] by 1
        access_channel (ImagePreprocessed, Channel, IndexChannel)
        scale_image (Channel, ImageNormChannel, 1 / real(DeviationValues[IndexChannel - 1]), -real(MeanValues[IndexChannel - 1]) / DeviationValues[IndexChannel - 1])
        append_channel (ImageNormChannels, ImageNormChannel, ImageNormChannels)
    endfor
    set_dict_object (ImageNormChannels, DLSample, 'image')    
    apply_dl_model (DLModelHandle, DLSample, [], DLResult)
    get_dict_object (AnomalyImage, DLResult, 'anomaly_image')
    get_dict_tuple (DLResult, 'anomaly_score', AnomalyScore)
    threshold (AnomalyImage, AnomalyRegion, AnomalySegmentationThreshold, 1.0)
    connection (AnomalyRegion, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', MinAnomalyRegionArea, ImageDimensions[0]*ImageDimensions[1]*0.5)
    union1 (SelectedRegions, AnomalyRegion)
    count_seconds(End)
    tics := [tics, (End - Start)*1000]
    dev_display (Image)
    dev_display (AnomalyRegion)
    Text := []
    if(AnomalyScore >AnomalyClassificationThreshold)
        Text[|Text|] := '异常: ' + AnomalyScore$'.2f'
        dev_disp_text (Text, 'window', 'top', 'left', 'red', 'box', 'true') 
    else
        Text[|Text|] := '正常: ' + AnomalyScore$'.2f'
        dev_disp_text (Text, 'window', 'top', 'left', 'green', 'box', 'true') 
    endif
    dump_window(WindowHandle, 'png', ResultDir + 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、付费专栏及课程。

余额充值