Friday, 29 September 2023

Testing invoking Sagemaker endpoint for Computer Vision Classification

I built a out-of-box CNN and deployed using AWS Step Functions. I have these custom functions for the endpoint:

def input_fn(data, content_type):
    '''
    take in image
    '''
    if content_type == 'application/json':
        img = Image.open(io.BytesIO(data))
        img_arr = np.array(img)
        resized_arr = cv2.resize(img_arr, (img_size, img_size)) 
        return resized_arr[None,...]
    else:
        raise RuntimeException("{} type is not supported by this endpoint.".format(content_type))
        
    
    
def model_fn():
    '''
    Return model
    '''

    client = boto3.client('s3')
    client.download_file(Bucket=s3_bucket_name, Key='model/kcvg_cv_model.h5', Filename='kcvg_cv_model.h5')
    model = tf.keras.saving.load_model('kcvg_cv_model.h5')   

    return model

def predict_fn(img_dir):
    model = model_fn()
    data = input_fn(img_dir)
    prob = model.predict(data)
    return np.argmax(prob, axis=-1)

When I run this code

from sagemaker.predictor import RealTimePredictor
from sagemaker.serializers import JSONSerializer

endpoint_name = 'odi-ds-belt-vision-cv-kcvg-endpoint-Final-Testing4'

# Read image into memory
payload = None
with open("117.jpg", 'rb') as f:
    payload = f.read()
    
predictor = RealTimePredictor(endpoint_name = endpoint_name, sagemaker_session=sm_sess, serializer=JSONSerializer)
inference_response = predictor.predict(data=payload)
print (inference_response)

I get the following error

The class RealTimePredictor has been renamed in sagemaker>=2.
See: https://sagemaker.readthedocs.io/en/stable/v2.html for details.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[14], line 11
      8     payload = f.read()
     10 predictor = RealTimePredictor(endpoint_name = endpoint_name, sagemaker_session=sm_sess, serializer=JSONSerializer)
---> 11 inference_response = predictor.predict(data=payload)
     12 print (inference_response)

    File ~/anaconda3/envs/odi-ds/lib/python3.9/site-packages/sagemaker/base_predictor.py:177, in Predictor.predict(self, data, initial_args, target_model, target_variant, inference_id, custom_attributes)
        129 def predict(
        130     self,
        131     data,
       (...)
        136     custom_attributes=None,
        137 ):
        138     """Return the inference from the specified endpoint.
        139 
        140     Args:
       (...)
        174             as is.
        175     """
    --> 177     request_args = self._create_request_args(
        178         data,
        179         initial_args,
        180         target_model,
        181         target_variant,
        182         inference_id,
        183         custom_attributes,
        184     )
        185     response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
        186     return self._handle_response(response)
    
    File ~/anaconda3/envs/odi-ds/lib/python3.9/site-packages/sagemaker/base_predictor.py:213, in Predictor._create_request_args(self, data, initial_args, target_model, target_variant, inference_id, custom_attributes)
        207     args["EndpointName"] = self.endpoint_name
        209 if "ContentType" not in args:
        210     args["ContentType"] = (
        211         self.content_type
        212         if isinstance(self.content_type, str)
    --> 213         else ", ".join(self.content_type)
        214     )
        216 if "Accept" not in args:
        217     args["Accept"] = self.accept if isinstance(self.accept, str) else ", ".join(self.accept)
    
    TypeError: can only join an iterable

I am guessing there is something I am doing wrong in the input_fn but I am not exactly sure, any suggestions?



from Testing invoking Sagemaker endpoint for Computer Vision Classification

No comments:

Post a Comment