# 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#luxonis_perlin_loader_torch-Classes-LuxonisLoaderPerlinNoise)
blends a texture image into a clean image, inside a random Perlin noise mask. The mask is the label.

## Classes

| Name | Description |
| --- | --- |
| [`LuxonisLoaderPerlinNoise`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md#luxonis_perlin_loader_torch-Classes-LuxonisLoaderPerlinNoise) | Loader that adds synthetic anomalies for the anomaly detection task. |

### class `LuxonisLoaderPerlinNoise`

```python
class LuxonisLoaderPerlinNoise(LuxonisLoaderTorch)
```

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#perlin-Functions-apply_anomaly_to_img).
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

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md#luxonis_perlin_loader_torch-Classes-LuxonisLoaderPerlinNoise-__init__) | Initialize the dataset and collect the texture images. |
| [`get_classes`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md#luxonis_perlin_loader_torch-Classes-LuxonisLoaderPerlinNoise-get_classes) | Return the two classes of the anomaly mask. |

#### method `__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.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `*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#luxonis_loader_torch-Classes-LuxonisLoaderTorch). |  |
| `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. | required |
| `noise_prob` | `float` | The probability that a sample of the `train` view gets an anomaly. | `0.5` |
| `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. | `None` |
| `**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#luxonis_loader_torch-Classes-LuxonisLoaderTorch). |  |

Raises

| Type | Description |
| --- | --- |
| `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. |

#### method `get_classes`

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

Return the two classes of the anomaly mask.

Returns

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