# 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

### CalibrationPreprocessing

Immutable preprocessing needed only by calibration preparation.

#### Methods

##### requires_input_preprocessing

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

Whether this snapshot contains non-identity preprocessing.

#### Attributes

##### data_type

##### encoding_from

##### encoding_mismatch

Whether the source model and its preprocessed input differ.

##### encoding_to

##### is_image

##### mean_values

##### normalization_required

Whether mean subtraction or scale division is non-identity.

##### scale_values

## Functions

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

### channels_last_4d_layout

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

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

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

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

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

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

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

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