Skip to content

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

Add TFLite serialized model

David Rubinstein5ceb6450f265
diff --git a/basic_pitch/__init__.py b/basic_pitch/__init__.py
index d8b78b2..64761f2 100644
--- a/basic_pitch/__init__.py
+++ b/basic_pitch/__init__.py
@@ -38,6 +38,19 @@ except ImportError:
"reinstall basic-pitch with `pip install 'basic-pitch[tf]'`"
)
+try:
+ import tflite_runtime.interpreter as tflite
+
+ TFLITE_PRESENT = True
+except ImportError:
+ TFLITE_PRESENT = False
+ logging.warning(
+ "tflite-runtime is not installed. "
+ "If you plan to use a TFLite Model, "
+ "reinstall basic-pitch with `pip install 'basic-pitch tflite-runtime'` or "
+ "`pip install 'basic-pitch[tf]'"
+ )
+
try:
import onnxruntime as ort
@@ -52,6 +65,8 @@ except ImportError:
if TF_PRESENT:
_filename = "nmp"
+elif TFLITE_PRESENT:
+ _filename = "nmp.tflite"
elif ONNX_PRESENT:
_filename = "nmp.onnx"
diff --git a/basic_pitch/inference.py b/basic_pitch/inference.py
index a116059..36c130e 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 ONNX_PRESENT, TF_PRESENT
+from basic_pitch import ONNX_PRESENT, TF_PRESENT, TFLITE_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()
+ TFLITE = enum.auto()
ONNX = enum.auto()
def __init__(self, model_path: Union[pathlib.Path, str]):
@@ -78,6 +79,17 @@ class Model:
except Exception:
pass
+
+ if TFLITE_PRESENT:
+ try:
+ self.model_type = Model.MODEL_TYPES.TFLITE
+ self.interpreter = tflite.Interpreter(model_path=model_path)
+ self.interpreter.allocate_tensors()
+ self.model = self.interpreter.get_signature_runner()
+ return
+ except Exception:
+ pass
+
if ONNX_PRESENT:
try:
self.model_type = Model.MODEL_TYPES.ONNX
@@ -95,6 +107,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.TFLITE:
+ return cast(tflite.Interpreter, self.model)(x)
elif self.model_type == Model.MODEL_TYPES.ONNX:
return cast(ort.InferenceSession, self.model).run(["note", "onset", "contour"], {"input": x})
diff --git a/basic_pitch/predict.py b/basic_pitch/predict.py
index ecdbf5d..dfec692 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, onnx]",
+ "[tensorflow, tensorflow-lite, onnx]",
)
parser.add_argument(
"--save-midi",
diff --git a/basic_pitch/saved_models/icassp_2022/nmp.tflite b/basic_pitch/saved_models/icassp_2022/nmp.tflite
new file mode 100644
index 0000000..85a41be
Binary files /dev/null and b/basic_pitch/saved_models/icassp_2022/nmp.tflite differ
diff --git a/setup.cfg b/setup.cfg
index 756c6d3..a06c8e4 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -40,6 +40,7 @@ install_requires =
pretty_midi>=0.2.9
resampy>=0.2.2
scipy>=1.4.1
+ tflite-runtime>=2.4.1; platform_system == "Linux"
typing_extensions
[options.entry_points]