You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting errors while deploying custom Sagemaker MXNet model #289
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.
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.
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...
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.
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.
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.
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)
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.
I am installing the module before importing them. Please find the screenshot.

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
andremoeller commentedon Jul 18, 2018
Hi @Harathi123 ,
I've reproduced the
pip installissue 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 supportrequirements.txtfor 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 520713654638And pull the image with:
docker pull 520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1-gpu-py2And 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_namekeyword 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 SageMakerModel-- 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!
Harathi123 commentedon Jul 18, 2018
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
andremoeller commentedon Jul 18, 2018
Hi @Harathi123 ,
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:
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.)
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
deploymethod 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!
Harathi123 commentedon Jul 18, 2018
Thanks @andremoeller for detailed explanation. I will reach out if i get any questions further.
Thanks,
Harathi
andremoeller commentedon Jul 20, 2018
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!
Merge pull request aws#289 from awslabs/arpin_local_mode_setup_fix
fix: remove pre/post scripts per latest validations (aws#289)
fix: remove pre/post scripts per latest validations (#289)
fix: remove pre/post scripts per latest validations (aws#289)