# base_visualizer

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

The base class every visualizer inherits.

## Classes

### BaseVisualizer

Base class for all visualizers.

A visualizer draws the predictions of a node, and the labels of the batch, on copies of the input images. Every subclass registers
itself in the
[VISUALIZERS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/registry.md)
registry under its class name, so a config names it as a string.

A subclass implements
[forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md).
[BaseAttachedModule.get_parameters](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/base_attached_module.md)
describes how
[run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
fills its non-canvas parameters.

#### Methods

##### init

```python
def __init__(*args, scale: float = 1.0, **kwargs):
```

Initialize the visualizer and store the canvas scale.

Parameters

 * `*args`: Positional arguments forwarded to
   [BaseAttachedModule](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/base_attached_module.md).
 * `scale` (`float`): Factor that
   [run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
   applies to both canvases with
   [scale_canvas](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
   before it calls
   [forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md).
   Defaults to `1.0`.
 * `**kwargs`: Keyword arguments forwarded to
   [BaseAttachedModule](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/base_attached_module.md),
   such as `node`.

##### forward

```python
def forward(target_canvas: Tensor, prediction_canvas: Tensor, *args: Unpack[Ts]) -> Tensor | tuple[Tensor, Tensor] | tuple[Tensor, list[Tensor]] | list[Tensor]:
```

Draw the labels and the predictions on the canvases.

Implementations return one of:

 * One image, as
   [ClassificationVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/classification_visualizer.md)
   does when `include_plot` is `False`.
 * A tuple `(labels, predictions)` of two images, as
   [BBoxVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/bbox_visualizer.md)
   does.
 * A tuple of the labels image and a list of images.
 * A list of unrelated images.

Parameters

 * `target_canvas` (`Tensor`): Images to draw the labels on, of shape `[B, 3, H, W]`.
 * `prediction_canvas` (`Tensor`): Images to draw the predictions on, of shape `[B, 3, H, W]`.
 * `*args` (`Unpack[Ts]`): The predictions and labels that
   [run](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
   resolves from the parameter names of the implementation.

Returns

 * `Tensor | tuple[Tensor, Tensor] | tuple[Tensor, list[Tensor]] | list[Tensor]`: The visualizations, in one of the four forms
   above.

##### run

```python
def run(prediction_canvas: Tensor, target_canvas: Tensor, inputs: Packet[Tensor], labels: Labels | None) -> Tensor | tuple[Tensor, Tensor] | tuple[Tensor, list[Tensor]]:
```

Scale the canvases, resolve the inputs, and call
[forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md).

[BaseAttachedModule.get_parameters](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/base_attached_module.md)
documents how the remaining
[forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
parameters select predictions and labels.

Parameters

 * `prediction_canvas` (`Tensor`): Images to draw the predictions on, of shape `[B, 3, H, W]`.
 * `target_canvas` (`Tensor`): Images to draw the labels on, of shape `[B, 3, H, W]`.
 * `inputs` (`Packet[Tensor]`): The output packet of the node.
 * `labels` (`Labels | None`): The labels of the batch, keyed `<task_name>/<label>`, or `None` when the batch has none. Then every
   optional `target` parameter receives `None`, and a required one raises `RuntimeError`.

Returns

 * `Tensor | tuple[Tensor, Tensor] | tuple[Tensor, list[Tensor]]`: What
   [forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md)
   returns.

##### scale_canvas

```python
def scale_canvas(canvas: Tensor, scale: float = 1.0) -> Tensor:
```

Resize a batch of images by a factor with bilinear interpolation.

> **Example**
> ```pycon
>>> import torch
>>> canvas = torch.zeros(1, 3, 4, 6)
>>> BaseVisualizer.scale_canvas(canvas, scale=0.5).shape
torch.Size([1, 3, 2, 3])
>>> BaseVisualizer.scale_canvas(canvas, scale=2.0).shape
torch.Size([1, 3, 8, 12])
```

Parameters

 * `canvas` (`Tensor`): Images of shape `[B, C, H, W]`.
 * `scale` (`float`): Multiplier for the height and the width. Defaults to `1.0`.

Returns

 * `Tensor`: Images of shape `[B, C, floor(H * scale), floor(W * scale)]`.

#### Attributes

##### colormap

A `ColorMap` that gives each label a distinct RGB color.

The map assigns a color on the first access to a label and returns the same color afterwards. This property creates the map on its first access and caches it.

## Attributes

### Ts
