Anomaly Detection, Global Context Anomaly Detection

List of Operators ↓

This chapter explains how to use anomaly detection and Global Context Anomaly Detection based on deep learning.

With those two methods we want to detect whether or not an image contains anomalies. An anomaly means something deviating from the norm, something unknown.

An anomaly detection or Global Context Anomaly Detection model learns common features of images without anomalies. The trained model will infer, how likely an input image contains only learned features or if the image contains something different. Latter one is interpreted as an anomaly. This inference result is returned as a gray value image. The pixel values therein indicate how likely the corresponding pixels in the input image pixels show an anomaly.

We differentiate between two model types that can be used:

Anomaly Detection

With anomaly detection (model type 'anomaly_detection'"anomaly_detection""anomaly_detection""anomaly_detection""anomaly_detection""anomaly_detection") structural anomalies are targeted, thus any feature that was not learned during training. This can, e.g., include scratches, cracks or contamination.

A possible example for anomaly detection: Every pixel of the input image gets assigned a value that indicates how likely the pixel is to be an anomaly. The worm is not part of the worm-free apples the model has seen during training and therefore its pixels get a much higher score.
Global Context Anomaly Detection

Global Context Anomaly Detection (model type 'gc_anomaly_detection'"gc_anomaly_detection""gc_anomaly_detection""gc_anomaly_detection""gc_anomaly_detection""gc_anomaly_detection") comprises two tasks:

A possible example for Global Context Anomaly Detection: Every pixel of the input image gets assigned a value that indicates how likely the pixel is to be an anomaly. Thereby two different types of anomalies can be detected, structural and logical ones. Structural anomaly: One apple contains a worm, which differs from the apples the model has seen during training. Logical anomaly: One apple is sorted among lemons. Although the apple itself is intact, the logical constraint is violated, as the model has only seen images with correctly sorted fruit during training.

The Global Context Anomaly Detection model consists of two subnetworks. The model can be reduced to one of the subnetworks, in order to improve the runtime and memory consumption. This is recommended if a single subnetwork performs well enough. See the parameter 'gc_anomaly_networks'"gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks" in get_dl_model_paramget_dl_model_paramGetDlModelParamGetDlModelParamGetDlModelParamget_dl_model_param for details. After setting 'gc_anomaly_networks'"gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks""gc_anomaly_networks", the model needs to be evaluated again, since this parameter can change the Global Context Anomaly Detection performance significantly.

Training image of an exemplary task. Apples and lemons are intact, sorted correctly, and tagged with the correct sticker.
image/svg+xml image/svg+xml
(1) (2)
image/svg+xml image/svg+xml
(3) (4)
Some anomalies that can be detected with Global Context Anomaly Detection: (1) Logical anomaly, most likely detected by the local subnetwork (wrong sticker). (2) Structural anomaly, most likely detected by local subnetwork (wormy apple). (3) Logical anomaly, most likely detected by global subnetwork (wrong sorting). (4) Logical anomaly, most likely detected by global subnetwork (missing apples).

General Workflow

In this paragraph, we describe the general workflow for an anomaly detection or Global Context Anomaly Detection task based on deep learning.

Preprocess the data

This part is about how to preprocess your data.

  1. The information content of your dataset needs to be converted. This is done by the procedure

    • read_dl_dataset_anomaly.

    It creates a dictionary DLDataset 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 the section “Data” below and the chapter Deep Learning / Model.

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

    • split_dl_dataset.

  3. The network imposes several requirements on the images. These requirements (for example the image size and gray value range) can be retrieved with

    For this you need to read the model first by using

  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, e.g., the image size. Store all the parameter with their values in a dictionary DLPreprocessParam, for which you can use the procedure

    • create_dl_preprocess_param.

    We recommend to save this dictionary DLPreprocessParam in order to have access to the preprocessing parameter values later during the inference phase.

Training of the model

