Gcloud ai-platform,无法创建具有自己的预测类的模型

标签 gcloud google-cloud-ml

我尝试关注AI Platform tutorial上传模型和预测例程,但有一部分失败了,我不明白为什么。

我的预测类(class)与他们的教程中的相同:

%%writefile predictor.py
import os
import pickle

import numpy as np
from sklearn.datasets import load_iris
from sklearn.externals import joblib

class MyPredictor(object):
  def __init__(self, model, preprocessor):
    self._model = model
    self._preprocessor = preprocessor
    self._class_names = load_iris().target_names

  def predict(self, instances, **kwargs):
    inputs = np.asarray(instances)
    preprocessed_inputs = self._preprocessor.preprocess(inputs)
    if kwargs.get('probabilities'):
      probabilities = self._model.predict_proba(preprocessed_inputs)
      return probabilities.tolist()
    else:
      outputs = self._model.predict(preprocessed_inputs)
      return [self._class_names[class_num] for class_num in outputs]

  @classmethod
  def from_path(cls, model_dir):
    model_path = os.path.join(model_dir, 'model.joblib')
    model = joblib.load(model_path)

    preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl')
    with open(preprocessor_path, 'rb') as f:
      preprocessor = pickle.load(f)

    return cls(model, preprocessor)

我用来在云中创建模型的代码是:

! gcloud beta ai-platform versions create {VERSION_NAME} \
  --model {MODEL_NAME} \
  --runtime-version 1.13 \
  --python-version 3.5 \
  --origin gs://{BUCKET_NAME}/custom_prediction_routine_tutorial/model/ \
  --package-uris gs://{BUCKET_NAME}/custom_prediction_routine_tutorial/my_custom_code-0.1.tar.gz \
  --prediction-class predictor.MyPredictor

但我最终遇到了一个奇怪的错误:

ERROR: (gcloud.beta.ai-platform.versions.create) Bad model detected with error:  "Failed to load model: Unexpected error when loading the model: 'ascii' codec can't decode byte 0xf9 in position 2: ordinal not in range(128) (Error code: 0)"

问题是,当我运行相同的命令时没有:

--prediction-class predictor.MyPredictor

工作正常。

有人知道这是什么原因吗?我认为 model.joblib 可能存在编码问题,但是当我自己加载它时没有任何问题

最佳答案

我已经找到解决办法

在教程中,他们使用 pickle 来保存创建的预处理器对象,并使用 Joblib 来保存模型。

您需要使用 Joblib 保存两者,然后将其发送到 Google 存储。

关于Gcloud ai-platform,无法创建具有自己的预测类的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56166553/

相关文章:

python - Google Cloud ML Engine + Tensorflow 在 input_fn() 中执行预处理/标记化

google-cloud-dataflow - apache_beam.runners.dataflow_runner.DataflowRuntimeException : Dataflow pipeline failed:

gcloud - 当 Json 请求包含 "_bytes"或 "b64"时,google cloud ml-engine 会做什么?

计算引擎上的 google cloudML 的 Docker 容器 - 验证安装桶

node.js - 如何从 stackdriver nodejs lib 获取当前跟踪 id 和跨度 id

gcloud - 使用gcloud cli创建服务帐户 key 时,权限被拒绝

node.js - gcloud docker 409 图像与 Node.js 冲突错误

gcloud - 从命令行激活 gcloud 服务帐户时出错

python - 从 Google Cloud ML-Engine 上部署的 SCIKITLEARN 模型进行预测