Skip to content

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.

  1. Preprocess the data.

    1. 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 DLDataset is 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.

    2. Split the dataset represented by the dictionary DLDataset. This can be done using the procedure

      • split_dl_dataset.

      ⤷ The resulting split is saved via the key split in each sample entry of DLDataset.

    3. Read in a pretrained network using the operator

      Note

      read_dl_model is also used when you want to read your own trained networks after you have saved them with write_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 with set_dl_model_param. Depending on the network, a change may make a retraining necessary. The actually set values can be retrieved with

    4. 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 procedure

      • create_dl_preprocess_param.

      We recommend saving this dictionary DLPreprocessParam to 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.

  2. Train the model.

    1. 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.
    2. 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_model is 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.

  3. Evaluate the trained model.

    1. The evaluation can conveniently be done using the procedure

      • evaluate_dl_model.
    2. The dictionary EvaluationResult holds the resulting evaluation measures. You can visualize your evaluation results using the procedure

      • dev_display_classification_evaluation.
    3. A heatmap can be generated for specified samples using

    These steps are also shown in the HDevelop example classify_pill_defects_deep_learning_3_evaluate.hdev.

  4. Optionally, fit the model to Out-of-Distribution Detection.

    1. Fit the Out-of-Distribution Detection using

    2. Optional: Add out-of-distribution data to the dataset for evaluation using the procedure

      • add_dl_out_of_distribution_data.
    3. Rerun the evaluation using the procedure

      • evaluate_dl_model.
    4. The dictionary EvaluationResult holds the resulting evaluation measures. You can visualize your evaluation results using the procedure

      • dev_display_classification_evaluation.

    These steps are also shown in the HDevelop example detect_out_of_distribution_samples_for_classification.hdev.

  5. Run inference on new images.

    1. Set the parameters as, for example, 'batch_size' using the operator

    2. Generate a data dictionary DLSample for each image. This can be done using the procedure

      • gen_dl_samples_from_images.
    3. Preprocess the images as done for the training. We recommend using the procedure

      • preprocess_dl_samples.

      If you saved the dictionary DLPreprocessParam during the preprocessing step, you can directly use it as input to specify all parameter values.

    4. Apply the model using the operator

    5. Retrieve the results from the dictionary 'DLResultBatch'.

    These steps are also shown in the HDevelop example classify_pill_defects_deep_learning_4_infer.hdev.