I am trying to run the following template script in HDevelop 22.11 Progress. But I keep getting either the following error:
Abnormal program termination: Received signal 11
and
Abnormal program termination: Received signal 4
I have been unable to find a good way to debug the issue. I have been able to find out that it may be a mismatch in CUDA version, but from what I can tell 22.11 should support CUDA 11 and up and the graphcis card is using cuda 12.
* Deep learning anomaly detection example.
*
* This example demonstrates the general workflow for anomaly detection
* based on deep learning.
*
* The workflow typically consists of four steps which are described here:
* 1. Dataset preprocessing.
* 2. Training of the model and threshold estimation on 'ok' images.
* 3. Qualitative 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)
*
* Base directory containing the folders with the used images.
* There must be a 'good' or 'ok' subfolder in ImageDir.
* For training, only images in the 'good' or 'ok' folders are used.
* Images in subfolders named differently will be treated as containing anomalies.
* They can be used for qualitative evalu ation later.
*
* Note: You can immediately train an anomaly detection model on your own data as long
* as your images not showing any anomaly are located in a subfolder called 'good' or 'ok'.
ImageDir := 'C:/Workplace/DL Training/L113 FST HALCON/'
ImageSubDirs := ['Good','Bad']
* Test from Stemmer:
* ImageDir := HalconImages + '/bottles/'
* ImageSubDirs := ['good','broken_large']
* ImageSubDirs := ['Good']
*
* Folder where the ground truth anomaly regions are stored.
* This folder is not required for training.
* If there is no ground truth data available, set AnomalyDir to [].
AnomalyDir := []
*
* Folder where the preprocessed samples will be stored.
OutputDir := './CIM_pek_tests_domainreduce'
*
* Dataset specific preprocessing.
ExampleSpecificPreprocessing := false
*
* Set image size. It should be large enough if the defects are small.
* Please refer to the documentation of read_dl_model for possible restrictions
* depending on the network.
ImageWidth := 288
ImageHeight := 192
*
* Set the complexity of the model which roughly describes the capability of the model
* to handle complex application images. A higher value can improve the performance but
* increases the time needed to train the model.
Complexity := 75
*
*
* *** 1.) PREPARE ***
*
* Read and preprocess an anomaly detection dataset.
* For optimal performance it is beneficial to do a custom
* preprocessing step that defines input domains.
*
* Load and split the dataset.
create_dict (GenParamDataset)
* #read_dict ('C:/Workspace/HALCON/StarWheel/SegmentationSyringeStarWheel.hdict', [], [], DLDataset)
* #set_dict_tuple (DLDataset, ImageDir, ImageDir)
set_dict_tuple (GenParamDataset, 'image_sub_dirs', ImageSubDirs)
read_dl_dataset_anomaly (ImageDir, AnomalyDir, [], [], GenParamDataset, DLDataset)
*
* Note: make sure that every split contains at least one 'ok' image.
* For a small number of images in the dataset you might need to increase
* the validation fraction.
split_dl_dataset (DLDataset, 50, 20, [])
*
* Load the anomaly detection model and set parameters.
* For documentation see set_dl_model_param () and get_dl_model_param ().
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)
* In this example, the training of the anomaly detection model is done on the CPU.
* query_available_compute_devices (DeviceIdentifier)
query_available_dl_devices (['runtime','id'], ['gpu',0], DLDevice)
* query_available_dl_devices (['type','runtime','id'], ['gpu','gpu',0], DLDevice)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
*
* Set preprocessing parameters and preprocess.
create_dict (PreprocessSettings)
set_dict_tuple (PreprocessSettings, 'overwrite_files', true)
create_dl_preprocess_param ('anomaly_detection', ImageWidth, ImageHeight, 3, [], [], 'constant_values', 'full_domain', [], [], [], [], DLPreprocessParam)
* Ændret ovenstående (ImageNumChannels) fra 3 til 1
preprocess_dl_dataset (DLDataset, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
*
* Run a specific preprocessing for this example.
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
read_dl_samples (DLDataset, [0:|DatasetSamples| - 1], DLSampleBatch)
for SampleIndex := 0 to |DLSampleBatch| - 1 by 1
get_dict_object (SampleImage, DLSampleBatch[SampleIndex], 'image')
*
rectangle1_domain (SampleImage, SampleImageReduced, 0, 0, ImageHeight, ImageWidth)
*
* Update the sample.
set_dict_object (SampleImageReduced, DLSampleBatch[SampleIndex], 'image')
endfor
write_dl_samples (DLDataset, [0:|DatasetSamples| - 1], DLSampleBatch, [], [])
*
* Visually inspect ten randomly selected preprocessed DLSamples.
create_dict (WindowDict)
for Index := 0 to 10 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.) TRAIN ***
*
* Create training parameters.
*
* Control whether the training progress is displayed (true/false).
EnableDisplay := true
*
* Set a threshold for the training error and a maximum number of training epochs.
* If the training error falls below this threshold, the training is finished.
* Otherwise the training continues until the maximum number of epochs is reached.
ErrorThreshold := 0.0000001
MaxNumEpochs := 75
*
* Set the domain ratio which controls the fraction of each image used for training.
* The training result might be improved by setting a greater value, but this also
* increases the training time.
DomainRatio := 0.9
*
* Regularization noise can make the training more robust. In case the training fails,
* setting a higher value might help.
RegularizationNoise := 0.25
*
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)
*
* The training and thus the call of train_dl_model_anomaly_dataset ()
* is done using the following procedure. This 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', [], [])
* read_dl_model ('C:/Workspace/HALCON/StarWheel/model_final_kks.hdl', DLModelHandle)
stop ()
*
dev_close_window ()
*
*
* *** 3.) EVALUATE ***
*
* Calculate classification and segmentation thresholds and perform evaluation.
*
* Set the factor used to calculate the anomaly score. See the documentation of
* get_dl_model_param for details.
* This parameter can help to improve the anomaly score for a better classification
* between 'ok' and 'nok' images. For example, in case the defects are small, a larger
* value might be suitable.
StandardDeviationFactor := 15.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, manual
* fine-tuning may be beneficial.
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 ()
*
* Set generic evaluation parameters and evaluate the model on the test split.
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)
*
* Set display parameters.
create_dict (GenParamDisplay)
*
* Visualize the histogram over the anomaly scores and with it the classification
* thresholds used for the evaluation.
set_dict_tuple (GenParamDisplay, 'display_mode', ['score_histogram','score_legend'])
create_dict (WindowDict)
dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict)
get_dict_tuple (WindowDict, 'window_score_histogram', Tuple1)
get_dict_tuple(WindowDict, 'window_score_legend',Tuple2)
dump_window_image (Image1, Tuple1)
dump_window_image (Image2, Tuple2)
write_image (Image1, 'png', 16711680, 'C:/Workplace/DL Training/L113 FST HALCON/Result/WindowScoreHistogram.png')
write_image (Image2, 'png', 16711680, 'C:/Workplace/DL Training/L113 FST HALCON/Result/WindowScoreLegend.png')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', 'box', 'true')
stop ()
*
dev_close_window_dict (WindowDict)
*
* Visualize evaluation results such as precision, recall, and confusion matrix
* for a given classification threshold.
set_dict_tuple (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. Note that
* you have to use the AnomalyClassificationThreshold which suits
* your application best.
ClassificationThresholdIndex := |AnomalyClassificationThresholds| - 1
set_dict_tuple (GenParamDisplay, 'classification_threshold_index', ClassificationThresholdIndex)
create_dict (WindowDict)
dev_display_anomaly_detection_evaluation (EvaluationResult, EvalParams, GenParamDisplay, WindowDict)
get_dict_tuple (WindowDict, 'window_pie_charts_precision', Tuple1)
get_dict_tuple(WindowDict, 'window_pie_charts_recall',Tuple2)
get_dict_tuple(WindowDict, 'window_absolute_confusion_matrix',Tuple3)
dump_window_image (Image1, Tuple1)
dump_window_image (Image2, Tuple2)
dump_window_image (Image3, Tuple3)
write_image (Image1, 'png', 16711680, 'C:/Workplace/DL Training/L113 FST HALCON/Result/WindowPieChartsPrecision.png')
write_image (Image2, 'png', 16711680, 'C:/Workplace/DL Training/L113 FST HALCON/Result/WindowPieChartsRecall.png')
write_image (Image3, 'png', 16711680, 'C:/Workplace/DL Training/L113 FST HALCON/Result/WindowAbsoluteConfusionMatrix.png')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
dev_close_window_dict (WindowDict)
*
* Store the trained model.
write_dl_model (DLModelHandle, 'model_final_fst.hdl')
*
*
* *** 4.) INFER ***
* ImageSubDirs := ['Good']
read_dl_model ('C:/Workplace/DL Training/HALCON/model_final_fst.hdl', DLModelHandle)
* 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)
*
* Choose thresholds for inference.
InferenceClassificationThreshold := AnomalyClassificationThresholds[ClassificationThresholdIndex]
InferenceSegmentationThreshold := AnomalySegmentationThreshold
* #InferenceClassificationThreshold := InferenceSegmentationThreshold+0.03
* #InferenceSegmentationThreshold := InferenceClassificationThreshold
* #InferenceClassificationThreshold := 0.80
* #InferenceSegmentationThreshold := 0.30
*
* Create dictionary with dataset parameters used for display.
create_dict (DLDatasetInfo)
set_dict_tuple (DLDatasetInfo, 'class_names', ['ok','nok'])
set_dict_tuple (DLDatasetInfo, 'class_ids', [0,1])
*
create_dict (WindowDict)
for IndexInference := 0 to min2(|ImageFilesShuffled|,737) - 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.
for SampleIndex := 0 to |DLSample| - 1 by 1
get_dict_object (SampleImage, DLSample[SampleIndex], 'image')
*
rectangle1_domain (SampleImage, SampleImageReduced, 0, 0, ImageHeight, ImageWidth)
*
* Update the sample.
set_dict_object (SampleImageReduced, DLSample[SampleIndex], 'image')
endfor
*
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)```