Halcon学习-检测是否戴口罩?

Halcon学习-检测是否戴口罩?

1.参考aaaaabin博主:https://blog.csdn.net/weixin_44490080/article/details/104283455

参考学习:https://blog.csdn.net/weixin_44490080/article/details/103925188

2.aaaaabin博主使用2018版本的分类例程,小改形成了这个demo,在原来基础上,我增加了:保存test结果的代码;文章中黄色字体为封装函数,代码段是封装函数的内容,大家把代码段内容封装好,就可以正常使用了。

3.遇到问题:图片直接从网络下载,保存为.png后缀,读取时,读取不了。解决:2345看图王,批量转换图片为png格式,猜测保存图片是我自己改的名字,可能本身并不是png格式,导致的错误。

4.完整代码百度云盘:

链接:https://pan.baidu.com/s/1_7y7tniFdWPyZHNnuE173A 
提取码:25ri 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

read_dl_classifier ('pretrained_dl_classifier_compact.hdl', DLClassifierHandle) //读取网络模型
get_dl_classifier_param (DLClassifierHandle, 'image_width', DlImageWidth) //读取网络需要的图片大小
get_dl_classifier_param (DLClassifierHandle, 'image_height', DlImageHeight) 
get_dl_classifier_param (DLClassifierHandle, 'image_num_channels', DlNumChannels) //读取网络需要的图片通道数
get_dl_classifier_param (DLClassifierHandle, 'image_range_min', DlRangeMin) //读取网络需要的图片灰度值范围
get_dl_classifier_param (DLClassifierHandle, 'image_range_max', DlRangeMax)

Halcon学习-检测是否戴口罩?
* 训练样本图就要做一个预处理,把每个样本图才处理成224*224分辨率、3通道、-127到128亮度级图像。
dev_close_window ()
WindowWidth:=800
WindowHeight:=600
dev_open_window (0, 0, WindowWidth, WindowHeight, 'black', WindowHandle)
*数据预处理
RawDataFolder := 'ImageData/'+['OK','NG']
read_dl_classifier_data_set (RawDataFolder, 'last_folder', RawImageFiles, Labels, LabelIndices, Classes)
PreprocessedFolder := 'preprocessedFolder'
OverwritePreprocessingFolder := true
 
*RemovePreprocessingAfterExample := true
 
file_exists (PreprocessedFolder, FileExists)
if (not FileExists or OverwritePreprocessingFolder)
    if (FileExists)
        remove_dir_recursively (PreprocessedFolder)
    endif
    make_dir (PreprocessedFolder)
    for I := 0 to |Classes| - 1 by 1
        make_dir (PreprocessedFolder + '/' + Classes[I])
    endfor
    parse_filename (RawImageFiles, BaseNames, Extensions, Directories)
    ObjectFilesOut := PreprocessedFolder + '/' + Labels + '/' + BaseNames + '.hobj'
    check_output_file_names_for_duplicates (RawImageFiles, ObjectFilesOut)  

CheckOutputFiles := uniq(sort(ObjectFilesOut))
if (|CheckOutputFiles| != |RawImageFiles|)
    SortedImageFiles := sort(ObjectFilesOut)
    for I := 0 to |SortedImageFiles| - 1 by 1
        if (SortedImageFiles[I] != CheckOutputFiles[I])
            throw ('Error some file(s) have the same output filenames: ' + SortedImageFiles[I])
        endif
    endfor
endif
return ()

    for I := 0 to |RawImageFiles| - 1 by 1
        read_image (Image, RawImageFiles[I]) //读取样本文件
        zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant') //将图片缩放到网络model需求的大小
        convert_image_type (Image, Image, 'real') //将图像的灰度缩放成网络model需求范围
        RescaleRange:=(DlRangeMax - DlRangeMin)/255.0
        scale_image (Image, Image, RescaleRange, DlRangeMin)
        count_obj (Image, Number) //合成三通道图片
        for Index := 1 to Number by 1
            select_obj (Image, ObjectSelected, Index)
            count_channels (ObjectSelected, Channel)
            if (Channel != DlNumChannels) //如果图片不是三通道图,就需要将图像合成三通道图
                compose3(ObjectSelected, ObjectSelected, ObjectSelected, ThreeChannelImage)
                replace_obj (Image, ThreeChannelImage, Image, 1) //替换图元数组
            endif
        endfor
        
        * 将预处理后的图像写入hobj文件
        write_object (Image, ObjectFilesOut[I])
    endfor
    dev_clear_window ()
    dev_disp_text ('图片预处理阶段完成!', 'window', 'top', 'left', 'black', [], [])
endif
 
read_dl_classifier_data_set (PreprocessedFolder, 'last_folder', ImageFiles, Labels, LabelsIndices, Classes) 
 

 

*训练占比
TrainingPercent := 70 

 

*验证占比
ValidationPercent := 15

*将数据分割成三大块,分别为:训练集(TrainingImages, TrainingLabels)、验证集(ValidationImages, ValidationLabels)、测试集(TestImages, TestLabels)
split_dl_classifier_data_set (ImageFiles, Labels, TrainingPercent, ValidationPercent, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels) 

