Skip to content

Getting errors while deploying custom Sagemaker MXNet model #289

Closed

Description

@Harathi123

System Information

MXNet Gluon
Python 3.6
GPU
Using custom model for training and inference

Problem Description:

I wrote custom MXNet model training and inference code and created training job. I am getting error while deploying the model.

  1. In the custom training script, I am importing skimage module and some other modules. That skimage module was imported successfully while fitting the model and created training job successfully. But while deploying the model, i am getting ‘No module name skimage.draw’ error.

  2. I am also loading pretarined resnet model as follows:
    from mxnet.gluon.model_zoo.vision import resnet34_v1
    While fitting the model, the pretrained resnet model was downloaded. But while deploying the model, i am getting the following error:
    ‘Mismatch in the content of model file detected. Downloading again.’
    'BadZipfile: Bad CRC-32 for file 'resnet34_v1-4dc2c239.params'

Can anyone give suggestions to resolve these issues...

Thanks in advance,
Harathi

Activity

  1. mvsusp commented on Jul 13, 2018

    @mvsusp
    Contributor

    Hello @Harathi123 ,

    Would mind sharing more details on how your importing the modules during training?

    You will have to install the same modules in the container when you deploy your model.

    Best,

    Márcio

  2. Harathi123 commented on Jul 13, 2018

    @Harathi123
    Author

    Hi @mvsusp , thanks for replying

    I am installing the module 'scikit-image' and the dependency module 'matplotlib' in the container as follows. Before importing modules, i am installing them using subprocess module.

          from subprocess import call
          call("pip install matplotlib".split(" ")) 
    
          from subprocess import call
          call("pip install scikit-image".split(" ")) 
    
          from skimage.draw import line_aa
    

    While creating training job and deploying, sagemaker uses same custom training and inference code (train.py) file right... In that file, i included above lines to install modules.

    Thanks,
    Harathi

  3. mvsusp commented on Jul 13, 2018

    @mvsusp
    Contributor

    @Harathi123,

    Would you mind confirming that these lines are being calling when the server starts up? You can either use local mode to deploy in a local server or look at your cloudwatch logs to confirm if these lines are being called.

    If the lines are being called, please share your logs, to further investigate.

    Thanks!

  4. Harathi123 commented on Jul 13, 2018

    @Harathi123
    Author

    @mvsusp ,

    Yes these lines are being called when the server started. Can i share the cloudwatch link to see the logs?

    Thanks,
    Harathi

  5. mvsusp commented on Jul 13, 2018

    @mvsusp
    Contributor

    @Harathi123 ,

    AWS teams do not have access to your cloudwatch logs due to security concerns.
    Feel free to share the log in this thread, and please ensure that no sensitive data is included on it.

    Thanks!

    Márcio

  6. Harathi123 commented on Jul 13, 2018

    @Harathi123
    Author

    @mvsusp ,

    Please find the traceback for resnet pretarined model downloading error:

    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/ggevent.py", line 203, in init_process
    super(GeventWorker, self).init_process()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 350, in import_app
    import(module)
    File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 96, in import
    result = _import(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/container_support/wsgi.py", line 16, in
    app = Server.from_env().app
    File "/usr/local/lib/python2.7/dist-packages/container_support/serving.py", line 60, in from_env
    transformer = framework.transformer(user_module)
    File "/usr/local/lib/python2.7/dist-packages/mxnet_container/serve/transformer.py", line 30, in transformer
    return MXNetTransformer.from_module(user_module)
    File "/usr/local/lib/python2.7/dist-packages/mxnet_container/serve/transformer.py", line 106, in from_module
    model = m.model_fn(env.model_dir)
    File "/opt/ml/code/localization_bb.py", line 484, in model_fn
    net = SSD(3)
    File "/opt/ml/code/localization_bb.py", line 329, in init
    self.body, self.downsamples, self.class_preds, self.box_preds = self.get_ssd_model()
    File "/opt/ml/code/localization_bb.py", line 411, in get_ssd_model
    body = self.get_body()
    File "/opt/ml/code/localization_bb.py", line 343, in get_body
    pretrained = resnet34_v1(pretrained=True, ctx=ctx)
    File "/usr/local/lib/python2.7/dist-packages/mxnet/gluon/model_zoo/vision/resnet.py", line 416, in resnet34_v1
    return get_resnet(1, 34, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/mxnet/gluon/model_zoo/vision/resnet.py", line 385, in get_resnet
    root=root), ctx=ctx)
    File "/usr/local/lib/python2.7/dist-packages/mxnet/gluon/model_zoo/model_store.py", line 108, in get_model_file
    with zipfile.ZipFile(zip_file_path) as zf:
    File "/usr/lib/python2.7/zipfile.py", line 770, in init
    self._RealGetContents()
    File "/usr/lib/python2.7/zipfile.py", line 811, in _RealGetContents
    raise BadZipfile, "File is not a zip file"

    Please find the traceback for importing skimage model:

    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/ggevent.py", line 203, in init_process
    super(GeventWorker, self).init_process()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
    File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 350, in import_app
    import(module)
    File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 96, in import
    result = _import(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/container_support/wsgi.py", line 16, in
    app = Server.from_env().app
    File "/usr/local/lib/python2.7/dist-packages/container_support/serving.py", line 57, in from_env
    user_module = env.import_user_module() if env.user_script_name else None
    File "/usr/local/lib/python2.7/dist-packages/container_support/environment.py", line 101, in import_user_module
    user_module = importlib.import_module(script)
    File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
    import(name)
    File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 96, in import
    result = _import(*args, **kwargs)
    File "/opt/ml/code/localization_bb.py", line 29, in
    from skimage.draw import line_aa
    File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 96, in import
    result = _import(*args, **kwargs)

    Thanks,
    Harathi

  7. mvsusp commented on Jul 13, 2018

    @mvsusp
    Contributor

    Hello @Harathi123,

    It seems that you are using skimage.draw in /opt/ml/code/localization_bb.py, line 29.

    Can you please make sure that you are installing the required modules before importing anything from skimage.

    I would install the module as follows:

    import subprocess
    import sys
          
    subprocess.call([sys.executable, '-m', 'pip', 'install', '-U', 'scikit-image') 

    The sys.executable insures that the module is installed using the correct python binary.

    Please, let me know if that happens.

    About the content mismatch error while downloading the gluon weights. It seems to me that is something related to gluon and model zoo. Would you mind reaching them out for this issue? I believe they will be able to assist you better.

    Best,

    Márcio

  8. Harathi123 commented on Jul 13, 2018

    @Harathi123
    Author

    Hi @mvsusp,

    I am installing the module before importing them. Please find the screenshot.
    ![2018-07-13

    I will try installing it using "subprocess.call([sys.executable, '-m', 'pip', 'install', '-U', 'scikit-image')"

    Regarding resnet download error, I am able to download that and use those weights for training while creating training job (fitting). I am getting error only while deploying. Is there any idea why i am getting that error only while deploying? I am using the same .py file for training and deploying.

    Thanks,
    Harathi

  9. mvsusp commented on Jul 14, 2018

    @mvsusp
    Contributor

    Hello @Harathi123 ,

    Unfortunately, I cannot see your screenshot.

    Please let me know if you managed to install using the suggested code above.

    Regarding the ResNet error. I am not as familiar with gluon model zoo (you will probably get better assistance in asking directly in the gluon repo), but I would wonder if it is necessary to load the pre trained weights during hosting again. Please double check if just loading the model from the checkpoints created in the end of the training would be enough.

    Best,

    Márcio

  10. Harathi123 commented on Jul 14, 2018

    @Harathi123
    Author

    Hi @mvsusp ,

    Thanks for getting back. Please find the screenshot that was missed in the last comment:
    2018-07-13 1

    I tried downloading with subprocess.call(). Even though i got the error again. I traied even importing as
    'from skimage import draw' instead of 'from skimage.draw import line_aa'. Still getting error 'No module named skimage'.
    Its getting installed and imported while creating training job. I don't understand why i am getting error while deploying only.

    Regarding resnet, does it mean no need to loading pretrained model while depolying? i.e., we need to give pretrained = False while deploying? Loading trained model parameters will load parameters for resnet as well?

    Thanks,
    Harathi

  11. andremoeller commented on Jul 18, 2018

    @andremoeller
    Contributor

    Hi @Harathi123 ,

    I've reproduced the pip install issue with hosting. The hosting container doesn't handle this case like the training container does. Since this isn't a supported use case yet, we should support requirements.txt for the MXNet container: #284.

    Until we do, I suggest creating your own Docker image. Since our images are public, you can get the image URI from an MXNet training job, authenticate with ecr with:

    aws ecr get-login --region us-west-2 --no-include-email --registry-ids 520713654638

    And pull the image with:

    docker pull 520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1-gpu-py2

    And install your additional dependencies in a Docker image based on that image, push your image to ECR, and give sagemaker.amazonaws.com permission to pull from that ECR repository. Then, when using the MXNet estimator, pass in your image URI to the image_name keyword argument to point the SageMaker Python SDK to your custom image.

    About the pre-trained model: are you attempting to download weights in your model_fn? I suspect that that's causing an issue with our model server, but I haven't been able to reproduce your issue. I suggest managing model weights in a SageMaker Model-- SageMaker will download model artifacts from S3, unzip them into /opt/ml/model. To create a SageMaker Model, uou can gzip and tar your model files, upload them to S3, and create a SageMaker Model through the console or using an SDK.

    Thanks!

  12. Harathi123 commented on Jul 18, 2018

    @Harathi123
    Author

    Hi @andremoeller ,

    Thanks for your valuable suggestions:
    So from my understanding, it is the issue with Sagemaker MXNet estimator. While hosting the model, it is unable to install dependencies. I need to create docker image with my custom code and add all dependencies to it. Am i correct?

    And regarding pretrained model, i need to download the pretrained resnet weights and gzip tar the files and upload them to model_dir while creating training job itself. So that i can extract them from model_dir while hosting the model. Is my understanding correct?

    Thanks,
    Harathi

  13. andremoeller commented on Jul 18, 2018

    @andremoeller
    Contributor

    Hi @Harathi123 ,

    So from my understanding, it is the issue with Sagemaker MXNet estimator. While hosting the model, it is unable to install dependencies. I need to create docker image with my custom code and add all dependencies to it. Am i correct?

    Almost -- the SageMaker MXNet Estimator in the Python SDK is working correctly, but this isn't a supported feature for our MXNet Docker image yet. You'll need to create your own Docker image, but you can use our own Docker image as the base and install dependencies on top of it.

    Here's a very basic Dockerfile to start from, but you'll have to pull the base image first. The following image uses MXNet 1.1, gpu, and Python 2, but if you want to pull a different MXNet version, or build from CPU, or use Python 3, you can change the tag in this URI:

    FROM 520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1-gpu-py2
    
    RUN pip install --no-cache --upgrade scikit-image
    

    After building and pushing this image to ECR and giving sagemaker.amazonaws.com pull permissions on this image, you can keep using the MXNet estimator like you're currently using it, but in the MXNet estimator's constructor, pass in `image_name="your.own.MXNet.image:uri", pointing to your own image in ECR instead. (This way, you don't have to add your custom code to the image.)

    And regarding pretrained model, i need to download the pretrained resnet weights and gzip tar the files and upload them to model_dir while creating training job itself. So that i can extract them from model_dir while hosting the model. Is my understanding correct?

    Almost -- SageMaker will actually unzip and untar the model files into model_dir for you, as long as you point SageMaker to an S3 URI with your model files, by creating a SageMaker Model that points to your model artifacts in S3. After a Training Job, SageMaker saves gzipped, tarred model artifacts to S3. When the deploy method is called, the Python SDK creates a SageMaker Model, which contains references to an S3 path and an image URI, then uses this SageMaker Model to create and Endpoint.

    Thanks, and feel free to reach out if you have any other questions!

  14. Harathi123 commented on Jul 18, 2018

    @Harathi123
    Author

    Thanks @andremoeller for detailed explanation. I will reach out if i get any questions further.

    Thanks,
    Harathi

  15. andremoeller commented on Jul 20, 2018

    @andremoeller
    Contributor

    Sure @Harathi123 !

    I'm going to close this issue since it's getting quite long, but please feel free to open another issue if you have additional questions. Thanks!

  16. added a commit that references this issue on Nov 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions