forked from guyroyse/redisai-iris
-
Notifications
You must be signed in to change notification settings - Fork 1
fix: updates for RedisAI v1.2 #1
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
bsbodden
merged 1 commit into
redis-developer:main
from
bsbodden:bsb/fixes-for-redis-ai-v1_2-use-pytorch-backend
Jul 20, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.DS_Store | ||
.vscode | ||
venv | ||
.venv | ||
target | ||
iris.pt |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,26 +1,36 @@ | ||
from sklearn.datasets import load_iris | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.linear_model import LogisticRegression | ||
from hummingbird.ml import convert | ||
from zipfile import ZipFile | ||
import shutil | ||
import os | ||
|
||
from skl2onnx import convert_sklearn | ||
from skl2onnx.common.data_types import FloatTensorType | ||
TORCH_FILE = 'iris.torch' | ||
TORCH_ARCHIVE = f'{TORCH_FILE}.zip' # the output of torch model save() | ||
TORCHSCRIPT_BLOB_SRC = 'deploy_model.zip' # internal (in zip) torchscript blob | ||
TORCHSCRIPT_BLOB_DEST = 'iris.pt' # output name for extracted torchscript blob | ||
|
||
# prepare the train and test data | ||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
X_train, X_test, y_train, y_test = train_test_split(X, y) | ||
|
||
# train a model | ||
# train the model - using logistic regression classifier | ||
model = LogisticRegression(max_iter=5000) | ||
model.fit(X_train, y_train) | ||
|
||
# convert the model to ONNX | ||
initial_types = [ | ||
('input', FloatTensorType([None, 4])) | ||
] | ||
|
||
onnx_model = convert_sklearn(model, initial_types=initial_types) | ||
# use hummingbird.ml to convert sklearn model to torchscript model (torch.jit backend) | ||
torch_model = convert(model, 'torch.jit', test_input=X_train, extra_config={}) | ||
|
||
# save the model | ||
with open("iris.onnx", "wb") as f: | ||
f.write(onnx_model.SerializeToString()) | ||
torch_model.save(TORCH_FILE) | ||
|
||
# extract the TorchScript binary payload | ||
with ZipFile(TORCH_ARCHIVE) as z: | ||
with z.open(TORCHSCRIPT_BLOB_SRC) as zf, open(TORCHSCRIPT_BLOB_DEST, 'wb') as f: | ||
shutil.copyfileobj(zf, f) | ||
|
||
# clean up - remove the zip file | ||
if os.path.exists(TORCH_ARCHIVE): | ||
os.remove(TORCH_ARCHIVE) |
Binary file not shown.
This file contains hidden or 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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
joblib==0.17.0 | ||
numpy==1.19.2 | ||
onnx==1.7.0 | ||
onnxconverter-common==1.7.0 | ||
protobuf==3.13.0 | ||
scikit-learn==0.23.2 | ||
scipy==1.5.2 | ||
six==1.15.0 | ||
skl2onnx==1.7.0 | ||
sklearn==0.0 | ||
threadpoolctl==2.1.0 | ||
typing-extensions==3.7.4.3 | ||
dill==0.3.4 | ||
hummingbird-ml==0.4.0 | ||
joblib==1.0.1 | ||
numpy==1.21.1 | ||
onnx==1.9.0 | ||
onnxconverter-common==1.8.1 | ||
protobuf==3.17.3 | ||
psutil==5.8.0 | ||
scikit-learn==0.24.2 | ||
bsbodden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scipy==1.7.0 | ||
six==1.16.0 | ||
threadpoolctl==2.2.0 | ||
torch==1.9.0 | ||
typing-extensions==3.10.0.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.