* This procedure divides the data set (images and ground truth labels)
* into three disjoint subsets: training, validation, and test.
* The number of images and labels in each subset is defined
* by the given percentages TrainingPercent and ValidationPercent.
* Each subset contains randomly distributed data,
* whereby the original ratio of class sizes is kept.
* 
* Check the input parameters.
if (|ImageFiles| != |GroundTruthLabels|)
    throw ('Please provide a label for every image file.')
endif
if (TrainingPercent < 0)
    throw ('TrainingPercent must not be smaller than zero.')
endif
if (ValidationPercent < 0)
    throw ('ValidationPercent must not be smaller than zero.')
endif
if (|ImageFiles| < 1)
    throw ('ImageFiles must not be empty.')
endif
if (TrainingPercent + ValidationPercent > 100)
    throw ('The sum of TrainingPercent and ValidationPercent must not be greater than 100.')
endif
* 
* Set classes and data ratios.
TrainingRatio := TrainingPercent * 0.01
ValidationRatio := ValidationPercent * 0.01
* 
* Prepare output tuples.
TrainingImages := []
TrainingLabels := []
ValidationImages := []
ValidationLabels := []
TestImages := []
TestLabels := []
* 
* Loop through all unique classes and add data
* according to the specified percentages.
UniqueClasses := uniq(sort(GroundTruthLabels))
for ClassIndex := 0 to |UniqueClasses| - 1 by 1
    * Select all images and ground truth labels with the class.
    Class := UniqueClasses[ClassIndex]
    ClassIndices := find(GroundTruthLabels,Class)
    ImageFilesClass := ImageFiles[ClassIndices]
    LabelsClass := gen_tuple_const(|ImageFilesClass|,Class)
    * Shuffle the images in this class.
    tuple_shuffle (ImageFilesClass, ImageFilesClass)
    * Determine the boundaries of the respective selection.
    IndexTrainingEnd := int(floor(|ImageFilesClass| * TrainingRatio)) - 1
    IndexValidationEnd := int(floor(|ImageFilesClass| * (ValidationRatio + TrainingRatio))) - 1
    * Add the respective images and labels.
    TrainingImages := [TrainingImages,ImageFilesClass[0:IndexTrainingEnd]]
    TrainingLabels := [TrainingLabels,LabelsClass[0:IndexTrainingEnd]]
    ValidationImages := [ValidationImages,ImageFilesClass[IndexTrainingEnd + 1:IndexValidationEnd]]
    ValidationLabels := [ValidationLabels,LabelsClass[IndexTrainingEnd + 1:IndexValidationEnd]]
    TestImages := [TestImages,ImageFilesClass[IndexValidationEnd + 1:|ImageFilesClass| - 1]]
    TestLabels := [TestLabels,LabelsClass[IndexValidationEnd + 1:|ImageFilesClass| - 1]]
endfor
* 
* Shuffle the output.
tuple_shuffle ([0:|TrainingImages| - 1], TrainingSequence)
TrainingImages := TrainingImages[TrainingSequence]
TrainingLabels := TrainingLabels[TrainingSequence]
tuple_shuffle ([0:|ValidationImages| - 1], ValidationSequence)
ValidationImages := ValidationImages[ValidationSequence]
ValidationLabels := ValidationLabels[ValidationSequence]
tuple_shuffle ([0:|TestImages| - 1], TestSequence)
TestImages := TestImages[TestSequence]
TestLabels := TestLabels[TestSequence]
return ()

stop ()
**设置超参数**
*设置类别超参数
set_dl_classifier_param (DLClassifierHandle, 'classes', Classes) 
BatchSize := 5
set_dl_classifier_param (DLClassifierHandle, 'batch_size', BatchSize) 
 
try 

   *初始化网络模型
    set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately') 
catch (Exception) 

     dev_disp_error_text (Exception) 

ErrorAndAdviceText := 'An error occurred during runtime initialization.'
ErrorAndAdviceText := [ErrorAndAdviceText,'']
ErrorAndAdviceText := [ErrorAndAdviceText,'Error ' + Exception[0] + ': \'' + Exception[2] + '\'']
if (Exception[0] == 4104)
    * In case of out of device memory we can give advice.
    ErrorAndAdviceText := [ErrorAndAdviceText,'']
    ErrorAndAdviceText := [ErrorAndAdviceText,'Install a GPU with more RAM or reduce the batch size.']
    ErrorAndAdviceText := [ErrorAndAdviceText,'']
    ErrorAndAdviceText := [ErrorAndAdviceText,'Note that changing the batch size will have an influence on the results.']
endif
dev_clear_window ()
* Display text with line breaks after 60 characters.
dev_disp_text (regexp_replace(ErrorAndAdviceText + ' ',['(.{0,60})\\s','replace_all'],'$1\n'), 'window', 'center', 'left', 'red', [], [])
return ()

 

    stop () 
endtry 
 

*学习率

InitialLearningRate := 0.001 
set_dl_classifier_param (DLClassifierHandle, 'learning_rate', InitialLearningRate) 
 

*学习率变化参数
LearningRateStepEveryNthEpoch := 50 
LearningRateStepRatio := 0.1 

 

*迭代次数
NumEpochs := 100
 
**训练分类器**
dev_clear_window () 
* 每次迭代的间隔,它会体现在学习过程中的图标上的‘x’轴
PlotIterationInterval := 1

*将训练好的网络模型序列化

FileName := 'classifier_minist.hdl' 

*训练模型

train_fruit_classifier (DLClassifierHandle, FileName, NumEpochs, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, LearningRateStepEveryNthEpoch, LearningRateStepRatio, PlotIterationInterval, WindowHandle) 

* For the plot during training,
* we need to concatenate some intermediate results.
TrainingErrors := []
ValidationErrors := []
LearningRates := []
Epochs := []
LossByIteration := []
get_dl_classifier_param (DLClassifierHandle, 'batch_size', BatchSize)
MinValidationError := 1
* 
* Create a tuple that includes all the iterations
* where the plot should be computed (including the last ieration).
NumBatchesInEpoch := int(floor(|TrainingImages| / real(BatchSize)))
NumTotalIterations := (NumBatchesInEpoch * NumEpochs) - 1
PlottedIterations := round([NumBatchesInEpoch * [0:PlotEveryNthEpoch:NumEpochs - 1],NumTotalIterations])
* 
* TrainSequence is used for easier indexing of the training data.
tuple_gen_sequence (0, |TrainingImages| - 1, 1, TrainSequence)
* 
* Select a subset of the training data set
* in order to obtain a fast approximation
* of the training error during training (plotting).
SelectPercentageTrainingImages := 100
select_percentage_dl_classifier_data (TrainingImages, TrainingLabels, SelectPercentageTrainingImages, TrainingImagesSelected, TrainingLabelsSelected)
* 
for Epoch := 0 to NumEpochs - 1 by 1
    * In order to get randomness in each epoch,
    * the training set is shuffled every epoch.
    tuple_shuffle (TrainSequence, TrainSequence)
    for Iteration := 0 to NumBatchesInEpoch - 1 by 1
        * Select a batch from the training data set.
        BatchStart := Iteration * BatchSize
        BatchEnd := BatchStart + (BatchSize - 1)
        BatchIndices := TrainSequence[BatchStart:BatchEnd]
        BatchImageFiles := TrainingImages[BatchIndices]
        BatchLabels := TrainingLabels[BatchIndices]
        * 
        * Read the image of the current batch.
        read_image (BatchImages, BatchImageFiles)
        * Augment the images to get a better variety of training images.
        GenParamName := 'mirror'
        GenParamValue := 'rc'
        augment_images (BatchImages, BatchImages, GenParamName, GenParamValue)
        * 
        * Train the network with these images and ground truth labels.
        train_dl_classifier_batch (BatchImages, DLClassifierHandle, BatchLabels, DLClassifierTrainResultHandle)
        * You can access the current value of the loss function,
        * which should decrease during the training.
        get_dl_classifier_train_result (DLClassifierTrainResultHandle, 'loss', Loss)
        * Store the loss in a tuple .
        LossByIteration := [LossByIteration,Loss]
        * 
        * In regular intervals, we want to evaluate
        * how well our classifier performs.
        CurrentIteration := int(Iteration + (NumBatchesInEpoch * Epoch))
        if (sum(CurrentIteration [==] PlottedIterations))
            * Plot the progress regularly.
            * Evaluate the current classifier on the training and validation set.
            apply_dl_classifier_batchwise (TrainingImagesSelected, DLClassifierHandle, TrainingDLClassifierResultIDs, TrainingPredictedLabels, TrainingConfidences)
            apply_dl_classifier_batchwise (ValidationImages, DLClassifierHandle, ValidationDLClassifierResultIDs, ValidationPredictedLabels, ValidationConfidences)
            * Evaluate the top-1 error on each dataset.
            evaluate_dl_classifier (TrainingLabelsSelected, DLClassifierHandle, TrainingDLClassifierResultIDs, 'top1_error', 'global', TrainingTop1Error)
            evaluate_dl_classifier (ValidationLabels, DLClassifierHandle, ValidationDLClassifierResultIDs, 'top1_error', 'global', ValidationTop1Error)
            * Concatenate the values for the plot.
            get_dl_classifier_param (DLClassifierHandle, 'learning_rate', LearningRate)
            TrainingErrors := [TrainingErrors,TrainingTop1Error]
            ValidationErrors := [ValidationErrors,ValidationTop1Error]
            LearningRates := [LearningRates,LearningRate]
            Epochs := [Epochs,PlottedIterations[|Epochs|] / real(NumBatchesInEpoch)]
            * Plot validation and error against the epochs in order to
            * observe the progress of the training.
            plot_dl_classifier_training_progress (TrainingErrors, ValidationErrors, LearningRates, Epochs, NumEpochs, WindowHandle)
            if (ValidationTop1Error <= MinValidationError)
                write_dl_classifier (DLClassifierHandle, FileName)
                MinValidationError := ValidationTop1Error
            endif
        endif
    endfor
    * Reduce the learning rate every nth epoch.
    if ((Epoch + 1) % LearningRateStepEveryNthEpoch == 0)
        set_dl_classifier_param (DLClassifierHandle, 'learning_rate', LearningRate * LearningRateStepRatio)
        get_dl_classifier_param (DLClassifierHandle, 'learning_rate', LearningRate)
    endif
