amazon-sagemaker-examples
amazon-sagemaker-examples copied to clipboard
I am trying to do inference on deployed model, An error occurred (ModelError) when calling the InvokeEndpoint operation.
Code
#importing required libreries from sagemaker import get_execution_role from sagemaker import Session role = get_execution_role() sess = Session() region = sess.boto_region_name bucket = sess.default_bucket()
from sagemaker import get_execution_role from sagemaker.utils import name_from_base from sagemaker.tensorflow import TensorFlowModel import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import zipfile from collections import defaultdict from io import StringIO from matplotlib import pyplot as plt from PIL import Image from IPython.display import display
tf_framework_version = tf.version tf_framework_version
#packages from TFOD models/research from object_detection.utils import ops as utils_ops from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util
#List of the strings that is used to add correct label for each box. category_index = label_map_util.create_category_index_from_labelmap('label_map.pbtxt', use_display_name=True)
#model.tar.gz file path from s3 model_artefact = 's3://{my-bucket-name}/model/model.tar.gz'
#deploying from sagemaker.tensorflow import TensorFlowModel model_tfm = TensorFlowModel(model_data=model_artefact, framework_version=tf_framework_version, role=role) predictor_tfm = model_tfm.deploy(initial_instance_count=1, instance_type='ml.m5.xlarge')
#function to convert image into numpy array def load_image_into_numpy_array(image): image_rez = image.resize((256, 256)) (im_width, im_height) = image_rez.size return np.array(image_rez.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8)
test_image = 'test-images/beet_test.jpg' image = Image.open(test_image) image_arr = load_image_into_numpy_array(image)
input = { 'instances': [image_arr.tolist()] }
#inference on test image #this below line results in error result = predictor_tfm.predict(input)['predictions'][0]
problem description
I am trying to deploy an already trained TFOD v2 mask rcnn model on sagemaker. Deployement is succesfully done and created endpoint. But when I do inference on test image this error occured, line: result = predictor_tfm.predict(input)['predictions'][0] output: ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{ "error": "Tensor name: raw_detection_scores has inconsistent batch size: 1 expecting: 300" }"
Please help me.
Logs
ModelError Traceback (most recent call last)
~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/sagemaker/tensorflow/model.py in predict(self, data, initial_args) 105 args["CustomAttributes"] = self._model_attributes 106 --> 107 return super(TensorFlowPredictor, self).predict(data, args) 108 109
~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant, inference_id) 159 data, initial_args, target_model, target_variant, inference_id 160 ) --> 161 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args) 162 return self._handle_response(response) 163
~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs) 413 "%s() only accepts keyword arguments." % py_operation_name) 414 # The "self" in this scope is referring to the BaseClient. --> 415 return self._make_api_call(operation_name, kwargs) 416 417 _api_call.name = str(py_operation_name)
~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params) 743 error_code = parsed_response.get("Error", {}).get("Code") 744 error_class = self.exceptions.from_code(error_code) --> 745 raise error_class(parsed_response, operation_name) 746 else: 747 return parsed_response
ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{ "error": "Tensor name: refined_box_encodings has inconsistent batch size: 300 expecting: 1" }". See {cloudwatch-link}#logEventViewer:group=/aws/sagemaker/Endpoints/tensorflow-inference-2022-06-16-10-58-30-168 in account {acc-number} for more information.