Skip to content

Commit fbb1466

Browse files
author
Dan Choi
committed
Add build and push script and address comments
1 parent 0da5bd2 commit fbb1466

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

advanced_functionality/tensorflow_bring_your_own/container/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ FROM tensorflow/tensorflow:1.8.0-py3
1717

1818
RUN apt-get update && apt-get install -y --no-install-recommends nginx curl
1919

20-
# Download tensorflow serving
20+
# Download TensorFlow Serving
2121
# https://www.tensorflow.org/serving/setup#installing_the_modelserver
2222
RUN echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | tee /etc/apt/sources.list.d/tensorflow-serving.list
2323
RUN curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | apt-key add -
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
# This script shows how to build the Docker image and push it to ECR to be ready for use
4+
# by SageMaker.
5+
6+
# The argument to this script is the image name. This will be used as the image on the local
7+
# machine and combined with the account and region to form the repository name for ECR.
8+
image=$1
9+
10+
if [ "$image" == "" ]
11+
then
12+
echo "Usage: $0 <image-name>"
13+
exit 1
14+
fi
15+
16+
chmod +x cifar10/train
17+
chmod +x cifar10/serve
18+
19+
# Get the account number associated with the current IAM credentials
20+
account=$(aws sts get-caller-identity --query Account --output text)
21+
22+
if [ $? -ne 0 ]
23+
then
24+
exit 255
25+
fi
26+
27+
28+
# Get the region defined in the current configuration (default to us-west-2 if none defined)
29+
region=$(aws configure get region)
30+
region=${region:-us-west-2}
31+
32+
33+
fullname="${account}.dkr.ecr.${region}.amazonaws.com/${image}:latest"
34+
35+
# If the repository doesn't exist in ECR, create it.
36+
37+
aws ecr describe-repositories --repository-names "${image}" > /dev/null 2>&1
38+
39+
if [ $? -ne 0 ]
40+
then
41+
aws ecr create-repository --repository-name "${image}" > /dev/null
42+
fi
43+
44+
# Get the login command from ECR and execute it directly
45+
$(aws ecr get-login --region ${region} --no-include-email)
46+
47+
# Build the docker image locally with the image name and then push it to ECR
48+
# with the full name.
49+
50+
docker build -t ${image} .
51+
docker tag ${image} ${fullname}
52+
53+
docker push ${fullname}

advanced_functionality/tensorflow_bring_your_own/tensorflow_bring_your_own.ipynb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Building your own algorithm container\n",
7+
"# Building your own TensorFlow container\n",
88
"\n",
9-
"With Amazon SageMaker, you can package your own algorithms that can than be trained and deployed in the SageMaker environment. This notebook will guide you through an example that shows you how to build a Docker container for SageMaker and use it for training and inference.\n",
9+
"With Amazon SageMaker, you can package your own algorithms that can than be trained and deployed in the SageMaker environment. This notebook will guide you through an example, using TensorFlow, that shows you how to build a Docker container for SageMaker and use it for training and inference.\n",
1010
"\n",
1111
"By packaging an algorithm in a container, you can bring almost any code to the Amazon SageMaker environment, regardless of programming language, environment, framework, or dependencies. \n",
1212
"\n",
@@ -51,7 +51,7 @@
5151
"\n",
5252
"## The example\n",
5353
"\n",
54-
"Here, we'll show how to package a custom TensorFlow container with a python example which works with the CIFAR-10 dataset and utilizes TensorFlow Serving for inferences. The example is purposefully fairly trivial since the point is to show the surrounding structure that you'll want to add to your own code so you can train and host it in Amazon SageMaker.\n",
54+
"Here, we'll show how to package a custom TensorFlow container with a Python example which works with the CIFAR-10 dataset and utilizes TensorFlow Serving for inferences. The example is purposefully fairly trivial since the point is to show the surrounding structure that you'll want to add to your own code so you can train and host it in Amazon SageMaker.\n",
5555
"\n",
5656
"The ideas shown here will work in any language or environment. You'll need to choose the right tools for your environment to serve HTTP requests for inference, but good HTTP environments are available in every language these days.\n",
5757
"\n",
@@ -365,10 +365,10 @@
365365
"1. IAM role - our AWS execution role\n",
366366
"2. train_instance_count - number of instances to use for training.\n",
367367
"3. train_instance_type - type of instance to use for training. For training locally, we will specify `local`.\n",
368-
"4. image_name - our custom TensorFlow docker image we created.\n",
368+
"4. image_name - our custom TensorFlow Docker image we created.\n",
369369
"5. hyperparameters - hyperparameters we want to pass.\n",
370370
"\n",
371-
"Lets start with setting up our IAM role. We will make use of a helper function within the Python SDK. This function will throw an exception if run outside of a SageMaker notebook instance, as it gets metadata from the notebook instance. If running outside, please provide an IAM role with proper access stated above in [Permissions](#Permissions)."
371+
"Let's start with setting up our IAM role. We will make use of a helper function within the Python SDK. This function will throw an exception if run outside of a SageMaker notebook instance, as it gets metadata from the notebook instance. If running outside, please provide an IAM role with proper access stated above in [Permissions](#Permissions)."
372372
]
373373
},
374374
{
@@ -539,12 +539,16 @@
539539
"\n",
540540
"data = {'instances': numpy.asarray(image).astype(float).tolist()}\n",
541541
"\n",
542+
"# The request and response format is JSON for TensorFlow Serving.\n",
543+
"# For more information: https://www.tensorflow.org/serving/api_rest#predict_api\n",
542544
"predictor.accept = 'application/json'\n",
543545
"predictor.content_type = 'application/json'\n",
544546
"\n",
545547
"predictor.serializer = json_serializer\n",
546548
"predictor.deserializer = json_deserializer\n",
547549
"\n",
550+
"# For more information on the predictor class.\n",
551+
"# https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/predictor.py\n",
548552
"predictor.predict(data)"
549553
]
550554
},
@@ -745,7 +749,7 @@
745749
"endpoint_name = predictor.endpoint\n",
746750
"\n",
747751
"response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))\n",
748-
"response_body = response['Body']\n",
752+
"response_body = response['Body'].decode('utf-8')\n",
749753
"\n",
750754
"print(response_body.read())"
751755
]
@@ -781,13 +785,6 @@
781785
"- [Dockerfile](https://docs.docker.com/engine/reference/builder/)\n",
782786
"- [scikit-bring-your-own](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.ipynb)"
783787
]
784-
},
785-
{
786-
"cell_type": "code",
787-
"execution_count": null,
788-
"metadata": {},
789-
"outputs": [],
790-
"source": []
791788
}
792789
],
793790
"metadata": {

0 commit comments

Comments
 (0)