endfor
stop ()
return ()

Halcon学习-检测是否戴口罩?(训练结果,此处黄色曲线验证误差率并不好)

clear_dl_classifier (DLClassifierHandle)  //清除网络句柄

read_dl_classifier (FileName, DLClassifierHandle) //读取序列化网络模型

*计算混淆矩
get_error_for_confusion_matrix (ValidationImages, DLClassifierHandle, Top1ClassValidation)

apply_dl_classifier_batchwise (Images, DLClassifierHandle, DLClassifierResultIDsTest, PredictedClasses, Confidences)
* 
Top1PredictedClasses := []
for Index := 0 to PredictedClasses.length() - 1 by 1
    Top1PredictedClasses := [Top1PredictedClasses,PredictedClasses.at(Index)[0]]
endfor
return ()

gen_confusion_matrix (ValidationLabels, Top1ClassValidation, [], [], WindowHandle, ConfusionMatrix) //生成混淆矩模型

* This procedure computes a confusion matrix.
* Therefore, it compares the classes
* assigned in GroundTruthLabels and PredictedClasses.
* The resulting confusion matrix can be
* visualized, returned, or both.
* In each case, the output can be changed
* via generic parameters using GenParamName and GenParamValue.
* For the visualization, the graphics window
* must be specified with WindowHandle.
* 
if (|GroundTruthLabels| != |PredictedClasses|)
    throw ('Number of ground truth labels and predicted classes must be equal.')
endif
* 
* Set generic parameter defaults.
DisplayMatrix := 'absolute'
ReturnMatrix := 'absolute'
DisplayColor := 'true'
DisplayColumnWidth := 'minimal'
* 
* Parse generic parameters.
for GenParamIndex := 0 to |GenParamName| - 1 by 1
    if (GenParamName[GenParamIndex] == 'display_matrix')
        * Set 'display_matrix'.
        DisplayMatrix := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'return_matrix')
        * Set 'return_matrix'.
        ReturnMatrix := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'display_color')
        * Set 'display_color'.
        DisplayColor := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'display_column_width')
        * Set 'display_column_width'.
        DisplayColumnWidth := GenParamValue[GenParamIndex]
    else
        throw ('Unknown generic parameter: \'' + GenParamName[GenParamIndex] + '\'')
    endif
endfor
* 
if (DisplayMatrix == 'relative' or ReturnMatrix == 'relative' or DisplayColor == 'true')
    CalculateRelativeMatrix := 1
else
    CalculateRelativeMatrix := 0
endif
* 
* Calculate the confusion matrix with absolute values
* and the confusion matrix with relative errors.
* We start with an empty matrix
* and add the number of matching labels.
Classes := uniq(sort(GroundTruthLabels))
NumClasses := |Classes|
create_matrix (NumClasses, NumClasses, 0, AbsoluteMatrixID)
if (CalculateRelativeMatrix)
    create_matrix (NumClasses, NumClasses, 0, RelativeMatrixID)
endif
for ColumnMatrix := 0 to NumClasses - 1 by 1
    Class := Classes[ColumnMatrix]
    ThisLabel := GroundTruthLabels [==] Class
    if (CalculateRelativeMatrix)
        * Obtain the number of ground truth labels per class.
        NumClassGroundTruth := sum(ThisLabel)
    endif
    for RowMatrix := 0 to NumClasses - 1 by 1
        * Select classes for this row/column.
        PredictedClass := Classes[RowMatrix]
        * Check whether the input data
        * corresponds to these classes.
        ThisPredictedClass := PredictedClasses [==] PredictedClass
        * Count the number of elements where the predicted class
        * matches the ground truth label.
        NumMatches := sum((ThisLabel + ThisPredictedClass) [==] 2)
        * Set value in matrix.
        set_value_matrix (AbsoluteMatrixID, RowMatrix, ColumnMatrix, NumMatches)
        if (CalculateRelativeMatrix)
            if (NumClassGroundTruth > 0)
                RelativeError := real(NumMatches) / NumClassGroundTruth
            else
                RelativeError := 0
            endif
            set_value_matrix (RelativeMatrixID, RowMatrix, ColumnMatrix, RelativeError)
        endif
    endfor
endfor
* 
* Return the result.
if (ReturnMatrix == 'absolute')
    copy_matrix (AbsoluteMatrixID, ConfusionMatrix)
elseif (ReturnMatrix == 'relative')
    copy_matrix (RelativeMatrixID, ConfusionMatrix)
elseif (ReturnMatrix == 'none')
    * No matrix is returned.
else
    throw ('Unsupported mode for \'return_matrix\'')
