# tasks

Python API: `luxonis_train.tasks`

The tasks a head can solve, and the labels each one requires.

A task connects a head to the losses, metrics, and visualizers that attach to it. It gives the key of the main prediction of the
head and the labels that the loader must supply.
[Tasks](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
gives an instance of each built-in task.

## Classes

### AnomalyDetection

The anomaly detection task.

Its name is `"anomaly_detection"`.
[LuxonisLoaderPerlinNoise](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/loaders/luxonis_perlin_loader_torch.md)
supplies both of its labels. The `"segmentation"` label is the one-hot anomaly mask, of shape `[2, H, W]`. The
`"original_segmentation"` label is the input image without the added anomaly.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### main_output

The key of the main prediction of a head with this task.

It is `name` unless a subclass overrides it. When `forward` returns a tensor or a list of tensors,
[BaseNode.run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md)
puts the result under this key. An attached module fills some arguments from this key. The name of such an argument starts with
`pred` and has no underscore, for example `predictions`.

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### BoundingBox

The bounding box detection task.

Its name is `"boundingbox"`. It requires only the `"boundingbox"` label.

#### Methods

##### init

```python
def __init__(self):
```

### Classification

The classification task.

Its name and required label are both `"classification"`.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Embeddings

The embedding task.

Its name is `"embeddings"`. It requires integer or categorical `"metadata/id"` labels.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Fomo

The FOMO detection task.

Its name is `"fomo"`. A FOMO head predicts a heatmap of object centers. The task requires the `"boundingbox"` label, and its main
prediction is `"heatmap"`.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### main_output

The key of the main prediction of a head with this task.

It is `name` unless a subclass overrides it. When `forward` returns a tensor or a list of tensors,
[BaseNode.run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md)
puts the result under this key. An attached module fills some arguments from this key. The name of such an argument starts with
`pred` and has no underscore, for example `predictions`.

### InstanceBaseTask

Base class for the tasks that detect object instances.

The subclasses require the `"boundingbox"` label. Some subclasses add more labels.

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### InstanceKeypoints

The keypoint detection task for object instances.

Its name is `"keypoints"`. It requires bounding boxes and keypoints.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### InstanceSegmentation

The instance segmentation task.

Its name is `"instance_segmentation"`. It requires bounding boxes and instance masks.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### InstanceSegmentationKeypoints

The instance segmentation and keypoint detection task.

Its name is `"instance_segmentation_keypoints"`. It requires bounding boxes, instance masks, and keypoints.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Keypoints

The keypoint task without bounding boxes.

Its name is `"pointcloud"`. Unlike
[InstanceKeypoints](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md),
it does not require the `"boundingbox"` label.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Metadata

A metadata label that a task requires.

The string form of the label is `"metadata/<name>"`. The key of the label in the loader output is `"<task_name>/metadata/<name>"`,
where `<task_name>` is the dataset task of the node. Two labels are equal when `name` and `typ` are equal. The hash depends only
on `name`.

> **Example**
> ```pycon
>>> from luxonis_train.tasks import Metadata
>>> label = Metadata("text", str)
>>> str(label)
'metadata/text'
```

#### Methods

##### check_type

```python
def check_type(typ: UnionType | type) -> bool:
```

Check if the label accepts values of a type.

When the `typ` attribute is a union, the method checks that the `typ` argument is one of the members of the union. Otherwise, it checks that the two types are equal. A subclass of an accepted type does not match.

> **Example**
> ```pycon
>>> from luxonis_train.tasks import Metadata
>>> label = Metadata("id", int | str)
>>> label.check_type(str), label.check_type(float)
(True, False)
>>> Metadata("flag", int).check_type(bool)
False
```

Parameters

 * `typ` (`UnionType | type`): The type to check, for example the type of the label in the dataset metadata.

Returns

 * `bool`: `True` when the label accepts `typ`.

#### Attributes

##### name

The name of the metadata label, for example `"id"`. The `metadata_task_override` field of a node config can rename it.

##### typ

The type that the label values must have, or a union of the accepted types.

### Ocr

The optical character recognition task.

Its name is `"ocr"`. It requires string `"metadata/text"` labels.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Segmentation

The semantic segmentation task.

Its name and required label are both `"segmentation"`.

#### Methods

##### init

```python
def __init__(self):
```

#### Attributes

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### staticproperty

Descriptor that calls a function on each attribute access.

[Tasks](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
uses it so that every attribute access returns a fresh task.

#### Methods

##### init

```python
def __init__(func: Callable):
```

### Task

Base class for all tasks.

A head sets its task in the `task` class attribute. A loss, a metric, or a visualizer lists the tasks it supports in
`supported_tasks`. Two tasks are equal when they have the same class and the same `name`.

A subclass must override
[required_labels](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md).
Python does not enforce this rule, because `functools.cached_property` hides the abstract method. An instance of a subclass
without the override returns `None` for
[required_labels](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md).

#### Attributes

##### main_output

The key of the main prediction of a head with this task.

It is `name` unless a subclass overrides it. When `forward` returns a tensor or a list of tensors,
[BaseNode.run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/base_node.md)
puts the result under this key. An attached module fills some arguments from this key. The name of such an argument starts with
`pred` and has no underscore, for example `predictions`.

##### name

The name of the task. It is the default value of
[main_output](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md).

##### required_labels

The labels that the loader must supply for this task.

An implementation returns a set of label types, such as `"boundingbox"`, and
[Metadata](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/tasks.md)
labels. The key of a label in the loader output is `"<task_name>/<label>"`. `<task_name>` is the dataset task of the node, not the
`name` of the task. The property computes the value once for each task instance.

### Tasks

The namespace that gives an instance of each built-in task.

Each access to an attribute builds a new task instance. The new instances are equal to each other, so a test such as `node.task in
supported_tasks` works.

> **Example**
> ```pycon
>>> from luxonis_train.tasks import Tasks
>>> task = Tasks.INSTANCE_KEYPOINTS
>>> task.name, task.main_output
('keypoints', 'keypoints')
>>> sorted(task.required_labels)
['boundingbox', 'keypoints']
>>> Tasks.FOMO.main_output
'heatmap'
>>> Tasks.BOUNDINGBOX == Tasks.BOUNDINGBOX
True
```

#### Attributes

##### ANOMALY_DETECTION

##### BOUNDINGBOX

##### CLASSIFICATION

##### EMBEDDINGS

##### FOMO

##### INSTANCE_KEYPOINTS

##### INSTANCE_SEGMENTATION

##### INSTANCE_SEGMENTATION_KEYPOINTS

##### KEYPOINTS

##### OCR

##### SEGMENTATION
