# luxonis_perlin_loader_torch

Python API: `luxonis_train.loaders.luxonis_perlin_loader_torch`

The loader of the anomaly detection task.

[LuxonisLoaderPerlinNoise](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md)
blends a texture image into a clean image, inside a random Perlin noise mask. The mask is the label.

## Classes

### LuxonisLoaderPerlinNoise

Loader that adds synthetic anomalies for the anomaly detection task.

The dataset must have only one task. When the first split of the view is `"train"`, the loader adds an anomaly to an image with
the probability `noise_prob`. The anomaly is a random texture image inside a random Perlin noise mask, see
[luxonis_train.loaders.perlin.apply_anomaly_to_img](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/perlin.md).
The image height and width of the `train` view must then be multiples of `32`. For other views, the loader reads the anomaly mask
from the `segmentation` label of the dataset.

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

```yaml
loader:
  name: LuxonisLoaderPerlinNoise
  params:
    dataset_name: mvtec_v2
    anomaly_source_path: ../data/dtd/images/
```

#### Methods

##### init

```python
def __init__(*args, anomaly_source_path: PathType, noise_prob: float = 0.5, beta: float | None = None, **kwargs):
```

Initialize the dataset and collect the texture images.

Parameters

 * `*args`: Positional arguments for
   [LuxonisLoaderTorch](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_loader_torch.md).
 * `anomaly_source_path` (`PathType`): The directory of the texture images. The loader collects all files in the directory tree
   with an extension from `IMAGE_FORMATS`, in any letter case. The loader downloads a remote URL into `./data`, and uses a local
   path directly.
 * `noise_prob` (`float`): The probability that a sample of the `train` view gets an anomaly.
 * `beta` (`float | None`): The weight of the clean image inside the mask. The texture gets the weight `1 - beta`, so `0.0` gives
   an opaque anomaly. `None` draws a new value from `[0, 0.8)` for each anomaly.
 * `**kwargs`: Keyword arguments for
   [LuxonisLoaderTorch](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_loader_torch.md).

Raises

 * `FileNotFoundError`: If the download of `anomaly_source_path` fails, or the directory has no image files.
 * `ValueError`: If the dataset has more than one task.

##### get_classes

```python
def get_classes(self) -> dict[str, Mapping[str, int]]:
```

Return the two classes of the anomaly mask.

Returns

 * `dict[str, Mapping[str, int]]`: One entry for the dataset task. Its `bidict` maps `"background"` to `0` and `"anomaly"` to `1`.
