Halcon20.11深度学习目标检测模型

news2024/11/24 16:03:19

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_set_window (WindowHandles[1])
    dev_disp_text (‘Press Run (F5) to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
    stop ()
    endfor

模型训练阶段***************************************************
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
  • 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
  • Close windows used for visualization.
    dev_close_window_dict (WindowHandleDict)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2058926.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

化挑战为机遇,联想凌拓迎来杨旭时代

【全球存储观察 | 科技热点关注】 2024年7月,联想凌拓CEO杨旭上任,引发业界广泛关注,成为国内数据存储领域的新闻焦点。 现在,联想凌拓迎来了杨旭时代。作为联想凌拓CEO,杨旭的到任给联想凌拓带来了怎样的…

又有不少人要为《黑神话:悟空》买电脑了

1. 什么是 3A 游戏?2. 《黑神话:悟空》是一款怎样的游戏?3. 又有不少人要为《黑神话:悟空》买电脑了 3.1. 《黑神话:悟空》对电脑性能的要求3.2. 性能测试工具 不管你是游戏玩家还是非游戏玩家,这两天肯定被“黑悟空”刷屏了。 因为就在昨…

爆赞!斯坦福大学力作《深度学习漫画书》,翻烂它都不为过!

“斯坦福大学深度学习漫画书”是一本以漫画形式介绍深度学习基础知识的书籍,作者是Andrew Ng。 这本书以漫画形式切入,将一图胜千言的道理玩到了极致!并通过生动有趣的漫画形式来深入了解深度学习的概念和应用,把复杂的深度学习技…

【本社翻译】Unity官方XR开发电子书

上个月(2024年7月),Unity 官方发布了一本聚焦 XR 开发的电子书,书名为《Create Virtual and Mixed Reality Experiences in Unity》。本书系统介绍了以 XR Interaction Toolkit 为代表的一系列 Unity XR 开发工具集,深…

基于JavaEE的远程医疗管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图详细视频演示技术栈系统测试为什么选择我官方认证玩家,服务很多代码文档,百分百好评,战绩可查!!入职于互联网大厂,可以交流,共同进步。有保障的售后 代码参考数据库参…

有源音箱申请Hi-Res认证指南

有源音箱(也称为主动式音箱)是一种内置功率放大器的音箱,其显著特点是音箱内部含有一套功率放大电路,可以直接通过音频线(如RCA线、3.5mm音频线或莲花线)与信号源(如电视、电脑、DVD播放器等&am…

LLM+Agent+多模态:大模型全栈入门、从0到企业落地、前沿论文,一定要看看!(全攻略保姆教程)

现在搞AI科研,基本上都离不开大模型。不管是“水”篇论文还是冲顶会,结合LLM的创新点都非常多。 但是LLM相关的内容很多,学校的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学,学习成本和门槛很高。 针对所有自学遇到…

使用yolov5实现目标检测和yolov8实现分割简单案例

一、前置 测试这个案例之前需要安装一些前置的东西,如果已经安装的可以忽略,下面我给出我跟着做的一些很好的博客提供大家参考,因为我们主要目的还是实现yolov5的目标检测。 1、安装nvidia显卡驱动 可以参考:【Windows】安装NV…

黑夜力作-Web爬虫入门与实战精讲-专栏导读

🏆作者简介,黑夜开发者,CSDN领军人物,全栈领域优质创作者✌,CSDN博客专家,阿里云社区专家博主,2023年CSDN全站百大博主。 🏆数年电商行业从业经验,历任核心研发工程师&am…

高性能minio集群环境搭建(配视频教程)

为后续进行《小卷原创视频教程:spring boot 3 vue3文件上传最佳实践》的大文件上传项目实战,这里带着小伙伴一起搭建下分布式开源文件存储minIO的集群环境。后续将对这个环境进行spring boot的集成,以进行企业级大文件上传的对接。 文章目录…

分布式基础理论——CAP理论和BASE理论

文章目录 CAP 理论BASE 理论参考资料 CAP 理论 CAP定理(CAP theorem)指出,在分布式系统中,设计读写操作时只能同时满足以下三个特性中的两个: 一致性(Consistency) : 所有节点访问同一份最新的…

Leetcode每日刷题之3.无重复字符的最长子串(C++)

1.题目解析 本题的目标是在给定的字符串中找出不含有重复字符的最长子串,并且返回其长度,这道题核心就是如何去重并且不能遗漏以保证子串长度最长,题目来源:3.无重复字符的最长子串 2.算法原理 本题的算法原理主要是"滑动窗口"也就…

做数据采集,你真的了解PLC插槽号吗?

有很多PLC可以在系统里配置多个独立CPU,各自有自己的任务。也有一些PLC,虽然只有一个CPU,但是,其位置是可变的。外部进行数据采集时,首先要搞明白采集目标是哪个CPU,否则,就会张冠李戴&#xff…

[大模型]Milvus Lite安装

文章目录 前提相关链接官方网站中文网站 创建虚拟环境安装Milvus连接Milvusattu连接工具attu官方开源地址下载地址连接 Milvus 是一款开源的向量数据库,它主要特点是高可用、高性能和易扩展,主要用于处理海量向量数据的实时召回。它基于诸如 FAISS、Anno…

nginx和tomcat负载均衡,动静分离

文章目录 一,tomcat1.tomca用途2.tomcat重要目录 二,nginx1.Nginx应用2.nginx作用3.nginx的正向代理和反向代理3.1正向代理3.2反向代理(单级)3.3反向代理(多级) 4.nginx负载均衡4.1Nginx支持的常见的分流算法1. 轮询(Round Robin):2.最少连接数(LeastCon…

【日记】黑神话的优化感觉有些微妙(1188 字)

正文 今天省分行一把手来我们县里。很奇怪。一整天都在为迎接他做准备。中午也没有什么午休,全员到工位上值班值守。 就算如此我还是抽了一点所剩无几的时间,体验了一下黑神话。 上午 10 点钟,远程控制电脑开始解压昨天的预载。大概解压了一个…

120KW可编程液冷负载优势和特点

120KW可编程液冷负载是一种先进的电力设备,它采用液冷技术进行冷却,具有高效、稳定、安全等特点。以下是其优势和特点的详细介绍: 1. 高效冷却:液冷负载采用液冷技术进行冷却,能够更有效地将热量传导出去,提…

基于vue3的模拟数据mock.js应用

一、mock.js介绍 Mock.js 是一个用于生成随机数据,拦截 Ajax 请求的 JavaScript 库。它主要用于前后端分离开发时,模拟后端数据接口,使得前端开发者在不需要后端实际编写接口的情况下,也能进行开发、测试。 1、主要功能 生成随…

统一认证及单点登录(SSO)技术探讨

在当今复杂的企业环境中,用户身份管理和访问控制变得越来越重要。随着企业应用系统的增多,如何高效地管理用户身份和简化用户登录流程成为了一个亟待解决的问题。统一认证和单点登录(SSO)技术应运而生,为企业提供了一种…

泊松自助法(Poisson Bootstrap Sampling):大型数据集上的自助抽样

自助抽样可以根据收集的样本推断总体的统计特征(如均值、十分位数、置信区间)。泊松自助抽样(Poisson Bootstrap Sampling)是一种用于统计分析中的重采样技术,特别是在机器学习和数据科学中用于模型评估和误差估计。这种方法的一个特点是保留…