# preprocessing

Python API: `modelconverter.utils.preprocessing`

Calibration-side representation of externalized input preprocessing.

Moving the preprocessing to an NN Archive clears it from the conversion config, but calibration still needs it: the quantizer then
sees the input of the source model, not of a model that carries the preprocessing nodes.

## Classes

| Name | Description |
| --- | --- |
| [`CalibrationPreprocessing`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Classes-CalibrationPreprocessing) | Immutable preprocessing needed only by calibration preparation. |

### class `CalibrationPreprocessing`

```python
class CalibrationPreprocessing
```

Immutable preprocessing needed only by calibration preparation.

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `data_type` | `DataType` |  |
| `encoding_from` | `Encoding` |  |
| `encoding_to` | `Encoding` |  |
| `is_image` | `bool` |  |
| `mean_values` | `tuple[float, ...] | None` |  |
| `scale_values` | `tuple[float, ...] | None` |  |

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`encoding_mismatch`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Classes-CalibrationPreprocessing-encoding_mismatch) | `bool` | Whether the source model and its preprocessed input differ. |
| [`normalization_required`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Classes-CalibrationPreprocessing-normalization_required) | `bool` | Whether mean subtraction or scale division is non-identity. |

Methods

| Name | Description |
| --- | --- |
| [`requires_input_preprocessing`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Classes-CalibrationPreprocessing-requires_input_preprocessing) | Whether this snapshot contains non-identity preprocessing. |

#### property `encoding_mismatch`

```python
encoding_mismatch: bool
```

Whether the source model and its preprocessed input differ.

#### property `normalization_required`

```python
normalization_required: bool
```

Whether mean subtraction or scale division is non-identity.

#### method `requires_input_preprocessing`

```python
def requires_input_preprocessing(*, reverse_only: bool = False) -> bool
```

Whether this snapshot contains non-identity preprocessing.

## Functions

| Name | Description |
| --- | --- |
| [`apply_calibration_preprocessing`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-apply_calibration_preprocessing) | Apply externalized color conversion and normalization to an array. |
| [`channels_last_4d_layout`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-channels_last_4d_layout) | Move a unique channel axis last in a four-dimensional layout. |
| [`channels_last_image_layout`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-channels_last_image_layout) | Return the NHWC/HWC counterpart of an image layout. |
| [`input_preprocessing_required`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-input_preprocessing_required) | Whether an input's color conversion or normalization is non-identity. |
| [`is_user_calibration_tensor`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-is_user_calibration_tensor) | Whether a tensor file uses the backend-ready calibration contract. |
| [`normalization_required`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-normalization_required) | Whether mean subtraction or scale division is non-identity. |
| [`read_user_calibration_tensor`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-read_user_calibration_tensor) | Load an opaque user tensor in a backend-required sample shape. |
| [`reorder_layout`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/preprocessing.md#preprocessing-Functions-reorder_layout) | Reorder axes, adding or removing only a singleton batch dimension. |

### function `apply_calibration_preprocessing`

```python
def apply_calibration_preprocessing(
    array: np.ndarray,
    preprocessing: CalibrationPreprocessing,
    *,
    layout: str,
) -> np.ndarray
```

Apply externalized color conversion and normalization to an array.

`layout` describes the array as it exists at this point. It may omit a singleton batch axis even when the configured model layout
has one, which is how a decoded image normally arrives.

### function `channels_last_4d_layout`

```python
def channels_last_4d_layout(layout: str) -> str
```

Move a unique channel axis last in a four-dimensional layout.

### function `channels_last_image_layout`

```python
def channels_last_image_layout(layout: str) -> str
```

Return the NHWC/HWC counterpart of an image layout.

A layout that does not describe an image is returned unchanged.

### function `input_preprocessing_required`

```python
def input_preprocessing_required(
    *,
    encoding_from: Encoding,
    encoding_to: Encoding,
    mean_values: Sequence[float] | None,
    scale_values: Sequence[float] | None,
    reverse_only: bool = False,
) -> bool
```

Whether an input's color conversion or normalization is non-identity.

### function `is_user_calibration_tensor`

```python
def is_user_calibration_tensor(path: Path, calib: ImageCalibrationConfig) -> bool
```

Whether a tensor file uses the backend-ready calibration contract.

### function `normalization_required`

```python
def normalization_required(
    mean_values: Sequence[float] | None,
    scale_values: Sequence[float] | None,
) -> bool
```

Whether mean subtraction or scale division is non-identity.

### function `read_user_calibration_tensor`

```python
def read_user_calibration_tensor(
    path: Path,
    *,
    raw_shape: list[int] | tuple[int, ...] | None = None,
    data_type: DataType,
    input_name: str,
) -> np.ndarray
```

Load an opaque user tensor in a backend-required sample shape.

NumPy files carry their own shape and are returned unchanged. Raw buffers require `raw_shape` and are reshaped without reordering
after their element count is validated.

### function `reorder_layout`

```python
def reorder_layout(
    array: np.ndarray,
    source_layout: str,
    target_layout: str,
) -> np.ndarray
```

Reorder axes, adding or removing only a singleton batch dimension.
