Python Google AI平台:加载模型时出现意外错误:';str';对象没有属性';解码';[Keras 2.3.1,TF 1.15]
我正试图使用谷歌人工智能平台中的测试版谷歌自定义预测例程来运行我的模型的实时版本 我在包Python Google AI平台:加载模型时出现意外错误:';str';对象没有属性';解码';[Keras 2.3.1,TF 1.15],python,tensorflow,keras,gcloud,google-ai-platform,Python,Tensorflow,Keras,Gcloud,Google Ai Platform,我正试图使用谷歌人工智能平台中的测试版谷歌自定义预测例程来运行我的模型的实时版本 我在包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 pre
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)
完整错误创建版本失败。检测到错误的模型:“加载模型失败:加载模型时出现意外错误:'str'对象没有“decode”属性(错误代码:0)”
表示此脚本中存在问题,特别是在加载模型时。但是,我能够使用predict.py
中的相同代码块在本地成功地将模型加载到笔记本中:
from keras.models import load_model
model = load_model('keras.model', compile=False)
我看到过类似的帖子,建议设置
h5py的版本。我在使用AI平台时遇到了同样的问题,该平台的代码在两个月前运行良好,当时我们上次训练我们的模型。事实上,这是由于对h5py的依赖,而h5py未能立即加载h5模型
过了一段时间,我就能够在运行时2.2
和python版本3.7
中使用它了。我还使用自定义预测例程,我的模型是一个简单的2层双向LSTM服务分类
我用TF==2.1设置了一个笔记本虚拟机,并将h5py降级为可以添加完整的回溯吗?我认为这是h5py的问题,因此请确认实际使用的版本是3.0.0之前的版本。@Dr.Snoopy感谢您的回复。为了调试的目的,我很乐意这么做,但谷歌AI平台告诉我的就是这个错误。这个错误消息似乎是由谷歌云包装的,我看不到在自定义预测例程的模型部署时调试东西的方法至少。@Dr.Snoopy你说得对,它奇怪地使用了3.1.0版。我做了一些粗制滥造的事情(注释掉了模型的加载,让predict函数只返回h5py版本)。事实上,我知道它正在正确设置keras的版本,而不是出于某种原因设置h5py。将调查:)
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'])
from setuptools import setup
REQUIRED_PACKAGES = ['tensorflow==2.1', 'h5py<3.0.0']
setup(
name="my_package",
version="0.1",
include_package_data=True,
scripts=["preprocess.py", "model_prediction.py"]
)
model = keras.models.load_model(
os.path.join(model_dir,'model.h5'), compile = False)