|
| 1 | +# Copyright 2019 Google LLC |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import pickle |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from sklearn.externals import joblib |
| 20 | + |
| 21 | + |
| 22 | +class MyPredictor(object): |
| 23 | + """An example Predictor for an AI Platform custom prediction routine.""" |
| 24 | + |
| 25 | + def __init__(self, model, preprocessor): |
| 26 | + """Stores artifacts for prediction. Only initialized via `from_path`. |
| 27 | + """ |
| 28 | + self._model = model |
| 29 | + self._preprocessor = preprocessor |
| 30 | + |
| 31 | + def predict(self, instances, **kwargs): |
| 32 | + """Performs custom prediction. |
| 33 | +
|
| 34 | + Preprocesses inputs, then performs prediction using the trained |
| 35 | + scikit-learn model. |
| 36 | +
|
| 37 | + Args: |
| 38 | + instances: A list of prediction input instances. |
| 39 | + **kwargs: A dictionary of keyword args provided as additional |
| 40 | + fields on the predict request body. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + A list of outputs containing the prediction results. |
| 44 | + """ |
| 45 | + inputs = np.asarray(instances) |
| 46 | + preprocessed_inputs = self._preprocessor.preprocess(inputs) |
| 47 | + outputs = self._model.predict(preprocessed_inputs) |
| 48 | + return outputs.tolist() |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def from_path(cls, model_dir): |
| 52 | + """Creates an instance of MyPredictor using the given path. |
| 53 | +
|
| 54 | + This loads artifacts that have been copied from your model directory in |
| 55 | + Cloud Storage. MyPredictor uses them during prediction. |
| 56 | +
|
| 57 | + Args: |
| 58 | + model_dir: The local directory that contains the trained |
| 59 | + scikit-learn model and the pickled preprocessor instance. These |
| 60 | + are copied from the Cloud Storage model directory you provide |
| 61 | + when you deploy a version resource. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + An instance of `MyPredictor`. |
| 65 | + """ |
| 66 | + model_path = os.path.join(model_dir, 'model.joblib') |
| 67 | + model = joblib.load(model_path) |
| 68 | + |
| 69 | + preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl') |
| 70 | + with open(preprocessor_path, 'rb') as f: |
| 71 | + preprocessor = pickle.load(f) |
| 72 | + |
| 73 | + return cls(model, preprocessor) |
0 commit comments