*
* Deep learning anomaly detection example.
*
* This example demonstrates the general workflow for anomaly detection
* based on deep learning.
*
* The workflow typically consists of the following steps:
* 1. Dataset preprocessing.
* 2. Training of the model on anomaly-free ('ok') images.
* 3. Threshold estimation and evaluation of the trained model.
* 4. Inference on new images.
*
dev_update_off ()
dev_close_window ()
set_system ('seed_rand', 73)
*
*
* *** 0.) SET INPUT/OUTPUT PATHS ***
*
* Set path to data.
*get_image_dir (HalconImages)
HalconImages := 'C:/coco/mvtec_anomaly_detection/tile'
*
* The base directory ImageDir has to contain subdirectories of images.
* One of these subdirectories must be named 'good' or 'ok'.
* For training, only images in this subdirectory are used. Images in
* subdirectories with name other than 'good' or 'ok' are considered
* to be anomalous. They can be used for qualitative evaluation of the
* trained model.
*
* Note: You can immediately train an anomaly detection model on your own
* data as long as your anomaly-free images are located in a subdirectory
* called 'good' or 'ok'.
ImageDir := HalconImages + '/test'
ImageSubDirs := ['good', 'crack', 'glue_strip', 'gray_stroke', 'oil', 'rough']
*
* If ground truth anomaly regions are available, the directory containing
* them is specified by AnomalyLabelsDir.
* Note that no ground truth annotations are required for training.
* If none are available, set AnomalyLabelsDir to [].
AnomalyLabelsDir := HalconImages + '/ground_truth'
*
* Directory in which the preprocessed data will be stored.
OutputDir := './anomaly_tile_data'
*
* If ExampleSpecificPreprocessing is set to true, the data will be
* preprocessed by a procedure specifically designed for the present dataset.
ExampleSpecificPreprocessing := true
*
* Define the input image size. It should be chosen sufficiently large such
* that small defects are still visible.
* Please refer to the documentation of read_dl_model for possible
* restrictions depending on the network type.
ImageWidth := 640
ImageHeight := 640
*
* Set the complexity which describes the capability of the model to handle
* complex applications. A higher value can improve the performance but
* increases the time needed to train the model.
Complexity := 15
*
*
* *** 1.) PREPARE ***
*
* Read and preprocess an anomaly detection dataset.
* After the general preprocessing, it might be beneficial to define
* an additional preprocessing procedure that adapts the domains
* of the training images. This procedure is unique for each dataset and
* can significantly increase the performance of the resulting model.
*
* Load and split the dataset.
GenParamDataset := dict{image_sub_dirs: ImageSubDirs}
read_dl_dataset_anomaly (ImageDir, AnomalyLabelsDir, [], [], GenParamDataset, DLDataset)
*
* Note: make sure that every split contains at least one 'ok' image.
* For a dataset that contains only a small number of images
* the fraction of validation images might have to be increased.
split_dl_dataset (DLDataset, 50, 10, [])
*
* Load the anomaly detection model and set key parameters.
* For documentation see set_dl_model_param () and get_dl_model_param ().
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)
* In this example, the anomaly detection model is trained on the CPU.
query_available_dl_devices (['runtime', 'id'], ['cpu', 0], DLDevice)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
*
* Set preprocessing parameters and preprocess the dataset.
create_dl_preprocess_param_from_model (DLModelHandle, 'constant_values', 'full_domain', [], [], [], DLPreprocessParam)
PreprocessSettings := dict{overwrite_files: 'true'}
preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
*
* In this example, we perform a specific preprocessing in addition to the general one.
* Please note: If it is possible to restrict the area (ROI) in the images where defects
* can occur, we recommend to define these areas as domains. Here, this is done by the
* procedure preprocess_dl_sample_bottle.
* For your own application, you have to adapt this procedure to suit your data.
if (ExampleSpecificPreprocessing)
read_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch)
preprocess_dl_samples_bottle (DLSampleBatch)
write_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch, [], [])
endif
*
* Visually inspect ten randomly selected preprocessed DLSamples.
WindowDict := dict{}
for Index := 0 to 9 by 1
SampleIndex := int(rand(1) * |DLDataset.samples|)
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', [], [])
*
dev_set_window (WindowDict.anomaly_ground_truth[0])
dev_disp_text ('Preprocessed image', 'window', 'top', 'left', 'black', [], [])
*
stop ()
endfor
dev_close_window_dict (WindowDict)
*
stop ()
*
*
* *** 2.) TRAIN ***
*
* Create training parameters.
*
* If EnableDisplay is set to true, the training progress is displayed.
EnableDisplay := true
*
* Set a threshold for the training error and a maximum number of training epochs.
* If the training error drops below this threshold, the training is finished.
* Otherwise, the training continues until the maximum number of epochs is reached.
TrainParamAnomaly := dict{error_threshold: 0.001}
MaxNumEpochs := 30
*
* Set the domain ratio which controls the fraction of each image used for training.
* The performance of the trained model might be improved by setting a larger value.
* This, however, will increase the training time.
TrainParamAnomaly.domain_ratio := 0.25
*
* Regularization noise can help to make the training more robust.
* In case the training fails, try to set a higher value.
TrainParamAnomaly.regularization_noise := 0.01
*
create_dl_train_param (DLModelHandle, MaxNumEpochs, [], EnableDisplay, 73, 'anomaly', TrainParamAnomaly, TrainParam)
*
* The training and thus the call of train_dl_model_anomaly_dataset () is
* encapsulated in the following procedure. The training may take some time.
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 ()
*
*
* *** 3.) EVALUATE ***
*
* Calculate classification and segmentation thresholds and evaluate the
* performance of the trained model.
*
* Set the factor used to calculate the anomaly score. See the documentation of
* get_dl_model_param () for details.
* This parameter can improve the classification between 'ok' and 'nok' images.
* In case the of small defects, for example, a larger value might be suitable.
StandardDeviationFactor := 1.0
set_dl_model_param (DLModelHandle, 'standard_deviation_factor', StandardDeviationFactor)
*
* Estimate threshold values. They are used to determine whether a pixel or image
* is regarded as anomalous. The procedure compute_dl_anomaly_thresholds returns
* possible suggestions based on the dataset used. Depending on the application,
* refining these thresholds manually may be beneficial.
* There are two different criteria to estimate the AnomalySegmentationThreshold.
* We refer to the documentation of compute_dl_anomaly_thresholds for details.
GenParamThresholds := dict{enable_display: true}
compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, GenParamThresholds, AnomalySegmentationThreshold, AnomalyClassificationThresholds)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
dev_close_window ()
*
* Set generic evaluation parameters and evaluate the model on the test split.
GenParamEvaluation := dict{measures: 'all'}
GenParamEvaluation.anomaly_classification_thresholds := AnomalyClassificationThresholds
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEvaluation, EvaluationResult, EvalParams)
*
* Visualize the histogram of the image-level anomaly scores of the test images
* and the classification thresholds used for the evaluation.
GenParamDisplay := dict{display_mode: ['score_histogram', 'score_legend']}
WindowDict := dict{}
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)
*
* Visualize several evaluation results such as precision, recall, and confusion matrix
* for a given classification threshold.
GenParamDisplay.display_mode := ['pie_charts_precision', 'pie_charts_recall', 'absolute_confusion_matrix']
* Select evaluation results for one threshold by its index in
* AnomalyClassificationThresholds for display. We use the last
* one of the computed AnomalyClassificationThresholds.
* Please note that you should check which AnomalyClassificationThreshold
* best fits your application.
ClassificationThresholdIndex := |AnomalyClassificationThresholds| - 1
GenParamDisplay.classification_threshold_index := ClassificationThresholdIndex
WindowDict := dict{}
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)
*
* Choose thresholds for inference and store them along with the model.
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
MetaData.anomaly_segmentation_threshold := AnomalySegmentationThreshold$'1.16e'
MetaData.anomaly_classification_threshold := AnomalyClassificationThresholds[ClassificationThresholdIndex]$'1.16e'
set_dl_model_param (DLModelHandle, 'meta_data', MetaData)
*
* Store the trained model.
write_dl_model (DLModelHandle, 'model_final.hdl')
*
*
* *** 4.) INFER ***
*
* To demonstrate the inference steps, we apply the
* trained model to some randomly chosen example images.
list_image_files (ImageDir + '/' + ImageSubDirs, 'default', 'recursive', ImageFiles)
tuple_shuffle (ImageFiles, ImageFilesShuffled)
*
* Get thresholds for inference. These have been stored along with
* the model in the meta data above.
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
InferenceClassificationThreshold := number(MetaData.anomaly_classification_threshold)
InferenceSegmentationThreshold := number(MetaData.anomaly_segmentation_threshold)
*
* Create dictionary with dataset parameters used for display.
DLDatasetInfo := dict{class_names: ['ok', 'nok'], class_ids: [0, 1]}
*
WindowDict := dict{}
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)
*
* Use the same dataset specific preprocessing as for training.
if (ExampleSpecificPreprocessing)
preprocess_dl_samples_bottle (DLSample)
endif
*
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
*
* Apply thresholds to classify regions and the entire image.
threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult)
*
* Display the inference result.
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)
*
* Deep learning anomaly detection example.
*
* This example demonstrates the general workflow for anomaly detection
* based on deep learning.
*
* The workflow typically consists of the following steps:
* 1. Dataset preprocessing.
* 2. Training of the model on anomaly-free ('ok') images.
* 3. Threshold estimation and evaluation of the trained model.
* 4. Inference on new images.
*
dev_update_off ()
dev_close_window ()
set_system ('seed_rand', 73)
*
*
* *** 0.) SET INPUT/OUTPUT PATHS ***
*
* Set path to data.
get_image_dir (HalconImages)
*
* The base directory ImageDir has to contain subdirectories of images.
* One of these subdirectories must be named 'good' or 'ok'.
* For training, only images in this subdirectory are used. Images in
* subdirectories with name other than 'good' or 'ok' are considered
* to be anomalous. They can be used for qualitative evaluation of the
* trained model.
*
* Note: You can immediately train an anomaly detection model on your own
* data as long as your anomaly-free images are located in a subdirectory
* called 'good' or 'ok'.
ImageDir := HalconImages + '/bottles'
ImageSubDirs := ['good', 'broken_large', 'broken_small', 'contamination']
*
* If ground truth anomaly regions are available, the directory containing
* them is specified by AnomalyLabelsDir.
* Note that no ground truth annotations are required for training.
* If none are available, set AnomalyLabelsDir to [].
AnomalyLabelsDir := HalconImages + '/labels/bottles'
*
* Directory in which the preprocessed data will be stored.
OutputDir := './anomaly_bottle_data'
*
* If ExampleSpecificPreprocessing is set to true, the data will be
* preprocessed by a procedure specifically designed for the present dataset.
ExampleSpecificPreprocessing := true
*
* Define the input image size. It should be chosen sufficiently large such
* that small defects are still visible.
* Please refer to the documentation of read_dl_model for possible
* restrictions depending on the network type.
ImageWidth := 320
ImageHeight := 320
*
* Set the complexity which describes the capability of the model to handle
* complex applications. A higher value can improve the performance but
* increases the time needed to train the model.
Complexity := 15
*
*
* *** 1.) PREPARE ***
*
* Read and preprocess an anomaly detection dataset.
* After the general preprocessing, it might be beneficial to define
* an additional preprocessing procedure that adapts the domains
* of the training images. This procedure is unique for each dataset and
* can significantly increase the performance of the resulting model.
*
* Load and split the dataset.
GenParamDataset := dict{image_sub_dirs: ImageSubDirs}
read_dl_dataset_anomaly (ImageDir, AnomalyLabelsDir, [], [], GenParamDataset, DLDataset)
*
* Note: make sure that every split contains at least one 'ok' image.
* For a dataset that contains only a small number of images
* the fraction of validation images might have to be increased.
split_dl_dataset (DLDataset, 50, 10, [])
*
* Load the anomaly detection model and set key parameters.
* For documentation see set_dl_model_param () and get_dl_model_param ().
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)
* In this example, the anomaly detection model is trained on the CPU.
query_available_dl_devices (['runtime', 'id'], ['cpu', 0], DLDevice)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
*
* Set preprocessing parameters and preprocess the dataset.
create_dl_preprocess_param_from_model (DLModelHandle, 'constant_values', 'full_domain', [], [], [], DLPreprocessParam)
PreprocessSettings := dict{overwrite_files: 'true'}
preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
*
* In this example, we perform a specific preprocessing in addition to the general one.
* Please note: If it is possible to restrict the area (ROI) in the images where defects
* can occur, we recommend to define these areas as domains. Here, this is done by the
* procedure preprocess_dl_sample_bottle.
* For your own application, you have to adapt this procedure to suit your data.
if (ExampleSpecificPreprocessing)
read_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch)
preprocess_dl_samples_bottle (DLSampleBatch)
write_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch, [], [])
endif
*
* Visually inspect ten randomly selected preprocessed DLSamples.
WindowDict := dict{}
for Index := 0 to 9 by 1
SampleIndex := int(rand(1) * |DLDataset.samples|)
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', [], [])
*
dev_set_window (WindowDict.anomaly_ground_truth[0])
dev_disp_text ('Preprocessed image', 'window', 'top', 'left', 'black', [], [])
*
stop ()
endfor
dev_close_window_dict (WindowDict)
*
stop ()
*
*
* *** 2.) TRAIN ***
*
* Create training parameters.
*
* If EnableDisplay is set to true, the training progress is displayed.
EnableDisplay := true
*
* Set a threshold for the training error and a maximum number of training epochs.
* If the training error drops below this threshold, the training is finished.
* Otherwise, the training continues until the maximum number of epochs is reached.
TrainParamAnomaly := dict{error_threshold: 0.001}
MaxNumEpochs := 30
*
* Set the domain ratio which controls the fraction of each image used for training.
* The performance of the trained model might be improved by setting a larger value.
* This, however, will increase the training time.
TrainParamAnomaly.domain_ratio := 0.25
*
* Regularization noise can help to make the training more robust.
* In case the training fails, try to set a higher value.
TrainParamAnomaly.regularization_noise := 0.01
*
create_dl_train_param (DLModelHandle, MaxNumEpochs, [], EnableDisplay, 73, 'anomaly', TrainParamAnomaly, TrainParam)
*
* The training and thus the call of train_dl_model_anomaly_dataset () is
* encapsulated in the following procedure. The training may take some time.
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 ()
*
*
* *** 3.) EVALUATE ***
*
* Calculate classification and segmentation thresholds and evaluate the
* performance of the trained model.
*
* Set the factor used to calculate the anomaly score. See the documentation of
* get_dl_model_param () for details.
* This parameter can improve the classification between 'ok' and 'nok' images.
* In case the of small defects, for example, a larger value might be suitable.
StandardDeviationFactor := 1.0
set_dl_model_param (DLModelHandle, 'standard_deviation_factor', StandardDeviationFactor)
*
* Estimate threshold values. They are used to determine whether a pixel or image
* is regarded as anomalous. The procedure compute_dl_anomaly_thresholds returns
* possible suggestions based on the dataset used. Depending on the application,
* refining these thresholds manually may be beneficial.
* There are two different criteria to estimate the AnomalySegmentationThreshold.
* We refer to the documentation of compute_dl_anomaly_thresholds for details.
GenParamThresholds := dict{enable_display: true}
compute_dl_anomaly_thresholds (DLModelHandle, DLDataset, GenParamThresholds, AnomalySegmentationThreshold, AnomalyClassificationThresholds)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
dev_close_window ()
*
* Set generic evaluation parameters and evaluate the model on the test split.
GenParamEvaluation := dict{measures: 'all'}
GenParamEvaluation.anomaly_classification_thresholds := AnomalyClassificationThresholds
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEvaluation, EvaluationResult, EvalParams)
*
* Visualize the histogram of the image-level anomaly scores of the test images
* and the classification thresholds used for the evaluation.
GenParamDisplay := dict{display_mode: ['score_histogram', 'score_legend']}
WindowDict := dict{}
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)
*
* Visualize several evaluation results such as precision, recall, and confusion matrix
* for a given classification threshold.
GenParamDisplay.display_mode := ['pie_charts_precision', 'pie_charts_recall', 'absolute_confusion_matrix']
* Select evaluation results for one threshold by its index in
* AnomalyClassificationThresholds for display. We use the last
* one of the computed AnomalyClassificationThresholds.
* Please note that you should check which AnomalyClassificationThreshold
* best fits your application.
ClassificationThresholdIndex := |AnomalyClassificationThresholds| - 1
GenParamDisplay.classification_threshold_index := ClassificationThresholdIndex
WindowDict := dict{}
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)
*
* Choose thresholds for inference and store them along with the model.
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
MetaData.anomaly_segmentation_threshold := AnomalySegmentationThreshold$'1.16e'
MetaData.anomaly_classification_threshold := AnomalyClassificationThresholds[ClassificationThresholdIndex]$'1.16e'
set_dl_model_param (DLModelHandle, 'meta_data', MetaData)
*
* Store the trained model.
write_dl_model (DLModelHandle, 'model_final.hdl')
*
*
* *** 4.) INFER ***
*
* To demonstrate the inference steps, we apply the
* trained model to some randomly chosen example images.
list_image_files (ImageDir + '/' + ImageSubDirs, 'default', 'recursive', ImageFiles)
tuple_shuffle (ImageFiles, ImageFilesShuffled)
*
* Get thresholds for inference. These have been stored along with
* the model in the meta data above.
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
InferenceClassificationThreshold := number(MetaData.anomaly_classification_threshold)
InferenceSegmentationThreshold := number(MetaData.anomaly_segmentation_threshold)
*
* Create dictionary with dataset parameters used for display.
DLDatasetInfo := dict{class_names: ['ok', 'nok'], class_ids: [0, 1]}
*
WindowDict := dict{}
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)
*
* Use the same dataset specific preprocessing as for training.
if (ExampleSpecificPreprocessing)
preprocess_dl_samples_bottle (DLSample)
endif
*
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
*
* Apply thresholds to classify regions and the entire image.
threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult)
*
* Display the inference result.
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)
推理:
HalconImages := 'C:/coco/mvtec_anomaly_detection/tile'
ImageDir := HalconImages + '/test'
ImageSubDirs := ['good', 'crack', 'glue_strip', 'gray_stroke', 'oil', 'rough']
AnomalyLabelsDir := HalconImages + '/ground_truth'
OutputDir := './anomaly_tile_data'
ExampleSpecificPreprocessing := true
GenParamDataset := dict{image_sub_dirs: ImageSubDirs}
read_dl_dataset_anomaly (ImageDir, AnomalyLabelsDir, [], [], GenParamDataset, DLDataset)
split_dl_dataset (DLDataset, 50, 10, [])
read_dl_model ('C:/Users/Administrator/AppData/Roaming/MVTec/HALCON-23.11-Progress/examples/hdevelop/Deep-Learning/AnomalyDetection/model_final.hdl', DLModelHandle)
query_available_dl_devices (['runtime', 'id'], ['cpu', 0], DLDevice)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
create_dl_preprocess_param_from_model (DLModelHandle, 'constant_values', 'full_domain', [], [], [], DLPreprocessParam)
PreprocessSettings := dict{overwrite_files: 'true'}
preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
if (ExampleSpecificPreprocessing)
read_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch)
preprocess_dl_samples_bottle (DLSampleBatch)
write_dl_samples (DLDataset, [0:|DLDataset.samples| - 1], DLSampleBatch, [], [])
endif
list_image_files (ImageDir + '/' + ImageSubDirs, 'default', 'recursive', ImageFiles)
tuple_shuffle (ImageFiles, ImageFilesShuffled)
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
InferenceClassificationThreshold := number(MetaData.anomaly_classification_threshold)
InferenceSegmentationThreshold := number(MetaData.anomaly_segmentation_threshold)
DLDatasetInfo := dict{class_names: ['ok', 'nok'], class_ids: [0, 1]}
WindowDict := dict{}
read_image (Image,'C:/coco/mvtec_anomaly_detection/tile/test/crack/010.png')
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)