Workflow for DL classification🔗
We assume that your dataset is already labeled; see also About data.
Tip
Take a look at the HDevelop example series classify_pill_defects_deep_learning for an application.
-
Preprocess the data.
-
The information about what is found in which image of your training dataset needs to be transferred. This is done by the procedure
read_dl_dataset_classification.
Thereby, a dictionary
DLDatasetis created, which serves as a database and stores all necessary information about your data. For more information about the data and the way it is transferred, see About data and the page Deep learning > HALCON DL model. -
Split the dataset represented by the dictionary
DLDataset. This can be done using the proceduresplit_dl_dataset.
⤷ The resulting split is saved via the key
splitin each sample entry ofDLDataset. -
Read in a pretrained network using the operator
Note
read_dl_modelis also used when you want to read your own trained networks after you have saved them withwrite_dl_model.The network imposes several requirements on the images, as the image dimensions and the gray value range. The default values are listed in
read_dl_model. These are the values with which the networks have been pretrained.
The network architectures allow different image dimensions, which can be set withset_dl_model_param. Depending on the network, a change may make a retraining necessary. The actually set values can be retrieved with -
Now you can preprocess your dataset. For this, you can use the procedure
preprocess_dl_dataset.
In case of custom preprocessing, this procedure offers guidance on the implementation.
To use this procedure, specify the preprocessing parameters as, for example, the image size. Store all the parameters with their values in a dictionary
DLPreprocessParam. To do this, you can use the procedurecreate_dl_preprocess_param.
We recommend saving this dictionary
DLPreprocessParamto have access to the preprocessing parameter values later during the inference phase.
These steps are also shown in the HDevelop example
classify_pill_defects_deep_learning_1_preprocess.hdev. -
-
Train the model.
-
Set the training parameters and store them in the dictionary
TrainParam. These parameters include:- Hyperparameters, as described in the deep learning overview page
- Parameters for possible data augmentation (optional)
- Parameters for the evaluation during training
- Parameters for the visualization of training results
- Parameters for serialization
This can be done using the procedure
create_dl_train_param.
-
Train the model. This can be done using the procedure
train_dl_model.
The procedure expects the following:
- Model handle
DLModelHandle - Dictionary with the data information
DLDataset - Dictionary with the training parameter
TrainParam - Information how many epochs the training shall run
If the procedure
train_dl_modelis used, the total loss as well as optional evaluation measures are visualized.
These steps are also shown in the HDevelop example
classify_pill_defects_deep_learning_2_train.hdev. -
-
Evaluate the trained model.
-
The evaluation can conveniently be done using the procedure
evaluate_dl_model.
-
The dictionary
EvaluationResultholds the resulting evaluation measures. You can visualize your evaluation results using the proceduredev_display_classification_evaluation.
-
A heatmap can be generated for specified samples using
- the operator
gen_dl_model_heatmapor - the procedure
gen_dl_model_classification_heatmap
- the operator
These steps are also shown in the HDevelop example
classify_pill_defects_deep_learning_3_evaluate.hdev. -
-
Optionally, fit the model to Out-of-Distribution Detection.
-
Fit the Out-of-Distribution Detection using
-
Optional: Add out-of-distribution data to the dataset for evaluation using the procedure
add_dl_out_of_distribution_data.
-
Rerun the evaluation using the procedure
evaluate_dl_model.
-
The dictionary
EvaluationResultholds the resulting evaluation measures. You can visualize your evaluation results using the proceduredev_display_classification_evaluation.
These steps are also shown in the HDevelop example
detect_out_of_distribution_samples_for_classification.hdev. -
-
Run inference on new images.
-
Set the parameters as, for example,
'batch_size'using the operator -
Generate a data dictionary
DLSamplefor each image. This can be done using the proceduregen_dl_samples_from_images.
-
Preprocess the images as done for the training. We recommend using the procedure
preprocess_dl_samples.
If you saved the dictionary
DLPreprocessParamduring the preprocessing step, you can directly use it as input to specify all parameter values. -
Apply the model using the operator
-
Retrieve the results from the dictionary
'DLResultBatch'.
These steps are also shown in the HDevelop example
classify_pill_defects_deep_learning_4_infer.hdev. -