# CLI

Python API: `modelconverter.__main__`

Entry point of the `modelconverter` command-line interface.

Defines the commands the CLI exposes: converting a model for a platform, running inference, benchmarking and analyzing it on a
device, visualizing the analysis, packaging a model into an NN Archive, opening a shell in a platform's container and managing the
cache. The commands that need the vendor conversion tools -- `convert`, `infer` and `shell` -- are re-run inside the Docker image
of the requested platform unless they already run inside one; the others run on the host.

## Functions

| Name | Description |
| --- | --- |
| [`analyze`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-analyze) | Run layer and cycle analysis on the specified DLC model. |
| [`archive`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-archive) | Convert a model file to a Luxonis NN Archive. |
| [`benchmark`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-benchmark) | Run benchmark on the specified platform. |
| [`cache_clean`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-cache_clean) | Remove the entire modelconverter cache. |
| [`cache_info`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-cache_info) | Report the location and disk usage of the modelconverter cache. |
| [`catch_exceptions`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-catch_exceptions) | Log any exception raised in the block and exit. |
| [`convert`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-convert) | Export the model for the specified platform. |
| [`infer`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-infer) | Run inference on the specified platform. |
| [`launcher`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-launcher) | Convert neural network models for Luxonis devices. |
| [`shell`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-shell) | Boot up a shell inside a docker container for the specified platform. |
| [`visualize`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Functions-visualize) | Visualize the analysis results. |

### function `analyze`

```python
def analyze(
    *,
    device_ip: str | None = None,
    device_id: str | None = None,
    dlc_model_path: str,
    onnx_model_path: str,
    image_subset: int | None = None,
    image_dirs: Annotated[list[str], Parameter(negative_iterable=[], consume_multiple=True)],
    analyze_outputs: bool = True,
    analyze_cycles: bool = True,
)
```

Run layer and cycle analysis on the specified DLC model.

Requires the RVC4 device to be connected and accessible using the `adb` command.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `device_ip` | `str | None` | IP address of the device to run the analysis on. Interchangeable with `device_id`. If neither is given, ADB selects the first connected device. If both are given, `device_id` takes precedence. | `None` |
| `device_id` | `str | None` | The unique ID of the device to run the analysis on. Interchangeable with `device_ip`. If neither is given, ADB selects the first connected device. If both are given, `device_id` takes precedence. | `None` |
| `dlc_model_path` | `str` | The path to the DLC model file. | required |
| `onnx_model_path` | `str` | The path to the corresponding ONNX model file that was used for converting to DLC. | required |
| `image_subset` | `int | None` | If provided, limit analysis to the first N supported input files per input directory. | `None` |
| `image_dirs` | `Annotated[list[str], Parameter(negative_iterable=[], consume_multiple=True)]` | A list of names and paths to directories with images for each input of the model. | required |
| `analyze_outputs` | `bool` | Whether to analyze the layer outputs. | `True` |
| `analyze_cycles` | `bool` | Whether to analyze the layer cycles. | `True` |

### function `archive`

```python
def archive(
    path: str,
    *,
    save_path: str | None = None,
    put_file_plugin: str | None = None,
)
```

Convert a model file to a Luxonis NN Archive.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `path` | `str` | A URL or a path to the model file. | required |
| `save_path` | `str | None` | Path or URL to save the archive to. By default, it is saved to the current directory under the name of the model. | `None` |
| `put_file_plugin` | `str | None` | The name of the plugin to use for uploading the file. | `None` |

### function `benchmark`

