import matplotlib failed while deploying my model in AWS sagemaker

Viewed 356

I have deployed my AWS model successfully.

but while testing i am getting runtime Error: "import matplotlib.pyplot as plt" . I think it is due to pytorch framework version i used(framework_version=1.2.0). I am facing the same issue when i use higher versions as well.

PyTorchModel(model_data=model_artifact,
                 role = role,
                 framework_version=1.2.0,
                 entry_point='predict.py',
                 predictor_cls=ImagePredictor)

I have other issue when i use version=1.0.0. i.e i am not able to import libraries from sub directories and deployment itself is failing.

Eg: i have some code files in "Code" directory.

 from Code.CTModel import NetWork  ---> **this line will fail as "No module named Code" when i use version=1.0.0**

Ultimately i want to how to use/import libraries which are written under sub-directories.

2 Answers

It sounds like you want to inject some additional code libraries into the SageMaker PyTorch serving container. You might have to dig into the source code for how the PyTorch serving container is built to further customize it: https://github.com/aws/sagemaker-pytorch-inference-toolkit, or build your own image.

Digging into that source code a bit, I see that the container has enabled the importing of arbitrary code, but only when "multi-model mode" is enabled. Can you verify that the code exists under a directory "code" in your model directory and that "multi-model mode" is enabled?

    def initialize(self, context):
        # Adding the 'code' directory path to sys.path to allow importing user modules when multi-model mode is enabled.
        if (not self._initialized) and ENABLE_MULTI_MODEL:
            code_dir = os.path.join(context.system_properties.get("model_dir"), 'code')
            sys.path.append(code_dir)
            self._initialized = True

Reference: https://github.com/aws/sagemaker-pytorch-inference-toolkit/blob/c4e7abc49aeebc2f9b6035337548a90e4330113d/src/sagemaker_pytorch_serving_container/handler_service.py#L47

If this all seems complicated to you (it is), you might want to look into some standardized formats for serializing your PyTorch model such as https://onnx.ai/. I'd love to learn more about what you're trying to do here sometime if you reach out to me at contact@modelzoo.dev. I'm beta-testing a platform that enables deployment in a single line of code and would love to test it out here.

Let me make my query little bit high level: I have predict.py, jupyter notebook , Code(Direcotry),Evoludation(directory) and other .py files in source_dir.

--Code
  --ResNet.py
  --Densenet.py
  --DataLoader.py
--Evaluation 
  --Evaluation.py 
--predict.py
--CT_Code.ipynb

When i execute the predict file from jupyter notebook in my local system, all the modules are imported properly and everything is working fine. But when i am deploying same thing in sagemaker notebook facing issues as mentioned in my question.(Not able to import libraries from Code directory and some basic modules like imageio,PIL, Matplotlib)

Related