diff --git a/api/mira/sagemaker_custom_model/Dockerfile b/api/mira/sagemaker_custom_model/Dockerfile new file mode 100644 index 0000000..25fb3b6 --- /dev/null +++ b/api/mira/sagemaker_custom_model/Dockerfile @@ -0,0 +1,8 @@ +FROM 763104351884.dkr.ecr.us-west-2.amazonaws.com/tensorflow-inference:1.14-cpu +COPY requirements.txt /home/requirements.txt +RUN pip install -r /home/requirements.txt +# ENV PATH="/opt/ml/code:${PATH}" +# # COPY ../models/mira-large/code /opt/ml/code +# # WORKDIR /opt/ml/code +# ENV SAGEMAKER_SUBMIT_DIRECTORY /opt/ml/code +# ENV SAGEMAKER_PROGRAM inference.py diff --git a/api/mira/sagemaker_custom_model/README.md b/api/mira/sagemaker_custom_model/README.md new file mode 100644 index 0000000..1b04523 --- /dev/null +++ b/api/mira/sagemaker_custom_model/README.md @@ -0,0 +1,24 @@ +### Local Testing (in development, required to deploy custom container of MIRA on Sagemaker Serverless, an upgrade from Realtime endpoints use dby the above instructions) + +Get the model and inference code for MIRA-Large +``` +bash download_untar_model.sh +``` + +Build the custom docker container we need to use Sagemaker Serverless and for local testing. this installs pillow and numpy in the sagemaker container for TF 1.14. + +``` +docker build -t test/tf-1.14-pillow-nump:latest . +``` + +Start the model server with docker. + +``` +bash docker_mira.sh +``` + +Attempt to send a request following this example that base64 encodes https://www.tensorflow.org/tfx/serving/api_rest + +``` +python local_test.py +``` diff --git a/api/mira/sagemaker_custom_model/docker_mira.sh b/api/mira/sagemaker_custom_model/docker_mira.sh new file mode 100644 index 0000000..699d3e5 --- /dev/null +++ b/api/mira/sagemaker_custom_model/docker_mira.sh @@ -0,0 +1,3 @@ +# dockerized version of the above, ran into dependency issue for termcolor +# https://github.com/pytorch/serve/tree/master/docker +docker run --rm -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 8501:8501 -p 7070:7070 -p 7071:7071 -v "$(pwd)":/app -it test/tf-1.14-pillow-nump:latest bash /app/serve_mira.sh diff --git a/api/mira/sagemaker_custom_model/download_untar_model.sh b/api/mira/sagemaker_custom_model/download_untar_model.sh new file mode 100644 index 0000000..827bce3 --- /dev/null +++ b/api/mira/sagemaker_custom_model/download_untar_model.sh @@ -0,0 +1,2 @@ +aws s3 cp s3://sagemaker-us-west-2-830244800171/model-mira-large/model.tar.gz ./ +tar -xvzf model.tar.gz diff --git a/api/mira/sagemaker_custom_model/local_test.py b/api/mira/sagemaker_custom_model/local_test.py new file mode 100644 index 0000000..0069658 --- /dev/null +++ b/api/mira/sagemaker_custom_model/local_test.py @@ -0,0 +1,16 @@ +import base64 +import requests +import json +API_URL = 'http://localhost:8501/v1/models/mira-large:classify' +IMG_PATH = '/home/rave/animl/animl-ml/input/sample-img-fox.jpg' +# Open and read image as bitstring +input_image = open(IMG_PATH, "rb").read() +print("Raw bitstring: " + str(input_image[:10]) + " ... " + str(input_image[-10:])) + +# Encode image in b64 +encoded_input_string = base64.b64encode(input_image) +input_string = encoded_input_string.decode("utf-8") +print("Base64 encoded string: " + input_string[:10] + " ... " + input_string[-10:]) +predict_request = '{"instances" : [{"b64": "%s"}]}' % input_string +response = requests.post(API_URL, data=predict_request) +print(response.json()) diff --git a/api/mira/sagemaker_custom_model/requirements.txt b/api/mira/sagemaker_custom_model/requirements.txt new file mode 100644 index 0000000..bd68d63 --- /dev/null +++ b/api/mira/sagemaker_custom_model/requirements.txt @@ -0,0 +1,2 @@ +pillow==8.3.2 +numpy==1.18.2 \ No newline at end of file diff --git a/api/mira/sagemaker_custom_model/serve_mira.sh b/api/mira/sagemaker_custom_model/serve_mira.sh new file mode 100644 index 0000000..d77e90d --- /dev/null +++ b/api/mira/sagemaker_custom_model/serve_mira.sh @@ -0,0 +1 @@ +tensorflow_model_server --port=8500 --rest_api_port=8501 --model_name=mira-large --model_base_path=/app/export/Servo/