# model

Python API: `luxonis_train.config.predefined_models.anomaly_detection.v1.model`

The unsupervised anomaly detection model.

## Classes

### AnomalyDetectionModel

Unsupervised anomaly detection, after [DRAEM](https://arxiv.org/abs/2108.07610).

The model trains on images with no anomaly. Train it with
[LuxonisLoaderPerlinNoise](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md).
With the probability `noise_prob`, the loader blends an image from a texture dataset into a training image inside a Perlin noise
mask. That mask marks the anomaly. The backbone reconstructs the clean image. The head reads the reconstruction and the input
image together and segments the anomaly. The loss compares the reconstruction with the clean image, and the predicted segmentation
with the mask.

 * `Throughput:`: Frames per second at 256x256. * `light`: 10 on RVC2, 170 on RVC4
    * `heavy`: 0.5 on RVC2, 61 on RVC4

> **Example**
> The `model` section of a config:

```yaml
model:
  predefined_model:
    name: AnomalyDetectionModel
    params:
      variant: light
```

 * `Components:`: * Nodes:
   [RecSubNet](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/recsubnet/recsubnet.md)
   ->
   [DiscSubNetHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/discsubnet_head/discsubnet_head.md)
    * Losses:
      [ReconstructionSegmentationLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/reconstruction_segmentation_loss.md)
    * Metrics:
      [JaccardIndex](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md)
    * Visualizers:
      [SegmentationVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/segmentation_visualizer.md)
    * Main metric:
      [JaccardIndex](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/torchmetrics.md)
    * Variants: * `light`
       * `heavy`

#### Methods

##### init

```python
def __init__(**kwargs):
```

Initialize the model with its default components.

A `metrics_params` given here replaces the whole default two- class configuration.

##### get_variants

```python
def get_variants() -> tuple[str, dict[str, Params]]:
```

Get the default variant name and the available variants.

The default is `light`. Each variant sets `backbone_variant` and `head_variant` to one size: `"n"` for `light` and `"l"` for
`heavy`.

> **Example**
> ```pycon
>>> default, variants = AnomalyDetectionModel.get_variants()
>>> default
'light'
>>> variants["heavy"]
{'backbone_variant': 'l', 'head_variant': 'l'}
```

Returns

 * `tuple[str, dict[str, Params]]`: `"light"` and the two variants with their constructor arguments.
