'Model pkl not found by SageMaker inference

I have the following model.tar.gz structure (as described here):

model
|- cuts34.pkl
|- code
  |- inference.py
  |- requirements.txt

I deploy locally my trained model (for inference) like this:

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)

model_fn in inference.py looks like this:

def model_fn(model_dir):
    logger.info("*** Loading the model ...")
    path = os.path.join(model_dir, "cuts34.pkl")
    learner = load_learner(path)
    return learner

When invoking the sagemaker.local.LocalSagemakerRuntimeClient() with

response = runtime.invoke_endpoint(
    EndpointName=endpoint_name,
    ContentType=content_type,
    Body=payload
)

I keep getting the following error:

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 transform\n    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_initialize\n    self._model = self._model_fn(model_dir)\n  File "/opt/ml/model/code/inference.py", line 265, in model_fn\n    learner = torch.load(path, map_location=device)\n  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 584, in load\n    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_like\n    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'

It seems that the model pkl cannot be found. Any thoughts on why not?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source