1.前言:.Halcon的深度学习标注工具一直在更新,我下载的20.11版本的Deep Learning Tool已经显示过期,无奈只能下载最新版MVTec Deep Learning Tool 24.05。不过最新版的标注工具做的很人性化,分类,目标检测,分割,异常值检测,OCR全都有,有兴趣的小伙伴可以下载来试试,可以在工具UI界面实现标注,训练加测试,很好用。如果想自己用代码实现训练也可以,先用标注工具把所有的图标注完毕后,导出标注数据集,即可利用此数据集,在halcon代码中训练。
2.上干货,深度目标检测训练源码:
模型训练预处理准备阶段************
dev_update_off ()
- 选择预处理模型.
Backbone := ‘pretrained_dl_classifier_compact.hdl’
*检测种类数
NumClasses := 6 - Image dimensions of the network. Later, these values are
- used to rescale the images during preprocessing.
ImageWidth := 512
ImageHeight := 320
ImageNumChannels := 3
*将容量设置为“中等”,这足以完成此任务
*并提供更好的推理和训练速度。与
*“高”,“中”型号的速度是“中”的两倍多,
*同时显示出几乎相同的检测性能。
Capacity := ‘medium’
*
- 分割数据集的百分比。
TrainingPercent := 70
ValidationPercent := 15 - In order to get a reproducible split we set a random seed.
- This means that rerunning the script results in the same split of DLDataset.
SeedRand := 42
- ** Set input and output paths ***
-
All example data is written to this folder.
ExampleDataDir := ‘E:/2024-4-10’ -
Path to the image directory.
HalconImageDir := ExampleDataDir + ‘/images/’ -
Write the initialized DL object detection model to train it in example part 2.
DLModelFileName := ExampleDataDir + ‘/pretrained_dl_model_detection.hdl’ -
Dataset directory for any outputs written by preprocess_dl_dataset.
DataDirectory := ExampleDataDir + ‘/dldataset_pcb_’ + ImageWidth + ‘x’ + ImageHeight -
Store preprocess parameters separately in order to use it e.g. during inference.
PreprocessParamFileName := DataDirectory + ‘/dl_preprocess_param.hdict’
*标注数据集路径
DataDirectPath:=ExampleDataDir + ‘/Pcb.hdict’ -
Output path of the best evaluated model.
BestModelBaseName := ExampleDataDir + ‘/best_dl_model_detection’ -
Output path for the final trained model.
FinalModelBaseName := ExampleDataDir + ‘/final_dl_model_detection’
InitialModelFileName := ExampleDataDir + ‘/pretrained_dl_model_detection.hdl’
DLDatasetFileName := DataDirectory + ‘/dl_dataset.hdict’
- ** Read the labeled dataset and split the dataset ***
- In order to get reproducible results we set a seed here.
set_system (‘seed_rand’, SeedRand) - Create the output directory if it does not exist yet.
file_exists (ExampleDataDir, FileExists)
if (not FileExists)
make_dir (ExampleDataDir)
endif - Read in a DLDataset.
read_dict (DataDirectPath, [], [], DLDataset) - Split the dataset into train/validation and test.
split_dl_dataset (DLDataset, TrainingPercent, ValidationPercent, [])
- ** Determine model parameters from data ***
- Generate model parameters min_level, max_level, anchor_num_subscales,
- and anchor_aspect_ratios from the dataset in order to improve the
- training result. Please note that optimizing the model parameters too
- much on the training data can lead to overfitting. Hence, this should
- only be done if the actual application data are similar to the training
- data.
create_dict (GenParam)
set_dict_tuple (GenParam, ‘split’, ‘train’)
determine_dl_model_detection_param (DLDataset, ImageWidth, ImageHeight, GenParam, DLDetectionModelParam)
*
- Get the generated model parameters.
get_dict_tuple (DLDetectionModelParam, ‘min_level’, MinLevel)
get_dict_tuple (DLDetectionModelParam, ‘max_level’, MaxLevel)
get_dict_tuple (DLDetectionModelParam, ‘anchor_num_subscales’, AnchorNumSubscales)
get_dict_tuple (DLDetectionModelParam, ‘anchor_aspect_ratios’, AnchorAspectRatios)
- ** Create the object detection model ***
- Create dictionary for generic parameters and create the object detection model.
create_dict (DLModelDetectionParam)
set_dict_tuple (DLModelDetectionParam, ‘image_width’, ImageWidth)
set_dict_tuple (DLModelDetectionParam, ‘image_height’, ImageHeight)
set_dict_tuple (DLModelDetectionParam, ‘image_num_channels’, ImageNumChannels)
set_dict_tuple (DLModelDetectionParam, ‘min_level’, MinLevel)
set_dict_tuple (DLModelDetectionParam, ‘max_level’, MaxLevel)
set_dict_tuple (DLModelDetectionParam, ‘anchor_num_subscales’, AnchorNumSubscales)
set_dict_tuple (DLModelDetectionParam, ‘anchor_aspect_ratios’, AnchorAspectRatios)
set_dict_tuple (DLModelDetectionParam, ‘capacity’, Capacity) - Get class IDs from dataset for the model.
get_dict_tuple (DLDataset, ‘class_ids’, ClassIDs)
set_dict_tuple (DLModelDetectionParam, ‘class_ids’, ClassIDs) - Create the model.
create_dl_model_detection (Backbone, NumClasses, DLModelDetectionParam, DLModelHandle) - Write the initialized DL object detection model
- to train it later in part 2.
write_dl_model (DLModelHandle, DLModelFileName)
- ** Preprocess the dataset ***
- Get preprocessing parameters from model.
create_dl_preprocess_param_from_model (DLModelHandle, ‘none’, ‘full_domain’, [], [], [], DLPreprocessParam) - Preprocess the dataset. This might take a few minutes.
create_dict (GenParam)
set_dict_tuple (GenParam, ‘overwrite_files’, true)
preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFilename) - Write preprocessing parameters to use them in later parts.
write_dict (DLPreprocessParam, PreprocessParamFileName, [], [])
- ** Preview the preprocessed dataset ***
- Before moving on to training, it is recommended to check the preprocessed dataset.
- Display the DLSamples for 10 randomly selected train images.
get_dict_tuple (DLDataset, ‘samples’, DatasetSamples)
find_dl_samples (DatasetSamples, ‘split’, ‘train’, ‘match’, SampleIndices)
tuple_shuffle (SampleIndices, ShuffledIndices)
*训练的数据种类是6,所以下面算子设置[0:5]
read_dl_samples (DLDataset, ShuffledIndices[0:5], DLSampleBatchDisplay) - Set parameters for dev_display_dl_data.
create_dict (WindowHandleDict)
create_dict (GenParam)
set_dict_tuple (GenParam, ‘scale_windows’, 1.2) - Display the samples in DLSampleBatchDisplay.
for Index := 0 to |DLSampleBatchDisplay| - 1 by 1
*- Loop over samples in DLSampleBatchDisplay.
dev_display_dl_data (DLSampleBatchDisplay[Index], [], DLDataset, ‘bbox_ground_truth’, GenParam, WindowHandleDict)
get_dict_tuple (WindowHandleDict, ‘bbox_ground_truth’, WindowHandles) - Add explanatory text.
dev_set_window (WindowHandles[0])
get_dict_object (Image, DLSampleBatchDisplay[Index], ‘image’)
get_image_size (Image, ImageWidth, ImageHeight)
dev_disp_text ('New image size after preprocessing: ’ + ImageWidth + ’ x ’ + ImageHeight, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
endfor - Loop over samples in DLSampleBatchDisplay.
模型训练阶段***************************************************
stop()
dev_update_off ()
*
-
Training can be performed on a GPU or CPU.
-
See the respective system requirements in the Installation Guide.
-
If possible a GPU is used in this example.
-
In case you explicitely wish to run this example on the CPU,
-
choose the CPU device instead.
query_available_dl_devices ([‘runtime’,‘runtime’], [‘gpu’,‘cpu’], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw (‘No supported device found to continue this example.’)
endif -
Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
get_dl_device_param (DLDevice, ‘type’, DLDeviceType)
if (DLDeviceType == ‘cpu’)- The number of used threads may have an impact
- on the training duration.
NumThreadsTraining := 4
set_system (‘thread_num’, NumThreadsTraining)
endif
- *** Set basic parameters. ***
- The following parameters need to be adapted frequently.
- Model parameters.
- Batch size.
BatchSize := 2 - Initial learning rate.
InitialLearningRate := 0.0005 - Momentum should be high if batch size is small.
Momentum := 0.99 - Parameters used by train_dl_model.
- Number of epochs to train the model.
NumEpochs := 60 - Evaluation interval (in epochs) to calculate evaluation measures on the validation split.
EvaluationIntervalEpochs := 1 - Change the learning rate in the following epochs, e.g. [15, 30].
- Set it to [] if the learning rate should not be changed.
ChangeLearningRateEpochs := 30 - Change the learning rate to the following values, e.g. InitialLearningRate * [0.1, 0.01].
- The tuple has to be of the same length as ChangeLearningRateEpochs.
ChangeLearningRateValues := InitialLearningRate * 0.1
- *** Set advanced parameters. ***
-
The following parameters might need to be changed in rare cases.
-
Model parameter.
-
Set the weight prior.
WeightPrior := 0.00001 -
Parameters used by train_dl_model.
-
Control whether training progress is displayed (true/false).
EnableDisplay := true -
Set a random seed for training.
RandomSeed := 42
set_system (‘seed_rand’, RandomSeed) -
In order to obtain nearly deterministic training results on the same GPU
-
(system, driver, cuda-version) you could specify “cudnn_deterministic” as
-
“true”. Note, that this could slow down training a bit.
-
set_system (‘cudnn_deterministic’, ‘true’)
-
Set generic parameters of create_dl_train_param.
-
Please see the documentation of create_dl_train_param for an overview on all available parameters.
GenParamName := []
GenParamValue := [] -
Augmentation parameter.
-
If samples should be augmented during training, create the dict required by augment_dl_samples.
-
Here, we set the augmentation percentage and method.
create_dict (AugmentationParam) -
Percentage of samples to be augmented.
set_dict_tuple (AugmentationParam, ‘augmentation_percentage’, 50) -
Mirror images along row and column.
set_dict_tuple (AugmentationParam, ‘mirror’, ‘rc’)
GenParamName := [GenParamName,‘augment’]
GenParamValue := [GenParamValue,AugmentationParam] -
Change strategies.
-
It is possible to change model parameters during training.
-
Here, we change the learning rate if specified above.
if (|ChangeLearningRateEpochs| > 0)
create_dict (ChangeStrategy)- Specify the model parameter to be changed, here the learning rate.
set_dict_tuple (ChangeStrategy, ‘model_param’, ‘learning_rate’) - Start the parameter value at ‘initial_value’.
set_dict_tuple (ChangeStrategy, ‘initial_value’, InitialLearningRate) - Reduce the learning rate in the following epochs.
set_dict_tuple (ChangeStrategy, ‘epochs’, ChangeLearningRateEpochs) - Reduce the learning rate to the following value at epoch 30.
set_dict_tuple (ChangeStrategy, ‘values’, ChangeLearningRateValues) - Collect all change strategies as input.
GenParamName := [GenParamName,‘change’]
GenParamValue := [GenParamValue,ChangeStrategy]
endif
- Specify the model parameter to be changed, here the learning rate.
-
Serialization strategies.
-
There are several options for saving intermediate models to disk (see create_dl_train_param).
-
Here, the best and final model are saved to the paths set above.
create_dict (SerializationStrategy)
set_dict_tuple (SerializationStrategy, ‘type’, ‘best’)
set_dict_tuple (SerializationStrategy, ‘basename’, BestModelBaseName)
GenParamName := [GenParamName,‘serialize’]
GenParamValue := [GenParamValue,SerializationStrategy]
create_dict (SerializationStrategy)
set_dict_tuple (SerializationStrategy, ‘type’, ‘final’)
set_dict_tuple (SerializationStrategy, ‘basename’, FinalModelBaseName)
GenParamName := [GenParamName,‘serialize’]
GenParamValue := [GenParamValue,SerializationStrategy] -
Display parameters.
-
In this example, the evaluation measure for the training spit is not displayed during
-
training (default). If you want to do so, select a certain percentage of the training
-
samples used to evaluate the model during training. A lower percentage helps to speed
-
up the evaluation. If the evaluation measure for the training split shall
-
not be displayed, set SelectedPercentageTrainSamples to 0.
SelectedPercentageTrainSamples := 0 -
Set the x-axis argument of the training plots.
XAxisLabel := ‘epochs’
create_dict (DisplayParam)
set_dict_tuple (DisplayParam, ‘selected_percentage_train_samples’, SelectedPercentageTrainSamples)
set_dict_tuple (DisplayParam, ‘x_axis_label’, XAxisLabel)
GenParamName := [GenParamName,‘display’]
GenParamValue := [GenParamValue,DisplayParam]
- *** Read initial model and dataset. ***
- Check if all necessary files exist.
check_data_availability (ExampleDataDir, InitialModelFileName, DLDatasetFileName) - Read in the model that was initialized during preprocessing.
read_dl_model (InitialModelFileName, DLModelHandle) - Read in the preprocessed DLDataset file.
read_dict (DLDatasetFileName, [], [], DLDataset)
- *** Set model parameters. ***
- Set model hyper-parameters as specified in the settings above.
set_dl_model_param (DLModelHandle, ‘learning_rate’, InitialLearningRate)
set_dl_model_param (DLModelHandle, ‘momentum’, Momentum)
if (BatchSize == ‘maximum’ and DLDeviceType == ‘gpu’)
set_dl_model_param_max_gpu_batch_size (DLModelHandle, 100)
else
set_dl_model_param (DLModelHandle, ‘batch_size’, BatchSize)
endif
if (|WeightPrior| > 0)
set_dl_model_param (DLModelHandle, ‘weight_prior’, WeightPrior)
endif - When the batch size is determined, set the device.
set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
- *** Train the model. ***
- Create training parameters.
create_dl_train_param (DLModelHandle, NumEpochs, EvaluationIntervalEpochs, EnableDisplay, RandomSeed, GenParamName, GenParamValue, TrainParam) - Start the training by calling the training operator
- train_dl_model_batch () within the following procedure.
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0.0, TrainResults, TrainInfos, EvaluationInfos) - Stop after the training has finished, before closing the windows.
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop () - Close training windows.
dev_close_window ()
模型评估阶段***********************************
dev_update_off ()
*
-
In this example, the evaluation steps are explained in graphics windows,
-
before they are executed. Set the following parameter to false in order to
-
skip this visualization.
ShowExampleScreens := true -
By default, this example uses a model pretrained by MVTec. To use the model
-
which was trained in part 2 of this example series, set the following
-
variable to false.
UsePretrainedModel := true -
The evaluation can be performed on GPU or CPU.
-
See the respective system requirements in the Installation Guide.
-
If possible a GPU is used in this example.
-
In case you explicitely wish to run this example on the CPU,
-
choose the CPU device instead.
query_available_dl_devices ([‘runtime’,‘runtime’], [‘gpu’,‘cpu’], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw (‘No supported device found to continue this example.’)
endif -
Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
- ** Set paths ***
- Example data folder containing the outputs of the previous example series.
ExampleDataDir := ‘E:/2024-4-10’ - File name of the finetuned object detection model.
RetrainedModelFileName := ExampleDataDir + ‘/best_dl_model_detection.hdl’ - Path to DL dataset.
- Note: Adapt DataDirectory after preprocessing with another image size.
DataDirectory := ExampleDataDir + ‘/dldataset_pcb_512x320’
DLDatasetFileName := DataDirectory + ‘/dl_dataset.hdict’
- ** Set evaluation parameters ***
- Specify measures of interest
EvaluationMeasures := ‘all’ - Specify considered IoU thresholds.
IoUThresholds := [] - Display detailed results for the following IoU threshold.
DisplayIoUThreshold := 0.7 - Batch size used during evaluation.
BatchSize := 1 - Specify evaluation subsets for objects of a certain size.
AreaNames := []
AreaMin := []
AreaMax := [] - Specify the maximum number of detections considered for each measure.
MaxNumDetections := []
- ** Read the model and data ***
- Check if all necessary files exist.
check_data_availability_COPY_1 (ExampleDataDir, DLDatasetFileName, RetrainedModelFileName, UsePretrainedModel) - Read the trained model.
read_dl_model (RetrainedModelFileName, DLModelHandle) - Set batch size of the model to 1 temporarily.
set_dl_model_param (DLModelHandle, ‘batch_size’, 1)
set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
*
- Read the evaluation data.
read_dict (DLDatasetFileName, [], [], DLDataset)
- ** Set optimized parameters for inference ***
- To reduce the number of false positives, set lower values for
- ‘max_overlap’ (default = 0.5) and ‘max_overlap_class_agnostic’
- (default = 1.0) and a higher confidence threshold (default = 0.5).
set_dl_model_param (DLModelHandle, ‘max_overlap_class_agnostic’, 0.7)
set_dl_model_param (DLModelHandle, ‘max_overlap’, 0.2)
set_dl_model_param (DLModelHandle, ‘min_confidence’, 0.6)
- ** First impression via visual inspection of results ***
- Create parameter dictionaries for visualization.
create_dict (WindowHandleDict)
create_dict (GenParam)
set_dict_tuple (GenParam, ‘bbox_display_confidence’, false) - Select test images randomly.
get_dict_tuple (DLDataset, ‘samples’, DatasetSamples)
find_dl_samples (DatasetSamples, ‘split’, ‘test’, ‘or’, DLSampleIndices)
tuple_shuffle (DLSampleIndices, DLSampleIndicesShuffled) - Apply the model and display results.
for Index := 0 to 5 by 1
read_dl_samples (DLDataset, DLSampleIndicesShuffled[Index], DLSampleBatch)
apply_dl_model (DLModelHandle, DLSampleBatch, [], DLResultBatch)
dev_display_dl_data (DLSampleBatch, DLResultBatch, DLDataset, ‘bbox_both’, GenParam, WindowHandleDict)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
endfor
dev_close_window_dict (WindowHandleDict)
set_dl_model_param (DLModelHandle, ‘batch_size’, BatchSize)
*
- ** Evaluate object detection model on evaluation data ***
- Set generic evaluation parameters.
create_dict (GenParamEval) - Set the measures of interest.
set_dict_tuple (GenParamEval, ‘measures’, EvaluationMeasures) - Set maximum number of detections considered for each measure.
if (|MaxNumDetections|)
set_dict_tuple (GenParamEval, ‘max_num_detections’, MaxNumDetections)
endif - Set the evaluation area subsets.
if (|AreaNames|)
if ((|AreaNames| != |AreaMin|) or (|AreaNames| != |AreaMax|))
throw (‘AreaNames, AreaMin, and AreaMax must have the same size.’)
endif
create_dict (AreaRanges)
set_dict_tuple (AreaRanges, ‘name’, AreaNames)
set_dict_tuple (AreaRanges, ‘min’, AreaMin)
set_dict_tuple (AreaRanges, ‘max’, AreaMax)
set_dict_tuple (GenParamEval, ‘area_ranges’, AreaRanges)
endif - Set IoU thresholds.
if (|IoUThresholds|)
set_dict_tuple (GenParamEval, ‘iou_threshold’, IoUThresholds)
endif - Enable detailed evaluation.
set_dict_tuple (GenParamEval, ‘detailed_evaluation’, true) - Show progress of evaluation.
set_dict_tuple (GenParamEval, ‘show_progress’, true) - Evaluate the finetuned model on the ‘test’ split of the dataset.
evaluate_dl_model (DLDataset, DLModelHandle, ‘split’, ‘test’, GenParamEval, EvaluationResultDetection, EvalParams) - Display results of the detailed evaluation.
create_dict (DisplayParam) - Set the IoU of interest. The default is the first ‘iou_threshold’ of EvalParams.
if (|DisplayIoUThreshold| == 1)
get_dict_tuple (EvalParams, ‘iou_threshold’, EvalIoUThresholds)
if (find(EvalIoUThresholds,DisplayIoUThreshold) != -1)
set_dict_tuple (DisplayParam, ‘iou_threshold’, DisplayIoUThreshold)
else
throw (‘No evaluation result for specified IoU threshold.’)
endif
endif - Display detailed precision and recall
set_dict_tuple (DisplayParam, ‘display_mode’, [‘pie_charts_precision’,‘pie_charts_recall’])
create_dict (WindowHandleDict)
dev_display_detection_detailed_evaluation (EvaluationResultDetection, EvalParams, DisplayParam, WindowHandleDict)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘top’, ‘right’, ‘black’, [], [])
stop ()
dev_close_window_dict (WindowHandleDict) - Display confusion matrix.
set_dict_tuple (DisplayParam, ‘display_mode’, ‘absolute_confusion_matrix’)
dev_display_detection_detailed_evaluation (EvaluationResultDetection, EvalParams, DisplayParam, WindowHandleDict)
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
dev_close_window_dict (WindowHandleDict) - Optimize the memory consumption.
set_dl_model_param (DLModelHandle, ‘batch_size’, 1)
set_dl_model_param (DLModelHandle, ‘optimize_for_inference’, ‘true’)
write_dl_model (DLModelHandle, RetrainedModelFileName) - Close the windows.
dev_close_window_dict (WindowHandleDict)
模型测试阶段****************************************
dev_update_off ()
*
- In this example, the inference steps are explained in graphics windows,
- before they are executed. Set the following parameter to false in order to
- skip this visualization.
ShowExampleScreens := true - By default, this example uses a model pretrained by MVTec. To use the model
- which was trained in part 2 of this example series, set the following
- variable to false.
UsePretrainedModel := true - Inference can be done on a GPU or CPU.
- See the respective system requirements in the Installation Guide.
- If possible a GPU is used in this example.
- In case you explicitely wish to run this example on the CPU,
- choose the CPU device instead.
query_available_dl_devices ([‘runtime’,‘runtime’], [‘gpu’,‘cpu’], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw (‘No supported device found to continue this example.’)
endif - Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
- ** Set paths and parameters for inference ***
-
We will demonstrate the inference on the example images.
-
In a real application newly incoming images (not used for training or evaluation)
-
would be used here.
-
In this example, we read the images from file.
-
Directory name with the images of the pill bag dataset.
ExampleDir:=‘E:/2024-4-10’
ImageDir := ExampleDir + ‘/PcbImgs’ -
File name of the dict containing parameters used for preprocessing.
-
Note: Adapt DataDirectory after preprocessing with another image size.
DataDirectory := ExampleDir + ‘/dldataset_pcb_512x320’
PreprocessParamFileName := DataDirectory + ‘/dl_preprocess_param.hdict’ -
File name of the finetuned object detection model.
RetrainedModelFileName := ExampleDir + ‘/best_dl_model_detection.hdl’ -
Provide the class names and IDs.
-
Class names.
ClassNames := [‘SR’,‘MR’,‘BR’,‘C’,‘D’,‘U’] -
Respective class ids.
ClassIDs := [0,1,2,3,4,5] -
Batch Size used during inference.
BatchSizeInference := 1 -
Postprocessing parameters for the detection model.
MinConfidence := 0.6
MaxOverlap := 0.2
MaxOverlapClassAgnostic := 0.7
- ** Inference ***
-
Check if all necessary files exist.
check_data_availability_COPY_2 (ExampleDir, PreprocessParamFileName, RetrainedModelFileName, UsePretrainedModel) -
Read in the retrained model.
read_dl_model (RetrainedModelFileName, DLModelHandle) -
Set the batch size.
set_dl_model_param (DLModelHandle, ‘batch_size’, BatchSizeInference) -
Initialize the model for inference.
set_dl_model_param (DLModelHandle, ‘device’, DLDevice) -
Set postprocessing parameters for model.
set_dl_model_param (DLModelHandle, ‘min_confidence’, MinConfidence)
set_dl_model_param (DLModelHandle, ‘max_overlap’, MaxOverlap)
set_dl_model_param (DLModelHandle, ‘max_overlap_class_agnostic’, MaxOverlapClassAgnostic) -
Get the parameters used for preprocessing.
read_dict (PreprocessParamFileName, [], [], DLPreprocessParam) -
Create window dictionary for displaying results.
create_dict (WindowHandleDict) -
Create dictionary with dataset parameters necessary for displaying.
create_dict (DLDataInfo)
set_dict_tuple (DLDataInfo, ‘class_names’, ClassNames)
set_dict_tuple (DLDataInfo, ‘class_ids’, ClassIDs) -
Set generic parameters for visualization.
create_dict (GenParam)
set_dict_tuple (GenParam, ‘scale_windows’, 1.2) -
Image Acquisition 01: Code generated by Image Acquisition 01
list_files (‘E:/2024-4-10/PcbImgs’, [‘files’,‘follow_links’], 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 (ImageBatch, ImageFiles[Index])- Generate the DLSampleBatch.
gen_dl_samples_from_images (ImageBatch, DLSampleBatch) - Preprocess the DLSampleBatch.
preprocess_dl_samples (DLSampleBatch, DLPreprocessParam) - Apply the DL model on the DLSampleBatch.
apply_dl_model (DLModelHandle, DLSampleBatch, [], DLResultBatch) - Postprocessing and visualization.
- Loop over each sample in the batch.
for SampleIndex := 0 to BatchSizeInference - 1 by 1
*- Get sample and according results.
DLSample := DLSampleBatch[SampleIndex]
DLResult := DLResultBatch[SampleIndex] - Count detected pills for each class.
get_dict_tuple (DLResult, ‘bbox_class_id’, DetectedClassIDs)
tuple_gen_const (|ClassIDs|, 0, NumberDetectionsPerClass)
for Index := 0 to |ClassIDs| - 1 by 1
NumberDetectionsPerClass[Index] := sum(DetectedClassIDs [==] ClassIDs[Index])
endfor - Create output text based on counted pills.
create_counting_result_text (NumberDetectionsPerClass, ClassNames, Text, TextColor, TextBoxColor) - Display results and text.
dev_display_dl_data (DLSample, DLResult, DLDataInfo, ‘bbox_result’, GenParam, WindowHandleDict)
get_dict_tuple (WindowHandleDict, ‘bbox_result’, WindowHandles)
dev_set_window (WindowHandles[0])
set_display_font (WindowHandles[0], 16, ‘mono’, ‘true’, ‘false’)
dev_disp_text (Text, ‘window’, ‘top’, ‘left’, TextColor, [‘box_color’,‘shadow’], [TextBoxColor,‘false’])
dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
endfor
endfor
- Get sample and according results.
- Generate the DLSampleBatch.
-
Close windows used for visualization.
dev_close_window_dict (WindowHandleDict)