Skip to content

Commit

Permalink
Fix examples after update to onnx 1.11 (#831)
Browse files Browse the repository at this point in the history
* Fix examples after update to onnx 1.11
  • Loading branch information
xadupre authored Feb 23, 2022
1 parent c0a0eb8 commit 64a7460
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines/linux-conda-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
sklearn.version: '>=1.0'
lgbm.version: ''
onnxcc.version: '>=1.8.1' # git
run.example: '0'
run.example: '1'
Py39-Onnx1102-Rt110-Skl10:
do.bench: '0'
python.version: '3.9'
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/plot_pipeline_lightgbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
model_onnx = convert_sklearn(
pipe, 'pipeline_lightgbm',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})

# And save.
with open("pipeline_lightgbm.onnx", "wb") as f:
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/plot_pipeline_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
try:
convert_sklearn(pipe, 'pipeline_xgboost',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})
except Exception as e:
print(e)

Expand Down Expand Up @@ -102,7 +102,7 @@
model_onnx = convert_sklearn(
pipe, 'pipeline_xgboost',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})

# And save.
with open("pipeline_xgboost.onnx", "wb") as f:
Expand Down
10 changes: 6 additions & 4 deletions docs/tutorial/plot_cbegin_opset.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
# ++++


onx = to_onnx(model, X[:1].astype(numpy.float32))
onx = to_onnx(model, X[:1].astype(numpy.float32),
target_opset={'': 15, 'ai.onnx.ml': 2})
print(onx)

##########################
Expand Down Expand Up @@ -89,9 +90,10 @@ def get_domain_opset(onx):
return {d['domain']: d['version'] for d in res}


for opset in range(1, onnx_opset_version() + 1):
for opset in range(6, onnx_opset_version() + 1):
try:
onx = to_onnx(model, X[:1].astype(numpy.float32), target_opset=opset)
onx = to_onnx(model, X[:1].astype(numpy.float32),
target_opset={'': opset, 'ai.onnx.ml': 2})
except RuntimeError as e:
print('target: %r error: %r' % (opset, e))
continue
Expand All @@ -112,7 +114,7 @@ def get_domain_opset(onx):
# ``''`` but the other opset domain can be changed as well.

for opset in range(9, onnx_opset_version() + 1):
for opset_ml in range(1, 3):
for opset_ml in range(1, 4):
tops = {'': opset, 'ai.onnx.ml': opset_ml}
try:
onx = to_onnx(
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/plot_gexternal_lightgbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
model_onnx = convert_sklearn(
pipe, 'pipeline_lightgbm',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})

# And save.
with open("pipeline_lightgbm.onnx", "wb") as f:
Expand Down
8 changes: 5 additions & 3 deletions docs/tutorial/plot_gexternal_lightgbm_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ def skl2onnx_convert_lightgbm(scope, operator, container):
# TreeEnsembleRegressor node, or more. *split* parameter is the number of
# trees per node TreeEnsembleRegressor.

model_onnx = to_onnx(reg, X[:1].astype(numpy.float32), target_opset=14)
model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32), target_opset=14,
model_onnx = to_onnx(reg, X[:1].astype(numpy.float32),
target_opset={'': 14, 'ai.onnx.ml': 2})
model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32),
target_opset={'': 14, 'ai.onnx.ml': 2},
options={'split': 100})

##########################
Expand Down Expand Up @@ -158,7 +160,7 @@ def skl2onnx_convert_lightgbm(scope, operator, container):
res = []
for i in tqdm(list(range(20, 170, 20)) + [200, 300, 400, 500]):
model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32),
target_opset=14,
target_opset={'': 14, 'ai.onnx.ml': 2},
options={'split': i})
sess_split = InferenceSession(model_onnx_split.SerializeToString())
got_split = sess_split.run(None, {'X': X32})[0].ravel()
Expand Down
7 changes: 4 additions & 3 deletions docs/tutorial/plot_gexternal_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
try:
convert_sklearn(pipe, 'pipeline_xgboost',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})
except Exception as e:
print(e)

Expand Down Expand Up @@ -97,7 +97,7 @@
model_onnx = convert_sklearn(
pipe, 'pipeline_xgboost',
[('input', FloatTensorType([None, 2]))],
target_opset=12)
target_opset={'': 12, 'ai.onnx.ml': 2})

# And save.
with open("pipeline_xgboost.onnx", "wb") as f:
Expand Down Expand Up @@ -154,7 +154,8 @@
#############################
# ONNX

onx = to_onnx(pipe, X_train.astype(numpy.float32))
onx = to_onnx(pipe, X_train.astype(numpy.float32),
target_opset={'': 12, 'ai.onnx.ml': 2})

sess = rt.InferenceSession(onx.SerializeToString())
pred_onx = sess.run(None, {"X": X_test[:5].astype(numpy.float32)})
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorial/plot_usparse_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def make_pipelines(df_train, y_train, models=None,
pipe,
initial_types=[('input', FloatTensorType([None, 2])),
('text', StringTensorType([None, 1]))],
target_opset=12, options=options)
target_opset={'': 12, 'ai.onnx.ml': 2},
options=options)

with open('model.onnx', 'wb') as f:
f.write(model_onnx.SerializeToString())
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/plot_wext_pyod_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def pyod_iforest_converter(scope, operator, container):

if IForest is not None:
onx = to_onnx(model1, initial_types=initial_type,
target_opset=14)
target_opset={'': 14, 'ai.onnx.ml': 2})

###############################################
# Checking discrepencies
Expand Down
31 changes: 29 additions & 2 deletions skl2onnx/common/_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _default_OPSET_TO_IR_VERSION():
return {
1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3,
7: 3, 8: 4, 9: 4, 10: 5, 11: 6, 12: 7,
13: 7, 14: 7, 15: 8
13: 7, 14: 7, 15: 8, 16: 8
}


Expand All @@ -52,11 +52,29 @@ def _default_OPSET_TO_IR_VERSION():
except (ImportError, KeyError):
OPSET_TO_IR_VERSION = _default_OPSET_TO_IR_VERSION()

OPSET_ML_TO_OPSET = {1: 11, 2: 15}
OPSET_ML_TO_OPSET = {1: 11, 2: 15, 3: 16}

logger = getLogger('skl2onnx')


def get_default_opset_for_domain(domain):
"""
Returns the associated for a domain given the main opset.
"""
from .. import __max_supported_opset__ as main_opset
if domain == '':
return main_opset
if domain == 'ai.onnx.ml':
if main_opset >= 16:
return 3
if main_opset < 6:
return 1
return 2
if domain == 'ai.onnx.training':
return 1
return None


class Variable:
"""
Defines a variable which holds any data defined
Expand Down Expand Up @@ -1516,7 +1534,16 @@ def _update_domain_version(container, onnx_model, verbose=0):
print('[_update_domain_version] +opset %d: name=%r, version=%s' % (
i, op_domain, op_version))
op_set.domain = op_domain
if op_set != '':
max_supported = get_default_opset_for_domain(op_domain)
if max_supported is not None and max_supported < op_version:
raise RuntimeError(
"The model is using version %d of domain %r not supported "
"yet by this library. You need to specify "
"target_opset={%r: %r}." % (
op_version, op_domain, op_domain, max_supported))
op_set.version = op_version

i += 1
if container.target_opset_any_domain(op_domain) < op_version:
raise RuntimeError(
Expand Down

0 comments on commit 64a7460

Please sign in to comment.