該方法屬於無監督式的深度學習方法,優點:
1 無需標注
2 只訓練正樣本
3 可以在CPU下進行訓練
4 具有較快的推斷速度
適用場景:適合缺陷較為明顯的項目
注意:設置的ImageWidth、ImageHeight ,以及自己采的圖,盡量是32的倍數
召回率(recall) == 92.1%,意味着ok圖中7.9%被預測為ng
精確率(precision) =79.8%,意味着被認為是ok的圖中有20.2%的ng圖,即ng容易被檢測成ok
主對角線數值越大越好,副對角線數值越小越好。47個OK被誤判為ng,3個ng被誤判為OK
dev_update_off () dev_close_window () set_system ('seed_rand', 25) * * *----------------------------- 0.) 樣本、保存模型路徑 -----------------------* * * 訓練只需ok文件夾,其他文件夾用於之后的評估 * * 路徑及子文件夾名 ImageDir := 'E:/整條' ImageSubDirs := ['ok','ng'] * * 缺陷區域的二值圖路徑(無) AnomalyDir := [] * * 所有樣本預處理后的保存路徑 OutputDir := ImageDir+'/anomaly_output_data' * 模型的保存路徑+模型名 ModelFileFullName := ImageDir+'/model_final.hdl' * ********************** 自己需要設定的值 ****************** * * 數據集特定的預處理 ExampleSpecificPreprocessing := true * 縮放后的大小(32的倍數) ImageWidth := 320 ImageHeight := 320 * 復雜度,越大准確率越高,訓練越耗時 Complexity := 15 * Complexity := 30 * *----------------------------- 1.) 讀取、拆分樣本集 DLDataset -----------------------* create_dict (GenParamDataset) set_dict_tuple (GenParamDataset, 'image_sub_dirs', ImageSubDirs) read_dl_dataset_anomaly (ImageDir, AnomalyDir, [], [], GenParamDataset, DLDataset) * 拆分樣本集為訓練集(60%)、驗證集(20%)、測試集(剩余的20%) split_dl_dataset (DLDataset, 60, 20, []) * * 加載預訓練模型、設置參數 read_dl_model ('initial_dl_anomaly_medium.hdl', DLModelHandle) *read_dl_model ('initial_dl_anomaly_large.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, 'runtime', 'cpu') set_dl_model_param (DLModelHandle, 'runtime', 'gpu') set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately') * 設置預處理參數,並進行預處理 create_dict (PreprocessSettings) set_dict_tuple (PreprocessSettings, 'overwrite_files', true) create_dl_preprocess_param ('anomaly_detection', ImageWidth, ImageHeight, 3, [], [], 'constant_values', 'full_domain', [], [], [], [], DLPreprocessParam) preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName) * * 獲取樣本集DLDataset中的樣本 get_dict_tuple (DLDataset, 'samples', DatasetSamples) if (ExampleSpecificPreprocessing) read_dl_samples (DLDataset, [0:|DatasetSamples| - 1], DLSampleBatch) preprocess_dl_samples_bottle(DLSampleBatch) write_dl_samples (DLDataset, [0:|DatasetSamples| - 1], DLSampleBatch, [], []) endif * * 展示10個隨機預處理后的 DLSamples create_dict (WindowDict) for Index := 0 to 9 by 1 SampleIndex := int(rand(1) * |DatasetSamples|) read_dl_samples (DLDataset, SampleIndex, DLSample) dev_display_dl_data (DLSample, [], DLDataset, 'anomaly_ground_truth', [], WindowDict) dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], []) * get_dict_tuple (WindowDict, 'anomaly_ground_truth', WindowHandles) dev_set_window (WindowHandles[0]) dev_disp_text ('Preprocessed image', 'window', 'top', 'left', 'black', [], []) * *stop () endfor dev_close_window_dict (WindowDict) * *stop () * *----------------------------- 2.) 訓練 DLDataset -----------------------* *--- 設置訓練參數 * 是否展示訓練過程 EnableDisplay := true * 設置訓練終止條件,錯誤率、次數,滿足其一則終止 ErrorThreshold := 0.001 MaxNumEpochs := 15 * 訓練集中用於訓練的樣本比 *DomainRatio := 0.25 DomainRatio := 0.75 * 正則化噪聲,使得訓練更健壯。為防止訓練失敗,可以設置大些 RegularizationNoise := 0.01 * 創建字典,並存儲鍵-值對 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, MaxNumEpochs, [], EnableDisplay, 73, 'anomaly', TrainParamAnomaly, TrainParam) *--- 開始訓練 train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos) dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], []) stop () * dev_close_window () * * 保存模型 write_dl_model (DLModelHandle, ModelFileFullName) * * *----------------------------- 3.) 評估模型(計算得到分類、分割的閾值) -----------------------* * 標准差因子(如果缺陷很小,推薦較大值) StandardDeviationFactor := 1.0 * 往字典DLModelHandle里存儲鍵-值對 set_dl_model_param (DLModelHandle, 'standard_deviation_factor', StandardDeviationFactor) * 計算閾值 create_dict (GenParamThreshold) set_dict_tuple (GenParamThreshold, 'enable_display', 'true') compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, GenParamThreshold, AnomalySegmentationThreshold, AnomalyClassificationThresholds) dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], []) stop () * dev_close_window () * * 設置評估參數,在test集上進行評估 create_dict (GenParamEvaluation) set_dict_tuple (GenParamEvaluation, 'measures', 'all') set_dict_tuple (GenParamEvaluation, 'anomaly_classification_thresholds', AnomalyClassificationThresholds) evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEvaluation, EvaluationResult, EvalParams) * * 要展示的參數 create_dict (GenParamDisplay) * 直方圖、圖例 set_dict_tuple (GenParamDisplay, 'display_mode', ['score_histogram','score_legend']) create_dict (WindowDict) dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict) dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', 'box', 'true') stop () * dev_close_window_dict (WindowDict) * * 可視化precision精確率, recall召回率, and confusion matrix set_dict_tuple (GenParamDisplay, 'display_mode', ['pie_charts_precision','pie_charts_recall','absolute_confusion_matrix']) * 展示 AnomalyClassificationThresholds 中的一個閾值(第三個) set_dict_tuple (GenParamDisplay, 'classification_threshold_index', 2) create_dict (WindowDict) dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict) dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], []) stop () * dev_close_window_dict (WindowDict) * * *----------------------------- 4.) 測試 -----------------------* *** read_dl_model(ModelFullName, DLModelHandle) ************************ 測試的樣本,隨機的10個ng圖(低於10以實際為准) *list_image_files (ImageDir + '/' + ImageSubDirs, 'default', 'recursive', ImageFiles) list_image_files (ImageDir + '/' + 'ng', 'default', 'recursive', ImageFiles) * 打亂數據集 tuple_shuffle (ImageFiles, ImageFilesShuffled) * 設置閾值(模型訓練后得到) InferenceClassificationThreshold := AnomalyClassificationThresholds[2] InferenceSegmentationThreshold := AnomalySegmentationThreshold * * 創建類別標簽字典(不起作用,但是必須有) create_dict (DLDatasetInfo) set_dict_tuple (DLDatasetInfo, 'class_names', ['ok','ng']) set_dict_tuple (DLDatasetInfo, 'class_ids', [0,1]) * 創建字典,承載窗體信息 create_dict (WindowDict) for IndexInference := 0 to min2(|ImageFilesShuffled|,10) - 1 by 1 * 讀圖 read_image (Image, ImageFilesShuffled[IndexInference]) gen_dl_samples_from_images (Image, DLSample) preprocess_dl_samples(DLSample, DLPreprocessParam) * 與訓練時相同的特定處理 if (ExampleSpecificPreprocessing) preprocess_dl_samples_bottle (DLSample) endif * apply_dl_model (DLModelHandle, DLSample, [], DLResult) threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult) * 展示結果 dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['anomaly_result','anomaly_image'], [], WindowDict) dev_disp_text ('Press F5 (continue)', 'window', 'bottom', 'center', 'black', [], []) stop () endfor * ************************ 測試的樣本,隨機的10個ok圖(低於10以實際為准) list_image_files (ImageDir + '/' + 'ok', 'default', 'recursive', ImageFiles) tuple_shuffle (ImageFiles, ImageFilesShuffled) for IndexInference := 0 to min2(|ImageFilesShuffled|,10) - 1 by 1 read_image (Image, ImageFilesShuffled[IndexInference]) gen_dl_samples_from_images (Image, DLSample) preprocess_dl_samples(DLSample, DLPreprocessParam) if (ExampleSpecificPreprocessing) preprocess_dl_samples_bottle (DLSample) endif apply_dl_model (DLModelHandle, DLSample, [], DLResult) threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult) dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['anomaly_result','anomaly_image'], [], WindowDict) dev_disp_text ('Press F5 (continue)', 'window', 'bottom', 'center', 'black', [], []) stop () endfor dev_close_window_dict (WindowDict)
如果已有模型 *.hdl,可以直接測試
* 讀取模型 read_dl_model ('E:/整條/model_final.hdl', DLModelHandle) * 設置閾值(模型訓練后得到) InferenceClassificationThreshold := 0.183618 InferenceSegmentationThreshold := 0.236205 * 用模型中已設定的尺寸縮放 get_dl_model_param (DLModelHandle, 'image_width', ImageWidth) get_dl_model_param (DLModelHandle, 'image_height', ImageHeight) create_dl_preprocess_param ('anomaly_detection', ImageWidth, ImageHeight, 3, [], [], 'constant_values', 'full_domain', [], [], [], [], DLPreprocessParam) * 創建類別標簽字典(不起作用,但是必須有) create_dict (DLDatasetInfo) set_dict_tuple (DLDatasetInfo, 'class_names', ['ok','ng']) set_dict_tuple (DLDatasetInfo, 'class_ids', ['0','1']) * 創建字典,承載窗體信息 create_dict (WindowDict) * 讀圖 list_files ('E:/整條/ng', ['files','follow_links','recursive'], ImageFiles) tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles) for Index := 0 to |ImageFiles| - 1 by 1 read_image (Image, ImageFiles[Index]) * Image Acquisition 01: Do something gen_dl_samples_from_images (Image, DLSample) preprocess_dl_samples(DLSample, DLPreprocessParam) preprocess_dl_samples_bottle (DLSample) apply_dl_model (DLModelHandle, DLSample, [], DLResult) threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult) * 展示結果 dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['anomaly_result','anomaly_image'], [], WindowDict) dev_disp_text ('Press F5 (continue)', 'window', 'bottom', 'center', 'black', [], []) stop () endfor dev_close_window_dict (WindowDict)