Halcon学习---深度学习篇segment4~推断新图像

*此示例是一系列示例的一部分,该示例总结了DL分段的工作流程。 它使用MVTec药丸数据集。 四个部分是:
* 1.数据集预处理。
* 2.训练模型。
* 3.评估训练后的模型。
* 4.推断新图像。
*
*此示例涵盖第4部分:推理(应用程序)
*在新图像上训练有素的模型。
*
*请注意:此脚本使用了预先训练的模型。 要使用本示例系列的第1部分和第2部分的输出,请在下面将UsePretrainedModel设置为false。
*

dev_update_off ()

*在此示例中,推理步骤在执行之前在图形窗口中进行了说明。 将以下参数设置为false可以跳过此可视化。
ShowExampleScreens := true

* 默认情况下,此示例使用MVTec预训练的模型。 要使用在本示例系列的第2部分中训练的模型,请将以下变量设置为false。
UsePretrainedModel := true

if (ShowExampleScreens)
    * 初始示例窗口和参数等。
    dev_example_init (ShowExampleScreens, ExampleInternals)
    * 
    * 示例系列介绍文字。
    dev_display_screen_introduction (ExampleInternals)
    stop ()
    * 
    *解释推理。
    dev_display_screen_example_images (ExampleInternals, UsePretrainedModel)
    stop ()
    * 
    * 解释推理步骤。

*********************************************************************************************************
一幅图像的推断步骤:

1. 使用过程\'gen_dl_samples_from_images \'为图像生成DLSample。*********************************************************************************************************
    dev_display_screen_inference_step_1 (ExampleInternals)
    stop ()

*********************************************************************************************************
2.使用过程“ preprocess_dl_samples”对图像进行预处理,以满足训练模型的要求。*********************************************************************************************************
    dev_display_screen_inference_step_2 (ExampleInternals)
    stop ()

*********************************************************************************************************
3.使用运算符\'apply_dl_model \'应用模型*********************************************************************************************************
    dev_display_screen_inference_step_3 (ExampleInternals)
    stop ()

*********************************************************************************************************
4.对输出的分割图像进行后处理,以获取每个类别的分割区域*********************************************************************************************************
    dev_display_screen_inference_step_4 (ExampleInternals)
    stop ()
    * 
    * 通过应用程序运行显示推理的程序。*********************************************************************************************************

现在,我们将使用\ apply_dl_model \将来自示例第2部分的训练模型(segmentation_pill_defects_deep_learning_2_train.hdev)应用于一些新图像,此外,我们还将后处理步骤应用于输出分割图像:
1.为每个分类划分一个区域。
2.将分段的区域拆分为连接的组件。
3.计算每个缺陷实例的面积。将为每个图像显示最终结果。*********************************************************************************************************
    dev_display_screen_ready_to_infer (ExampleInternals)
    stop ()
    * 
    * 消除示例屏幕。
    dev_display_example_reset_windows (ExampleInternals)
endif


* *************************************************
* **   设置推理的路径和参数   ***
* *************************************************

*我们将在示例图像上演示推理。
*在实际应用中,此处将使用新传入的图像(不用于训练或评估)。
*
*在此示例中,我们从文件中读取图像。
*目录名称与要分割的图像。
ImageDir := 'pill'

* 示例数据文件夹,包含先前示例系列的输出。

ExampleDataDir := 'segment_pill_defects_data'

if (UsePretrainedModel)
    * 将预训练的模型与HALCON一起使用。
    * 
    *dict的文件名,其中包含用于预处理的参数。
    PreprocessParamFileName := 'segment_pill_defects_preprocess_param.hdict'
    * 重新训练的分割模型路径
    RetrainedModelFileName := 'segment_pill_defects.hdl'
else
    * dict的文件名,其中包含用于预处理的参数。
    PreprocessParamFileName := ExampleDataDir + '/dl_preprocess_param_400x400.hdict'
    * 重新训练的分割模型路径
    RetrainedModelFileName := ExampleDataDir + '/best_dl_model_segmentation.hdl'
endif

* 提供类名称和ID
*类名
ClassNames := ['good','contamination','crack']
* 各自的类别ID。
ClassIDs := [0,1,2]

*推理期间使用的批处理大小。
BatchSizeInference := 1

* 推断可以在GPU或CPU上完成。
UseGPU := true

* ********************
* **   Inference   ***
* ********************

* 检查GPU模式的可用性。
if (UseGPU)
    get_system ('cuda_loaded', CudaLoaded)
    get_system ('cudnn_loaded', CuDNNLoaded)
    get_system ('cublas_loaded', CuBlasLoaded)
    if (not (CudaLoaded == 'true' and CuDNNLoaded == 'true' and CuBlasLoaded == 'true'))
        UseGPU := false
    endif
endif

*检查是否所有必需的文件都存在。
check_model_availability (ExampleDataDir, PreprocessParamFileName, RetrainedModelFileName, UsePretrainedModel)

*阅读重新训练的模型。
read_dl_model (RetrainedModelFileName, DLModelHandle)
* 设置批量大小。
set_dl_model_param (DLModelHandle, 'batch_size', BatchSizeInference)
* 初始化模型以进行推断。
if (not UseGPU)
    set_dl_model_param (DLModelHandle, 'runtime', 'cpu')
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')