```python
def benchmark(
    platform: Platform,
    /,
    *,
    model_path: str,
    full: bool = False,
    save: bool = False,
    repetitions: Annotated[int, Parameter(group=['RVC2', 'RVC4'])] = 10,
    benchmark_time: Annotated[int, Parameter(group=['RVC2', 'RVC4'])] = 20,
    input_fps: Annotated[float, Parameter(group=['RVC2', 'RVC4'])] = -1.0,
    num_threads: Annotated[int, Parameter(group=['RVC2', 'RVC4'])] = 2,
    num_messages: Annotated[int, Parameter(group=['RVC2', 'RVC4'])] = 50,
    requests: Annotated[int, Parameter(group='RVC3')] = 1,
    profile: Annotated[Literal['low_balanced', 'balanced', 'high_performance', 'sustained_high_performance', 'burst', 'low_power_saver', 'power_saver', 'high_power_saver', 'extreme_power_saver', 'system_settings'], Parameter(group='RVC4')] = 'balanced',
    runtime: Annotated[Literal['dsp', 'cpu'], Parameter(group='RVC4')] = 'dsp',
    num_images: Annotated[int, Parameter(group='RVC4')] = 500,
    device_ip: Annotated[str | None, Parameter(group='RVC4')] = None,
    device_id: Annotated[str | None, Parameter(group='RVC4')] = None,
    dai_benchmark: Annotated[bool, Parameter(group='RVC4')] = True,
    device_monitor: Annotated[bool, Parameter(group='RVC4')] = True,
)
```

Run benchmark on the specified platform.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | The platform to run the benchmark on. | required |
| `model_path` | `str` | A URL or a path to the model file. | required |
| `full` | `bool` | If `True`, runs the full benchmark using all configurations. | `False` |
| `save` | `bool` | If `True`, saves the benchmark results to a file. | `False` |
| `repetitions` | `Annotated[int, Parameter(group=['RVC2', 'RVC4'])]` | The number of repetitions to perform. Only relevant for DAI benchmark. | `10` |
| `benchmark_time` | `Annotated[int, Parameter(group=['RVC2', 'RVC4'])]` | The duration in seconds for time-based benchmarking (overrides repetitions). | `20` |
| `input_fps` | `Annotated[float, Parameter(group=['RVC2', 'RVC4'])]` | Rate at which inputs are sent by the DAI benchmark. Use `-1` for no limit. | `-1.0` |
| `num_threads` | `Annotated[int, Parameter(group=['RVC2', 'RVC4'])]` | The number of threads to use for inference. Only relevant for DAI benchmark. | `2` |
| `num_messages` | `Annotated[int, Parameter(group=['RVC2', 'RVC4'])]` | The number of messages to measure for each report. Only relevant for DAI benchmark. | `50` |
| `requests` | `Annotated[int, Parameter(group='RVC3')]` | The number of requests to perform. | `1` |
| `profile` | `Annotated[Literal['low_balanced', 'balanced', 'high_performance', 'sustained_high_performance', 'burst', 'low_power_saver', 'power_saver', 'high_power_saver', 'extreme_power_saver', 'system_settings'], Parameter(group='RVC4')]` | The SNPE profile to use for inference. | `'balanced'` |
| `runtime` | `Annotated[Literal['dsp', 'cpu'], Parameter(group='RVC4')]` | The SNPE runtime to use for inference (dsp or cpu). | `'dsp'` |
| `num_images` | `Annotated[int, Parameter(group='RVC4')]` | The number of images to use for inference. Only relevant for SNPE backend. | `500` |
| `device_ip` | `Annotated[str | None, Parameter(group='RVC4')]` | IP address of the device to run the benchmark on. Interchangeable with `device_id`. If neither is given, DAI selects the default device. If both are given, `device_id` takes precedence. | `None` |
| `device_id` | `Annotated[str | None, Parameter(group='RVC4')]` | The unique ID of the device to run the benchmark on. Interchangeable with `device_ip`. If neither is given, DAI selects the default device. If both are given, `device_id` takes precedence. | `None` |
| `dai_benchmark` | `Annotated[bool, Parameter(group='RVC4')]` | Whether to run the benchmark using the DAI V3. If `False`, the SNPE tools are used. | `True` |
| `device_monitor` | `Annotated[bool, Parameter(group='RVC4')]` | Whether to monitor the device performance during benchmarking and include it in the results. Only relevant for RVC4. | `True` |

