From f248763b5dc5b1fbedbcc24215cde5be2a6e85a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kozlovsk=C3=BD?= Date: Thu, 10 Oct 2024 01:50:43 +0200 Subject: [PATCH] LuxonisML - v0.4.0 (#38) --- .github/workflows/modelconverter_test.yaml | 12 +++++ .github/workflows/publish.yaml | 4 +- modelconverter/packages/base_exporter.py | 56 ++++++++++++---------- modelconverter/packages/rvc2/exporter.py | 1 + modelconverter/utils/__init__.py | 4 +- modelconverter/utils/nn_archive.py | 3 +- requirements.txt | 5 +- 7 files changed, 51 insertions(+), 34 deletions(-) diff --git a/.github/workflows/modelconverter_test.yaml b/.github/workflows/modelconverter_test.yaml index 82bd2a6..f7e7aeb 100644 --- a/.github/workflows/modelconverter_test.yaml +++ b/.github/workflows/modelconverter_test.yaml @@ -53,6 +53,18 @@ jobs: credentials_json: ${{ secrets.GCP_CREDENTIALS }} token_format: access_token + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@v1 + + - name: Download file from GCS + run: | + cd docker/extra_packages + if [ "$PACKAGE" = "rvc4" ]; then + gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/snpe.zip . + elif [ "$PACKAGE" = "rvc2" ] || [ "$PACKAGE" = "rvc3" ]; then + gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/openvino_2022_3_vpux_drop_patched.tar.gz . + fi + - name: Run Tests run: | pytest -s --verbose "tests/test_packages/test_$PACKAGE.py" diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 96c873d..fc17b1a 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -108,9 +108,9 @@ jobs: run: | cd docker/extra_packages if [ "$PACKAGE" = "rvc4" ]; then - gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/snpe.zip snpe.zip + gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/snpe.zip . elif [ "$PACKAGE" = "rvc2" ] || [ "$PACKAGE" = "rvc3" ]; then - gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/openvino_2022_3_vpux_drop_patched.tar.gz openvino_2022_3_vpux_drop_patched.tar.gz + gsutil cp gs://luxonis-test-bucket/modelconverter/build-artifacts/openvino_2022_3_vpux_drop_patched.tar.gz . fi - name: Publish diff --git a/modelconverter/packages/base_exporter.py b/modelconverter/packages/base_exporter.py index 5b9e6ee..6ef47a7 100644 --- a/modelconverter/packages/base_exporter.py +++ b/modelconverter/packages/base_exporter.py @@ -76,32 +76,8 @@ def __init__( logger.warning("Calibration has been disabled.") logger.warning("The quantization step will be skipped.") - for name, inp in self.inputs.items(): - calib = inp.calibration - if calib is None: - continue - if not isinstance(calib, RandomCalibrationConfig): - continue - logger.warning( - f"Random calibration is being used for input '{name}'." - ) - dest = self.intermediate_outputs_dir / "random" / name - dest.mkdir(parents=True) - if inp.shape is None: - exit_with( - ValueError( - f"Random calibration requires shape to be specified for input '{name}'." - ) - ) - - for i in range(calib.max_images): - arr = np.random.normal(calib.mean, calib.std, inp.shape) - arr = np.clip(arr, calib.min_value, calib.max_value) - - arr = arr.astype(calib.data_type.as_numpy_dtype()) - np.save(dest / f"{i}.npy", arr) - - self.inputs[name].calibration = ImageCalibrationConfig(path=dest) + if self.target != Target.RVC2: + self._prepare_random_calibration_data() @property def inference_model_path(self) -> Path: @@ -180,6 +156,34 @@ def read_img_dir(self, path: Path, max_images: int) -> List[Path]: imgs = imgs[:max_images] return imgs + def _prepare_random_calibration_data(self) -> None: + for name, inp in self.inputs.items(): + calib = inp.calibration + if calib is None: + continue + if not isinstance(calib, RandomCalibrationConfig): + continue + logger.warning( + f"Random calibration is being used for input '{name}'." + ) + dest = self.intermediate_outputs_dir / "random" / name + dest.mkdir(parents=True) + if inp.shape is None: + exit_with( + ValueError( + f"Random calibration requires shape to be specified for input '{name}'." + ) + ) + + for i in range(calib.max_images): + arr = np.random.normal(calib.mean, calib.std, inp.shape) + arr = np.clip(arr, calib.min_value, calib.max_value) + + arr = arr.astype(calib.data_type.as_numpy_dtype()) + np.save(dest / f"{i}.npy", arr) + + self.inputs[name].calibration = ImageCalibrationConfig(path=dest) + @staticmethod def _attach_suffix(path: Union[Path, str], suffix: str) -> Path: return Path(str(Path(path).with_suffix("")) + f"-{suffix.lstrip('-')}") diff --git a/modelconverter/packages/rvc2/exporter.py b/modelconverter/packages/rvc2/exporter.py index f29e48c..f49e20e 100644 --- a/modelconverter/packages/rvc2/exporter.py +++ b/modelconverter/packages/rvc2/exporter.py @@ -1,6 +1,7 @@ import subprocess import tempfile from functools import partial +from importlib.metadata import version from logging import getLogger from multiprocessing import Pool, cpu_count from os import environ as env diff --git a/modelconverter/utils/__init__.py b/modelconverter/utils/__init__.py index 5efd3ce..b057562 100644 --- a/modelconverter/utils/__init__.py +++ b/modelconverter/utils/__init__.py @@ -22,10 +22,10 @@ from .layout import guess_new_layout, make_default_layout from .metadata import Metadata, get_metadata from .nn_archive import ( + archive_from_model, get_archive_input, modelconverter_config_to_nn, process_nn_archive, - archive_from_model, ) from .onnx_tools import onnx_attach_normalization_to_inputs from .subprocess import subprocess_run @@ -56,5 +56,5 @@ "make_default_layout", "Metadata", "get_metadata", - "archive_from_model" + "archive_from_model", ] diff --git a/modelconverter/utils/nn_archive.py b/modelconverter/utils/nn_archive.py index 1303810..87505ff 100644 --- a/modelconverter/utils/nn_archive.py +++ b/modelconverter/utils/nn_archive.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import Any, Dict, Optional, Tuple +from luxonis_ml.nn_archive.config import CONFIG_VERSION from luxonis_ml.nn_archive.config import Config as NNArchiveConfig from luxonis_ml.nn_archive.config_building_blocks import ( Input as NNArchiveInput, @@ -133,7 +134,7 @@ def modelconverter_config_to_nn( cfg = config.stages[main_stage_key] archive_cfg = { - "config_version": "1.0", + "config_version": CONFIG_VERSION.__args__[-1], # type: ignore "model": { "metadata": { "name": model_name.stem, diff --git a/requirements.txt b/requirements.txt index 5947eb3..172b92d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,8 @@ Pillow PyYAML gcsfs -# luxonis-ml[data,nn_archive] >= 0.2.3 -luxonis-ml[data,nn_archive] @ git+https://github.com/luxonis/luxonis-ml.git@dev -onnx +luxonis-ml[data,nn_archive] >= 0.4.0 +onnx<1.17.0 onnxruntime onnxsim s3fs