*获取用于预处理的参数。
read_dict (PreprocessParamFileName, [], [], DLPreprocessParam)

* 设置参数以可视化结果。
create_dict (WindowHandleDict)
create_dict (DatasetInfo)
set_dict_tuple (DatasetInfo, 'class_ids', ClassIDs)
set_dict_tuple (DatasetInfo, 'class_names', ClassNames)
create_dict (GenParamDisplay)
set_dict_tuple (GenParamDisplay, 'segmentation_exclude_class_ids', 0)
set_dict_tuple (GenParamDisplay, 'segmentation_transparency', '80')
set_dict_tuple (GenParamDisplay, 'font_size', 16)

* 列出文件,应用模型(例如,使用list_image_files)。
*在此示例中,我们手动选择了一些图像。
get_inference_images (ImageDir, ImageFiles)

* 循环遍历所有大小为BatchSizeInference的图像以进行推理。
for BatchIndex := 0 to floor(|ImageFiles| / real(BatchSizeInference)) - 1 by 1
    * 
    * 获取批处理图像的路径。
    Batch := ImageFiles[BatchIndex * BatchSizeInference:(BatchIndex + 1) * BatchSizeInference - 1]
    * 阅读批处理的图像。
    read_image (ImageBatch, Batch)
    * 
    * 生成DLSampleBatch。
    gen_dl_samples_from_images (ImageBatch, DLSampleBatch)
    * 
    * 预处理DLSampleBatch。
   preprocess_dl_samples (DLSampleBatch, DLPreprocessParam)
    * 
    * 在DLSampleBatch上应用DL模型。
   apply_dl_model (DLModelHandle, DLSampleBatch, ['segmentation_image','segmentation_confidence'], DLResultBatch)
    * 
    *后处理和可视化。
    *循环处理批次中的每个样本。
    for SampleIndex := 0 to BatchSizeInference - 1 by 1
        * 
        * 获取图像
        get_dict_object (Image, DLSampleBatch[SampleIndex], 'image')
        *获取结果图像。
        get_dict_object (SegmentationImage, DLResultBatch[SampleIndex], 'segmentation_image')
        * 
        *后处理:获取每个类的细分区域。
        threshold (SegmentationImage, ClassRegions, ClassIDs, ClassIDs)
        * 
        * 显示结果。
        dev_display_dl_data (DLSampleBatch[SampleIndex], DLResultBatch[SampleIndex], DatasetInfo, 'segmentation_image_result', GenParamDisplay, WindowHandleDict)
        get_dict_tuple (WindowHandleDict, 'segmentation_image_result', WindowHandles)
        dev_set_window (WindowHandles[0])
        * 
        * 分隔类区域的任何组成部分,并显示结果区域及其区域。
        * 
        * 获取类别区域的面积。
        region_features (ClassRegions, 'area', Areas)
        * 
        * 在这里,我们不显示第一类,因为它是“好”类,我们只想显示缺陷区域。
        for ClassIndex := 1 to |Areas| - 1 by 1
            if (Areas[ClassIndex] > 0)
                select_obj (ClassRegions, ClassRegion, ClassIndex + 1)
                * 获取分割类区域的连接组件。
                connection (ClassRegion, ConnectedRegions)
                area_center (ConnectedRegions, Area, Row, Column)
                for ConnectIndex := 0 to |Area| - 1 by 1
                    select_obj (ConnectedRegions, CurrentRegion, ConnectIndex + 1)
                    dev_disp_text (ClassNames[ClassIndex] + '\narea: ' + Area[ConnectIndex] + 'px' + '\nrow:' + Row[ConnectIndex] + ',' + 'col:' + Column[ConnectIndex] , 'image', Row[ConnectIndex] - 10, Column[ConnectIndex] + 10, 'black', [], [])
                endfor
            endif
        endfor
        * 
        * Display whether the pill is OK, or not.
        dev_display_ok_nok (Areas, WindowHandles[0])
        * 
        stop ()
    endfor
endfor

* Close windows.
dev_display_dl_data_close_windows (WindowHandleDict)

if (ShowExampleScreens)
    * Final explanations.
    dev_display_screen_final (ExampleInternals)
    stop ()
    * Close example windows.
    dev_close_example_windows (ExampleInternals)
endif

总结:

一幅图像的推断步骤:

1. 使用过程\'gen_dl_samples_from_images \'为图像生成DLSample

2.使用过程\'preprocess_dl_samples\'对图像进行预处理,以满足训练模型的要求

3.使用运算符\'apply_dl_model \'应用模型(第2部分的训练模型(segmentation_pill_defects_deep_learning_2_train.hdev))

4.对输出的分割图像进行后处理,以获取每个类别的分割区域

现在,我们将使用\ apply_dl_model \应用于一些新图像,此外,我们还将后处理步骤应用于输出分割图像:
1.为每个分类划分一个区域。
2.将分段的区域拆分为连接的组件。
3.计算每个缺陷实例的面积。将为每个图像显示最终结果。

 

 


版权声明:本文为qq_26572229原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。