endif
* 
* Display the matrix.
if (DisplayMatrix != 'none')
    * 
    * Find maximal string width and set display position parameters.
    StringWidths := []
    * Get the string width of each class.
    for StringIndex := 0 to |Classes| - 1 by 1
        String := Classes[StringIndex]
        get_string_extents (WindowHandle, String, Ascent, Descent, StringWidth, StringHeight)
        StringWidths := [StringWidths,StringWidth]
    endfor
    * The columns should have a minimum width for 4 characters.
    get_string_extents (WindowHandle, 'test', Ascent, Descent, StringWidth, StringHeight)
    MaxStringWidth := max2(max(StringWidths),StringWidth)
    * Get the maximum string width
    * and resize the window accordingly.
    RowStart := 80
    RowDistance := StringHeight + 10
    RowEnd := StringHeight * 7
    ColumnStart := 50 + MaxStringWidth
    ColumnOffset := 20
    ColumnEnd := ColumnOffset
    * 
    * Adapt the window size to fit the confusion matrix.
    if (DisplayColumnWidth == 'minimal')
        * Every column of the confusion matrix is as narrow as possible
        * based to the respective string widths.
        Width := sum(StringWidths) + ColumnOffset * NumClasses + ColumnStart + ColumnEnd
    elseif (DisplayColumnWidth == 'equal')
        * Every column of the confusion matrix should have the same width.
        * based on the maximum string width.
        Width := (MaxStringWidth + ColumnOffset) * NumClasses + ColumnStart + ColumnEnd
    else
        throw ('')
    endif
    Height := RowDistance * NumClasses + RowStart + RowEnd
    dev_set_window (WindowHandle)
    dev_clear_window ()
    * 
    * Set reasonable limits for graphics window (adapt if necessary).
    WidthLimit := [450,1920]
    HeightLimit := [250,1080]
    if (Width > WidthLimit[1] or Height > HeightLimit[1])
        throw ('Confusion Matrix does not fit into graphics window. Please adapt font and/or size limits.')
    endif
    dev_resize_window_fit_size (0, 0, Width, Height, WidthLimit, HeightLimit)
    * 
    * Get display coordinates.
    * Get row coordinates for display.
    TextRow := []
    for ColumnMatrix := 0 to NumClasses - 1 by 1
        TextRow := [TextRow,[0:RowDistance:(NumClasses - 1) * RowDistance]]
    endfor
    * Get column coordinates for display.
    TextColumn := []
    for Index := 0 to NumClasses - 1 by 1
        TextColumn := [TextColumn,gen_tuple_const(NumClasses,ColumnStart)]
        if (DisplayColumnWidth == 'minimal')
            ColumnStart := ColumnStart + StringWidths[Index] + ColumnOffset
        elseif (DisplayColumnWidth == 'equal')
            ColumnStart := ColumnStart + MaxStringWidth + ColumnOffset
        endif
    endfor
    * Display the confusion matrix with a margin from the top.
    TextRow := TextRow + RowStart
    * Display axis titles.
    dev_disp_text ('Ground truth labels', 'window', 'top', 'right', 'white', 'box', 'false')
    dev_disp_text ('Predicted classes', 'window', 'bottom', 'left', 'white', 'box', 'false')
    for Index := 0 to |Classes| - 1 by 1
        Text := Classes[Index]
        * Display predicted class names.
        Row := TextRow[Index]
        Column := TextColumn[0] - MaxStringWidth - ColumnOffset
        dev_disp_text (Text, 'window', Row, Column, 'light gray', 'box', 'false')
        * Display ground truth label names.
        Row := TextRow[0] - RowDistance
        Column := TextColumn[Index * NumClasses]
        dev_disp_text (Text, 'window', Row, Column, 'light gray', 'box', 'false')
    endfor
    * 
    * Get the confusion matrix values for display.
    if (DisplayMatrix == 'absolute')
        * Displayed matrix corresponds to the transposed returned matrix.
        transpose_matrix (AbsoluteMatrixID, AbsoluteTransposedMatrixID)
        get_full_matrix (AbsoluteTransposedMatrixID, MatrixText)
        clear_matrix (AbsoluteTransposedMatrixID)
        * Align the numbers right.
        max_matrix (AbsoluteMatrixID, 'full', MatrixMaxID)
        get_full_matrix (MatrixMaxID, MaxValue)
        clear_matrix (MatrixMaxID)
        StringConversion := int(ceil(log10(MaxValue))) + '.0f'
        MatrixText := MatrixText$StringConversion
    else
        * Displayed matrix corresponds to the transposed returned matrix.
        transpose_matrix (RelativeMatrixID, RelativeTransposedMatrixID)
        get_full_matrix (RelativeTransposedMatrixID, MatrixText)
        clear_matrix (RelativeTransposedMatrixID)
        MatrixText := MatrixText$'.2f'
    endif
    * Set color for displayed confusion matrix.
    if (DisplayColor == 'true')
        tuple_gen_const (|MatrixText|, '#666666', TextColor)
        * Use the relative values to adapt the color of the text.
        transpose_matrix (RelativeMatrixID, RelativeTransposedMatrixID)
        get_full_matrix (RelativeTransposedMatrixID, RelativeValues)
        clear_matrix (RelativeTransposedMatrixID)
        * Set the colors and respective thresholds for the off-diagonal values.
        Thresholds := [0.0,0.05,0.1,0.2]
        Colors := ['#8C4D4D','#B33333','#D91A1A','#FF0000']
        for Index := 0 to |Thresholds| - 1 by 1
            tuple_greater_elem (RelativeValues, Thresholds[Index], Greater)
            tuple_find (Greater, 1, Indices)
            if (Indices != -1)
                tuple_replace (TextColor, Indices, Colors[Index], TextColor)
            else
                break
            endif
        endfor
        * Set the colors and respective thresholds for the diagonal values.
        Thresholds := [-0.01,0.60,0.80,0.90,0.95,0.98]
        Colors := ['#666666','#508650','#419C41','#2BBD2B','#15DE15','#00FF00']
        for DiagonalIndex := 0 to NumClasses - 1 by 1
            get_value_matrix (RelativeMatrixID, DiagonalIndex, DiagonalIndex, Value)
            for Index := 0 to |Thresholds| - 1 by 1
                if (Value > Thresholds[Index])
                    TextColor[DiagonalIndex * (NumClasses + 1)] := Colors[Index]
                else
                    break
                endif
            endfor
        endfor
    else
        * Default value for the text color.
        tuple_gen_const (|MatrixText|, 'white', TextColor)
    endif
    * 
    * Display confusion matrix.
    dev_disp_text (MatrixText, 'window', TextRow, TextColumn, TextColor, 'box', 'false')
    * 
    * Clean up.
    if (CalculateRelativeMatrix)
        clear_matrix (RelativeMatrixID)
    endif
    clear_matrix (AbsoluteMatrixID)
