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.09k stars 6.76k forks source link

pytorch_torchvision_neo_studio.ipynb: ImportError: cannot import name 'NEO_IMAGE_ACCOUNT' from 'sagemaker.model' #1778

Open CloudaYolla opened 3 years ago

CloudaYolla commented 3 years ago

I tested the NB using SM Studio:

Src: https://github.com/aws/amazon-sagemaker-examples/blob/master/aws_sagemaker_studio/sagemaker_neo_compilation_jobs/pytorch_torchvision/pytorch_torchvision_neo_studio.ipynb

Issue

from sagemaker.model import NEO_IMAGE_ACCOUNT
from sagemaker.fw_utils import create_image_uri

model_name = name_from_base('TorchVision-ResNet18-Neo')

image_uri = create_image_uri(region, 'neo-' + framework.lower(), target_device.replace('_', '.'),
                             framework_version, py_version='py3', account=NEO_IMAGE_ACCOUNT[region])

response = sm_client.create_model(
    ModelName=model_name,
    PrimaryContainer={
        'Image': image_uri,
        'ModelDataUrl': compiled_model_path,
        'Environment': { 'SAGEMAKER_SUBMIT_DIRECTORY': source_path }
    },
    ExecutionRoleArn=role
)
print(response)

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-11-f83c82fd86a9> in <module>
----> 1 from sagemaker.model import NEO_IMAGE_ACCOUNT
      2 from sagemaker.fw_utils import create_image_uri
      3 
      4 model_name = name_from_base('TorchVision-ResNet18-Neo')
      5 

ImportError: cannot import name 'NEO_IMAGE_ACCOUNT' from 'sagemaker.model' (/opt/conda/lib/python3.7/site-packages/sagemaker/model.py)

How to Resolve

Seems like following are depreciated: Quote: https://sagemaker.readthedocs.io/en/stable/v2.html

The following functions have been deprecated in favor of sagemaker.image_uris.retrieve():

sagemaker.amazon_estimator.get_image_uri()

sagemaker.fw_utils.create_image_uri()

But couldn't find info about the NEO_IMAGE_ACCOUNT.

ngluna commented 3 years ago

This notebook is also using APIs from the Python SDK <2 . Namely, create_image_uri

ngluna commented 3 years ago

Solved the above issue by removing NEO_IMAGE_ACCOUNT and using the new name create_image_uri -> image_uris:

from sagemaker import image_uris

model_name = name_from_base('TorchVision-ResNet18-Neo')

image_uri = image_uris.retrieve(region=region, 
                                framework='neo-' + framework.lower(), 
                                instance_type=target_device.replace('_', '.'),
                                py_version='py3')

response = sm_client.create_model(
    ModelName=model_name,
    PrimaryContainer={
        'Image': image_uri,
        'ModelDataUrl': compiled_model_path,
        'Environment': { 'SAGEMAKER_SUBMIT_DIRECTORY': source_path }
    },
    ExecutionRoleArn=role
)
print(response)
ngluna commented 3 years ago

However, running:

response = sm_runtime.invoke_endpoint(EndpointName=endpoint_name,
                                      ContentType='application/x-image',
                                      Body=payload)

breaks. Returns:

---------------------------------------------------------------------------
ModelError                                Traceback (most recent call last)
<ipython-input-32-c36a6ba27f54> in <module>
      9 response = sm_runtime.invoke_endpoint(EndpointName=endpoint_name,
     10                                       ContentType='application/x-image',
---> 11                                       Body=payload)
     12 print(response)
     13 result = json.loads(response['Body'].read().decode())

/opt/conda/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    355                     "%s() only accepts keyword arguments." % py_operation_name)
    356             # The "self" in this scope is referring to the BaseClient.
--> 357             return self._make_api_call(operation_name, kwargs)
    358 
    359         _api_call.__name__ = str(py_operation_name)

/opt/conda/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    674             error_code = parsed_response.get("Error", {}).get("Code")
    675             error_class = self.exceptions.from_code(error_code)
--> 676             raise error_class(parsed_response, operation_name)
    677         else:
    678             return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "Content type application/x-image is not supported by this framework.

            Please implement input_fn to to deserialize the request data or an output_fn to
            serialize the response. For more information, see the SageMaker Python SDK README.
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/decoder.py", line 106, in decode
    decoder = _decoder_map[content_type]
KeyError: 'application/x-image'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 126, in transform
    result = self._transform_fn(self._model, input_data, content_type, accept)
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 215, in _default_transform_fn
    data = self._input_fn(input_data, content_type)
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_pytorch_serving_container/default_inference_handler.py", line 97, in default_input_fn
    np_array = decoder.decode(input_data, content_type)
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/decoder.py", line 109, in decode
    raise errors.UnsupportedFormatError(content_type)
sagemaker_inference.errors.UnsupportedFormatError: Content type application/x-image is not supported by this framework.

            Please implement input_fn to to deserialize the request data or an output_fn to
            serialize the response. For more information, see the SageMaker Python SDK README.
". See https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=/aws/sagemaker/Endpoints/TorchVision-ResNet18-Neo-2020-12-23-23-12-12-235-Endpoint in account 688520471316 for more information.

Looking into this.

minhtcai commented 3 years ago

botocore.errorfactory.ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "Content type application/x-image is not supported by this framework.

I got the same problem, any help here?

sicklife commented 2 years ago

@ngluna I got the same issue