*
* This example is part of a series of examples, which summarizes
* the workflow for DL classification. It uses the MVTec pill dataset.
*
* The four parts are:
* 1. Dataset preprocessing.
* 2. Training of the model.
* 3. Evaluation of the trained model.
* 4. Inference on new images.
*
* This examples contains part 2: 'Training of the model.'
*
* Please note: This script requires the output of part 1:
* classify_pill_defects_deep_learning_1_preprocess.hdev
*
dev_update_off ()
*
ShowExampleScreens := true
*
* Training can be performed on a GPU or CPU.
* See the respective system requirements in the Installation Guide.
* If possible a GPU is used in this example.
* In case you explicitely wish to run this example on the CPU,
* choose the CPU device instead.
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
*
* Display the explanatory screens about this example.
if (ShowExampleScreens)
*
* Initial example windows and parameters.
dev_example_init (ShowExampleScreens, ExampleInternals)
*
* Introduction text of example series.
dev_display_screen_introduction_train (ExampleInternals)
stop ()
*
* Check for requirements.
dev_display_screen_error (ExampleInternals, Error)
if (Error)
stop ()
endif
*
* Explain goals during training.
dev_display_screen_training_goals_1 (ExampleInternals)
stop ()
dev_display_screen_training_goals_2 (ExampleInternals)
stop ()
*
* Explain set_dl_model_param and create_dl_train_param.
dev_display_screen_parameters (ExampleInternals)
stop ()
*
* Explain batch size.
dev_display_screen_batch_size (ExampleInternals)
stop ()
*
* Explain learning rate.
dev_display_screen_learning_rate (ExampleInternals)
stop ()
*
* Explain number of epochs.
dev_display_screen_num_epochs (ExampleInternals)
stop ()
*
* Explain further parameters.
dev_display_screen_other_params (ExampleInternals)
stop ()
*
* Explain train_dl_model.
dev_display_screen_training_process (ExampleInternals)
stop ()
*
* Mention on which device the deep learning operators will run.
dev_display_screen_device (ExampleInternals, DLDevice)
stop ()
*
* Explain that training will start now.
dev_display_screen_training_starts (ExampleInternals)
stop ()
*
* Close any example screen windows.
dev_close_example_windows (ExampleInternals)
endif
*
* *************************************
* ** Set input and output paths ***
* *************************************
*
* All example data is written to this folder.
ExampleDataDir := 'classify_pill_defects_data'
* File path of the initialized model.
*初始化模型(预训练的参数)
ModelFileName := 'pretrained_dl_classifier_compact.hdl'
* File path of the preprocessed DLDataset.
* Note: Adapt DataDirectory after preprocessing with another image size.
DataDirectory := ExampleDataDir + '/dldataset_pill_300x300'
DLDatasetFileName := DataDirectory + '/dl_dataset.hdict'
DLPreprocessParamFileName := DataDirectory + '/dl_preprocess_param.hdict'
* Output path of the best evaluated model.
BestModelBaseName := ExampleDataDir + '/best_dl_model_classification'
* Output path for the final trained model.
FinalModelBaseName := ExampleDataDir + '/final_dl_model_classification'
*
* *******************************
* ** Set basic parameters ***
* *******************************
* The following parameters need to be adapted frequently.
*
* Model parameters.
* Batch size. In case this example is run on a GPU,
* you can set BatchSize to 'maximum' and it will be
* determined automatically.
BatchSize := 64
* Initial learning rate.
InitialLearningRate := 0.001
* Momentum should be high if batch size is small.
Momentum := 0.9
*
* Parameters used by train_dl_model.
* Number of epochs to train the model.
NumEpochs := 16
* Evaluation interval (in epochs) to calculate evaluation measures on the validation split.
*每跑多少个epoch做一次验证
EvaluationIntervalEpochs := 1
* Change the learning rate in the following epochs, e.g. [4, 8, 12].
* Set it to [] if the learning rate should not be changed.
*在第几个epoch的时候改变学习率
ChangeLearningRateEpochs := [4,8,12]
* Change the learning rate to the following values, e.g. InitialLearningRate * [0.1, 0.01, 0.001].
* The tuple has to be of the same length as ChangeLearningRateEpochs.
*在第几个epoch的时候将学习率的值改成多少
ChangeLearningRateValues := InitialLearningRate * [0.1,0.01,0.001]
*
* **********************************
* ** Set advanced parameters ***
* **********************************
* The following parameters might need to be changed in rare cases.
*
* Model parameter.
* Set the weight prior.
WeightPrior := 0.0005
*
* Parameters used by train_dl_model.
* Control whether training progress is displayed (true/false).
EnableDisplay := true
* Set a random seed for training.
RandomSeed := 42
set_system ('seed_rand', RandomSeed)
*
* In order to obtain nearly deterministic training results on the same GPU
* (system, driver, cuda-version) you could specify "cudnn_deterministic" as
* "true". Note, that this could slow down training a bit.
* set_system ('cudnn_deterministic', 'true')
*
* Set generic parameters of create_dl_train_param.
* Please see the documentation of create_dl_train_param for an overview on all available parameters.
GenParamName := []
GenParamValue := []
*
* Augmentation parameters.
* If samples should be augmented during training, create the dict required by augment_dl_samples.
* Here, we set the augmentation percentage and method.
*设置数据增强的参数
create_dict (AugmentationParam)
* Percentage of samples to be augmented.
set_dict_tuple (AugmentationParam, 'augmentation_percentage', 50)
* Mirror images along row and column.
set_dict_tuple (AugmentationParam, 'mirror', 'rc')
GenParamName := [GenParamName,'augment']
GenParamValue := [GenParamValue,AugmentationParam]
*
* Change strategies.
* It is possible to change model parameters during training.
* Here, we change the learning rate if specified above.
*设置学习率改变的情况
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
*
* Serialization strategies.
* There are several options for saving intermediate models to disk (see create_dl_train_param).
* Here, we save the best and the final model to the paths set above.
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]
*
* Display parameters.
* In this example, 20% of the training split are selected to display the
* evaluation measure for the reduced training split during the training. A lower percentage
* helps to speed up the evaluation/training. If the evaluation measure for the training split
* shall not be displayed, set this value to 0 (default).
SelectedPercentageTrainSamples := 20
* Set the x-axis argument of the training plots.
XAxisLabel := 'epochs'
create_dict (DisplayParam)
set_dict_tuple (DisplayParam, 'selected_percentage_train_samples', SelectedPercentageTrainSamples)
set_dict_tuple (DisplayParam, 'x_axis_label', XAxisLabel)
GenParamName := [GenParamName,'display']
GenParamValue := [GenParamValue,DisplayParam]
*
*
* *****************************************
* ** Read initial model and dataset ***
* *****************************************
*
* Check if all necessary files exist.
check_data_availability (ExampleDataDir, DLDatasetFileName, DLPreprocessParamFileName)
*
* Read in the model that was initialized during preprocessing.
*读了一个深度学习模型,带预训练参数
read_dl_model (ModelFileName, DLModelHandle)
*
* Read in the preprocessed DLDataset file.
*读数据
read_dict (DLDatasetFileName, [], [], DLDataset)
*
* *******************************
* ** Set model parameters 下面全部是在设置模型的参数***
* *******************************
*
* Set model hyper-parameters as specified in the settings above.
set_dl_model_param (DLModelHandle, 'learning_rate', InitialLearningRate)
set_dl_model_param (DLModelHandle, 'momentum', Momentum)
* Set the class names for the model.
*获取到类别的名字
get_dict_tuple (DLDataset, 'class_names', ClassNames)
set_dl_model_param (DLModelHandle, 'class_names', ClassNames)
* Get image dimensions from preprocess parameters and set them for the model.
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' and DLDeviceType == 'gpu')
set_dl_model_param_max_gpu_batch_size (DLModelHandle, 100)
else
set_dl_model_param (DLModelHandle, 'batch_size', BatchSize)
endif
* When the batch size is determined, set the device.
set_dl_model_param (DLModelHandle, 'device', DLDevice)
if (|WeightPrior| > 0)
set_dl_model_param (DLModelHandle, 'weight_prior', WeightPrior)
endif
* Set class weights to counteract unbalanced training data. In this example
* we choose the default values, since the classes are evenly distributed in the dataset.
tuple_gen_const (|ClassNames|, 1.0, ClassWeights)
set_dl_model_param (DLModelHandle, 'class_weights', ClassWeights)
*
* **************************
* ** Train the model ***
* **************************
*
* Create training parameters.
create_dl_train_param (DLModelHandle, NumEpochs, EvaluationIntervalEpochs, EnableDisplay, RandomSeed, GenParamName, GenParamValue, TrainParam)
*
* Start the training by calling the training operator
* train_dl_model_batch () within the following procedure.
train_dl_model (DLDataset, DLModelHandle, TrainParam, 0, TrainResults, TrainInfos, EvaluationInfos)
*
* Stop after the training has finished, before closing the windows.
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Close training windows.
dev_close_window ()
*
* Show the final example screen.
if (ShowExampleScreens)
* Hint at the DL classification evaluation and inference example.
dev_display_screen_final (ExampleInternals)
stop ()
* Close example windows.
dev_close_example_windows (ExampleInternals)
endif
版权声明:本文为qq_35275007原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。