# analyze

Python API: `modelconverter.platforms.rvc4.analyze`

Layer-level analysis of converted RVC4 (DLC) models.

Runs the converted model on a connected RVC4 device with `snpe-net-run`, compares every layer's output against the ONNX model the
DLC was converted from, and measures how long each layer takes on the device. Both reports are written as CSV files into the
`analysis` subdirectory of the modelconverter output directory.

## Classes

### RVC4Analyzer

Analyzer of a DLC model running on a connected RVC4 device.

Compares the model's layer outputs against the ONNX model it was converted from and measures the execution time of each layer.

#### Methods

##### init

```python
def __init__(device_ip: str | None, device_id: str | None, dlc_model_path: str, image_dirs: dict[str, str], image_subset: int | None = None):
```

Initialize the analyzer and connect to the device.

Parameters

 * `device_ip` (`str | None`): IP address of the device to analyze on.
 * `device_id` (`str | None`): ID of the device to analyze on.
 * `dlc_model_path` (`str`): Path or URL of the DLC model to analyze.
 * `image_dirs` (`dict[str, str]`): Mapping of the model's input names to the directories holding the data for that input.
 * `image_subset` (`int | None`): If given, only the first `image_subset` supported files of every directory are used.

##### analyze_layer_cycles

```python
def analyze_layer_cycles(self):
```

Measure how long each layer takes on the device.

Runs the DLC model on the device and turns the SNPE diagnostic log into `layer_cycles.csv` in the analysis output directory,
holding the mean execution time of every layer and its share of the total time.

##### analyze_layer_outputs

```python
def analyze_layer_outputs(onnx_model_path: Path):
```

Compare the DLC layer outputs against the ONNX model.

Runs both models on every prepared input sample and writes the per-layer statistics -- maximum absolute difference, MSE, cosine
similarity and mean absolute percentage error, each averaged over the samples -- to `layer_comparison.csv` in the analysis output
directory.

Parameters

 * `onnx_model_path` (`Path`): Path to the ONNX model the analyzed DLC model was converted from.
