dev_update_off()
*获取当前window的句柄
dev_get_window(WindowHandle)
*为了获得可重复的分裂,我们设置了一个随机种子。
*这意味着重新运行脚本会导致相同的DLDataset分割。
SeedRand := 42
- 设置随机种子。
set_system (‘seed_rand’, SeedRand)
*****************预处理数据
*训练的文件路径
train_path:=‘E:/image2/train’
*预处理后保存数据的路径
preprocess_dataset_save_path:=‘E:/image2/classificationJob/PreprocessDataDirectory’
*预处理参数保存的路径
preprocess_paramter_save_path:=preprocess_dataset_save_path+‘/dl_preprocess_param.hdict’
*读取要分类的数据,根据文件夹的名称来确定分的类型
read_dl_dataset_classification (train_path, ‘last_folder’, DLDataset)
*分割数据,训练80,验证20
split_dl_dataset (DLDataset, 80, 20, [])
创建预处理参数 640640 通道3 灰度-127到128
create_dl_preprocess_param (‘classification’, 640, 640, 3, -127, 128, ‘none’, ‘full_domain’, [], [], [], [], DLPreprocessParam)
*保存预处理数据集
create_dict (GenParam)
set_dict_tuple (GenParam, ‘overwrite_files’, ‘auto’)
preprocess_dl_dataset (DLDataset,preprocess_dataset_save_path , DLPreprocessParam, GenParam, DLDatasetFileName)
*单独存储预处理参数,以便在推理期间使用它。
write_dict(DLPreprocessParam, preprocess_paramter_save_path, [], [])
*创建一个显示窗口字典
create_dict (WindowHandleDict)
*随机显示10个训练的图片
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)
for Index:=0 to 10-1 by 1
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()
***********推理
*获取训练的设备gpu或cpu
query_available_dl_devices ([‘runtime’, ‘runtime’], [‘gpu’, ‘cpu’], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw (‘No supported device found to continue this example.’)
endif
DLDevice := DLDeviceHandles[0]
*检测训练驱动是cpu还是gpu
get_dl_device_param (DLDevice, ‘type’, DLDeviceType)
if (DLDeviceType == ‘cpu’)
*使用的线程数可能会有影响
*关于培训时间。
NumThreadsTraining := 4
set_system (‘thread_num’, NumThreadsTraining)
endif
*读入预处理的DLDataset文件。
*read_dict (DLDatasetFileName, [], [], DLDataset)
*读取训练的模型
read_dl_model(‘pretrained_dl_classifier_compact.hdl’, DLModelHandle)
*read_dl_model(best_model_save_path+‘.hdl’,DLModelHandle)
设置模型的学习率
set_dl_model_param (DLModelHandle, ‘learning_rate’, 0.001)
设置动量采用默认
set_dl_model_param (DLModelHandle, ‘momentum’, 0.4)
设置分类名称
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])
*设置批量
set_dl_model_param (DLModelHandle, ‘batch_size’, 20)
*设置训练的设备
set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
*初始化学习率
InitialLearningRate := 0.001
训练模型的epoch数。
NumEpochs := 400
训练用到的参数
GenParamName := []
GenParamValue := []
设置改变学习率参数
ChangeLearningRateEpochs := int([NumEpochs/4, NumEpochs/2,NumEpochs/43])
递减率
DecayRate := 0.5
ChangeLearningRateValues := InitialLearningRate * [DecayRate,DecayRateDecayRate,DecayRateDecayRateDecayRate]
****初始化训练参数的数据
*这里,我们设置了递增百分比和递增方法。
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)
*指定要更改的模型参数,这里是学习率
set_dict_tuple (ChangeStrategy, ‘model_param’, ‘learning_rate’)
*从“initial_value”开始。
set_dict_tuple (ChangeStrategy, ‘initial_value’, InitialLearningRate)
*在接下来的迭代中降低学习率。
set_dict_tuple (ChangeStrategy, ‘epochs’, ChangeLearningRateEpochs)
*将学习率降低到以下值
set_dict_tuple (ChangeStrategy, ‘values’, ChangeLearningRateValues)
*收集所有变更策略作为输入。
GenParamName := [GenParamName,‘change’]
GenParamValue := [GenParamValue,ChangeStrategy]
endif
*创建最好模型保存路径字典参数
create_dict(best_model_save_path_dict)
set_dict_tuple(best_model_save_path_dict, ‘type’, ‘best’)
*训练过程中最好模型的保存路径
best_model_save_path:=preprocess_dataset_save_path+‘/best_dl_model_classification’
set_dict_tuple(best_model_save_path_dict, ‘basename’, best_model_save_path)
GenParamName := [GenParamName,‘serialize’]
GenParamValue := [GenParamValue,best_model_save_path_dict]
*创建训练好模型最终保存的路径
create_dict(final_model_save_path_dict)
set_dict_tuple(final_model_save_path_dict, ‘type’, ‘final’)
*训练过程中最后模型的保存路径
final_model_save_path:=preprocess_dataset_save_path+‘/final_dl_model_classification’
set_dict_tuple(final_model_save_path_dict, ‘basename’, final_model_save_path)
GenParamName := [GenParamName,‘serialize’]
GenParamValue := [GenParamValue,final_model_save_path_dict]
*创建训练参数 训练迭代次数,评估间隔次数
create_dl_train_param (DLModelHandle, 400, 1, true, 42, GenParamName, GenParamValue, TrainParam)
*开始训练
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
stop()
*获取所有的窗口指令
dev_close_window()
***************评估
*重新读取模型最好的模型
read_dl_model(best_model_save_path+‘.hdl’,DLModelHandle)
*设置模型批次大小
set_dl_model_param (DLModelHandle, ‘batch_size’, 1)
*设置模型驱动
set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
*读取预处理的文件数据
*read_dict (DLDatasetFileName, [], [], DLDataset)
- 设置评估参数。
ClassificationMeasures := [‘top1_error’, ‘precision’, ‘recall’, ‘f_score’, ‘absolute_confusion_matrix’, ‘relative_confusion_matrix’]
create_dict (GenParamEval)
set_dict_tuple (GenParamEval, ‘measures’, ClassificationMeasures)
set_dict_tuple (GenParamEval, ‘show_progress’, ‘true’)
*评估重新训练的模型.
evaluate_dl_model (DLDataset, DLModelHandle, ‘split’, [‘validation’], GenParamEval, EvaluationResult, EvalParams)
****显示结果
-
显示的措施
create_dict (WindowHandleDict)
create_dict (GenParamEvalDisplay)
set_dict_tuple (GenParamEvalDisplay, ‘display_mode’, [‘measures’, ‘pie_charts_precision’, ‘pie_charts_recall’, ‘absolute_confusion_matrix’])
dev_display_classification_evaluation (EvaluationResult, EvalParams, GenParamEvalDisplay, WindowHandleDict)
dev_disp_text (‘Press F5 to continue’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
dev_close_window_dict (WindowHandleDict) -
调用交互式混淆矩阵。
dev_display_dl_interactive_confusion_matrix (DLDataset, EvaluationResult, [])
*关闭窗口把手
dev_close_window_dict (WindowHandleDict)
*热图显示
*获取评估结果
get_dict_tuple (EvaluationResult, ‘evaluated_samples’, EvaluatedSamples)
*获取评估图片的id
get_dict_tuple (EvaluatedSamples, ‘image_ids’, ImageIDs)
*获取图片标签属于ok或ng
get_dict_tuple (EvaluatedSamples, ‘image_label_ids’, ImageLabelIDs)
*获取推理结果
get_dict_tuple (EvaluatedSamples, ‘top1_predictions’, Predictions)
*获取类名称
- get_dict_tuple (DLDataset, ‘class_names’, ClassNames)
*获取类id - get_dict_tuple (DLDataset, ‘class_ids’, ClassIDs)
*显示热图方法
HeatmapMethod := ‘heatmap_grad_cam’
*设置热图的参数
TargetClassID := []
create_dict (HeatmapParam)
if (HeatmapMethod == ‘heatmap_grad_cam’)
* Set generic parameters for operator heatmap.
set_dict_tuple (HeatmapParam, ‘use_conv_only’, ‘true’)
set_dict_tuple (HeatmapParam, ‘scaling’, ‘scale_after_relu’)
elseif (HeatmapMethod == ‘heatmap_confidence_based’)
* Set target class ID.
set_dict_tuple (HeatmapParam, ‘target_class_id’, TargetClassID)
* Set the feature size and the sampling size for the
* confidence based approach.
FeatureSize := 30
SamplingSize := 10
set_dict_tuple (HeatmapParam, ‘feature_size’, FeatureSize)
set_dict_tuple (HeatmapParam, ‘sampling_size’, SamplingSize)
else
throw (‘Unsupported heatmap method.’)
endif
*创建窗口字典
create_dict (WindowHandleDict)
*获取样品
get_dict_tuple (DLDataset, ‘samples’, DatasetSamples)
*显示所有热图,不管ok ng
for Index:=0 to |ImageIDs|-1 by 1
find_dl_samples (DatasetSamples, ‘image_id’, ImageIDs[Index], ‘match’, DLSampleIndex)
read_dl_samples (DLDataset, DLSampleIndex, DLSample)
*
if (HeatmapMethod == ‘heatmap_grad_cam’)
gen_dl_model_heatmap (DLModelHandle, DLSample, ‘grad_cam’, TargetClassID, HeatmapParam, DLResult)
else
* Create temporary DLResult for display.
create_dict (DLResult)
gen_dl_model_classification_heatmap (DLModelHandle, DLSample, DLResult, HeatmapParam)
endif
dev_display_dl_data (DLSample, DLResult, DLDataset, HeatmapMethod, [], WindowHandleDict)
dev_disp_text (‘Press F5 to continue.’, ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])
stop ()
endfor
*优化内存消耗
set_dl_model_param (DLModelHandle, ‘optimize_for_inference’, ‘true’)
*重新模型
write_dl_model (DLModelHandle, best_model_save_path+‘.hdl’)
- Close the windows.
dev_close_window_dict (WindowHandleDict)
stop()
**推理
- 重新读取模型
read_dl_model (best_model_save_path+‘.hdl’, DLModelHandle) - 设置批处理大小
set_dl_model_param (DLModelHandle, ‘batch_size’, 1) - 设置模型驱动
set_dl_model_param (DLModelHandle, ‘device’, DLDevice)
*读取一张图片
read_image (ImageBatch, ‘E:/橡胶检测/image2/test/14.jpg’)
*生成DLSampleBatch
gen_dl_samples_from_images (ImageBatch, DLSampleBatch)
*预处理DLSampleBatch
preprocess_dl_samples (DLSampleBatch, DLPreprocessParam)
*在DLSampleBatch上应用DL模型。
apply_dl_model (DLModelHandle, DLSampleBatch, [], DLResultBatch)
*获取分类结果
DLResult := DLResultBatch[0]
get_dict_param(DLResult,‘keys’,[],dict_keys)
get_dict_tuple(DLResult, dict_keys[0], socres)
get_dict_tuple(DLResult, dict_keys[1], ids)
get_dict_tuple(DLResult, dict_keys[2], names)
dev_get_window(WindowHandle)
set_tposition(WindowHandle, 200, 200)
set_display_font(WindowHandle, 48, ‘sans’, ‘false’, ‘false’)
write_string(WindowHandle, names[0])
set_display_font(WindowHandle, 16, ‘sans’, ‘false’, ‘false’)
dev_disp_text (‘socre:’+socres[0]+‘-id:’+ids[0]+‘-class:’+names[0], ‘window’, ‘bottom’, ‘right’, ‘black’, [], [])