首页 > 解决方案 > GCP AI Platform:创建自定义预测模型版本时出错(训练模型 Pytorch 模型 + torchvision.transform)

问题描述

我目前正在尝试按照https://cloud.google.com/ai-platform/prediction/docs/deploying-models#gcloud_1将自定义模型部署到 AI 平台。它基于来自'Pytorch'和 ' torchvision.transform'的预训练模型的组合。目前,我一直低于错误,这恰好与自定义预测的 500MB 限制有关。

错误:(gcloud.beta.ai-platform.versions.create)创建版本失败。检测到错误的模型错误:模型需要的内存超出了允许的范围。请尝试减小模型大小并重新部署。如果您继续遇到错误,请联系支持人员。

安装程序.py

from setuptools import setup
from pathlib import Path

base = Path(__file__).parent
REQUIRED_PACKAGES = [line.strip() for line in open(base/"requirements.txt")]
print(f"\nPackages: {REQUIRED_PACKAGES}\n\n")

# [torch==1.3.0,torchvision==0.4.1, ImageHash==4.2.0
# Pillow==6.2.1,pyvis==0.1.8.2] installs 800mb worth of files

setup(description="Extract features of a image",
      author=,
      name='test',
      version='0.1',
      install_requires=REQUIRED_PACKAGES,
      project_urls={
                    'Documentation':'https://cloud.google.com/ai-platform/prediction/docs/custom-prediction-routines#tensorflow',
                    'Deploy':'https://cloud.google.com/ai-platform/prediction/docs/deploying-models#gcloud_1',
                    'Ai_platform troubleshooting':'https://cloud.google.com/ai-platform/training/docs/troubleshooting',
                    'Say Thanks!': 'https://medium.com/searce/deploy-your-own-custom-model-on-gcps-ai-platform- 
 7e42a5721b43',
                    'google Torch wheels':"http://storage.googleapis.com/cloud-ai-pytorch/readme.txt",
                    'Torch & torchvision wheels':"https://download.pytorch.org/whl/torch_stable.html "
                    },
    python_requires='~=3.7',
    scripts=['predictor.py', 'preproc.py'])

采取的步骤: 尝试将“torch”和 torchvision 直接添加到 setup.py 文件中的“REQUIRED_PACKAGES”列表中,以便在部署时提供 PyTorch + torchvision 作为要安装的依赖项。我猜,Ai 平台内部为 PyTorch 下载了 +500 MB 的 PyPI 包,这导致我们的模型部署失败。如果我只使用“火炬”部署模型并且它似乎正在工作(当然会因为找不到库“火炬视觉”而引发错误)

文件大小

总的来说,模型依赖项应该小于 250MB,但仍然会出现此错误。还尝试使用 Google 镜像包http://storage.googleapis.com/cloud-ai-pytorch/readme.txt提供的 torch 和 torchvision ,但同样的内存问题仍然存在。人工智能平台对我们来说是相当新的,希望专业人士提供一些意见。

更多信息:

GCP CLI 输入:

我的环境变量:

BUCKET_NAME= “something”
MODEL_DIR=<>
VERSION_NAME='v6'
MODEL_NAME="something_model"
STAGING_BUCKET=$MODEL_DIR<>
# TORCH_PACKAGE=$MODEL_DIR"package/torch-1.3.1+cpu-cp37-cp37m-linux_x86_64.whl"
# TORCHVISION_PACKAGE=$MODEL_DIR"package/torchvision-0.4.1+cpu-cp37-cp37m-linux_x86_64.whl"
TORCH_PACKAGE=<>
TORCHVISION_PACKAGE=<>
CUSTOM_CODE_PATH=$STAGING_BUCKET"imt_ai_predict-0.1.tar.gz"
PREDICTOR_CLASS="predictor.MyPredictor"
REGION=<>
MACHINE_TYPE='mls1-c4-m2'
 
gcloud beta ai-platform versions create $VERSION_NAME   \
--model=$MODEL_NAME   \
--origin=$MODEL_DIR  \
 --runtime-version=2.3  \
 --python-version=3.7   \
--machine-type=$MACHINE_TYPE  \
 --package-uris=$CUSTOM_CODE_PATH,$TORCH_PACKAGE,$TORCHVISION_PACKAGE   \
--prediction-class=$PREDICTOR_CLASS \ 

GCP CLI 输出:

 **[1] global**
 [2] asia-east1
 [3] asia-northeast1
 [4] asia-southeast1
 [5] australia-southeast1
 [6] europe-west1
 [7] europe-west2
 [8] europe-west3
 [9] europe-west4
 [10] northamerica-northeast1
 [11] us-central1
 [12] us-east1
 [13] us-east4
 [14] us-west1
 [15] cancel
Please enter your numeric choice:  1
 
To make this the default region, run `gcloud config set ai_platform/region global`.
 
Using endpoint [https://ml.googleapis.com/]
Creating version (this might take a few minutes)......failed.                                                                                                                                            
ERROR: (gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: **Model requires more memory than allowed. Please try to decrease the model size and re-deploy. If you continue to experience errors, please contact support.**

我的发现: 发现有人以同样的方式为 PyTorch 软件包苦苦挣扎,并通过在 GCS 上安装火炬轮使其工作(https://medium.com/searce/deploy-your-own-custom-model-on-gcps -ai-平台- 7e42a5721b43)。已经尝试过使用 torch 和 torchvision 的相同方法,但到目前为止还没有运气,等待“cloudml-feedback@google.com cloudml-feedback@google.com”的回复。在 AI 平台上获得基于自定义 torch_torchvision 的自定义预测器的任何帮助都会很棒。

标签: pythongoogle-cloud-platformpytorchtorchvisiongoogle-ai-platform

解决方案


通过几件事的组合解决了这个问题。我坚持使用 4gb CPU MlS1 机器和自定义预测程序(<500MB)。

  • 使用 setup.py 参数安装库,而不是只解析包名称和版本,添加正确的火炬轮(理想情况下 <100 mb)。
REQUIRED_PACKAGES = [line.strip() for line in open(base/"requirements.txt")] +\
['torchvision==0.5.0', 'torch @ https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-linux_x86_64.whl']
  • 我减少了预处理的步骤。不适合所有这些,因此将您的 SEND 响应 jsonify 并从 preproc.py 和 predictor.py 获取一个
import json
json.dump(your data to send to predictor class)
  • 从需要的库的类中导入这些函数。
from torch import zeros,load 
    your code

[重要的]

  • 尚未为经过训练的模型测试不同类型的序列化对象,可能存在差异以及哪个(torch.save、pickle、joblib 等)节省内存。

  • 为那些组织与 GCP 合作的组织找到此链接可能能够请求更多配额(我猜测从 500MB 到 2GB 左右)。不必朝这个方向走,因为我的问题已经解决并且其他弹出了大声笑。 https://cloud.google.com/ai-platform/training/docs/quotas


推荐阅读