我有以下模型。tar.gz结构(如下所述):
model
|- cuts34.pkl
|- code
|- inference.py
|- requirements.txt
我在本地部署我的训练模型(用于推理),像这样:
class Predictor(RealTimePredictor):
def __init__(self, endpoint_name, sagemaker_session):
super().__init__(endpoint_name,
sagemaker_session=sagemaker_session,
serializer=None,
deserializer=None,
content_type='text/plain'
)
entry_point = './model/code/inference.py'
model_data = 'model.tar.gz'
model = PyTorchModel(model_data=model_data,
framework_version="1.5.0",
py_version="py3",
role=role,
entry_point=entry_point
)
serverless_config = ServerlessInferenceConfig()
predictor = model.deploy(instance_type="local", initial_instance_count=1)
inference.py
中的model_fn
看起来像这样:
def model_fn(model_dir):
logger.info("*** Loading the model ...")
path = os.path.join(model_dir, "cuts34.pkl")
learner = load_learner(path)
return learner
用
调用sagemaker.local.LocalSagemakerRuntimeClient()
时response = runtime.invoke_endpoint(
EndpointName=endpoint_name,
ContentType=content_type,
Body=payload
)
我一直得到以下错误:
b'[Errno 2] No such file or directory: '/.sagemaker/mms/models/model/cuts34.pkl'nTraceback (most recent call last):n File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 110, in transformn self.validate_and_initialize(model_dir=model_dir)n File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 158, in validate_and_initializen self._model = self._model_fn(model_dir)n File "/opt/ml/model/code/inference.py", line 265, in model_fnn learner = torch.load(path, map_location=device)n File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 584, in loadn with _open_file_like(f, 'rb') as opened_file:n File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 234, in _open_file_liken return _open_file(name_or_buffer, mode)n File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 215, in __init__n super(_open_file, self).__init__(open(name, mode))nFileNotFoundError: [Errno 2] No such file or directory: '/.sagemaker/mms/models/model/cuts34.pkl'n'
似乎找不到模型pkl。为什么不呢?
请确认您的模型tar球结构与这里的文档内联。
。无父model
目录。
我还建议在您的推理脚本中使用ls
命令,以便在调试时查看文件的位置。
print(os.system("ls"))