【问题标题】:Unable to use configparser in ai-platform custom prediction无法在 ai 平台自定义预测中使用 configparser
【发布时间】:2020-05-15 03:56:44
【问题描述】:

我正在寻找一种在我的预测器代码中使用configparser 来进行自定义预测例程的方法。 我尝试了以下代码 sn-p

common.cfg

[MODEL]
VERSION=config-true

setup.py

from setuptools import setup

REQUIRED_PACKAGES = [
    'joblib==0.13.0'
]

setup(
    name='test',
    description='Custom prediction routine',
    version=0.1,
    install_requires=REQUIRED_PACKAGES,
    scripts=['src/predictor.py', 'config/common.cfg']
)

predictor.py

import os
import joblib
import subprocess
import configparser

class CustomPredictor(object):
    def __init__(self, model, config):
        self._model = model
        self._config = config

    def predict(self, instances, **kwargs):
        version_value = self._config.get('MODEL', 'VERSION', fallback='config-false')
        print(f'version value = {version_value}', flush=True) # printing config-false

        preprocessed_input = self._preprocess(instances)

        score = self._model.predict(preprocessed_input)

        print(f'predicted score {score}', flush=True)
        return score.to_list()

    @classmethod
    def from_path(cls, model_dir):
        config = configparser.RawConfigParser()
        result = config.read('config/common.cfg')
        print(f'read config result: {result}', flush=True) # empty
        print(f'config sections: {config.sections()}', flush=True) # empty

        subprocess.run(["ls", "-l"]) # don't see the config file or folder

        model_path = os.path.join(model_dir, "model.joblib")
        model = joblib.load(model_path)

        return cls(model, config)

对我做错了什么或遗漏有什么建议?

【问题讨论】:

    标签: setuptools google-cloud-ml


    【解决方案1】:

    我运行了这段代码,它打印了预期的结果:

    config = configparser.RawConfigParser()
    result = config.read('common.cfg')
    print(f'read config result: {result}', flush=True) # empty
    print(f'config sections: {config.sections()}', flush=True) # empty
    

    输出是

    read config result: ['common.cfg']
    config sections: ['MODEL']
    

    所以可能的错误可能是您的 common.cfg 文件的路径。

    当我复制您的代码并运行它时,它显示空列表作为输出,因为文件在同一目录中而不是在配置目录中,但是一旦我更正了路径,它就会打印出预期的结果。

    【讨论】:

    • 我的代码在本地工作,我面临的挑战是将配置文件与预测代码一起打包,并在部署到 AI 平台后使其工作
    • 那里的目录结构也一样吗?
    • 不,我无权在那里配置目录结构,我提供的只是 tar.gz 文件,它按照他们的代码(这是一个黑盒子)cloud.google.com/ai-platform/prediction/docs/… 解包跨度>
    【解决方案2】:

    你有两个选择:

    1. 配置setup.py并在此处添加configparser
    REQUIRED_PACKAGES = [
        'joblib==0.13.0',
        'configparser'
    ]
    
    1. 在创建模型时,使用package-uris 将配置解析器作为一个包传递。从pypi 我已经找到了 tar.gz 文件。 请看this 示例,当我们传递 PyTorch 包时,在您的情况下,从 pypi 下载文件并将其放入 GCS Bucket,然后在创建模型时定义它:
    gcloud beta ai-platform versions create {MODEL_VERSION} --model {MODEL_NAME} \
     --origin=gs://{BUCKET_NAME}/{MODEL_DIR}/ \
     --python-version=3.7 \
     --runtime-version={RUNTIME_VERSION} \
     --package-uris=gs://{BUCKET_NAME}/{PACKAGES_DIR}/text_classification-0.1.tar.gz,
    gs://{BUCKET_NAME}/configparser-5.0.0.tar.gz  \
     --machine-type=mls1-c4-m4 \
     --prediction-class=model.CustomModelPrediction
    

    【讨论】:

    【解决方案3】:

    我发现我在做的错误,在部署自定义预测例程期间,ai-platform 将文件保存在/tmp/custom_lib/bin/ 下,并将执行路径保留为根目录。因此,在我的代码中,我已将配置路径更新为类似的内容

    config_file = pathlib.Path(__file__).parent.absolute() / 'common.cfg'
    config.read(config_file)
    

    这解决了问题!

    注意:我也认为我们需要将配置文件保留在 scripts 标签下,因为当部署逻辑安装包时,setuptools 会将脚本复制到 ai-platform 定义的 PATH 并使其可用。 p>

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 2021-07-19
      • 2021-11-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2020-03-28
      相关资源
      最近更新 更多