Skip to content

Commit

Permalink
Merge pull request #147 from cliveseldon/minikube_rbac
Browse files Browse the repository at this point in the history
Minikube RBAC updates and Notebooks for Model examples
  • Loading branch information
ukclivecox authored Apr 29, 2018
2 parents 9b8a0a5 + 00897d6 commit dbe92b4
Show file tree
Hide file tree
Showing 16 changed files with 1,768 additions and 506 deletions.
90 changes: 0 additions & 90 deletions examples/models/deep_mnist/README.md

This file was deleted.

261 changes: 261 additions & 0 deletions examples/models/deep_mnist/deep_mnist.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tensorflow MNIST Model Deployment\n",
"\n",
" * Wrap a Tensorflow MNIST python model for use as a prediction microservice in seldon-core\n",
" * Run locally on Docker to test\n",
" * Deploy on seldon-core running on minikube\n",
" \n",
"## Depenencies\n",
"\n",
" * [Helm](https://github.com/kubernetes/helm)\n",
" * [Minikube](https://github.com/kubernetes/minikube)\n",
" * [S2I](https://github.com/openshift/source-to-image)\n",
"\n",
"```bash\n",
"pip install tensorflow\n",
"pip install grpcio-tools\n",
"```\n",
"\n",
"## Train locally\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.examples.tutorials.mnist import input_data\n",
"mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot = True)\n",
"import tensorflow as tf\n",
"\n",
"if __name__ == '__main__':\n",
" \n",
" x = tf.placeholder(tf.float32, [None,784], name=\"x\")\n",
"\n",
" W = tf.Variable(tf.zeros([784,10]))\n",
" b = tf.Variable(tf.zeros([10]))\n",
"\n",
" y = tf.nn.softmax(tf.matmul(x,W) + b, name=\"y\")\n",
"\n",
" y_ = tf.placeholder(tf.float32, [None, 10])\n",
"\n",
"\n",
" cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))\n",
"\n",
" train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
"\n",
" init = tf.initialize_all_variables()\n",
"\n",
" sess = tf.Session()\n",
" sess.run(init)\n",
"\n",
" for i in range(1000):\n",
" batch_xs, batch_ys = mnist.train.next_batch(100)\n",
" sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
"\n",
" correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\n",
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
" print(sess.run(accuracy, feed_dict = {x: mnist.test.images, y_:mnist.test.labels}))\n",
"\n",
" saver = tf.train.Saver()\n",
"\n",
" saver.save(sess, \"model/deep_mnist_model\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wrap model using s2i"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!s2i build . seldonio/seldon-core-s2i-python2 deep-mnist:0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!docker run --name \"mnist_predictor\" -d --rm -p 5000:5000 deep-mnist:0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!cd ../../../wrappers/testing && make build_protos"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Send some random features that conform to the contract"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!docker rm mnist_predictor --force"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!minikube start --memory 4096 --feature-gates=CustomResourceValidation=true --extra-config=apiserver.Authorization.Mode=RBAC"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!kubectl create clusterrolebinding kube-system-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!helm init"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!helm install ../../../helm-charts/seldon-core-crd --name seldon-core-crd --set usage_metrics.enabled=true\n",
"!helm install ../../../helm-charts/seldon-core --name seldon-core "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!eval $(minikube docker-env) && s2i build . seldonio/seldon-core-s2i-python2 deep-mnist:0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!kubectl create -f deep_mnist.json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wait until ready (replicas == replicasAvailable)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!kubectl get seldondeployments deep-mnist -o jsonpath='{.status}' "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!cd ../../../util/api_tester && make build_protos "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python ../../../util/api_tester/api-tester.py contract.json \\\n",
" `minikube ip` `kubectl get svc -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].spec.ports[0].nodePort}'` \\\n",
" --oauth-key oauth-key --oauth-secret oauth-secret -p"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!minikube delete"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion examples/models/deep_mnist/deep_mnist.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"spec": {
"containers": [
{
"image": "deep-mnist:latest",
"image": "deep-mnist:0.1",
"imagePullPolicy": "IfNotPresent",
"name": "classifier",
"resources": {
Expand Down
Loading

0 comments on commit dbe92b4

Please sign in to comment.