### function `cache_clean`

```python
def cache_clean(*, yes: Annotated[bool, Parameter(name='-y', negative_bool=[])] = False)
```

Remove the entire modelconverter cache.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `yes` | `Annotated[bool, Parameter(name='-y', negative_bool=[])]` | Clear the cache without prompting for confirmation. | `False` |

### function `cache_info`

```python
def cache_info()
```

Report the location and disk usage of the modelconverter cache.

### function `catch_exceptions`

```python
def catch_exceptions() -> Generator[None, None, None]
```

Log any exception raised in the block and exit.

Exits with status 1 for a `ModelconverterException` and with status 2 for any other exception.

### function `convert`

```python
def convert(
    platform: Platform,
    /,
    *opts: str,
    path: str | None = None,
    output_dir: str | None = None,
    to: Literal['native', 'nn_archive'] = 'nn_archive',
    main_stage: str | None = None,
    archive_preprocess: bool = False,
)
```

Export the model for the specified platform.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | The platform to export the model for. | required |
| `*opts` | `str` | A list of optional CLI overrides for the configuration file. |  |
| `path` | `str | None` | A URL or a path to the configuration file, NN Archive or a standalone model file. | `None` |
| `output_dir` | `str | None` | Directory under `output/` for all conversion results when running through Docker. Defaults to `<name>_to_<platform>_<YYYY_MM_DD_HH_MM_SS>` (UTC), with the package name sanitized for the directory. | `None` |
| `to` | `Literal['native', 'nn_archive']` | Whether to export the model to a simple model file or a Luxonis NN Archive. | `'nn_archive'` |
| `main_stage` | `str | None` | Name of the stage with the main model. Only needed for multistage configs and when converting to NN Archive. When converting from NN Archive, the main stage uses `model.metadata.name`; postprocessor stages use their model filenames without the suffix. The package `name` does not rename archive stages. | `None` |
| `archive_preprocess` | `bool` | Force preprocessing into NN Archive metadata instead of embedding it in the model. Only valid with `to="nn_archive"`. Without this option, preprocessing is first embedded when possible and otherwise falls back to archive metadata for NN Archive output. | `False` |

Raises

| Type | Description |
| --- | --- |
| `SystemExit` | If the conversion fails or is interrupted. The exit status is 130 on Ctrl-C or `SIGTERM`, 1 on a `ModelconverterException` and 2 on any other `Exception`. A `SystemExit` from the conversion itself keeps its own status. |

### function `infer`

```python
def infer(
    platform: Platform,
    /,
    *opts: str,
    model_path: str,
    input_path: Path,
    output_dir: str,
    config: str | None = None,
    path: str | None = None,
    stage: str | None = None,
)
```

Run inference on the specified platform.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | The platform to run the inference on. | required |
| `*opts` | `str` | A list of optional CLI overrides for the configuration file. |  |
| `model_path` | `str` | A URL or a path to the model file. | required |
| `input_path` | `Path` | Path to the directory with data for inference. The directory must contain one subdirectory per input, named the same as the input. The files may be images, `.npy` arrays or `.raw` buffers. | required |
| `output_dir` | `str` | Name of the directory where the inference results will be saved. | required |
| `config` | `str | None` | A URL or a path to the configuration file. | `None` |
| `path` | `str | None` | An alias for `config`. Deprecated. | `None` |
| `stage` | `str | None` | Name of the stage to run. Only needed for multistage configs. If not provided, the first stage will be used. | `None` |

### function `launcher`

