dev_close_window ()
dev_update_off ()
set_system ('seed_rand', 42)
*
* *** 0) SET INPUT/OUTPUT PATHS ***
*
get_system ('example_dir', PathExample)
* Give all folders that contain images.
ImageBaseFolder := PathExample + '/images/food/'
RawImageFolder := ImageBaseFolder + ['apple_braeburn','apple_golden_delicious','apple_topaz','peach','pear']
OutputDir := 'classify_fruits_data'
* Set to true, if the results should be deleted after running this program.
RemoveResults := false
*
* *** 1.) PREPARE ***
* 根据路径读取数据集——以每个文件夹的名字对图片打标签分类
read_dl_dataset_classification (RawImageFolder, '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)
*
*将这个数据集拆分成0.7训练,0.15验证,0.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)
*
*从数据集中随机挑选10张进行可视化
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, 'classification_ground_truth', [], WindowDict)
* dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
* stop ()
* endfor
dev_close_window_dict (WindowDict)
*
* *** 2.) TRAIN ***
*选择CPU和GPU
query_available_dl_devices (['runtime','runtime'], ['gpu','cpu'], DLDeviceHandles)
if (|DLDeviceHandles| == 0)
throw ('No supported device found to continue this example.')
endif
* Due to the filter used in query_available_dl_devices, the first device is a GPU, if available.
DLDevice := DLDeviceHandles[0]
get_dl_device_param (DLDevice, 'type', DLDeviceType)
if (DLDeviceType == 'cpu')
* The number of used threads may have an impact
* on the training duration.
NumThreadsTraining := 4
set_system ('thread_num', NumThreadsTraining)
endif
*
*设置模型训练时的参数
set_dl_model_param (DLModelHandle, 'batch_size', 64)
set_dl_model_param (DLModelHandle, 'learning_rate', 0.001)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
*设置总的训练参数
create_dl_train_param (DLModelHandle, 20, 1, 'true', 42, [], [], TrainParam)
*训练开始
*train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
*
*读取训练出来的最好的模型到句柄中
read_dl_model ('model_best.hdl', DLModelHandle)
set_dl_model_param (DLModelHandle, 'device', DLDevice)
dev_disp_text ('Press F5 to continue', 'window', 'bottom', 'left', 'black', [], [])
stop ()
*
dev_close_window ()
dev_close_window ()
*
* *** 3.) EVALUATE ***
*
* Set generic evaluation parameters.
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)
*
* 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)
* Save the model in this optimized state.
write_dl_model (DLModelHandle, 'model_best.hdl')
*
* *** 4.) INFER ***
*
* To demonstrate the inference steps, we apply the trained model to some randomly
* chosen example images.
list_image_files (RawImageFolder, 'default', 'recursive', ImageFiles)
create_dict (WindowDict)
for IndexInference := 0 to 9 by 1
SampleIndex := round(rand(1) * (|DatasetSamples| - 1))
read_image (Image, ImageFiles[SampleIndex])
gen_dl_samples_from_images (Image, DLSample)
preprocess_dl_samples (DLSample, DLPreprocessParam)
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
*
dev_display_dl_data (DLSample, DLResult, DLDataset, 'classification_result', [], WindowDict)
stop ()
endfor
dev_close_window_dict (WindowDict)
*
* *** 5.) REMOVE FILES ***
*
clean_up_output (OutputDir, RemoveResults)