Halcon 进行基于深度学习的异常检测

2024-01-11 09:19:21 浏览数 (1)

Halcon 在 2022年5月对外宣布更新了异常检测深度学习算法,本文记录使用方法。

简介

Halcon 深度学习异常检测可以用若干没有瑕疵的数据训练模型,用于检测出现问题 (异常) 的数据。

核心流程

准备数据

需要准备一个包含数据的字典列表,每个字典表示一张数据图像,字典中 image 字段下存放图像,其他字段记录该图像配套的信息

过程中对图像做需要的预处理

数据集建议使用 MVTec 自己的异常检测数据集

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116

list_image_files (train_dir, 'default', [], ImageFiles)train_samples := []image_width := image_size0image_height := image_size1for Index := 0 to |ImageFiles|-1 by 1 image_path := ImageFilesIndex create_dict (sample_info) set_dict_tuple (sample_info, 'image_id', Index) set_dict_tuple (sample_info, 'anomaly_label', 'ok') set_dict_tuple (sample_info, 'anomaly_label_id', 0) read_image (Image, image_path) convert_image_type (Image, Image, 'real') if (Index < 1) image_sum := Image else add_image (Image, image_sum, image_sum, 1, 0) endifendforsub_image (image_sum, image_sum, Imagezero, 1, |ImageFiles|)div_image (image_sum, Imagezero, ImageResult, 1, 0)count_channels (ImageResult, Channels)* 初始化均值和方差列表MeanValues := []DeviationValues := []if (Channels == 3)* 处理彩色图像decompose3(ImageResult, Image1, Image2, Image3)else Image1 := ImageResult Image2 := ImageResult Image3 := ImageResultendifgen_empty_obj (EmptyObject)concat_obj (EmptyObject, Image1, EmptyObject)concat_obj (EmptyObject, Image2, EmptyObject)concat_obj (EmptyObject, Image3, EmptyObject)* 计算每个通道的均值和标准差for ChannelIndex := 1 to 3 by 1 select_obj (EmptyObject, ObjectSelected, ChannelIndex) intensity(ObjectSelected, ObjectSelected, Mean, Deviation) MeanValues := MeanValues, Mean DeviationValues := DeviationValues, Deviationendforcreate_dict (data_param)set_dict_tuple (data_param, 'DeviationValues', DeviationValues)set_dict_tuple (data_param, 'MeanValues', MeanValues)for Index := 0 to |ImageFiles|-1 by 1 image_path := ImageFilesIndex create_dict (sample_info) set_dict_tuple (sample_info, 'image_id', Index) set_dict_tuple (sample_info, 'anomaly_label', 'ok') set_dict_tuple (sample_info, 'anomaly_label_id', 0) read_image (Image, image_path) convert_image_type (Image, Image, 'real') zoom_image_size (Image, Image, image_width, image_height, 'bilinear') count_channels (Image, Channels) if (Channels == 3) * 处理彩色图像 decompose3(Image, Image1, Image2, Image3) else Image1 := Image Image2 := Image Image3 := Image endif gen_empty_obj (ImageScaled) Scale1 := 1.0 / DeviationValues0 Shift1 := - Scale1 * MeanValues0 scale_image (Image1, ImageScaled1, Scale1, Shift1) append_channel (ImageScaled, ImageScaled1, ImageScaled) Scale2 := 1.0 / DeviationValues1 Shift2 := - Scale1 * MeanValues1 scale_image (Image2, ImageScaled2, Scale2, Shift2) append_channel (ImageScaled, ImageScaled2, ImageScaled) Scale3 := 1.0 / DeviationValues2 Shift3 := - Scale3 * MeanValues2 scale_image (Image3, ImageScaled3, Scale3, Shift3) append_channel (ImageScaled, ImageScaled3, ImageScaled) gen_empty_obj (ImagesScaled) concat_obj (ImagesScaled, ImageScaled, ImagesScaled) set_dict_object (ImagesScaled, sample_info, 'image') train_samples :=train_samples, sample_infoendforreturn ()

训练参数

需要配置训练时的部分参数,主要包括提前停止阈值、数据使用率、训练轮数、正则项水平等

1234

TrainParamAnomaly := dict{error_threshold: 0.001}TrainParamAnomaly.MaxNumEpochs := 30TrainParamAnomaly.domain_ratio := 0.25TrainParamAnomaly.regularization_noise := 0.01

初始化模型

模型初始化,使用 Halcon 的模型加载算子加载预训练的异常检测模型

1234567891011

ImageWidth := 256ImageHeight := 256Complexity := 15query_available_dl_devices ('runtime', 'id', 'gpu', 0, DLDevice)read_dl_model ('initial_dl_anomaly_medium.hdl', DLModelHandle)set_dl_model_param (DLModelHandle, 'image_width', ImageWidth)set_dl_model_param (DLModelHandle, 'image_height', ImageHeight)set_dl_model_param (DLModelHandle, 'complexity', Complexity)set_dl_model_param (DLModelHandle, 'device', DLDevice)

训练模型

将上述数据用训练算子一起训练

1

train_dl_model_anomaly_dataset (DLModelHandle, train_samples, TrainParamAnomaly, DLTrainResult)

模型保存

保存训练完成的模型

12

model_file_name := result_home_dir '/model.hdl'write_dl_model (DLModelHandle, model_file_name)

模型推断

加载测试数据,与训练好的模型一起送入推断算子

1

apply_dl_model (DLModelHandle, test_dataset, [], DLResultBatch)

参考资料

  • https://www.mvtec.com/cn/services-support/videos-tutorials/single-video/deep-learning-based-anomaly-detection-with-mvtec-halcon

文章链接: https://cloud.tencent.com/developer/article/2378396

0 人点赞