Halcon 18 深度学习配置与手写字符(MNIST)测试

具体的配置 参考了  dearpeer 的  halcon 17 深度学习环境搭建, 链接地址: http://www.ihalcon.com/read-8749.html

1 系统配置: 

win10 x64   Halcon 18  images for deep-learning  CUDA9 cudnn

CUDA 和 cuDNN 的下载地址是: 链接: https://pan.baidu.com/s/1QlRRiyHcrgl_Ecb1_7H9UQ 密码:mr59

2 安装步骤:

第一步:安装 Halcon18.05 Progress 和 Deeplearning Images 下载地址:  https://www.mvtec.com/download/halcon/  需要注册,如下图。



第三步:安装 CUDA,给大家一个网络安装版cuda_9.0.176_win10.exe(针对win10 系统)
第四部: 解压 cuDNN,网盘上对应文件是cudnn-9.0-windows10-x64-v7.1.zip(针对win10 系统
备注 :我在笔记本win10 上安装cuDNN 最新版本9.1的时候,提示 cuDNN Errors,更新为9.0版本后OK
虽然第三,四步大家都没有操作过,没关系,傻瓜式下一步,知道完成。
第五步:在halcon的安装执行文件目录下创建thirdparty文件夹, 然后在CUDA的安装目录下找到 cublas64_90.dll文件,在cuDNN解压文件中提取出cudnn64_7.dll,讲这两个dll拷贝到刚才新建的thirdparty文件夹里。如我的目录结构如下



3 测试和运行:

首先手写数据是准备好的,放在对于的文件夹下,每一幅的尺寸是28×28




代码如下(共两个文件 )
  文件1 

* 数据准备
* Read data and pretrained network.
read_dl_classifier_data_set ('D:/ocr_result - 副本', 'last_folder', ImageFiles, GroundTruthLabels, \
                             LabelIndices, UniqueClasses)
read_dl_classifier ('pretrained_dl_classifier_compact.hdl', DLClassifierHandle)
*

*read_image (Image41910Jpg, 'C:/Users/yang/Downloads/vid055_deep_learning_tutorial_preprocessing/mnist_preprocessed/0/4_19.10.jpg_.hobj')

* Create the directories for writing the images.
PreprocessedFolder := 'mnist_preprocessed'
create_directories (PreprocessedFolder, UniqueClasses)
*
* Prepare the new image names.
parse_filename (ImageFiles, BaseNames, Extensions, Directories)
ObjectFilesOut := PreprocessedFolder + '/' + GroundTruthLabels + '/' + BaseNames + '.hobj'
*
* Loop through all images,
* preprocess and then write them.
for ImageIndex := 0 to |ImageFiles|-1 by 1
    read_image (Image, ImageFiles[ImageIndex])
    preprocess_pill_image (Image, ImagePreprocessed, DLClassifierHandle)
    write_object (ImagePreprocessed, ObjectFilesOut[ImageIndex])
endfor


文件2
* 训练和识别
init_program (WindowHandle)
*
* ** Training **
*
* Read the classifier and the preprocessed data.
read_dl_classifier ('pretrained_dl_classifier_compact.hdl', DLClassifierHandle)
* The best classifier (according to the validation error)
* is saved during training.
BestClassifier := 'classifier_mnist.hdl'
read_dl_classifier_data_set ('mnist_preprocessed', 'last_folder', ImageFiles, GroundTruthLabels, LabelIndices, UniqueClasses)
* Set the classes the classifier should distinguish.
set_dl_classifier_param (DLClassifierHandle, 'classes', UniqueClasses)
*set_dl_classifier_param (DLClassifierHandle, 'runtime', 'cpu')

* Split the data into three subsets for training, validation, and testing.
split_dl_classifier_data_set (ImageFiles, GroundTruthLabels, 70, 15, TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, TestImages, TestLabels)
*
* Adapt the folowing training parameters for your application:
*
* The number of epochs determines the duration of the training.
NumEpochs := 20
* Plot the training and validation error every nth epoch.
PlotEveryNthEpoch := 0.5
*
* Set the batch_size as high as possible with your hardware.
set_dl_classifier_param (DLClassifierHandle, 'batch_size', 32)
set_dl_classifier_param (DLClassifierHandle, 'runtime_init', 'immediately')
*
* Set the initial learning rate.
set_dl_classifier_param (DLClassifierHandle, 'learning_rate', 0.001)
* Decrease the learning rate regularily to ideally fine-tune your classifier.
LearningRateStepEveryNthEpoch := 6
LearningRateStepRatio := 0.1
*
* This local procedure encapsulates both the training and the visualization
* of the HDevelop example classify_pill_defects_deep_learning.hdev.
train_dl_classifier_plot_progress (TrainingImages, TrainingLabels, ValidationImages, ValidationLabels, NumEpochs, PlotEveryNthEpoch, DLClassifierHandle, WindowHandle, BestClassifier, LearningRateStepEveryNthEpoch, LearningRateStepRatio)
stop ()
*
* ** Evaluation **
*
* Read the best classifier from the training.
read_dl_classifier (BestClassifier, DLClassifierHandle)
*
* Generate a confusion matrix of the validation data.
read_image (ImagesValidation, ValidationImages)
apply_dl_classifier (ImagesValidation, DLClassifierHandle, DLClassifierResultHandle)
get_dl_classifier_result (DLClassifierResultHandle, 'all', 'predicted_classes', ValidationPredictedClasses)
gen_confusion_matrix (ValidationLabels, ValidationPredictedClasses, [], [], WindowHandle, ConfusionMatrix)
stop ()
get_dl_classifier_image_results (ImagesValidation, ValidationImages, ValidationLabels, ValidationPredictedClasses, [], [], WindowHandle)
*
* Display some examplary heatmaps using test images.
for ImageIndex := 0 to 20 by 1
    read_image (TestImage, TestImages[ImageIndex])
    gen_dl_classifier_heatmap (TestImage, HeatmapRegions, DLClassifierHandle, ['feature_size'], [70], WindowHandle)
    stop ()
endfor


运行结果:

手写字符的样本是自己准备的,不是网上下的。因此在分错的情况。除去人为分错的,效果还是可以的。





错分的结果:









猜你喜欢

转载自blog.csdn.net/xuanbi8560/article/details/80911015