-
Notifications
You must be signed in to change notification settings - Fork 101
How can I forwards effectively numpy arrays to a SageMaker endpoint with inference? #118
Comments
just to clarify, which version of TFS are you using? you could write your own For the predictor, you'll want to do something like: from sagemaker.predictor import npy_serializer
from sagemaker.tensorflow import serving
model = serving.Model(...) # or estimator = TensorFlow(...)
predictor = model.deploy(...) # or estimator.deploy
predictor.content_type = 'application/x-npy'
predictor.serializer = npy_serializer
predictor.predict(...) There is also |
if you are set on using the REST API, I unfortunately don't believe there is a way around using JSON:
|
Can I use other methods as well then REST API for SageMaker endpoints? |
We expose gRPC port, so you should be able to use it instead of REST API in your inference.py input_handler. |
Also since you are using images, as an alternative solution you could change your TF model input to accept image data. There's an example notebook that shows how to do this. It's written for Batch transform jobs, but the model preparation and inference script would be the same for an Endpoint:
|
Closing the issue. Please, feel free to reach out if you have any further questions! |
I have a TensorFlow Model deployed with AWS SageMaker endpoint exposed . The TensorFlow model accepts 3 inputs as follows
I get this error. Outside SageMaker everithing works fine. Please i need your Help! |
@Patrick-devX can you post the full stacktrace? |
@laurenyuTypeError Traceback (most recent call last) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/sagemaker/tensorflow/model.py in predict(self, data, initial_args) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant, inference_id) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/sagemaker/predictor.py in _create_request_args(self, data, initial_args, target_model, target_variant, inference_id) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/sagemaker/serializers.py in serialize(self, data) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/json/init.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in encode(self, o) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot) ~/anaconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in default(self, o) TypeError: Object of type 'ndarray' is not JSON serializable Thank You! https://stackoverflow.com/questions/66635745/how-to-send-many-numpy-arrays-to-sagemaker-endpoint https://gitlab.com/patricksardin08/data-science/-/blob/master/user_traking/deploy_model.ipynb |
@Patrick-devX the issue is occurring client-side when your predictor object tries to serialize the data as JSON. based on your code, it looks like if not, you'll probably want to look into:
(btw, in the future, instead of commenting on an unrelated issue with just an error message, open a new issue and provide as much info/logs/code as you can. For client-side errors like this one, you'll have better luck opening a new issue in this repo.) |
@laurenyu it sends this error: Then i tried to use the IdentitySerializer: (I used those Serializer with and without the parameter Yes regarding future issues , I understand Thank you. I find this similar issue. When I'm in the notebook of training the model, it works for me to predict with the pred_set dataset, whose format is exactly as same as the training dataset kmeans_predictor = kmeans.deploy(initial_instance_count=1, result = kmeans_predictor.predict(pred_set) But when I called the endpoint in another notebook and trying to predict with the same pred_set dataset from sagemaker.predictor import RealTimePredictor I got |
@Patrick-devX aws/sagemaker-python-sdk#166 is a different issue because using KMeans is different from using TF. This is the sequence of events with what's happening:
Since your model requires numpy arrays, you're going to need to write your own input handler to deal with step 4 - see https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/deploying_tensorflow_serving.html#providing-python-scripts-for-pre-pos-processing |
Hi @laurenyu These Numpy arrays are not accepted by the Endpoint, although they are the same that work successfully outside SageMaer on the same model. |
@Patrick-devX what is your error message and stacktrace after implementing a handler function? What does your handler function look like? |
@laurenyu Here is the handler:
Debugging:
When i try to invoke the model like this: i become:
|
@Patrick-devX thanks for providing more context. two things - First, by input handler, I mean the function You will need to implement your own Second, the error you just posted here is coming from invoking |
I deployed an object detection algorithm on a SageMaker endpoint, with this structure:
I tested the model locally, so I created a session, I loaded the model, and I predicted with session.run(). On very similar hardware, the local model predicts was 10x faster than the on deployed one. I know that a lot of factors that can increase the remote prediction time. To get clear sight, I added the time log to my inference.py function. The results are:
The np2json code is:
The RGB image size is 633 x 1333 x 3, and during the preprocessing, I convert it to float16.
Based on the measurements, I realized the np2json function increased the latency significantly, which was not needed at local predictions. Is there a more efficient solution to send a numpy array to the deployed model?
Thanks for the help!
The text was updated successfully, but these errors were encountered: