# base_inferer

Python API: `modelconverter.platforms.base_inferer`

Abstract base for running inference with a converted model.

[Inferer](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer)
implements the platform-independent half of the `infer` command: it pairs up the files found under the source directory, invokes
the model once per set of inputs and stores the raw outputs as `.npy` files. Every platform -- RVC2, RVC3, RVC4 and Hailo --
subclasses it inside its own Docker image and supplies only the runtime setup and a single inference step.

## Classes

| Name | Description |
| --- | --- |
| [`Inferer`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer) | Platform-independent base for running a converted model. |

### class `Inferer`

```python
class Inferer(ABC)
```

Platform-independent base for running a converted model.

Instances are usually created with
[Inferer.from_config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-from_config).
The output directory and the runtime are prepared during construction, after which
[Inferer.run](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-run)
performs the inference.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `config` | `SingleStageConfig | None` | Configuration the inferer was created from, if any. |
| `dest` | `Path` | Directory the outputs are written to, one sub-directory per model output. |
| `encoding` | `dict[str, Encoding]` | Color encoding expected by every input, keyed by input name. |
| `in_dtypes` | `dict[str, DataType]` | Data type of every model input, keyed by input name. |
| `in_shapes` | `dict[str, list[int]]` | Shape of every model input, keyed by input name. |
| `layout` | `dict[str, str | None]` | Layout of an input, keyed by input name. Inputs with no known layout map to `None`. |
| `model_path` | `Path` | Path to the converted model to run. |
| `out_dtypes` | `dict[str, DataType]` | Data type of every model output, keyed by output name. |
| `out_shapes` | `dict[str, list[int]]` | Shape of every model output, keyed by output name. |
| `resize_method` | `dict[str, ResizeMethod]` | How an image is resized to the shape of the input it is fed to, keyed by input name. |
| `src` | `Path` | Directory holding one sub-directory of input files per model input, each named after that input. |

Methods

| Name | Description |
| --- | --- |
| [`from_config`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-from_config) | Create an inferer from a single-stage configuration. |
| [`infer`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-infer) | Run the model on a single set of inputs. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-run) | Run the model over every input in the source directory. |
| [`setup`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/platforms/base_inferer.md#base_inferer-Classes-Inferer-setup) | Initialize the platform-specific runtime. |

#### class method `from_config`

```python
def from_config(
    model_path: str,
    src: Path,
    dest: Path,
    config: SingleStageConfig,
) -> Self
```

Create an inferer from a single-stage configuration.

The shapes, data types, resize methods, encodings and layouts are taken from the configured inputs and outputs.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model_path` | `str` | Path to the converted model, resolved relative to the current working directory. | required |
| `src` | `Path` | Directory holding the input files. | required |
| `dest` | `Path` | Directory the outputs are written to. | required |
| `config` | `SingleStageConfig` | Configuration of the stage to run. | required |

Returns

| Type | Description |
| --- | --- |
| `Self` | The constructed inferer. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If any configured input or output has no shape. |

#### method `infer`

```python
def infer(inputs: dict[str, Path]) -> dict[str, npt.NDArray[np.generic]]
```

Run the model on a single set of inputs.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputs` | `dict[str, Path]` | Path to the file holding the data for every model input, keyed by input name. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, npt.NDArray[np.generic]]` | The model outputs, keyed by output name. |

#### method `run`

```python
def run()
```

Run the model over every input in the source directory.

The per-input sub-directories of the source directory are walked in lockstep, so one file from each of them forms a set of inputs.
Every output is saved as a `.npy` file under a sub-directory of the destination named after that output, and named after the first
input file of the set.

#### method `setup`

```python
def setup()
```

Initialize the platform-specific runtime.

Called once during construction, before any inference.
