Google AI平台:加载模型时发生意外错误:“ str”对象没有属性“ decode” [Keras 2.3.1,TF 1.15]

问题描述

我正在尝试使用Google AI平台中的beta版Google自定义预测例程来运行我的模型的实时版本。

我在我的软件包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平台中找到调试模型部署的好方法,并且故障排除指南也无济于事。任何指针将不胜感激。谢谢!

编辑1:

h5py模块的版本错误-在3.1.0中,尽管在setup.py中将其设置为2.10.0。有人知道为什么吗?我确认Keras版本和其他模块设置正确。我尝试了'h5py==2.9.0''h5py<3.0.0'无济于事。有关包括PyPi软件包依赖项here的更多信息。

编辑2:

因此,事实证明Google当前不支持功能

解决方法

两个月前,当我们上次训练我们的模型时,使用AI平台运行代码时遇到了同样的问题。的确,这是由于对h5py的依赖导致无法将h5模型突然加载。

过了一会儿,我使它可以与运行时2.2和python版本3.7一起使用。我还使用了自定义预测例程,我的模型是一个简单的2层双向LSTM服务分类。

我有一个笔记本虚拟机,其TF == 2.1,并使用以下命令将h5py降级为

!pip uninstall -y h5py

!pip install 'h5py < 3.0.0'

我的setup.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"]
)

我在模型加载代码中添加了compile=False。没有它,我遇到了另一个部署问题,该错误给出以下错误:Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: 'sample_weight_mode' (Error code: 0)"

代码从OP更改:

model = keras.models.load_model(
        os.path.join(model_dir,'model.h5'),compile = False)

这使得模型可以像以前一样毫无问题地部署。我怀疑 compile=False可能意味着较慢的预测服务,但到目前为止尚未发现任何东西。

希望这可以帮助任何陷入困境并搜索这些问题的人!