You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the ``serialize`` in ``GenericModel`` class is True, and it will serialize the model using cloudpickle. However, you can set ``serialize=False`` to disable it. And serialize the model on your own. You just need to copy the serialized model into the ``.artifact_dir``. This example shows step by step how you can do that.
180
+
The example is illustrated using an AutoMLx model.
181
+
182
+
.. code-block:: python3
183
+
184
+
import tempfile
185
+
from ads import set_auth
186
+
from ads.model import GenericModel
187
+
from sklearn.datasets import load_iris
188
+
from sklearn.linear_model import LogisticRegression
189
+
from sklearn.model_selection import train_test_split
190
+
191
+
set_auth(auth="resource_principal")
192
+
193
+
# Load dataset and Prepare train and test split
194
+
iris = load_iris()
195
+
X, y = iris.data, iris.target
196
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
197
+
198
+
# Train a LogisticRegression model
199
+
sklearn_estimator = LogisticRegression()
200
+
sklearn_estimator.fit(X_train, y_train)
201
+
202
+
# Serialize your model. You can choose your own way to serialize your model.
203
+
import cloudpickle
204
+
with open("./model.pkl", "wb") as f:
205
+
cloudpickle.dump(sklearn_estimator, f)
206
+
207
+
model = GenericModel(sklearn_estimator, artifact_dir = "model_artifact_folder", serialize=False)
Now copy the model.pkl file and paste into the ``model_artifact_folder`` folder. And open the score.py in the ``model_artifact_folder`` folder to add implementation of the ``load_model`` function. You can also add your preprocessing steps in ``pre_inference`` function and postprocessing steps in ``post_inference`` function. Below is an example implementation of the score.py.
211
+
Replace your score.py with the code below.
212
+
213
+
.. code-block:: python3
214
+
:emphasize-lines: 28, 29, 30, 31, 123
215
+
216
+
# score.py 1.0 generated by ADS 2.8.2 on 20230301_065458
217
+
import os
218
+
import sys
219
+
import json
220
+
from functools import lru_cache
221
+
222
+
model_name = 'model.pkl'
223
+
224
+
225
+
"""
226
+
Inference script. This script is used for prediction by scoring server when schema is known.
227
+
"""
228
+
229
+
@lru_cache(maxsize=10)
230
+
def load_model(model_file_name=model_name):
231
+
"""
232
+
Loads model from the serialized format
233
+
234
+
Returns
235
+
-------
236
+
model: a model instance on which predict API can be invoked
Returns data type information fetch from input_schema.json.
254
+
255
+
Parameters
256
+
----------
257
+
input_schema_path: path of input schema.
258
+
259
+
Returns
260
+
-------
261
+
data_type: data type fetch from input_schema.json.
262
+
263
+
"""
264
+
data_type = {}
265
+
if os.path.exists(input_schema_path):
266
+
schema = json.load(open(input_schema_path))
267
+
for col in schema['schema']:
268
+
data_type[col['name']] = col['dtype']
269
+
else:
270
+
print("input_schema has to be passed in in order to recover the same data type. pass `X_sample` in `ads.model.framework.sklearn_model.SklearnModel.prepare` function to generate the input_schema. Otherwise, the data type might be changed after serialization/deserialization.")
271
+
return data_type
272
+
273
+
def deserialize(data, input_schema_path):
274
+
"""
275
+
Deserialize json serialization data to data in original type when sent to predict.
276
+
277
+
Parameters
278
+
----------
279
+
data: serialized input data.
280
+
input_schema_path: path of input schema.
281
+
282
+
Returns
283
+
-------
284
+
data: deserialized input data.
285
+
286
+
"""
287
+
288
+
import pandas as pd
289
+
import numpy as np
290
+
import base64
291
+
from io import BytesIO
292
+
if isinstance(data, bytes):
293
+
return data
294
+
295
+
data_type = data.get('data_type', '') if isinstance(data, dict) else ''
296
+
json_data = data.get('data', data) if isinstance(data, dict) else data
Returns prediction given the model and data to predict
343
+
344
+
Parameters
345
+
----------
346
+
model: Model instance returned by load_model API.
347
+
data: Data format as expected by the predict API of the core estimator. For eg. in case of sckit models it could be numpy array/List of list/Pandas DataFrame.
348
+
input_schema_path: path of input schema.
349
+
350
+
Returns
351
+
-------
352
+
predictions: Output from scoring server
353
+
Format: {'prediction': output from model.predict method}
354
+
355
+
"""
356
+
features = pre_inference(data, input_schema_path)
357
+
yhat = post_inference(
358
+
model.predict(features)
359
+
)
360
+
return {'prediction': yhat}
361
+
362
+
Save the score.py and now call ``.verify()`` to check if it works locally.
0 commit comments