Skip to content

Commit

Permalink
feat: add source code to our onnx example
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Apr 2, 2024
1 parent 733b854 commit e341779
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pkl
*.onnx
15 changes: 15 additions & 0 deletions src/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
scikit-learn = "*"
skl2onnx = "*"
onnxruntime-gpu = "*"

[dev-packages]
pylint = "*"

[requires]
python_version = "3.11"
333 changes: 333 additions & 0 deletions src/Pipfile.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: "3.11"

services:
ort:
volumes:
- ".:/models"
ports:
- 9001:8001
- 50051:50051
image: mcr.microsoft.com/onnxruntime/server
command:
- --model_path
- /models/model.onnx
27 changes: 27 additions & 0 deletions src/load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pickle

from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

import onnx

if __name__ == "__main__":
MODEL = None
with open("model.pkl", "rb") as f:
MODEL = pickle.load(f)

assert MODEL is not None

INITIAL_TYPE = [
("float_input", FloatTensorType([None, 7])),
]
ONX = convert_sklearn(MODEL, initial_types=INITIAL_TYPE)
onnx.save_model(
ONX,
"model.onnx",
save_as_external_data=True,
all_tensors_to_one_file=True,
location="filename",
size_threshold=1024,
convert_attribute=False,
)
36 changes: 36 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
To reduce the time, we create and load onnx model
in the same script. The example is written based on:
https://onnx.ai/sklearn-onnx/auto_tutorial/plot_abegin_convert_pipeline.html
"""

import pickle

import numpy
import onnxruntime as ort
from skl2onnx import to_onnx

import onnx.reference

if __name__ == "__main__":
# load trained model from a pickle file.
MODEL = None
with open("model.pkl", "rb") as f:
MODEL = pickle.load(f)

assert MODEL is not None

# create a sample train data.
x_sample = numpy.array([[12.3, 24.4, 45.3, 65.7, 567, 4.5, 1]])

# calls convert_sklearn() with simplified parameters.
onx = to_onnx(MODEL, x_sample[:1].astype(numpy.float64))

session_options = ort.SessionOptions()
session_options.graph_optimization_level = (
ort.GraphOptimizationLevel.ORT_DISABLE_ALL
)

oinf = onnx.reference.ReferenceEvaluator(onx)
print(oinf)
3 changes: 3 additions & 0 deletions src/model_dlw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

mcli cp smapp-teh1/nostra-baly/1/rf_1_25_85_0_0_0_0.pkl model.pkl
8 changes: 8 additions & 0 deletions src/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import onnxruntime as ort

# Load the model and create InferenceSession
model_path = "./model.onnx"
session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])

outputs = session.run(output_names={}, input_feed={})
print(outputs)
3 changes: 3 additions & 0 deletions src/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import onnx

onnx.checker.check_model("model.onnx")

0 comments on commit e341779

Please sign in to comment.