Skip to content

Chapter3 Serving Client Solution #11

@tylee33

Description

@tylee33

1> Conda Environment Configuration
#tensorflow 2.0 Environment Configuration (Client)
#conda create -n keras keras tensorflow
#source activate keras
#pip install numpy scipy scikit-learn pillow h5py
#pip install grpcio
#pip install tensorflow-serving-api
#pip install tensorflow_datasets
#conda deactivate

2> Docker Server Environment Configuration
------------------------- DockerFile --------------------------
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository ppa:deadsnakes/ppa &&
apt-get update && apt-get install -y python3.6 python3.6-dev python3-pip git

RUN ln -sfn /usr/bin/python3.6 /usr/bin/python3 && ln -sfn /usr/bin/python3 /usr/bin/python && ln -sfn /usr/bin/pip3 /usr/bin/pip

docker build -t docker-ubuntu16-python3.6 .
docker run -it docker-ubuntu16-python3.6 bash
apt-get install pkg-config zip g++ zlib1g-dev unzip

apt-get install wget
wget https://github.com/bazelbuild/bazel/releases/download/0.15.0/bazel-0.15.0-installer-linux-x86_64.sh
chmod +x bazel-0.15.0-installer-linux-x86_64.sh
./bazel-0.15.0-installer-linux-x86_64.sh
export PATH="$PATH:$HOME/bin"
apt-get install sudo
sudo apt-get update && sudo apt-get install -y
automake
build-essential
curl
libcurl3-dev
git
libtool
libfreetype6-dev
libpng12-dev
libzmq3-dev
pkg-config
python-dev
python-numpy
python-pip
software-properties-common
swig
zip
zlib1g-dev
pip install --upgrade pip
pip install grpcio
git clone --recursive https://github.com/tensorflow/serving
echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list
curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install tensorflow-model-server
pip install tensorflow-serving-api
cd serving/
python tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model

tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/mnist_model

3> Source Customization (TensorFlow 2.0)
from grpc.beta import implementations
import numpy
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
import matplotlib.pyplot as plt
import os

mnist = tfds.load("mnist", shuffle_files=False, as_supervised=False)
mnist.keys()

print(mnist.keys())

concurrency = 1
num_tests = 100
host = ''
port = 9000
work_dir = './tmp/4'

try:
if not(os.path.isdir(work_dir)):
os.makedirs(os.path.join(work_dir))
except OSError:
print('Failed to create directory!' + work_dir)

def _create_rpc_callback():
def _callback(result):
response = numpy.array(
result.result().outputs['y'].float_val)
prediction = numpy.argmax(response)
print(prediction)
return _callback

train_data, test_data = mnist["train"], mnist["test"]
#test_data_set = mnist["test"]
#test_image = mnist.test.images[0]

train_data, test_data = tfds.load(
"mnist",
split=(tfds.Split.TRAIN, tfds.Split.TEST),
shuffle_files=False, as_supervised=False
)
print(train_data.output_types)
print(train_data.output_shapes)

data = []
iterator = iter(train_data.batch(2))
data.append(next(iterator))
data.append(next(iterator))
data.append(next(iterator))

print(data[0].keys())
print(data[0]["image"].shape)
print(data[0]["label"].shape)

plt.figure(figsize=(10,2))
for i in range(3):
for j in range(2):
plt.subplot(1,6,2*i+j+1)
plt.imshow(data[i]["image"][j,:,:,0])
plt.axis("off")
#plt.tight_layout()
#plt.show()

print(data[0]["label"].numpy(), data[1]["label"].numpy(), data[2]["label"].numpy())

test_image = data[0]["image"].numpy()

#print('test_image:'+ test_image)

predict_request = predict_pb2.PredictRequest()
predict_request.model_spec.name = 'mnist'
predict_request.model_spec.signature_name = 'prediction'

predict_channel = implementations.insecure_channel(host, int(port))
predict_stub = prediction_service_pb2.beta_create_PredictionService_stub(predict_channel)

#print(test_image)

predict_request.inputs['x'].CopyFrom(
tf.make_tensor_proto(test_image, shape=[1, test_image.size]))
result = predict_stub.Predict.future(predict_request, 3.0)
result.add_done_callback(
_create_rpc_callback())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions