-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdeploy_as_onnx.py
More file actions
190 lines (158 loc) · 6.07 KB
/
deploy_as_onnx.py
File metadata and controls
190 lines (158 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Convert a simple RSMTool model to ONNX."""
from pathlib import Path
import numpy as np
from onnxruntime import InferenceSession
def convert(
model_file: Path,
feature_file: Path,
calibration_file: Path,
trim_min: float,
trim_max: float,
trim_tolerance: float,
verify_correctness: bool = True,
) -> None:
"""Convert a simple rsmtool model to onnx.
Parameters
----------
model_file:
Path to the file containing the SKLL learner.
feature_file:
Path to the file containing the feature statistics.
calibration_file:
Path to the file containing the label statistics.
trim_min,trim_max,trim_tolerance:
Trimming arguments for `fast_predict`.
verify_correctness:
Whether to verify that the converted model produces the same output.
Raises
------
AssertionError
If an unsupported operation is encountered or the correctness test failed.
"""
import json
import pandas as pd
from skl2onnx import to_onnx
from skll.learner import Learner
# load files
learner = Learner.from_file(model_file)
feature_information = pd.read_csv(feature_file, index_col=0)
calibrated_values = json.loads(Path(calibration_file).read_text())
# validate simplifying assumptions
assert (feature_information["transform"] == "raw").all(), "Only transform=raw is implemented"
assert (feature_information["sign"] == 1).all(), "Only sign=1 is implemented"
assert learner.feat_selector.get_support().all(), "Remove features from df_feature_info"
# sort features names (FeatureSet does that)
feature_information = feature_information.sort_values(by="feature")
# combine calibration values into one transformation
scale = calibrated_values["human_labels_sd"] / calibrated_values["train_predictions_sd"]
shift = (
calibrated_values["human_labels_mean"] - calibrated_values["train_predictions_mean"] * scale
)
# export model and statistics
onnx_model = to_onnx(
learner.model,
feature_information["train_mean"].to_numpy().astype(np.float32)[None],
target_opset=20,
)
model_file.with_suffix(".onnx").write_bytes(onnx_model.SerializeToString())
statistics = {
"feature_names": feature_information.index.to_list(),
"feature_outlier_min": (
feature_information["train_mean"] - 4 * feature_information["train_sd"]
).to_list(),
"feature_outlier_max": (
feature_information["train_mean"] + 4 * feature_information["train_sd"]
).to_list(),
"feature_means": feature_information["train_transformed_mean"].to_list(),
"feature_stds": feature_information["train_transformed_sd"].to_list(),
"label_mean": shift,
"label_std": scale,
"label_min": trim_min - trim_tolerance,
"label_max": trim_max + trim_tolerance,
}
(model_file.parent / f"{model_file.with_suffix('').name}_statistics.json").write_text(
json.dumps(statistics)
)
if not verify_correctness:
return
# verify that the converted model produces the same output
from time import time
from rsmtool import fast_predict
from rsmtool.modeler import Modeler
onnx_model = InferenceSession(model_file.with_suffix(".onnx"))
rsm_model = Modeler.load_from_learner(learner)
onnx_duration = 0
rsm_duration = 0
iterations = 1_000
for _ in range(iterations):
# sample random input data
features = (
feature_information["train_mean"]
+ (np.random.rand(feature_information.shape[0]) - 0.5)
* 10
* feature_information["train_sd"]
).to_dict()
start = time()
onnx_prediction = predict(features, model=onnx_model, statistics=statistics)
onnx_duration += time() - start
start = time()
rsm_prediction = fast_predict(
features,
modeler=rsm_model,
df_feature_info=feature_information,
trim=True,
trim_min=trim_min,
trim_max=trim_max,
trim_tolerance=trim_tolerance,
scale=True,
train_predictions_mean=calibrated_values["train_predictions_mean"],
train_predictions_sd=calibrated_values["train_predictions_sd"],
h1_mean=calibrated_values["human_labels_mean"],
h1_sd=calibrated_values["human_labels_sd"],
)["scale_trim"]
rsm_duration += time() - start
assert np.isclose(onnx_prediction, rsm_prediction), f"{onnx_prediction} vs {rsm_prediction}"
print(f"ONNX duration: {round(onnx_duration/iterations, 5)}")
print(f"RSMTool duration: {round(rsm_duration/iterations, 5)}")
def predict(
features: dict[str, float],
*,
model: InferenceSession,
statistics: dict[str, np.ndarray | float | list[str]],
) -> float:
"""Make a single prediction with the convered ONNX model.
Parameters
----------
features:
Dictionary of the input features.
model:
ONNX inference session of the converted model.
statistics:
Dictionary containing the feature and label statistics.
Returns
-------
A single prediction.
"""
# get features in the expected order
features = np.array([features[name] for name in statistics["feature_names"]])
# clip outliers
features = np.clip(
features, a_min=statistics["feature_outlier_min"], a_max=statistics["feature_outlier_max"]
)
# normalize
features = (features - statistics["feature_means"]) / statistics["feature_stds"]
# predict
prediction = model.run(None, {"X": features[None].astype(np.float32)})[0].item()
# transform to human scale
prediction = prediction * statistics["label_std"] + statistics["label_mean"]
# trim prediction
return np.clip(prediction, a_min=statistics["label_min"], a_max=statistics["label_max"])
if __name__ == "__main__":
convert(
Path("test.model"),
Path("features.csv"),
Path("calibrated_values.json"),
trim_min=1,
trim_max=3,
trim_tolerance=0.49998,
)