endif
return ()* This procedure computes a confusion matrix.
* Therefore, it compares the classes
* assigned in GroundTruthLabels and PredictedClasses.
* The resulting confusion matrix can be
* visualized, returned, or both.
* In each case, the output can be changed
* via generic parameters using GenParamName and GenParamValue.
* For the visualization, the graphics window
* must be specified with WindowHandle.
* 
if (|GroundTruthLabels| != |PredictedClasses|)
    throw ('Number of ground truth labels and predicted classes must be equal.')
endif
* 
* Set generic parameter defaults.
DisplayMatrix := 'absolute'
ReturnMatrix := 'absolute'
DisplayColor := 'true'
DisplayColumnWidth := 'minimal'
* 
* Parse generic parameters.
for GenParamIndex := 0 to |GenParamName| - 1 by 1
    if (GenParamName[GenParamIndex] == 'display_matrix')
        * Set 'display_matrix'.
        DisplayMatrix := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'return_matrix')
        * Set 'return_matrix'.
        ReturnMatrix := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'display_color')
        * Set 'display_color'.
        DisplayColor := GenParamValue[GenParamIndex]
    elseif (GenParamName[GenParamIndex] == 'display_column_width')
        * Set 'display_column_width'.
        DisplayColumnWidth := GenParamValue[GenParamIndex]
    else
        throw ('Unknown generic parameter: \'' + GenParamName[GenParamIndex] + '\'')
    endif
endfor
* 
if (DisplayMatrix == 'relative' or ReturnMatrix == 'relative' or DisplayColor == 'true')
    CalculateRelativeMatrix := 1
else
    CalculateRelativeMatrix := 0
endif
* 
* Calculate the confusion matrix with absolute values
* and the confusion matrix with relative errors.
* We start with an empty matrix
* and add the number of matching labels.
Classes := uniq(sort(GroundTruthLabels))
NumClasses := |Classes|
create_matrix (NumClasses, NumClasses, 0, AbsoluteMatrixID)
if (CalculateRelativeMatrix)
    create_matrix (NumClasses, NumClasses, 0, RelativeMatrixID)
endif
for ColumnMatrix := 0 to NumClasses - 1 by 1
    Class := Classes[ColumnMatrix]
    ThisLabel := GroundTruthLabels [==] Class
    if (CalculateRelativeMatrix)
        * Obtain the number of ground truth labels per class.
        NumClassGroundTruth := sum(ThisLabel)
    endif
    for RowMatrix := 0 to NumClasses - 1 by 1
        * Select classes for this row/column.
        PredictedClass := Classes[RowMatrix]
        * Check whether the input data
        * corresponds to these classes.
        ThisPredictedClass := PredictedClasses [==] PredictedClass
        * Count the number of elements where the predicted class
        * matches the ground truth label.
        NumMatches := sum((ThisLabel + ThisPredictedClass) [==] 2)
        * Set value in matrix.
        set_value_matrix (AbsoluteMatrixID, RowMatrix, ColumnMatrix, NumMatches)
        if (CalculateRelativeMatrix)
            if (NumClassGroundTruth > 0)
                RelativeError := real(NumMatches) / NumClassGroundTruth
            else
                RelativeError := 0
            endif
            set_value_matrix (RelativeMatrixID, RowMatrix, ColumnMatrix, RelativeError)
        endif
    endfor
