From faf0f1d9d356990e58f11c155e501cb4e860a988 Mon Sep 17 00:00:00 2001 From: Svyatoslav Date: Sat, 23 Dec 2023 16:03:42 +0300 Subject: [PATCH] Tried to infer the onnx version of the RF model but suffered fiasco --- README.md | 15 ++++++++++++--- create_example_request.py | 4 ++-- example_request.json | 2 +- mlopscourse/train.py | 4 ++-- poetry.lock | 8 ++++---- pyproject.toml | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 001afc7..5198d08 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,14 @@ If you want to infer a previously trained model, make sure you've placed the che poetry run python3 commands.py infer --config_name [config_name_without_extension] ``` -### Deployment +### Deployment with MLflow + +**Warning! This feature works stably only with the CatBoost model.** Predictions of the +onnx version of the Random Forest differ from the original one (see +[this](https://github.com/onnx/sklearn-onnx/issues/1047#issuecomment-1851837537)). +Moreover, I was not able to infer the onnx version with MLflow (although everything worked +fine with the `mlflow.sklearn` flavour as you can see in the `hw2` version of the +repository). In order to deploy a trained model, run: @@ -102,8 +109,10 @@ poetry run mlflow models serve -p 5001 -m checkpoints/mlflow_[model_type]_ckpt/ where `[model_type]` is `cb` or `rf`. -After this, it is possible to send requests to the model. I've created a script for the -correct json generation with the first example from the training set: +After this, it is possible to send requests to the model. I've created a script to +generate the correct json containing the first example from the training set, but the json +itself is in the repository, so you can skip this step. If you want to generate the json +by yourself, run: ``` poetry run python3 create_example_request.py create_example_request diff --git a/create_example_request.py b/create_example_request.py index 00b2078..27d74f0 100644 --- a/create_example_request.py +++ b/create_example_request.py @@ -11,9 +11,9 @@ def create_example_request() -> None: "season": "spring", "month": 1, "hour": 0, - "holiday": False, + "holiday": 0, "weekday": 6, - "workingday": False, + "workingday": 0, "weather": "clear", "temp": 9.84, "feel_temp": 14.395, diff --git a/example_request.json b/example_request.json index 16d4743..ee59751 100644 --- a/example_request.json +++ b/example_request.json @@ -14,6 +14,6 @@ "humidity", "windspeed" ], - "data": [["spring", 1, 0, false, 6, false, "clear", 9.84, 14.395, 0.81, 0.0]] + "data": [["spring", 1, 0, 0, 6, 0, "clear", 9.84, 14.395, 0.81, 0.0]] } } diff --git a/mlopscourse/train.py b/mlopscourse/train.py index ee1356d..caec4db 100644 --- a/mlopscourse/train.py +++ b/mlopscourse/train.py @@ -65,10 +65,10 @@ def train(self) -> None: ) model.log_fis_and_metrics(exp_id, X_train.columns) else: - model_onnx = to_onnx(model.model, X=X_train.iloc[:1], verbose=1) + model_onnx = to_onnx(model.model, X=X_train[:1], verbose=1) mlflow.onnx.save_model( model_onnx, - f"checkpoints/mlflow_{self.cfg.model.name}_onnx_ckpt/", + f"checkpoints/mlflow_{self.cfg.model.name}_ckpt/", signature=signature, ) model.log_fis_and_metrics(exp_id, X_train, y_train) diff --git a/poetry.lock b/poetry.lock index a16650b..a880597 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3803,13 +3803,13 @@ files = [ [[package]] name = "skl2onnx" -version = "1.15.0" +version = "1.16.0" description = "Convert scikit-learn models to ONNX" optional = false python-versions = "*" files = [ - {file = "skl2onnx-1.15.0-py2.py3-none-any.whl", hash = "sha256:13a9ea5d50619ce42381c67001db8c87ce574a459a8f0738b45d2f4b93f465f6"}, - {file = "skl2onnx-1.15.0.tar.gz", hash = "sha256:05b2c2643ad0357ec1ea684d138438a2df657df828e57d07cb78c2e76be20e37"}, + {file = "skl2onnx-1.16.0-py2.py3-none-any.whl", hash = "sha256:7de548580c625bfa5893fe79c9dd3213c3720b12e1ff8e3fd28967da0698242d"}, + {file = "skl2onnx-1.16.0.tar.gz", hash = "sha256:3370b3d4065ce2dc5933878c3273f4aea41f945cc6514543b13ad2d57f369ce5"}, ] [package.dependencies] @@ -4354,4 +4354,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">= 3.9, < 3.13" -content-hash = "57b2a352e5631d0ff96681a6d4c1f08403d3b182e9e346c84bb4708a67830f79" +content-hash = "803fb59fa7a242d4ad8ca1c7fc2a3c8d0c32bc0cf9462ca3a17f245cb4dceecd" diff --git a/pyproject.toml b/pyproject.toml index 1417852..bf039fe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ catboost = "^1.2.2" fire = "^0.5.0" hydra-core = "^1.3.2" mlflow = "^2.8.1" -skl2onnx = "^1.15.0" +skl2onnx = "^1.16.0" onnxruntime = "^1.16.3" [tool.poetry.group.dev.dependencies]