Halcon之AI图像分类算子的推理使用




*** 一 读取模型
read_dl_model ('model_best.hdl', DLModelHandle)

*** 二 设置参数

TestImageDir := 'test/'
GpuId := 0
ClassifyThreshold := 0.5
dev_update_off()
dev_close_window ()


set_dl_model_param (DLModelHandle, 'batch_size', 1)
if (GpuId != 0)
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')

*** 三 获取推理参数

get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
get_dl_model_param(DLModelHandle, 'class_names', ClassNames)
get_dl_model_param(DLModelHandle, 'class_ids', ClassIds)

dev_open_window(0, 0, 600, ImageDimensions[1] * 600 / ImageDimensions[0], 'black', WindowHandle)
set_font(WindowHandle, 'Microsoft YaHei UI-Bold-36')

*** 四 图像读取
create_dict (DLSample)
read_image (Image, 'C:/Users/10464/Desktop/classification/classification/test/pill_magnesium_crack_203.png')
count_seconds(Start)
zoom_image_size (Image, ImagePreprocessedByte, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessedByte, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 

*** 五 模型推理
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
get_dict_tuple (DLResult, 'classification_confidences', Confidences)
get_dict_tuple (DLResult, 'classification_class_names', PredictClasses)


*** 六 结果显示

tuple_max (Confidences, Max)
tuple_find (Confidences, Max, IndexMax)    
count_seconds(End)
dev_clear_window ()
dev_display(Image)

Text := PredictClasses[IndexMax] + ', ' + Max$'.2f'
dev_disp_text (Text, 'window', 'top', 'left', 'red', 'box', 'false')    


*显示热度图
create_dict (HeatmapParam)
ChosenLayers := ['res2_block0_conv1']
*set_dict_tuple (HeatmapParam, 'target_conv_layer', ChosenLayers)
*set_dict_tuple (HeatmapParam, 'use_conv_only', 'false')
*set_dict_tuple (HeatmapParam, 'scaling', 'scale_after_relu')
create_dict (DLDatasetInfo)
set_dict_tuple (DLDatasetInfo, 'class_names', ClassNames)
set_dict_tuple (DLDatasetInfo, 'class_ids', ClassIds)
TargetClassID := []
create_dict (WindowHandleDict)

zoom_image_size (Image, ImagePreprocessed, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessed, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 
gen_dl_model_heatmap (DLModelHandle, DLSample, 'grad_cam', TargetClassID, HeatmapParam, DLResult)
dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, 'heatmap_grad_cam', [], WindowHandleDict)
get_dict_tuple (WindowHandleDict, 'heatmap_grad_cam', WindowHandle)


单独获取热力图




*** 一 读取模型
read_dl_model ('model_best.hdl', DLModelHandle)

*** 二 设置参数

TestImageDir := 'test/'
GpuId := 0
ClassifyThreshold := 0.5
dev_update_off()
dev_close_window ()


set_dl_model_param (DLModelHandle, 'batch_size', 1)
if (GpuId != 0)
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')

*** 三 获取推理参数

get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
get_dl_model_param(DLModelHandle, 'class_names', ClassNames)
get_dl_model_param(DLModelHandle, 'class_ids', ClassIds)

dev_open_window(0, 0, 600, ImageDimensions[1] * 600 / ImageDimensions[0], 'black', WindowHandle)
set_font(WindowHandle, 'Microsoft YaHei UI-Bold-36')

*** 四 图像读取
create_dict (DLSample)
read_image (Image, 'C:/Users/10464/Desktop/classification/classification/test/pill_magnesium_crack_203.png')
count_seconds(Start)
zoom_image_size (Image, ImagePreprocessedByte, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessedByte, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 

*** 五 模型推理
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
get_dict_tuple (DLResult, 'classification_confidences', Confidences)
get_dict_tuple (DLResult, 'classification_class_names', PredictClasses)


*** 六 结果显示

tuple_max (Confidences, Max)
tuple_find (Confidences, Max, IndexMax)    
count_seconds(End)
dev_clear_window ()
dev_display(Image)

Text := PredictClasses[IndexMax] + ', ' + Max$'.2f'
dev_disp_text (Text, 'window', 'top', 'left', 'red', 'box', 'false')    


*显示热度图
create_dict (HeatmapParam)
ChosenLayers := ['res2_block0_conv1']
*set_dict_tuple (HeatmapParam, 'target_conv_layer', ChosenLayers)
*set_dict_tuple (HeatmapParam, 'use_conv_only', 'false')
*set_dict_tuple (HeatmapParam, 'scaling', 'scale_after_relu')
create_dict (DLDatasetInfo)
set_dict_tuple (DLDatasetInfo, 'class_names', ClassNames)
set_dict_tuple (DLDatasetInfo, 'class_ids', ClassIds)
TargetClassID := []
create_dict (WindowHandleDict)

zoom_image_size (Image, ImagePreprocessed, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessed, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 
gen_dl_model_heatmap (DLModelHandle, DLSample, 'grad_cam', TargetClassID, HeatmapParam, DLResult)
* dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, 'heatmap_grad_cam', [], WindowHandleDict)
* get_dict_tuple (WindowHandleDict, 'heatmap_grad_cam', WindowHandle)
get_dict_tuple (DLResult, 'heatmap_grad_cam', Tuple)
get_dict_object (Object, Tuple, 'heatmap_image_class_0')


* 将热图转换为 RGB 伪彩色
* heatmap_to_rgb(Heatmap, HeatmapRGB, 'jet', 'normalize')


算子封装


* 模型路径
modelPath := 'model_best.hdl'
* 图像
read_image (Image, 'C:/Users/10464/Desktop/classification/classification/test/pill_magnesium_crack_203.png')

AI_Classify (Image, HeatMapImage, modelPath, Name, classId)




*** 一 读取模型
read_dl_model (modelPath, DLModelHandle)

*** 二 设置参数
GpuId := 0
set_dl_model_param (DLModelHandle, 'batch_size', 1)
if (GpuId != -1)
    set_dl_model_param (DLModelHandle, 'gpu', GpuId)
endif
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')

*** 三 获取推理参数

get_dl_model_param(DLModelHandle, 'image_dimensions', ImageDimensions)
get_dl_model_param(DLModelHandle, 'class_names', ClassNames)
get_dl_model_param(DLModelHandle, 'class_ids', ClassIds)


*** 四 图像读取
create_dict (DLSample)
count_seconds(Start)
zoom_image_size (Image, ImagePreprocessedByte, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessedByte, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 

*** 五 模型推理
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
get_dict_tuple (DLResult, 'classification_confidences', Confidences)
get_dict_tuple (DLResult, 'classification_class_names', PredictClasses)


*** 六 结果显示

tuple_max (Confidences, Max)
tuple_find (Confidences, Max, IndexMax)    

*** 输出
Name := PredictClasses[IndexMax] 
classId := Max

*显示热度图
create_dict (HeatmapParam)
ChosenLayers := ['res2_block0_conv1']
*set_dict_tuple (HeatmapParam, 'target_conv_layer', ChosenLayers)
*set_dict_tuple (HeatmapParam, 'use_conv_only', 'false')
*set_dict_tuple (HeatmapParam, 'scaling', 'scale_after_relu')
create_dict (DLDatasetInfo)
set_dict_tuple (DLDatasetInfo, 'class_names', ClassNames)
set_dict_tuple (DLDatasetInfo, 'class_ids', ClassIds)
TargetClassID := []
create_dict (WindowHandleDict)

zoom_image_size (Image, ImagePreprocessed, ImageDimensions[0], ImageDimensions[1], 'constant')
convert_image_type (ImagePreprocessed, ImagePreprocessed, 'real')
scale_image (ImagePreprocessed, ImagePreprocessed, 1, -127)
set_dict_object (ImagePreprocessed, DLSample, 'image') 
gen_dl_model_heatmap (DLModelHandle, DLSample, 'grad_cam', TargetClassID, HeatmapParam, DLResult)
* dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, 'heatmap_grad_cam', [], WindowHandleDict)
* get_dict_tuple (WindowHandleDict, 'heatmap_grad_cam', WindowHandle)
get_dict_tuple (DLResult, 'heatmap_grad_cam', Tuple)
get_dict_object (HeatMapImage, Tuple, 'heatmap_image_class_0')

return ()
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