endfor
* 
* Return the result.
if (ReturnMatrix == 'absolute')
    copy_matrix (AbsoluteMatrixID, ConfusionMatrix)
elseif (ReturnMatrix == 'relative')
    copy_matrix (RelativeMatrixID, ConfusionMatrix)
elseif (ReturnMatrix == 'none')
    * No matrix is returned.
else
    throw ('Unsupported mode for \'return_matrix\'')
endif
* 
* Display the matrix.
if (DisplayMatrix != 'none')
    * 
    * Find maximal string width and set display position parameters.
    StringWidths := []
    * Get the string width of each class.
    for StringIndex := 0 to |Classes| - 1 by 1
        String := Classes[StringIndex]
        get_string_extents (WindowHandle, String, Ascent, Descent, StringWidth, StringHeight)
        StringWidths := [StringWidths,StringWidth]
    endfor
    * The columns should have a minimum width for 4 characters.
    get_string_extents (WindowHandle, 'test', Ascent, Descent, StringWidth, StringHeight)
    MaxStringWidth := max2(max(StringWidths),StringWidth)
    * Get the maximum string width
    * and resize the window accordingly.
    RowStart := 80
    RowDistance := StringHeight + 10
    RowEnd := StringHeight * 7
    ColumnStart := 50 + MaxStringWidth
    ColumnOffset := 20
    ColumnEnd := ColumnOffset
    * 
    * Adapt the window size to fit the confusion matrix.
    if (DisplayColumnWidth == 'minimal')
        * Every column of the confusion matrix is as narrow as possible
        * based to the respective string widths.
        Width := sum(StringWidths) + ColumnOffset * NumClasses + ColumnStart + ColumnEnd
    elseif (DisplayColumnWidth == 'equal')
        * Every column of the confusion matrix should have the same width.
        * based on the maximum string width.
        Width := (MaxStringWidth + ColumnOffset) * NumClasses + ColumnStart + ColumnEnd
    else
        throw ('')
    endif
    Height := RowDistance * NumClasses + RowStart + RowEnd
    dev_set_window (WindowHandle)
    dev_clear_window ()
    * 
    * Set reasonable limits for graphics window (adapt if necessary).
    WidthLimit := [450,1920]
    HeightLimit := [250,1080]
    if (Width > WidthLimit[1] or Height > HeightLimit[1])
        throw ('Confusion Matrix does not fit into graphics window. Please adapt font and/or size limits.')
    endif
    dev_resize_window_fit_size (0, 0, Width, Height, WidthLimit, HeightLimit)
    * 
    * Get display coordinates.
    * Get row coordinates for display.
    TextRow := []
    for ColumnMatrix := 0 to NumClasses - 1 by 1
        TextRow := [TextRow,[0:RowDistance:(NumClasses - 1) * RowDistance]]
    endfor
    * Get column coordinates for display.
    TextColumn := []
    for Index := 0 to NumClasses - 1 by 1
        TextColumn := [TextColumn,gen_tuple_const(NumClasses,ColumnStart)]
        if (DisplayColumnWidth == 'minimal')
            ColumnStart := ColumnStart + StringWidths[Index] + ColumnOffset
        elseif (DisplayColumnWidth == 'equal')
            ColumnStart := ColumnStart + MaxStringWidth + ColumnOffset
        endif
    endfor
    * Display the confusion matrix with a margin from the top.
    TextRow := TextRow + RowStart
    * Display axis titles.
    dev_disp_text ('Ground truth labels', 'window', 'top', 'right', 'white', 'box', 'false')
    dev_disp_text ('Predicted classes', 'window', 'bottom', 'left', 'white', 'box', 'false')
    for Index := 0 to |Classes| - 1 by 1
        Text := Classes[Index]
        * Display predicted class names.
        Row := TextRow[Index]
        Column := TextColumn[0] - MaxStringWidth - ColumnOffset
        dev_disp_text (Text, 'window', Row, Column, 'light gray', 'box', 'false')
        * Display ground truth label names.
        Row := TextRow[0] - RowDistance
        Column := TextColumn[Index * NumClasses]
        dev_disp_text (Text, 'window', Row, Column, 'light gray', 'box', 'false')
    endfor
    * 
    * Get the confusion matrix values for display.
    if (DisplayMatrix == 'absolute')
        * Displayed matrix corresponds to the transposed returned matrix.
        transpose_matrix (AbsoluteMatrixID, AbsoluteTransposedMatrixID)
        get_full_matrix (AbsoluteTransposedMatrixID, MatrixText)
        clear_matrix (AbsoluteTransposedMatrixID)
        * Align the numbers right.
        max_matrix (AbsoluteMatrixID, 'full', MatrixMaxID)
        get_full_matrix (MatrixMaxID, MaxValue)
        clear_matrix (MatrixMaxID)
        StringConversion := int(ceil(log10(MaxValue))) + '.0f'
        MatrixText := MatrixText$StringConversion
    else
        * Displayed matrix corresponds to the transposed returned matrix.
        transpose_matrix (RelativeMatrixID, RelativeTransposedMatrixID)
        get_full_matrix (RelativeTransposedMatrixID, MatrixText)
        clear_matrix (RelativeTransposedMatrixID)
        MatrixText := MatrixText$'.2f'
    endif
    * Set color for displayed confusion matrix.
    if (DisplayColor == 'true')
        tuple_gen_const (|MatrixText|, '#666666', TextColor)
        * Use the relative values to adapt the color of the text.
        transpose_matrix (RelativeMatrixID, RelativeTransposedMatrixID)
        get_full_matrix (RelativeTransposedMatrixID, RelativeValues)
        clear_matrix (RelativeTransposedMatrixID)
        * Set the colors and respective thresholds for the off-diagonal values.
        Thresholds := [0.0,0.05,0.1,0.2]
        Colors := ['#8C4D4D','#B33333','#D91A1A','#FF0000']
        for Index := 0 to |Thresholds| - 1 by 1
            tuple_greater_elem (RelativeValues, Thresholds[Index], Greater)
            tuple_find (Greater, 1, Indices)
            if (Indices != -1)
                tuple_replace (TextColor, Indices, Colors[Index], TextColor)
            else
                break
            endif
        endfor
        * Set the colors and respective thresholds for the diagonal values.
        Thresholds := [-0.01,0.60,0.80,0.90,0.95,0.98]
        Colors := ['#666666','#508650','#419C41','#2BBD2B','#15DE15','#00FF00']
        for DiagonalIndex := 0 to NumClasses - 1 by 1
            get_value_matrix (RelativeMatrixID, DiagonalIndex, DiagonalIndex, Value)
            for Index := 0 to |Thresholds| - 1 by 1
                if (Value > Thresholds[Index])
                    TextColor[DiagonalIndex * (NumClasses + 1)] := Colors[Index]
                else
                    break
                endif
            endfor
        endfor
    else
        * Default value for the text color.
        tuple_gen_const (|MatrixText|, 'white', TextColor)
    endif
    * 
    * Display confusion matrix.
    dev_disp_text (MatrixText, 'window', TextRow, TextColumn, TextColor, 'box', 'false')
    * 
    * Clean up.
    if (CalculateRelativeMatrix)
        clear_matrix (RelativeMatrixID)
    endif
    clear_matrix (AbsoluteMatrixID)
