我在 AI Platform Serving 上部署简单的 Pytorch 训练模型时遇到问题。
这是错误:
Creating version (this might take a few minutes)......failed.
ERROR: (gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: Can't get attribute 'Net'
on <module '__main__' from 'prediction_server_beta.py'> (Error code: 0)"
这是自定义预测类:
import os
import pickle
import numpy as np
import torch
from torch_model import *
class CustomModelPrediction(object):
def __init__(self, model):
self._model = model
def predict(self, instances):
input = torch.Tensor(instances)
predictions = self._model(input)
return predictions
@classmethod
def from_path(cls, model_dir):
from torch_model import Net
model = Net()
model = torch.load(os.path.join(model_dir, 'pickle_saved_model.pkl'))
#model.eval()
return cls(model)
我尝试更改代码并保存模型格式(.pkl、.pth、.pt),但似乎没有任何效果。我还尝试将我的模型类包含在与我的自定义预测器类相同的 .py 脚本中,但这也不起作用。pip-installable 包包含所有必要的代码,即模型代码以及自定义预测器代码。感谢帮助!