# 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)
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

### Inferer

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).
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)
performs the inference.

#### Methods

##### 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.

Parameters

 * `model_path` (`str`): Path to the converted model, resolved relative to the current working directory.
 * `src` (`Path`): Directory holding the input files.
 * `dest` (`Path`): Directory the outputs are written to.
 * `config` (`SingleStageConfig`): Configuration of the stage to run.

Returns

 * `Self`: The constructed inferer.

Raises

 * `ValueError`: If any configured input or output has no shape.

##### infer

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

Run the model on a single set of inputs.

Parameters

 * `inputs` (`dict[str, Path]`): Path to the file holding the data for every model input, keyed by input name.

Returns

 * `dict[str, np.ndarray]`: The model outputs, keyed by output name.

##### run

```python
def run(self):
```

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.

##### setup

```python
def setup(self):
```

Initialize the platform-specific runtime.

Called once during construction, before any inference.

#### Attributes

##### config

Configuration the inferer was created from, if any.

##### dest

Directory the outputs are written to, one sub-directory per model output.

##### encoding

Color encoding expected by every input, keyed by input name.

##### in_dtypes

Data type of every model input, keyed by input name.

##### in_shapes

Shape of every model input, keyed by input name.

##### layout

Layout of an input, keyed by input name. Inputs with no known layout map to `None`.

##### model_path

Path to the converted model to run.

##### out_dtypes

Data type of every model output, keyed by output name.

##### out_shapes

Shape of every model output, keyed by output name.

##### resize_method

How an image is resized to the shape of the input it is fed to, keyed by input name.

##### src

Directory holding one sub-directory of input files per model input, each named after that input.
