Skip to content

Commit

Permalink
Tried to infer the onnx version of the RF model but suffered fiasco
Browse files Browse the repository at this point in the history
  • Loading branch information
TopCoder2K committed Dec 23, 2023
1 parent 38bdf23 commit faf0f1d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions create_example_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion example_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
}
}
4 changes: 2 additions & 2 deletions mlopscourse/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit faf0f1d

Please sign in to comment.