Skip to content

Commit

Permalink
LuxonisML - v0.4.0 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlov721 authored Oct 9, 2024
1 parent 972ea22 commit f248763
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/modelconverter_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 30 additions & 26 deletions modelconverter/packages/base_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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('-')}")
Expand Down
1 change: 1 addition & 0 deletions modelconverter/packages/rvc2/exporter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions modelconverter/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,5 +56,5 @@
"make_default_layout",
"Metadata",
"get_metadata",
"archive_from_model"
"archive_from_model",
]
3 changes: 2 additions & 1 deletion modelconverter/utils/nn_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f248763

Please sign in to comment.