```python
def launcher(
    *tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True, json_dict=False, json_list=False)],
    dev: Annotated[bool, Parameter(group=docker_parameters)] = False,
    gpu: Annotated[bool, Parameter(group=docker_parameters)] = True,
    tool_version: Annotated[str | None, Parameter(group=docker_parameters)] = None,
    image: Annotated[str | None, Parameter(['image', 'docker-image'], group=docker_parameters)] = None,
    memory: Annotated[str | None, Parameter(group=docker_parameters)] = None,
    cpus: Annotated[float | None, Parameter(group=docker_parameters)] = None,
)
```

Convert neural network models for Luxonis devices.

The `convert`, `infer` and `shell` commands run in a docker container, unless they already run inside one. The other commands run
on the host.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `*tokens` | `Annotated[str, Parameter(show=False, allow_leading_hyphen=True, json_dict=False, json_list=False)]` | The command and its arguments, parsed by the wrapped app. |  |
| `dev` | `Annotated[bool, Parameter(group=docker_parameters)]` | If `True`, builds and runs the platform's `dev` image, which also mounts the host's sources, tests and `pyproject.toml` over the ones baked into it. | `False` |
| `gpu` | `Annotated[bool, Parameter(group=docker_parameters)]` | If `True`, runs the container with the `nvidia` runtime. Only has an effect for the `hailo` platform. | `True` |
| `tool_version` | `Annotated[str | None, Parameter(group=docker_parameters)]` | Version of the underlying conversion tools to use. Available options differ based on the platform. | `None` |
| `image` | `Annotated[str | None, Parameter(['image', 'docker-image'], group=docker_parameters)]` | Full name of the docker image to use. If the name includes a tag (e.g. `:latest`), it will be used as is and the `--tool-version` argument will be ignored. | `None` |
| `memory` | `Annotated[str | None, Parameter(group=docker_parameters)]` | Amount of memory to allocate to the docker container, as a number with an optional binary unit: `4g` for four gibibytes, `512m`, `2GiB`, or a bare count of bytes. By default, uses all available system memory. | `None` |
| `cpus` | `Annotated[float | None, Parameter(group=docker_parameters)]` | Number of CPU cores to allocate to the docker container. Can be a fractional number, e.g. `0.5` for half a core. By default, uses all available CPU cores. | `None` |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `memory` is not a positive size or `cpus` is not a positive number. |

### function `shell`

```python
def shell(
    platform: Platform,
    /,
    *,
    command: Annotated[str | None, Parameter(name=['-c', '--command'])] = None,
)
```

Boot up a shell inside a docker container for the specified platform.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | The platform. | required |
| `command` | `Annotated[str | None, Parameter(name=['-c', '--command'])]` | The command to run in the shell. If not provided, a bash shell is started. If you want to run a command with arguments, use quotes around the command. | `None` |

### function `visualize`

```python
def visualize(dir_path: str)
```

Visualize the analysis results.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `dir_path` | `str` | The path to the directory holding the analysis CSV files. The analyzer writes them to `output/analysis/<model_name>`. | required |

## Module Attributes

| Name | Description |
| --- | --- |
| [`app`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Module-Attributes-app) |  |
| [`cache_app`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Module-Attributes-cache_app) |  |
| [`device_commands`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Module-Attributes-device_commands) |  |
| [`docker_commands`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Module-Attributes-docker_commands) |  |
| [`docker_parameters`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/__main__.md#CLI-Module-Attributes-docker_parameters) |  |

### variable `app`

```python
app = App(
    name='Modelconverter',
    version=lambda : f"ModelConverter v{importlib.metadata.version('modelconv')}",
)
```

### variable `cache_app`

```python
cache_app = App(
    name='cache',
    help='Manage the hidden modelconverter cache (staged inputs and remote downloads).',
)
```

### variable `device_commands`

```python
device_commands = Group.create_ordered('Device Commands')
```

### variable `docker_commands`

```python
docker_commands = Group.create_ordered('Docker Commands')
```

### variable `docker_parameters`

```python
docker_parameters = Group.create_ordered(
    'Docker Parameters',
    help='Global parameters for all docker commands',
)
```
