-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add source code to our onnx example
- Loading branch information
1 parent
733b854
commit e341779
Showing
9 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pkl | ||
*.onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import onnx | ||
|
||
onnx.checker.check_model("model.onnx") |