aws / amazon-sagemaker-examples

Example 📓 Jupyter notebooks that demonstrate how to build, train, and deploy machine learning models using 🧠 Amazon SageMaker.
https://sagemaker-examples.readthedocs.io
Apache License 2.0
10.18k stars 6.79k forks source link

What is the requirements of the entry_point needed by the mxnet if I want to inference only #769

Open stereomatchingkiss opened 5 years ago

stereomatchingkiss commented 5 years ago

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

icywang86rui commented 5 years ago

@stereomatchingkiss - You can follow the instructions here - https://sagemaker.readthedocs.io/en/stable/using_mxnet.html#working-with-existing-model-data-and-training-jobs

Here is the instructions on how to construct the entry point file - https://sagemaker.readthedocs.io/en/stable/using_mxnet.html#the-sagemaker-mxnet-model-server

stereomatchingkiss commented 5 years ago

Thanks, I read some posts about sageMaker and ec2. The conclusions I get are

  1. EC2 is cheaper
  2. SageMaker is run on top of EC2, all it did is help you setup the environment needed by some machine learning task
  3. If you know how to and willing to spend the time to setup the environment, you don't need SageMaker

Is this right? If it was, then SageMaker do not has much benefits for me

chuyang-deng commented 5 years ago

@stereomatchingkiss,

  1. Based on the type of instances you are using and the type of services you want to utilize in SageMaker, the prices may vary
  2. SageMaker trains and deploy your models on EC2 instance
  3. SageMaker is a platform that you can easily handle the training, deploying, predicting, transforming etc. of your data and models. So yes, if you know what you need to do with your data, algorithm, models, you don't need SageMaker, but that means you don't use any AWS SageMaker resources.

Thank you