# fomo_visualizer

Python API: `luxonis_train.attached_modules.visualizers.fomo_visualizer`

Draws the points a FOMO head predicts, and the target boxes.

## Classes

| Name | Description |
| --- | --- |
| [`FOMOVisualizer`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer) | Visualize FOMO heatmap detections as points and optional boxes. |

### class `FOMOVisualizer`

```python
class FOMOVisualizer(BBoxVisualizer)
```

Visualize FOMO heatmap detections as points and optional boxes.

![https://raw.githubusercontent.com/luxonis/luxonis-train/e542cf0efa20a0fc5c781ff505d699031cb0d228/media/example_viz/fomo_parking_lot.png](https://raw.githubusercontent.com/luxonis/luxonis-train/e542cf0efa20a0fc5c781ff505d699031cb0d228/media/example_viz/fomo_parking_lot.png)

The predicted points in a parking lot. Each class has its own color.

 * `Inputs:`: * `prediction_canvas`, `target_canvas` (`Tensor`): [B, 3, H, W]
    * `keypoints` (`list[Tensor]`): [Ki, 1, 4] per image, `(x, y, prob, class)`, pixels
    * `target_boundingbox` (`Tensor | None`): [N, 6], `[batch, class, x, y, w, h]`, `xywh` normalized
 * `Outputs:`: * `Tensor | tuple[Tensor, Tensor]`: [B, 3, H, W], a pair when targets are given

> **References**
> * Source: This project.
 * License: Apache-2.0 (this project)

> **Notes**
> Reuses bounding box target drawing and keypoint rendering for decoded FOMO predictions.

> **Example**
> Attached to a `FOMOHead` in `model.nodes`:

```yaml
- name: FOMOHead
  inputs: [EfficientRep]
  visualizers:
    - name: FOMOVisualizer
```

 * `Compatible with:`: * Used by:
   [FOMOModel](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/fomo/v1/model.md#model-Classes-FOMOModel)
    * Nodes:
      [FOMOHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/fomo_head.md#fomo_head-Classes-FOMOHead)

Attributes

| Name | Description |
| --- | --- |
| `supported_tasks` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-__init__) | Initialize the visualizer and store the point options. |
| [`draw_predictions_per_class`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-draw_predictions_per_class) | Draw the predicted points of a batch, colored by class. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-forward) | Draw the predicted points, and the target boxes when given. |

#### method `__init__`

```python
def __init__(visibility_threshold: float = 0.5, radius: int = 5, **kwargs)
```

Initialize the visualizer and store the point options.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `visibility_threshold` | `float` | Minimum probability of a point. [draw_predictions_per_class](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-draw_predictions_per_class) skips a point below it. [forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-forward) ignores it for points with three values. Defaults to `0.5`. | `0.5` |
| `radius` | `int` | Radius of a drawn point, in pixels. Defaults to `5`. | `5` |
| `**kwargs` |  | Keyword arguments forwarded to [BBoxVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/bbox_visualizer.md#bbox_visualizer-Classes-BBoxVisualizer), such as `labels`, `colors`, `scale`, and `node`. |  |

#### method `draw_predictions_per_class`

```python
def draw_predictions_per_class(canvas: Tensor, predictions: list[Tensor]) -> Tensor
```

Draw the predicted points of a batch, colored by class.

For each image, the method scales the coordinates by the `scale` factor and keeps the points with a probability of at least
`visibility_threshold`. Then it clamps them into the image and draws them with `torchvision.utils.draw_keypoints`. A class takes
the color of its name in `colors`, and white when the name has no color.

> **Example**
> ```pycon
>>> import torch
>>> visualizer = FOMOVisualizer(labels=["cat"], colors=["red"])
>>> canvas = torch.zeros(1, 3, 8, 8, dtype=torch.uint8)
>>> points = [torch.tensor([[[4.0, 4.0, 0.9, 0.0]]])]
>>> viz = visualizer.draw_predictions_per_class(canvas, points)
>>> bool((viz[0, 0] == 255).any()), bool((viz[0, 1] == 255).any())
(True, False)
>>> faint = [torch.tensor([[[4.0, 4.0, 0.1, 0.0]]])]
>>> bool(
...     visualizer.draw_predictions_per_class(canvas, faint).any()
... )
False
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `canvas` | `Tensor` | `uint8` images of shape `[B, 3, H, W]`. The method does not modify it. | required |
| `predictions` | `list[Tensor]` | One tensor per image, of shape `[K_i, 1, 4]` with rows `(x, y, prob, class)` in pixels. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor` | A copy of `canvas` with the points drawn. |

#### method `forward`

```python
def forward(
    prediction_canvas: Tensor,
    target_canvas: Tensor,
    keypoints: list[Tensor],
    target_boundingbox: Tensor | None,
) -> tuple[Tensor, Tensor] | Tensor
```

Draw the predicted points, and the target boxes when given.

When every tensor in `keypoints` has three values per point, the method draws the points in red with [KeypointVisualizer.draw_predictions](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/keypoint_visualizer.md#keypoint_visualizer-Classes-KeypointVisualizer-draw_predictions) and `radius`. That call ignores `visibility_threshold` and the `scale` factor. It draws a point with a probability of at least `0.5` at its coordinates, and any other point at the top-left corner. Otherwise the method calls [draw_predictions_per_class](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/fomo_visualizer.md#fomo_visualizer-Classes-FOMOVisualizer-draw_predictions_per_class), which colors the points by class and applies `visibility_threshold` and the `scale` factor. [FOMOHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/fomo_head.md#fomo_head-Classes-FOMOHead) always produces four values per point, so its output takes the second path.

> **Example**
> ```pycon
>>> import torch
>>> visualizer = FOMOVisualizer(labels=["cat"], colors=["red"])
>>> canvas = torch.zeros(1, 3, 8, 8, dtype=torch.uint8)
>>> points = [torch.tensor([[[4.0, 4.0, 0.9, 0.0]]])]
>>> visualizer(canvas, canvas, points, None).shape
torch.Size([1, 3, 8, 8])
>>> boxes = torch.tensor([[0, 0, 0.25, 0.25, 0.5, 0.5]])
>>> len(visualizer(canvas, canvas, points, boxes))
2
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `prediction_canvas` | `Tensor` | `uint8` images of shape `[B, 3, H, W]` to draw the points on. | required |
| `target_canvas` | `Tensor` | `uint8` images of shape `[B, 3, H, W]` to draw the target boxes on. | required |
| `keypoints` | `list[Tensor]` | One tensor per image, of shape `[K_i, 1, 4]` with rows `(x, y, prob, class)` in pixels, or of shape `[K_i, 1, 3]` without the class. | required |
| `target_boundingbox` | `Tensor | None` | Boxes of shape `[N, 6]` with rows `[batch_index, class, x, y, w, h]`, `xywh` normalized to `[0, 1]`. `None` when the batch has no `boundingbox` labels. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[Tensor, Tensor] | Tensor` | The pair `(targets, predictions)` of drawn images when `target_boundingbox` is not `None`; otherwise only the predictions image. |
