Skip to content

A lightweight yet powerful audio-to-MIDI converter with pitch bend detection

Add onnx serialized model

David Rubinsteindfb20ef559df
diff --git a/basic_pitch/__init__.py b/basic_pitch/__init__.py
index e390847..d8b78b2 100644
--- a/basic_pitch/__init__.py
+++ b/basic_pitch/__init__.py
@@ -38,7 +38,21 @@ except ImportError:
"reinstall basic-pitch with `pip install 'basic-pitch[tf]'`"
)
+try:
+ import onnxruntime as ort
+
+ ONNX_PRESENT = True
+except ImportError:
+ ONNX_PRESENT = False
+ logging.warning(
+ "onnxruntime is not installed. "
+ "If you plan to use an ONNX Model, "
+ "reinstall basic-pitch with `pip install 'basic-pitch[onnx]'`"
+ )
+
if TF_PRESENT:
_filename = "nmp"
+elif ONNX_PRESENT:
+ _filename = "nmp.onnx"
ICASSP_2022_MODEL_PATH = pathlib.Path(__file__).parent / "saved_models/icassp_2022" / _filename
diff --git a/basic_pitch/inference.py b/basic_pitch/inference.py
index f770f6c..a116059 100644
--- a/basic_pitch/inference.py
+++ b/basic_pitch/inference.py
@@ -54,7 +54,7 @@ from basic_pitch.constants import (
ANNOTATIONS_FPS,
FFT_HOP,
)
-from basic_pitch import TF_PRESENT
+from basic_pitch import ONNX_PRESENT, TF_PRESENT
from basic_pitch.commandline_printing import (
generating_file_message,
no_tf_warnings,
@@ -67,6 +67,7 @@ import basic_pitch.note_creation as infer
class Model:
class MODEL_TYPES(enum.Enum):
TENSORFLOW = enum.auto()
+ ONNX = enum.auto()
def __init__(self, model_path: Union[pathlib.Path, str]):
if TF_PRESENT:
@@ -77,6 +78,14 @@ class Model:
except Exception:
pass
+ if ONNX_PRESENT:
+ try:
+ self.model_type = Model.MODEL_TYPES.ONNX
+ self.model = ort.InferenceSession(model_path)
+ return
+ except Exception:
+ pass
+
raise ValueError(
f"File {model_path} cannot be loaded into either "
"TensorFlow, CoreML, TFLite or ONNX. "
@@ -86,6 +95,8 @@ class Model:
def predict(self, x: npt.NDArray[np.float32]) -> Dict[str, npt.NDArray[np.float32]]:
if self.model_type == Model.MODEL_TYPES.TENSORFLOW:
return {k: v.numpy() for k, v in cast(tf.keras.Model, self.model(x)).items()}
+ elif self.model_type == Model.MODEL_TYPES.ONNX:
+ return cast(ort.InferenceSession, self.model).run(["note", "onset", "contour"], {"input": x})
def window_audio_file(
diff --git a/basic_pitch/predict.py b/basic_pitch/predict.py
index 1299e63..ecdbf5d 100644
--- a/basic_pitch/predict.py
+++ b/basic_pitch/predict.py
@@ -38,7 +38,7 @@ def main() -> None:
default=ICASSP_2022_MODEL_PATH,
help="path to the saved model directory. Defaults to a ICASSP 2022 model. "
"The preferred model is determined by the first library available in "
- "[tensorflow]",
+ "[tensorflow, onnx]",
)
parser.add_argument(
"--save-midi",
diff --git a/basic_pitch/saved_models/icassp_2022/nmp.onnx b/basic_pitch/saved_models/icassp_2022/nmp.onnx
new file mode 100644
index 0000000..c30e5f9
Binary files /dev/null and b/basic_pitch/saved_models/icassp_2022/nmp.onnx differ
diff --git a/setup.cfg b/setup.cfg
index 9b25c3b..756c6d3 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -36,11 +36,10 @@ install_requires =
librosa>=0.8.0
mir_eval>=0.6
numpy<1.24,>=1.18
+ onnxruntime; platform_system == "Windows"
pretty_midi>=0.2.9
resampy>=0.2.2
scipy>=1.4.1
- tensorflow>=2.4.1; platform_machine != 'arm64'
- tensorflow-macos>=2.4.1; platform_machine == 'arm64'
typing_extensions
[options.entry_points]
@@ -55,10 +54,12 @@ test =
tf =
tensorflow>=2.4.1; platform_system != "Darwin"
tensorflow-macos>=2.4.1; platform_system == "Darwin"
+onnx =
+ onnxruntime
docs =
mkdocs>=1.0.4
dev =
- basic_pitch[test,tf,docs]
+ basic_pitch[test,tf,onnx,docs]
bump2version>=1.0.1
mypy
tox