aws / aws/amazon-sagemaker-examples
What is the requirements of the entry_point needed by the mxnet if I want to inference only
- Dominant language
- Jupyter Notebook
- Stars
- 11k
- Forks
- 7k
- Avg merge
- 8h 29m
- Merged PRs (30d)
- 8
Description
As the title mention, this drive me crazy. What I want to do is
1. Create an endpoint to predict the image type based on the mxnet
2. Call the endpoint by lambda function(not sure doable or not)
About entry_point--I cannot find any document mentioned what kind of functions needed by this file, how should I define them, all I could find are scrap data and use my instinct to do some test.
the entry_point--mxnet_imagenet_classify.py
```
from __future__ import print_function
import bisect
import json
import logging
import time
import random
import re
from collections import Counter, namedtuple
from itertools import chain, islice
import mxnet as mx
import mxnet.contrib.onnx as onnx_mxnet
import numpy as np
from mxnet import gluon, autograd, nd
from mxnet.io import DataIter, DataBatch, DataDesc
from mxnet.gluon import nn
logging.basicConfig(level=logging.DEBUG)
def model_fn(model_dir):
"""
Load the onnx model. Called once when hosting service starts.
:param: model_dir The directory where model files are stored.
:return: a model
"""
sym, arg_params, aux_params = onnx_mxnet.import_model('{}/resnet101v1.onnx'.format(model_dir))
# create module
mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))],
label_shapes=mod._label_shapes)
mod.set_params(arg_params, aux_params, allow_missing=True)
return mod
def transform_fn(mod, data, input_content_type, output_content_type):
input_data = json.loads(data)
batch = namedtuple('Batch', ['data'])
mod.forward(batch([mx.nd.array(input_data)]))
prob = mod.get_outputs()[0].asnumpy()
# print the top-5
prob = np.squeeze(prob)
a = np.argsort(prob)[::-1]
result = {}
for i in a[0:5]:
result[i] = prob[i]
return json.dumps(result), output_content_type
```
Create endpoint
```
from sagemaker import get_execution_role
role = get_execution_role()
import tarfile
import boto3
from sagemaker.session import Session
with tarfile.open('onnx_model.tar.gz', mode='w:gz') as archive:
archive.add('resnet101v1.onnx')
bucket_name='sagemaker-gluoncv' # put your s3 bucket name here, and create s3 bucket
role = get_execution_role()
region = boto3.Session().region_name
prefix = 'image_classify'
# customize to your bucket where you have stored the data
bucket_path = 'https://s3-{}.amazonaws.com/{}'.format(region, bucket_name)
def write_to_s3(filename, bucket_name, key):
content = open(filename, 'rb')
s3 = boto3.client('s3')
s3.put_object(
Bucket=bucket_name,
Key=key,
Body=content
)
write_to_s3('onnx_model.tar.gz', bucket_name, "image_classify/{}".format('onnx_model.tar.gz'))
from sagemaker.mxnet import MXNetModel
mxnet_model = MXNetModel(model_data='s3://sagemaker-gluoncv/image_classify/onnx_model.tar.gz',
entry_point='mxnet_imagenet_classify.py',
role=role,
py_version='py3',
framework_version='1.3.0')
%%time
predictor = mxnet_model.deploy(initial_instance_count=1, instance_type='ml.t2.medium')
```
And then the server always give me error messages:
**ValueError: Error hosting endpoint sagemaker-mxnet-2019-06-18-02-01-03-704: Failed Reason: Request to service failed. Please contact customer support.**
Very confuse, reading what is docker and try to create a docker container, since I can't find an easy way to make things work
Contributor guide
Research direction
Review mxnet_imagenet_classify.py and the MXNetModel deployment configuration first; inspect the endpoint or container logs for the hosting failure. Confirm the entry point contract for model_fn and transform_fn and that the packaged ONNX model is available, then redeploy and verify that an inference request succeeds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, jupyter-notebook, python
- Domain
- api, cloud, machine-learning
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100