Skip to content

Commit

Permalink
Merge pull request #150 from cliveseldon/tester_bug
Browse files Browse the repository at this point in the history
Fix gRPC tests for wrappers and update sklearn iris example to show use
  • Loading branch information
ukclivecox authored May 6, 2018
2 parents 01f6758 + 292c8f2 commit ea85b7e
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 16 deletions.
4 changes: 4 additions & 0 deletions examples/models/sklearn_iris/.s2i/environment_grpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=IrisClassifier
API_TYPE=GRPC
SERVICE_TYPE=MODEL
PERSISTENCE=0
283 changes: 271 additions & 12 deletions examples/models/sklearn_iris/sklearn_iris.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading iris data set...\n",
"Dataset loaded!\n",
"Training model...\n",
"Model trained!\n",
"Saving model in IrisClassifier.sav\n",
"Model saved!\n"
]
}
],
"source": [
"import numpy as np\n",
"import os\n",
Expand Down Expand Up @@ -65,29 +78,74 @@
"Wrap model using s2i"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## REST test"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---> Installing application source...\n",
"---> Installing dependencies ...\n",
"Collecting scikit-learn==0.19.0 (from -r requirements.txt (line 1))\n",
"Downloading https://files.pythonhosted.org/packages/a4/b3/209652a5d60ce4a2a8a35ad893d7565bbb0f87ce043264ba5c9e7de304cd/scikit_learn-0.19.0-cp36-cp36m-manylinux1_x86_64.whl (12.4MB)\n",
"Collecting scipy==0.18.1 (from -r requirements.txt (line 2))\n",
"Downloading https://files.pythonhosted.org/packages/74/c0/f0bf4eaef1b6aa7bdd1ae5597ce1d9e729417b3ca085c47d0f1c640d34f8/scipy-0.18.1-cp36-cp36m-manylinux1_x86_64.whl (42.5MB)\n",
"Installing collected packages: scikit-learn, scipy\n",
"Successfully installed scikit-learn-0.19.0 scipy-0.18.1\n",
"You are using pip version 9.0.1, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\n",
"Build completed successfully\n"
]
}
],
"source": [
"!s2i build . seldonio/seldon-core-s2i-python3 sklearn-iris:0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"92a3d2d30811f084881df501b16eba1ac52334927ba407ecf07f627a9c67015b\r\n"
]
}
],
"source": [
"!docker run --name \"iris_predictor\" -d --rm -p 5000:5000 sklearn-iris:0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"rm -f proto/prediction*.py\r\n",
"rm -f proto/prediction.proto\r\n",
"rm -rf proto/__pycache__\r\n",
"cp ../../proto/prediction.proto ./proto\r\n",
"python -m grpc.tools.protoc -I. --python_out=. --grpc_python_out=. ./proto/prediction.proto\r\n"
]
}
],
"source": [
"!cd ../../../wrappers/testing && make build_protos"
]
Expand All @@ -101,22 +159,223 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------------------------\r\n",
"SENDING NEW REQUEST:\r\n",
"{'meta': {}, 'data': {'names': ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], 'ndarray': [[6.369, 3.498, 6.062, 2.57]]}}\r\n",
"RECEIVED RESPONSE:\r\n",
"{'data': {'names': ['t:0', 't:1', 't:2'], 'ndarray': [[0.00021525713562610643, 0.11504072045280644, 0.8847440224115675]]}}\r\n",
"\r\n",
"Time 0.004708290100097656\r\n"
]
}
],
"source": [
"!python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"iris_predictor\r\n"
]
}
],
"source": [
"!docker rm iris_predictor --force"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## grpc test"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---> Installing application source...\n",
"---> Installing dependencies ...\n",
"Collecting scikit-learn==0.19.0 (from -r requirements.txt (line 1))\n",
"Downloading https://files.pythonhosted.org/packages/a4/b3/209652a5d60ce4a2a8a35ad893d7565bbb0f87ce043264ba5c9e7de304cd/scikit_learn-0.19.0-cp36-cp36m-manylinux1_x86_64.whl (12.4MB)\n",
"Collecting scipy==0.18.1 (from -r requirements.txt (line 2))\n",
"Downloading https://files.pythonhosted.org/packages/74/c0/f0bf4eaef1b6aa7bdd1ae5597ce1d9e729417b3ca085c47d0f1c640d34f8/scipy-0.18.1-cp36-cp36m-manylinux1_x86_64.whl (42.5MB)\n",
"Installing collected packages: scikit-learn, scipy\n",
"Successfully installed scikit-learn-0.19.0 scipy-0.18.1\n",
"You are using pip version 9.0.1, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\n",
"Build completed successfully\n"
]
}
],
"source": [
"!s2i build -E .s2i/environment_grpc . seldonio/seldon-core-s2i-python3 sklearn-iris:0.1"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b2e8b9153904380d976e3a421d345066fc735d0217efd1e478a0cab70d39ca54\r\n"
]
}
],
"source": [
"!docker run --name \"iris_predictor\" -d --rm -p 5000:5000 sklearn-iris:0.1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Test using NDArray payload"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------------------------\n",
"SENDING NEW REQUEST:\n",
"data {\n",
" names: \"sepal_length\"\n",
" names: \"sepal_width\"\n",
" names: \"petal_length\"\n",
" names: \"petal_width\"\n",
" ndarray {\n",
" values {\n",
" list_value {\n",
" values {\n",
" number_value: 6.893\n",
" }\n",
" values {\n",
" number_value: 4.302\n",
" }\n",
" values {\n",
" number_value: 8.419\n",
" }\n",
" values {\n",
" number_value: 1.102\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n",
"\n",
"RECEIVED RESPONSE:\n",
"data {\n",
" names: \"t:0\"\n",
" names: \"t:1\"\n",
" names: \"t:2\"\n",
" ndarray {\n",
" values {\n",
" list_value {\n",
" values {\n",
" number_value: 1.3326678349719564e-05\n",
" }\n",
" values {\n",
" number_value: 0.37925859004409174\n",
" }\n",
" values {\n",
" number_value: 0.6207280832775586\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n",
"\n",
"\n"
]
}
],
"source": [
"!python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p --grpc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Test using Tensor payload"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------------------------\n",
"SENDING NEW REQUEST:\n",
"data {\n",
" names: \"sepal_length\"\n",
" names: \"sepal_width\"\n",
" names: \"petal_length\"\n",
" names: \"petal_width\"\n",
" tensor {\n",
" shape: 1\n",
" shape: 4\n",
" values: 7.986\n",
" values: 3.85\n",
" values: 6.16\n",
" values: 0.769\n",
" }\n",
"}\n",
"\n",
"RECEIVED RESPONSE:\n",
"data {\n",
" names: \"t:0\"\n",
" names: \"t:1\"\n",
" names: \"t:2\"\n",
" tensor {\n",
" shape: 1\n",
" shape: 3\n",
" values: 0.005626056223331023\n",
" values: 0.9558037301171475\n",
" values: 0.03857021365952149\n",
" }\n",
"}\n",
"\n",
"\n"
]
}
],
"source": [
"!python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p --grpc --tensor"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
7 changes: 3 additions & 4 deletions wrappers/testing/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import urllib
from proto import prediction_pb2
from proto import prediction_pb2_grpc
from google.protobuf import json_format
import grpc
from time import time

Expand Down Expand Up @@ -86,10 +87,8 @@ def gen_GRPC_request(batch,features,tensor=True):
)
)
else:
datadef = prediction_pb2.DefaultData(
names = features,
ndarray = array_to_list_value(batch)
)
v = '{"names":'+json.dumps(features)+',"ndarray":'+json.dumps(batch.tolist())+'}'
datadef = json_format.Parse(v,prediction_pb2.DefaultData())
request = prediction_pb2.SeldonMessage(
data = datadef
)
Expand Down

0 comments on commit ea85b7e

Please sign in to comment.