Halcon人工智能实例
·
1、环境搭建
halcon四个包+深度学习标注工具:
Halcon官方提供限时测试密匙,以上安装包在大恒(halcon代理)官网可以下载:

链接: 大恒官网.
2、标注
简单易上手:
可以标注三种类型:
1、分类;
2、对象检测(轴平行);
3、对象检测(自由矩形);

3、准备数据:
下面展示一些 内联代码片。
* *** 0) SET INPUT/OUTPUT PATHS ***
*
*get_system ('example_dir', HalconExampleDir)
InputImageDir := 'D:/Program Files/MVTec/proj/img'
*
PreprocessingOutputDir := 'D:/Program Files/MVTec/proj/detect_data'
* Set to true, if the results should be deleted after running this program.
RemoveResults := false
*
* *** 1.) PREPARE ***
*
* Here, we read the DLDataset from a DLDataset dictionary using read_dict()
* and set the image directory.
read_dict ('D:/Program Files/MVTec/proj/xian2.hdict', [], [], DLDataset)
set_dict_tuple (DLDataset, 'image_dir', InputImageDir)
*
* Create the detection model with parameters suitable for
* the dataset.
create_dict (DLModelDetectionParam)
set_dict_tuple (DLModelDetectionParam, 'image_dimensions', [512,384,1])
set_dict_tuple (DLModelDetectionParam, 'min_level', 3)
set_dict_tuple (DLModelDetectionParam, 'max_level', 4)
set_dict_tuple (DLModelDetectionParam, 'anchor_num_subscales', 2)
set_dict_tuple (DLModelDetectionParam, 'anchor_aspect_ratios', [0.1,0.16,0.35,1.0])
set_dict_tuple (DLModelDetectionParam, 'capacity', 'high')
get_dict_tuple (DLDataset, 'class_ids', ClassIDs)
set_dict_tuple (DLModelDetectionParam, 'class_ids', ClassIDs)
*
* For a better understanding of the parameters concerning the instance_type 'rectangle2'
* we set them explicitly in the following lines.
set_dict_tuple (DLModelDetectionParam, 'instance_type', 'rectangle2')
*set_dict_tuple (DLModelDetectionParam, 'anchor_angles', rad([-120,-60,0,60,120,180]))
set_dict_tuple (DLModelDetectionParam, 'ignore_direction', 'false')
*set_dict_tuple (DLModelDetectionParam, 'class_ids_no_orientation', [6,8,9,10])
*
create_dl_model_detection ('pretrained_dl_classifier_compact.hdl', |ClassIDs|, DLModelDetectionParam, DLModelHandle)
*
* Preprocess the data in the DLDataset.
split_dl_dataset (DLDataset, 70, 15, [])
create_dict (PreprocessSettings)
* Here, existing preprocessed data will be overwritten.
set_dict_tuple (PreprocessSettings, 'overwrite_files', true)
create_dl_preprocess_param_from_model (DLModelHandle, 'none', 'full_domain', [], [], [], DLPreprocessParam)
preprocess_dl_dataset (DLDataset, PreprocessingOutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName)
*
* Inspect 10 randomly selected preprocessed DLSamples visually.
create_dict (WindowDict)
get_dict_tuple (DLDataset, 'samples', DatasetSamples)
for Index := 0 to 9 by 1
SampleIndex := round(rand(1) * (|DatasetSamples| - 1))
read_dl_samples (DLDataset, SampleIndex, DLSample)
dev_display_dl_data (DLSample, [], DLDataset, 'bbox_ground_truth', [], WindowDict)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endfor
dev_close_window_dict (WindowDict)
4、训练
check_dl_devices (PossibleRuntimes)
if (PossibleRuntimes =~ 'gpu')
Runtime := 'gpu'
elseif (PossibleRuntimes =~ 'cpu')
Runtime := 'cpu'
* The number of used threads may have an impact
* on the training duration.
NumThreadsTraining := 4
set_system ('thread_num', NumThreadsTraining)
else
throw ('No supported device found to continue this example.')
endif
*
* For details see the documentation of set_dl_model_param () and get_dl_model_param ().
InitialLearningRate := 0.0005
set_dl_model_param (DLModelHandle, 'batch_size', 2)
set_dl_model_param (DLModelHandle, 'learning_rate', InitialLearningRate)
set_dl_model_param (DLModelHandle, 'momentum', 0.99)
set_dl_model_param (DLModelHandle, 'runtime', Runtime)
set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
*
* Set change strategy for the learning rate.
GenParamName := []
GenParamValue := []
create_dict (ChangeStrategy)
set_dict_tuple (ChangeStrategy, 'model_param', 'learning_rate')
set_dict_tuple (ChangeStrategy, 'initial_value', InitialLearningRate)
set_dict_tuple (ChangeStrategy, 'epochs', 50)
set_dict_tuple (ChangeStrategy, 'values', InitialLearningRate * 0.5)
GenParamName := [GenParamName,'change']
GenParamValue := [GenParamValue,ChangeStrategy]
*
* Here, we use the 'pretrained_dl_classifier_compact' network and train for 70 epochs.
* The resulting detection performance could be optimized further at the cost of longer
* training time. For example, you can train over more epochs and/or use the more complex
* network 'pretrained_dl_classifier_resnet50'. Please keep in mind to adapt the training
* parameters (e.g. learning_rate = 0.001 and the change strategy) accordingly.
create_dl_train_param (DLModelHandle, 70, 2, 'true', 42, GenParamName, GenParamValue, TrainParam)
* The training and thus the call of train_dl_model_batch ()
* is done using the following procedure.
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
*
* Read the best model, which has been written to file by train_dl_model.
read_dl_model ('model_best.hdl', DLModelHandle)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'left', 'black', [], [])
stop ()

经过一点时间的等待就搞定了!
5、评估
* Set evaluation related model parameters.
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, 'detailed_evaluation', true)
set_dict_tuple (GenParamEval, 'show_progress', true)
set_dict_tuple (GenParamEval, 'measures', 'all')
set_dict_tuple (GenParamEval, 'iou_threshold', [0.5,0.7])
*
set_dl_model_param (DLModelHandle, 'min_confidence', 0.5)
set_dl_model_param (DLModelHandle, 'max_overlap', 0.2)
set_dl_model_param (DLModelHandle, 'max_overlap_class_agnostic', 0.3)
evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEval, EvaluationResult, EvalParams)
*
create_dict (DisplayMode)
set_dict_tuple (DisplayMode, 'display_mode', ['pie_charts_precision','pie_charts_recall','absolute_confusion_matrix'])
dev_display_detection_detailed_evaluation (EvaluationResult, EvalParams, DisplayMode, WindowDict)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
dev_close_window_dict (WindowDict)
*
* Optimize the model for inference,
* meaning, reduce its memory consumption.
set_dl_model_param (DLModelHandle, 'optimize_for_inference', 'true')
set_dl_model_param (DLModelHandle, 'batch_size', 1)

这个数据好像不是很好哦!
6、检测
未完待续。。。。
2020.11.05
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)