这是一个例子,通过读取桃子和梨的图片集来进行模型训练,得到一个可以识别桃子或是梨的深度学习识别器。
********************************************* * Halcon-深度学习-分类测试 * 这是一个例子,通过读取桃子和梨的图片集来进行模型训练, * 得到一个可以识别桃子或是梨的深度学习识别器 ********************************************* dev_close_window () dev_update_off () set_system ('seed_rand', 42) get_system ('example_dir', PathExample) ImageBaseFolder := PathExample + '/images/food/' ImageFolder := ImageBaseFolder + ['peach','pear'] OutputDir := './images/classify/' ************ * 预处理 ************ read_dl_dataset_classification(ImageFolder, 'last_folder', DLDataSet) read_dl_model ('pretrained_dl_classifier_compact.hdl', DLModelHandle) get_dict_tuple(DLDataSet, 'class_names', ClassNames) set_dl_model_param(DLModelHandle, 'class_names', ClassNames) * 分割数据集,70%用作训练,15%用作验证,剩下作为测试 split_dl_dataset(DLDataSet, 70, 15, []) create_dl_preprocess_param_from_model(DLModelHandle, 'none', 'full_domain', [], [], [], DLPreprocessParam) create_dict(PreprocessSettings) set_dict_tuple(PreprocessSettings, 'overwrite_files', true) preprocess_dl_dataset(DLDataSet, OutputDir, DLPreprocessParam, PreprocessSettings, DLDatasetFileName) ************ * 训练模型 ************ * 每次迭代的训练样本数,值过大可能会报内存不足 set_dl_model_param(DLModelHandle, 'batch_size', 64) * 学习率,会影响识别准确率 set_dl_model_param(DLModelHandle, 'learning_rate', 0.001) set_dl_model_param(DLModelHandle, 'runtime_init', 'immediately') create_dl_train_param(DLModelHandle, 20, 1, 'true', 41, [], [], TrainParam) train_dl_model(DLDataSet, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos) * 训练完成后将模型写入文件 write_dl_model(DLModelHandle, './trainModel.hdl') stop () ************ * 分类识别测试 ************ dev_clear_window () dev_open_window (0, 0, 256, 256, 'black', WindowHandle) set_display_font (WindowHandle, 20, '黑体', 'false', 'false') * 读取训练模型 read_dl_model('./trainModel.hdl', DLModelHandle) create_dl_preprocess_param_from_model(DLModelHandle, 'none', 'full_domain', [], [], [], DLPreprocessParam) list_files('./images/test/', 'files', Files) for I := 0 to |Files|-1 by 1 read_image (Image, Files[I]) * 对测试图片进行识别 gen_dl_samples_from_images(Image, DLSample) preprocess_dl_samples(DLSample, DLPreprocessParam) apply_dl_model(DLModelHandle, DLSample, [], DLResult) get_dict_tuple(DLResult, 'classification_class_names', ClassNames) dev_display(Image) dev_disp_text('识别结果:' + ClassNames[0], 'window', 'top', 'center', 'red', ['box'], ['false']) stop() endfor
训练过程会有图形化输出显示:
训练完成后,在网上找了一些图片进行测试,效果还不错:
标签:dl,set,DLModelHandle,分类,param,Halcon,dev,深度,model From: https://www.cnblogs.com/lgyup/p/17517214.html