-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmodel_deserializer.py
37 lines (32 loc) · 1.13 KB
/
model_deserializer.py
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
"""
Copyright 2021 DataRobot, Inc. and its affiliates.
All rights reserved.
This is proprietary source code of DataRobot, Inc. and its affiliates.
Released under the terms of DataRobot Tool and Utility Agreement.
"""
from tensorflow.keras.models import load_model
from sklearn.pipeline import Pipeline
import joblib
import h5py
from pathlib import Path
def deserialize_estimator_pipeline(input_dir: str) -> Pipeline:
"""
Load estimator pipeline from the given joblib file.
Parameters
----------
input_dir: str
The directory from which joblib file would be loaded.
Returns
-------
pipeline: Pipeline
Constructed pipeline with necessary preprocessor steps and estimator to predict/score.
"""
# load the dictionary obj from the joblib file
joblib_file_path = Path(input_dir) / "artifact.joblib"
estimator_dict = joblib.load(joblib_file_path)
with h5py.File(estimator_dict["model"], mode="r") as fp:
keras_model = load_model(fp)
pipeline = Pipeline(
[("preprocessor", estimator_dict["preprocessor_pipeline"]), ("estimator", keras_model)]
)
return pipeline