赞
踩
分类识别系统下载地址:https://download.csdn.net/download/jbossjf/78056467
- *准备训练集
- dev_update_off ()
- *存放分类图片的上一级路径
- RawImageFolder := 'D:/软件项目备份/halcon项目/深度学习分类/分类检测/'
- *存放预处理数据的总路径
- ExampleDataDir:= 'D:/软件项目备份/halcon项目/深度学习分类/分类检测/class_data'
- *存放预处理数据的二级路径
- DataDirectoryBaseName := ExampleDataDir + '/dldataset_pokeman'
- *默认参数,用来预处理数据集的
- LabelSource := 'last_folder'
- *
- *分割数据集的百分比。
- *训练集
- TrainingPercent := 70
- *验证集
- ValidationPercent := 15
- *
- * 用户想要输入的图像大小,可更改
- ImageWidth := 900
- ImageHeight := 500
- ImageNumChannels := 3
- *
- *用于图像预处理的其他参数。
- NormalizationType := 'none'
- DomainHandling := 'full_domain'
- *
- *为了获得可复制的分割,我们设置了一个随机种子。
- *这意味着重新运行脚本将导致DLDataset的相同拆分。
- SeedRand := 42
- *设置随机种子。
- set_system ('seed_rand', SeedRand)
- *
- *使用过程read_dl_dataset_classification读取数据集。
- *或者,您可以使用read_dict()读取由MVTec深度学习工具创建的DLDataset字典。
- read_dl_dataset_classification (RawImageFolder, LabelSource, DLDataset)
- *分割数据集
- split_dl_dataset (DLDataset, TrainingPercent, ValidationPercent, [])
- *创建输出目录(如果尚不存在)。
- file_exists (ExampleDataDir, FileExists)
- if (not FileExists)
- make_dir (ExampleDataDir)
- endif
- *
- *创建预处理参数。
- create_dl_preprocess_param ('classification', ImageWidth, ImageHeight, ImageNumChannels, -127, 128, NormalizationType, DomainHandling, [], [], [], [], DLPreprocessParam)
- *
- *数据集目录,用于由preprocess_dl_dataset编写的任何输出。
- DataDirectory := DataDirectoryBaseName + '_' + ImageWidth + 'x' + ImageHeight
- *
- *预处理数据集。 这可能需要几秒钟。
- create_dict (GenParam)
- set_dict_tuple (GenParam, 'overwrite_files', true)
- preprocess_dl_dataset (DLDataset, DataDirectory, DLPreprocessParam, GenParam, DLDatasetFileName)
- *
- *单独存储预处理参数以便使用,例如 在推论过程中。
- PreprocessParamFileBaseName := DataDirectory + '/dl_preprocess_param.hdict'
- write_dict (DLPreprocessParam, PreprocessParamFileBaseName, [], [])
-
-
- *打标签
- *在进行培训之前,建议先检查预处理后的数据集。
- *
- *显示10个随机选择的火车图像的DLSamples。
- get_dict_tuple (DLDataset, 'samples', DatasetSamples)
- find_dl_samples (DatasetSamples, 'split', 'train', 'match', SampleIndices)
- tuple_shuffle (SampleIndices, ShuffledIndices)
- read_dl_samples (DLDataset, ShuffledIndices[0:9], DLSampleBatchDisplay)
- *
- create_dict (WindowHandleDict)
- for Index := 0 to |DLSampleBatchDisplay| - 1 by 1
- *在DLSampleBatchDisplay中循环采样。
- dev_display_dl_data (DLSampleBatchDisplay[Index], [], DLDataset, 'classification_ground_truth', [], WindowHandleDict)
- Text := 'Press Run (F5) to continue'
- dev_disp_text (Text, 'window', 'bottom', 'right', 'black', [], [])
- stop ()
- endfor
- *
- * 关闭窗口,数据预处理完成
- dev_close_window_dict (WindowHandleDict)
- stop()
-
-
-
- *训练模型
- *预处理后的文件路径
- DLDatasetFileName := DataDirectory+'/dl_dataset.hdict'
- *预处理的参数文件路径
- DLPreprocessParamFileName := DataDirectory + '/dl_preprocess_param.hdict'
- *最佳评估模型的输出路径。
- BestModelBaseName := ExampleDataDir + '/best_dl_model_classification'
- *最终训练模型的输出路径。
- FinalModelBaseName := ExampleDataDir + '/final_dl_model_classification'
- * Batch size.
- BatchSize := 2
- *学习率
- InitialLearningRate := 0.0001
- *如果批量小,动量应高。
- Momentum := 0.9
- * train_dl_model使用的参数。
- *训练模型的时迭代次数。
- NumEpochs := 20
- *评估间隔(以时期计),以计算验证拆分上的评估度量。每迭代一次验证一次
- EvaluationIntervalEpochs := 1
- *在以下时期更改学习率,例如 [4、8、12]。
- *如果不应该改变学习率,则将其设置为[]。
- ChangeLearningRateEpochs := []
- *将学习率更改为以下值,例如 InitialLearningRate * [0.1,0.01,0.001]。
- *元组的长度必须与ChangeLearningRateEpochs相同。
- ChangeLearningRateValues := InitialLearningRate * [0.1,0.01,0.001]
- *事先设定权重。
- WeightPrior := 0.0005
- *控制是否显示训练进度(是/否)。
- DisplayEvaluation := true
- *设置一个随机的种子进行训练。
- RandomSeed := 42
- *设置create_dl_train_param的通用参数。
- *请参阅create_dl_train_param的文档以获取所有可用参数的概述。
- GenParamName := []
- GenParamValue := []
- *扩充参数。
- *如果在训练过程中应增加样本,请创建extend_dl_samples所需的字典。
- *在这里,我们设置增强百分比和方法。
- *create_dict (AugmentationParam)
- *要增加的样本百分比。
- *set_dict_tuple (AugmentationParam, 'augmentation_percentage', 50)
- *沿行和列镜像。
- *set_dict_tuple (AugmentationParam, 'mirror', 'rc')
- *GenParamName := [GenParamName,'augment']
- *GenParamValue := [GenParamValue,AugmentationParam]
- *更改策略。
- *在训练过程中可以更改模型参数。
- *在这里,如果上面指定了,我们将更改学习率。
- if (|ChangeLearningRateEpochs| > 0)
- create_dict (ChangeStrategy)
- * Specify the model parameter to be changed, here the learning rate.
- set_dict_tuple (ChangeStrategy, 'model_param', 'learning_rate')
- * Start the parameter value at 'initial_value'.
- set_dict_tuple (ChangeStrategy, 'initial_value', InitialLearningRate)
- * Reduce the learning rate in the following epochs.
- set_dict_tuple (ChangeStrategy, 'epochs', ChangeLearningRateEpochs)
- * Reduce the learning rate to the following values.
- set_dict_tuple (ChangeStrategy, 'values', ChangeLearningRateValues)
- * Collect all change strategies as input.
- GenParamName := [GenParamName,'change']
- GenParamValue := [GenParamValue,ChangeStrategy]
- endif
- *
- *序列化策略。
- *有多种选项可用于将中间模型保存到磁盘(请参见create_dl_train_param)。
- *在这里,我们将最佳模型和最终模型保存到上面设置的路径中。
- create_dict (SerializationStrategy)
- set_dict_tuple (SerializationStrategy, 'type', 'best')
- set_dict_tuple (SerializationStrategy, 'basename', BestModelBaseName)
- GenParamName := [GenParamName,'serialize']
- GenParamValue := [GenParamValue,SerializationStrategy]
- create_dict (SerializationStrategy)
- set_dict_tuple (SerializationStrategy, 'type', 'final')
- set_dict_tuple (SerializationStrategy, 'basename', FinalModelBaseName)
- GenParamName := [GenParamName,'serialize']
- GenParamValue := [GenParamValue,SerializationStrategy]
- *
- *显示参数。
- *在此示例中,选择了20%的训练分组以显示
- *评估在训练过程中减少训练次数的评估措施。 较低的百分比
- *有助于加快评估/培训。 如果评估措施用于培训分裂
- *不会显示,将此值设置为0(默认值)。
- *SelectedPercentageTrainSamples := 15
- *create_dict (DisplayParam)
- *set_dict_tuple (DisplayParam, 'selected_percentage_train_samples', SelectedPercentageTrainSamples)
- *GenParamName := [GenParamName,'display']
- *GenParamValue := [GenParamValue,DisplayParam]
- *检查是否所有必需的文件都存在。
- *check_data_availability (ExampleDataDir, DLDatasetFileName, DLPreprocessParamFileName)
- *
- *读入在预处理过程中初始化的模型。也就是读取神经网络模型,halcon自带的
- read_dl_model ('pretrained_dl_classifier_compact.hdl', DLModelHandle)
- *读取预处理后的图片DLDataset文件。
- read_dict (DLDatasetFileName, [], [], DLDataset)
- *按照上述设置指定模型超参数。
- set_dl_model_param (DLModelHandle, 'learning_rate', InitialLearningRate)
- set_dl_model_param (DLModelHandle, 'momentum', Momentum)
- *设置模型的类名。
- get_dict_tuple (DLDataset, 'class_names', ClassNames)
- set_dl_model_param (DLModelHandle, 'class_names', ClassNames)
- *从预处理参数获取图像尺寸并为模型设置它们。
- read_dict (DLPreprocessParamFileName, [], [], DLPreprocessParam)
- get_dict_tuple (DLPreprocessParam, 'image_width', ImageWidth)
- get_dict_tuple (DLPreprocessParam, 'image_height', ImageHeight)
- get_dict_tuple (DLPreprocessParam, 'image_num_channels', ImageNumChannels)
- *设置输入模型的图片大小和维度
- set_dl_model_param (DLModelHandle, 'image_dimensions', [ImageWidth,ImageHeight,ImageNumChannels])
- if (BatchSize == 'maximum')
- set_dl_model_param_max_gpu_batch_size (DLModelHandle, 100)
- else
- set_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
- endif
- if (|WeightPrior| > 0)
- set_dl_model_param (DLModelHandle, 'weight_prior', WeightPrior)
- endif
- set_dl_model_param (DLModelHandle, 'runtime_init', 'immediately')
- stop()
- *创建训练参数。有超参数的传入GenParamName、GenParamValue
- create_dl_train_param (DLModelHandle, NumEpochs, EvaluationIntervalEpochs, DisplayEvaluation, RandomSeed, GenParamName, GenParamValue, TrainParam)
- *create_dl_train_param (DLModelHandle, NumEpochs, EvaluationIntervalEpochs, DisplayEvaluation, RandomSeed, [], [], TrainParam)
- *
- *以下过程中的train_dl_model_batch()。
- train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
- stop()
- * Close training windows.
- dev_close_window ()
-
-
-
- *评估模型
- *******************************************************************评估****************************************************
- *创建字典
- create_dict (GenParamEval)
- *设置参数
- set_dict_tuple (GenParamEval, 'class_names_to_evaluate', 'global')
- set_dict_tuple (GenParamEval, 'measures', ['top1_error','precision','recall','f_score','absolute_confusion_matrix'])
- * Evaluate the trained model.
- evaluate_dl_model (DLDataset, DLModelHandle, 'split', 'test', GenParamEval, EvaluationResult, EvalParams)
- *
- create_dict (EvalDisplayMode)
- set_dict_tuple (EvalDisplayMode, 'display_mode', ['measures','pie_charts_precision','pie_charts_recall','absolute_confusion_matrix'])
- create_dict (WindowDict)
- dev_display_classification_evaluation (EvaluationResult, EvalParams, EvalDisplayMode, WindowDict)
- dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'right', 'black', [], [])
- stop ()
- dev_close_window_dict (WindowDict)
-
-
-
-
-
- BestModelBaseName := 'D:/软件项目备份/halcon项目/深度学习分类/分类检测/class_data/best_dl_model_classification.hdl'
-
- *读取保存好的最佳模型
- read_dl_model (BestModelBaseName, DLModelHandle)
- *读取预处理的参数
- read_dict ('D:/软件项目备份/halcon项目/深度学习分类/分类检测/class_data/dldataset_pokeman_900x500/dl_preprocess_param.hdict', [], [], DLPreprocessParam)
- get_dict_tuple (DLPreprocessParam, 'image_width', ImageWidth)
- get_dict_tuple (DLPreprocessParam, 'image_height', ImageHeight)
- get_dict_tuple (DLPreprocessParam, 'image_num_channels', ImageNumChannels)
-
- *create_dict (WindowDict)
- dev_open_window (0, 0, 900, 512, 'black', WindowHandle)
- * Image Acquisition 01: Code generated by Image Acquisition 01
- list_files ('D:/软件项目备份/halcon项目/深度学习分类/分类检测/苹果', ['files','follow_links'], ImageFiles)
- tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
- for Index := 0 to |ImageFiles| - 1 by 1
- read_image (Image, ImageFiles[Index])
- dev_display (Image)
- * 处理图片
- 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', Tuple)
- predictionname:=Tuple[0]
- *显示预测的类型
- disp_message (WindowHandle, predictionname, 'window', 12, 12, 'black', 'true')
- *dev_display_dl_data (DLSample, DLResult, DLDataset, 'classification_result', [], WindowDict)
- stop()
- endfor
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。