A CLI tool to convert ms2deepscore PyTorch models to ONNX
Project description
ms2deepscore pytorch 2 onnx converter
This library/cli tool aims at converting ms2deepscore models from pytorch to onnx.
Installation
git clone git@github.com:julianpollmann/ms2deepscore-onnx-converter.git
cd ms2deepscore-onnx-converter
uv sync
# or using pip:
pip install .
Usage
The tool can either convert a local ms2deepscore model or download one from zenodo and convert it to onnx.
# Using the CLI:
ms2ds_onnx https://zenodo.org/records/17826815 -o onnx_model_dir
ms2ds_onnx ms2deepscore_model.pt -o onnx_model_dir
# or within your python script:
from ms2ds_converter import convert_to_onnx
convert_to_onnx("ms2deepscore_model.pt", "onnx_model_dir")
Inference using onnx runtime
After converting a ms2deepscore model to pytorch you can use the ONNX Runtime for inference. You'll need to install onnxruntime separately.
import json
import numpy as np
import onnxruntime as ort
from matchms import Spectrum
from matchms.importing import load_spectra
from ms2deepscore import SettingsMS2Deepscore
from ms2deepscore.tensorize_spectra import tensorize_spectra
def compute_embeddings_onnx(
onnx_session: ort.InferenceSession,
spectra: list[Spectrum],
settings: SettingsMS2Deepscore,
) -> np.ndarray:
# We use ms2deepscore to create tensors from spectra and convert them to np arrays.
X_binned_torch, X_metadata_torch = tensorize_spectra(spectra, settings)
X_binned = X_binned_torch.numpy().astype(np.float32)
X_metadata = X_metadata_torch.numpy().astype(np.float32)
# Build the input data, depending on additional metadata in model, e.g. ["input_peaks", "input_metadata"] or just ["input_peaks"].
input_names = [inp.name for inp in onnx_session.get_inputs()]
output_name = onnx_session.get_outputs()[0].name
input_feed = {input_names[0]: X_binned}
if len(input_names) > 1 and X_metadata.shape[1] > 0:
input_feed[input_names[1]] = X_metadata
# inference step.
embeddings = onnx_session.run([output_name], input_feed)[0]
return embeddings
def main():
# load some spectra with matchms and maybe do some filtering...
spectra = list(load_spectra("spectra.mgf"))
# Load exported SettingsMS2Deepscore.
with open(
"onnx_model_dir/ms2deepscore_model_settings.json", "r", encoding="utf-8"
) as file:
settings_dict = json.load(file)
# Remove spectrum_file_path from settings for inference, since it will fail validation.
settings_dict["spectrum_file_path"] = None
settings = SettingsMS2Deepscore(**settings_dict)
# Load ONNX Model and compute embeddings, will use either GPU or CPU as fallback.
ort_session = ort.InferenceSession(
"onnx_model_dir/ms2deepscore_model.onnx",
providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
)
embeddings = compute_embeddings_onnx(ort_session, spectra, settings)
# Use your embeddings in some way...
np.save("embeddings_onnx.npy", embeddings)
if __name__ == "__main__":
main()
License
GNU GPLv3. See License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ms2deepscore_onnx_converter-0.1.0.tar.gz.
File metadata
- Download URL: ms2deepscore_onnx_converter-0.1.0.tar.gz
- Upload date:
- Size: 501.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66939cfd9235d1046009d6cc2e9ae30e3b6b12d4dc9d5279f7b9e375d7f40f0b
|
|
| MD5 |
80749163d705b736840417e004264647
|
|
| BLAKE2b-256 |
40466a9d0f81eac004d144f9e4c93b5c17512eb8eb969975b967355f749cf153
|
File details
Details for the file ms2deepscore_onnx_converter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ms2deepscore_onnx_converter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa15d3a2a9c2699db40e7d224e4902ea6a07f4772d2ffad8eddf78bdf2ea4bdd
|
|
| MD5 |
2a03b89e88dc0f143d2fa6c6792fef3e
|
|
| BLAKE2b-256 |
00d1000e94c47321d61442be885127b0324f72a20989dae89a9567cf2e5591f5
|