This part explains how to train a model.

  1. Set the training parameters and store them in the dictionary TrainParam. 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

    The procedure expects:

    • the model handle DLModelHandleDLModelHandleDLModelHandleDLModelHandleDLModelHandledlmodel_handle

    • the dictionary DLDataset containing the data information

    • the dictionary TrainParam containing the training parameters

  3. Normalize the network. This step is only necessary when using a Global Context Anomaly Detection model. The anomaly scores need to be normalized by applying the procedure

    • normalize_dl_gc_anomaly_scores.

    This needs to be done in order to get reasonable results when applying a threshold on the anomaly scores later (see section “Specific Parameters” below).

Evaluation of the trained model

In this part, we evaluate the trained model.

  1. Set the model parameters which may influence the evaluation.

  2. The evaluation can be done conveniently using the procedure

    • evaluate_dl_model.

    This procedure expects a dictionary GenParam with the evaluation parameters.

  3. The dictionary EvaluationResult holds the desired evaluation measures.

Inference on new images

This part covers the application of an anomaly detection or Global Context Anomaly Detection model. For a trained model, perform the following steps:

  1. Request the requirements the model imposes on the images using the operator

    or the procedure

    • create_dl_preprocess_param_from_model.

  2. Set the model parameter described in the section “Model Parameters” below, using the operator

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

    • gen_dl_samples_from_images.

  4. Every image has to be preprocessed the same way as for the training. For this, you can use the procedure

    • preprocess_dl_samples.

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

  5. Apply the model using the operator

  6. Retrieve the results from the dictionary DLResult.


We distinguish between data used for training, evaluation, and inference on new images.

As a basic concept, the model handles data by dictionaries, meaning it receives the input data from a dictionary DLSample and returns a dictionary DLResult and DLTrainResult, respectively. More information on the data handling can be found in the chapter Deep Learning / Model.


In anomaly detection and Global Context Anomaly Detection there are exactly two classes:

These classes apply to the whole image as well as single pixels.

Data for training

This dataset consists only of images without anomalies and the corresponding information. They have to be provided in a way the model can process them. Concerning the image requirements, find more information in the section “Images” below.

The training data is used to train a model for your specific task. With the aid of this data the model can learn which features the images without anomalies have in common.

Data for evaluation

This dataset should include images without anomalies but it can also contain images with anomalies. Every image within this set needs a ground truth label image_label specifying the class of the image (see the section above). This indicates if the image shows an anomaly ('nok'"nok""nok""nok""nok""nok") or not ('ok'"ok""ok""ok""ok""ok").

Evaluating the model performance on finding anomalies can visually also be done on pixel level if an image anomaly_file_name is included in the DLSampleDLSampleDLSampleDLSampleDLSampledlsample dictionary. In this image anomaly_file_name every pixel indicates the class ID, thus if the corresponding pixel in the input image shows an anomaly (pixel value > 0) or not (pixel value equal to 0).

image/svg+xml image/svg+xml
(1) (2)
Scheme of anomaly_file_name. For visibility, gray values are used to represent numbers. (1) Input image. (2) The corresponding anomaly_file_name providing the class annotations, 0: 'ok'"ok""ok""ok""ok""ok" (white and light gray), 2: 'nok'"nok""nok""nok""nok""nok" (dark gray).

The model poses requirements on the images, such as the dimensions, the gray value range, and the type. The specific values depend on the model itself. See the documentation of read_dl_modelread_dl_modelReadDlModelReadDlModelReadDlModelread_dl_model for the specific values of different models. For a read model they can be queried with get_dl_model_paramget_dl_model_paramGetDlModelParamGetDlModelParamGetDlModelParamget_dl_model_param. In order to fulfill these requirements, you may have to preprocess your images. Standard preprocessing of an entire sample, including the image, is implemented in preprocess_dl_samples. In case of custom preprocessing these procedure offers guidance on the implementation.

Model output

The training output differs depending on the used model type:

As inference and evaluation output, the model will return a dictionary DLResultDLResultDLResultDLResultDLResultdlresult for every sample. For anomaly detection and Global Context Anomaly Detection, this dictionary includes the following extra entries: