Skip to content

Commit 0c4c63e

Browse files
author
Dan Choi
committed
Add wip notebook
1 parent 10b8abc commit 0c4c63e

File tree

10 files changed

+969
-123
lines changed

10 files changed

+969
-123
lines changed

advanced_functionality/tensorflow_bring_your_own/Dockerfile renamed to advanced_functionality/tensorflow_bring_your_own/container/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ RUN apt-get update && apt-get install tensorflow-model-server
1010

1111
ENV PATH="/opt/ml/code:${PATH}"
1212

13-
COPY src /opt/ml/code
13+
COPY /cifar10 /opt/ml/code
1414
WORKDIR /opt/ml/code

advanced_functionality/tensorflow_bring_your_own/src/cifar10.py renamed to advanced_functionality/tensorflow_bring_your_own/container/cifar10/cifar10.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import argparse
1818
import functools
19-
import logging
2019

2120
import os
2221

@@ -47,8 +46,6 @@
4746

4847
_BATCHES_PER_EPOCH = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / BATCH_SIZE
4948

50-
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', level=logging.DEBUG)
51-
5249

5350
def model_fn(features, labels, mode):
5451
"""Model function for CIFAR-10."""
@@ -186,7 +183,6 @@ def train(model_dir, data_dir, train_steps):
186183
def main(model_dir, data_dir, train_steps):
187184
tf.logging.set_verbosity(tf.logging.INFO)
188185
train(model_dir, data_dir, train_steps)
189-
print('Training Done!')
190186

191187

192188
if __name__ == '__main__':

advanced_functionality/tensorflow_bring_your_own/src/nginx.conf renamed to advanced_functionality/tensorflow_bring_your_own/container/cifar10/nginx.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ events {
88
http {
99
server {
1010
# configures the server to listen to the port 8080
11+
# https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-code-container-response
1112
listen 8080 deferred;
1213

1314
# redirects requests from SageMaker to TF Serving
1415
location /invocations {
1516
proxy_pass http://localhost:8501/v1/models/cifar10_model:predict;
1617
}
1718

18-
# Used my SageMaker to confirm if server is alive.
19+
# Used by SageMaker to confirm if server is alive.
20+
# https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-algo-ping-requests
1921
location /ping {
2022
return 200 "OK";
2123
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
# This file implements the hosting solution, which just starts TensorFlow Model Serving.
4+
5+
from __future__ import print_function
6+
7+
import subprocess
8+
9+
10+
def start_server():
11+
print('Starting TensorFlow Serving.')
12+
13+
# link the log streams to stdout/err so they will be logged to the container logs
14+
subprocess.check_call(['ln', '-sf', '/dev/stdout', '/var/log/nginx/access.log'])
15+
subprocess.check_call(['ln', '-sf', '/dev/stderr', '/var/log/nginx/error.log'])
16+
17+
nginx = subprocess.Popen(['nginx', '-c', '/opt/ml/code/nginx.conf'])
18+
tf_model_server = subprocess.call(['tensorflow_model_server',
19+
'--rest_api_port=8501',
20+
'--model_name=cifar10_model',
21+
'--model_base_path=/opt/ml/model/export/Servo'])
22+
23+
24+
# The main routine just invokes the start function.
25+
if __name__ == '__main__':
26+
start_server()

advanced_functionality/tensorflow_bring_your_own/src/train renamed to advanced_functionality/tensorflow_bring_your_own/container/cifar10/train

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
from __future__ import print_function
88

9-
import logging
109
import os
1110
import json
1211
import sys
1312
import subprocess
1413
import traceback
1514

1615
# These are the paths to where SageMaker mounts interesting things in your container.
17-
from fcntl import fcntl, F_GETFL, F_SETFL
18-
1916
prefix = '/opt/ml/'
20-
21-
input_path = prefix + 'input/data'
17+
input_path = os.path.join(prefix,'input/data')
2218
output_path = os.path.join(prefix, 'output')
2319
model_path = os.path.join(prefix, 'model')
2420
param_path = os.path.join(prefix, 'input/config/hyperparameters.json')
@@ -32,49 +28,10 @@ training_path = os.path.join(input_path, channel_name)
3228
training_script = 'cifar10.py'
3329
default_params = ['--model-dir', str(model_path)]
3430

35-
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', level=logging.DEBUG)
36-
37-
38-
def _stream_output(process):
39-
"""Stream the output of a process to stdout
40-
This function takes an existing process that will be polled for output. Both stdout and
41-
stderr will be polled and both will be sent to sys.stdout.
42-
Args:
43-
process(subprocess.Popen): a process that has been started with
44-
stdout=PIPE and stderr=PIPE
45-
Returns (int): process exit code
46-
"""
47-
exit_code = None
48-
49-
# Get the current flags for the stderr file descriptor
50-
# And add the NONBLOCK flag to allow us to read even if there is no data.
51-
# Since usually stderr will be empty unless there is an error.
52-
flags = fcntl(process.stderr, F_GETFL) # get current process.stderr flags
53-
fcntl(process.stderr, F_SETFL, flags | os.O_NONBLOCK)
54-
55-
while exit_code is None:
56-
stdout = process.stdout.readline().decode("utf-8")
57-
sys.stdout.write(stdout)
58-
try:
59-
stderr = process.stderr.readline().decode("utf-8")
60-
sys.stdout.write(stderr)
61-
except IOError:
62-
# If there is nothing to read on stderr we will get an IOError
63-
# this is fine.
64-
pass
65-
66-
exit_code = process.poll()
67-
68-
if exit_code != 0:
69-
raise RuntimeError("Process exited with code: %s" % exit_code)
70-
71-
return exit_code
72-
7331

74-
# The function to execute the training.
32+
# Execute your training algorithm.
7533
def _run(cmd):
7634
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ)
77-
_stream_output(process)
7835
stdout, stderr = process.communicate()
7936

8037
return_code = process.poll()
@@ -102,7 +59,6 @@ if __name__ == '__main__':
10259
cmd_args = _hyperparameters_to_cmd_args(training_params)
10360

10461
train_cmd = [python_executable, training_script] + default_params + cmd_args
105-
print(train_cmd)
10662

10763
_run(train_cmd)
10864
print('Training complete.')

advanced_functionality/tensorflow_bring_your_own/src/serve

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)