我正在尝试使用 Google AI Platform 中的 beta Google Custom Prediction Routine 来运行我的模型的实时版本。
我在我的包predictor.py
中包含一个这样的Predictor
类:
import os
import numpy as np
import pickle
import keras
from keras.models import load_model
class Predictor(object):
"""Interface for constructing custom predictors."""
def __init__(self, model, preprocessor):
self._model = model
self._preprocessor = preprocessor
def predict(self, instances, **kwargs):
"""Performs custom prediction.
Instances are the decoded values from the request. They have already
been deserialized from JSON.
Args:
instances: A list of prediction input instances.
**kwargs: A dictionary of keyword args provided as additional
fields on the predict request body.
Returns:
A list of outputs containing the prediction results. This list must
be JSON serializable.
"""
# pre-processing
preprocessed_inputs = self._preprocessor.preprocess(instances[0])
# predict
outputs = self._model.predict(preprocessed_inputs)
# post-processing
outputs = np.array([np.fliplr(x) for x in x_test])
return outputs.tolist()
@classmethod
def from_path(cls, model_dir):
"""Creates an instance of Predictor using the given path.
Loading of the predictor should be done in this method.
Args:
model_dir: The local directory that contains the exported model
file along with any additional files uploaded when creating the
version resource.
Returns:
An instance implementing this Predictor class.
"""
model_path = os.path.join(model_dir, 'keras.model')
model = load_model(model_path, compile=False)
preprocessor_path = os.path.join(model_dir, 'preprocess.pkl')
with open(preprocessor_path, 'rb') as f:
preprocessor = pickle.load(f)
return cls(model, preprocessor)
完整错误Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: 'str' object has no attribute 'decode' (Error code: 0)"
表明问题出在此脚本中,特别是在加载模型时。但是,我可以使用相同的代码块在本地成功地将模型加载到我的笔记本中predict.py
:
from keras.models import load_model
model = load_model('keras.model', compile=False)
我看过类似的帖子,建议设置版本,h5py<3.0.0
但这没有帮助。我可以在文件中为我的自定义预测例程设置模块版本setup.py
:
from setuptools import setup
REQUIRED_PACKAGES = ['keras==2.3.1', 'h5py==2.10.0', 'opencv-python', 'pydicom', 'scikit-image']
setup(
name='my_custom_code',
install_requires=REQUIRED_PACKAGES,
include_package_data=True,
version='0.23',
scripts=['predictor.py', 'preprocess.py'])
不幸的是,我还没有找到在 google 的 AI Platform 中调试模型部署的好方法,并且故障排除指南也无济于事。任何指针将不胜感激。谢谢!
编辑1:
h5py 模块的版本是错误的——在 3.1.0,尽管在setup.py
. 有谁知道为什么?我确认 Keras 版本和其他模块设置正确。我已经尝试过'h5py==2.9.0'
,'h5py<3.0.0'
但无济于事。更多关于在此处包含 PyPi 包依赖项的信息。
编辑2:
所以事实证明谷歌目前不支持这种能力。