endif
return ()

Halcon学习-检测是否戴口罩?(混淆矩阵)

dev_disp_text ('Validation data', 'window', 'top', 'left', 'gray', 'box', 'false')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
clear_matrix (ConfusionMatrix)
dev_clear_window ()
 
 
clear_dl_classifier (DLClassifierHandle)
read_dl_classifier (FileName, DLClassifierHandle)
 
set_dl_classifier_param (DLClassifierHandle, 'batch_size', 1)
 
set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately')
 
dev_resize_window_fit_size (0, 0, WindowWidth, WindowHeight, -1, -1)

ResultDir := 'result/'  
file_exists(ResultDir, FileExists)
if(FileExists)
    remove_dir_recursively (ResultDir)  //将已存在的result删除
    *remove_dir_recursively (ResultDir + 'labels/')
endif
make_dir (ResultDir)  //生成目录
*使用训练好的模型进行检测
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
list_files ('Test', 'files', Files)
for Index := 0 to |Files|-1 by 1
    read_image (Image, Files[Index])
    zoom_image_size (Image, Image, DlImageWidth, DlImageHeight, 'constant')
    convert_image_type (Image, Image, 'real')
    RescaleRange:=(DlRangeMax - DlRangeMin)/255.0
    scale_image (Image, Image, RescaleRange, DlRangeMin)
    
    apply_dl_classifier (Image, DLClassifierHandle, DLClassifierResultHandle)
    
    get_dl_classifier_result (DLClassifierResultHandle, 'all', 'predicted_classes', PredictedClass)
   
    *clear_dl_1classifier_result (DLClassifierResultHandle)
    * 
    dev_display (Image)
    Text := 'Predicted class: ' + PredictedClass
    if (PredictedClass == 'OK')
        disp_message (WindowHandle, Text, 'window', 12, 12, 'green', 'false')
        parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
        dump_window (WindowHandle, 'png', ResultDir + BaseName)
    else
        disp_message (WindowHandle, Text, 'window', 12, 12, 'red', 'false')
        parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
        dump_window (WindowHandle, 'png', ResultDir + BaseName)
    endif
    stop ()
endfor
clear_dl_classifier (DLClassifierHandle)

Halcon学习-检测是否戴口罩?Halcon学习-检测是否戴口罩?

(模型还需要优化,小女孩识别应该是:OK,这里误判为NG,10张错一张)

优化思路:

1.增加小女孩这样的训练集  2.增大数据集、增加训练epoch、适当修改学习率变化参数

 

第二次训练:

Halcon学习-检测是否戴口罩?Halcon学习-检测是否戴口罩?

Halcon学习-检测是否戴口罩?(识别错误图)10张错1张

 

上一篇:halcon C# 学习笔记-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=【不定时更新】成都


下一篇:Halcon 识别车牌学习笔记