Last active
June 4, 2020 09:10
-
-
Save krikit/32c918cc03b52315ade562267a91fa6b to your computer and use it in GitHub Desktop.
Regression API Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Regress API Test\n", | |
"====" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Imports" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import shutil\n", | |
"import numpy as np\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"WARNING:tensorflow:From /Users/deploy/.pyenv/versions/3.7.6/envs/danke/lib/python3.7/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n", | |
"Instructions for updating:\n", | |
"Call initializer instance with the dtype argument instead of passing it to the constructor\n", | |
"WARNING:tensorflow:From /Users/deploy/.pyenv/versions/3.7.6/envs/danke/lib/python3.7/site-packages/tensorflow/python/ops/nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n", | |
"Instructions for updating:\n", | |
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n", | |
"Model: \"model\"\n", | |
"__________________________________________________________________________________________________\n", | |
"Layer (type) Output Shape Param # Connected to \n", | |
"==================================================================================================\n", | |
"x1 (InputLayer) [(None, 1)] 0 \n", | |
"__________________________________________________________________________________________________\n", | |
"x2 (InputLayer) [(None, 1)] 0 \n", | |
"__________________________________________________________________________________________________\n", | |
"concat (Concatenate) (None, 2) 0 x1[0][0] \n", | |
" x2[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"dense (Dense) (None, 10) 30 concat[0][0] \n", | |
"__________________________________________________________________________________________________\n", | |
"y (Dense) (None, 1) 11 dense[0][0] \n", | |
"==================================================================================================\n", | |
"Total params: 41\n", | |
"Trainable params: 41\n", | |
"Non-trainable params: 0\n", | |
"__________________________________________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"inputs = {\n", | |
" 'x1': tf.keras.layers.Input(shape=(1, ), name='x1', dtype='float32'),\n", | |
" 'x2': tf.keras.layers.Input(shape=(1, ), name='x2', dtype='float32'),\n", | |
"}\n", | |
"concat = tf.keras.layers.Concatenate(name='concat')([inputs['x1'], inputs['x2']])\n", | |
"dense = tf.keras.layers.Dense(10, use_bias=True, activation='relu', name='dense')(concat)\n", | |
"outputs = tf.keras.layers.Dense(1, use_bias=True, activation='sigmoid', name='y')(dense)\n", | |
"model = tf.keras.models.Model(inputs=inputs, outputs=outputs)\n", | |
"model.compile(optimizer='SGD', loss='binary_crossentropy')\n", | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Train" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10000/10000 [==============================] - 0s 25us/sample - loss: 0.3056\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.keras.callbacks.History at 0x131192c90>" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"num_exam = 10000\n", | |
"model.fit({'x1': np.random.randn(num_exam), 'x2': np.random.rand(num_exam)}, np.random.randn(num_exam))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Save" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"WARNING:tensorflow:From <ipython-input-4-662274ec3ef5>:1: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.\n", | |
"Instructions for updating:\n", | |
"This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor_info or tf.compat.v1.saved_model.build_tensor_info.\n", | |
"inputs {\n", | |
" key: \"x1\"\n", | |
" value {\n", | |
" name: \"x1:0\"\n", | |
" dtype: DT_FLOAT\n", | |
" tensor_shape {\n", | |
" dim {\n", | |
" size: -1\n", | |
" }\n", | |
" dim {\n", | |
" size: 1\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"inputs {\n", | |
" key: \"x2\"\n", | |
" value {\n", | |
" name: \"x2:0\"\n", | |
" dtype: DT_FLOAT\n", | |
" tensor_shape {\n", | |
" dim {\n", | |
" size: -1\n", | |
" }\n", | |
" dim {\n", | |
" size: 1\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"outputs {\n", | |
" key: \"y\"\n", | |
" value {\n", | |
" name: \"y/Sigmoid:0\"\n", | |
" dtype: DT_FLOAT\n", | |
" tensor_shape {\n", | |
" dim {\n", | |
" size: -1\n", | |
" }\n", | |
" dim {\n", | |
" size: 1\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"method_name: \"tensorflow/serving/regress\"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"input_infos = {name: tf.saved_model.build_tensor_info(tensor) for name, tensor in model.input.items()}\n", | |
"output_infos = {'y': tf.saved_model.build_tensor_info(model.outputs[0])}\n", | |
"signature = tf.saved_model.build_signature_def(\n", | |
" inputs=input_infos,\n", | |
" outputs=output_infos,\n", | |
" method_name=tf.saved_model.signature_constants.REGRESS_METHOD_NAME\n", | |
")\n", | |
"print(signature)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"INFO:tensorflow:No assets to save.\n", | |
"INFO:tensorflow:No assets to write.\n", | |
"INFO:tensorflow:SavedModel written to: ./random_regression/1/saved_model.pb\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"b'./random_regression/1/saved_model.pb'" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model_dir = './random_regression/1'\n", | |
"shutil.rmtree(model_dir, ignore_errors=True)\n", | |
"model_builder = tf.saved_model.builder.SavedModelBuilder(model_dir)\n", | |
"model_builder.add_meta_graph_and_variables(\n", | |
" tf.keras.backend.get_session(),\n", | |
" tags=[tf.saved_model.tag_constants.SERVING, ],\n", | |
" signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}\n", | |
")\n", | |
"model_builder.save()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Show Saved Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:\n", | |
"\n", | |
"signature_def['serving_default']:\n", | |
" The given SavedModel SignatureDef contains the following input(s):\n", | |
" inputs['x1'] tensor_info:\n", | |
" dtype: DT_FLOAT\n", | |
" shape: (-1, 1)\n", | |
" name: x1:0\n", | |
" inputs['x2'] tensor_info:\n", | |
" dtype: DT_FLOAT\n", | |
" shape: (-1, 1)\n", | |
" name: x2:0\n", | |
" The given SavedModel SignatureDef contains the following output(s):\n", | |
" outputs['y'] tensor_info:\n", | |
" dtype: DT_FLOAT\n", | |
" shape: (-1, 1)\n", | |
" name: y/Sigmoid:0\n", | |
" Method name is: tensorflow/serving/regress\n" | |
] | |
} | |
], | |
"source": [ | |
"!saved_model_cli show --dir ./random_regression/1 --all" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### REST API Results" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1) __regress__ API with \"examples\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{ \"error\": \"Expected one input Tensor.\" }" | |
] | |
} | |
], | |
"source": [ | |
"!curl -X POST -H \"Content-Type: application/json\" http://localhost:8501/v1/models/random_regression/versions/1:regress -d ' \\\n", | |
"{ \\\n", | |
" \"examples\": [ \\\n", | |
" { \\\n", | |
" \"x1\": [0.1], \\\n", | |
" \"x2\": [0.2] \\\n", | |
" }, \\\n", | |
" { \\\n", | |
" \"x1\": [0.1], \\\n", | |
" \"x2\": [0.3] \\\n", | |
" } \\\n", | |
" ] \\\n", | |
"}'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"2) __predict__ API with \"instances\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\r\n", | |
" \"predictions\": [[0.143165469], [0.124352224]\r\n", | |
" ]\r\n", | |
"}" | |
] | |
} | |
], | |
"source": [ | |
"!curl -X POST -H \"Content-Type: application/json\" http://localhost:8501/v1/models/random_regression/versions/1:predict -d ' \\\n", | |
"{ \\\n", | |
" \"instances\": [ \\\n", | |
" { \\\n", | |
" \"x1\": [0.1], \\\n", | |
" \"x2\": [0.2] \\\n", | |
" }, \\\n", | |
" { \\\n", | |
" \"x1\": [0.1], \\\n", | |
" \"x2\": [0.3] \\\n", | |
" } \\\n", | |
" ] \\\n", | |
"}'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"3) __predict__ API with \"context\" and \"examples\" that I really want" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{ \"error\": \"Expected one input Tensor.\" }" | |
] | |
} | |
], | |
"source": [ | |
"!curl -X POST -H \"Content-Type: application/json\" http://localhost:8501/v1/models/random_regression/versions/1:regress -d ' \\\n", | |
"{ \\\n", | |
" \"context\": { \\\n", | |
" \"x1\": [0.1] \\\n", | |
" }, \\\n", | |
" \"examples\": [ \\\n", | |
" { \\\n", | |
" \"x2\": [0.2] \\\n", | |
" }, \\\n", | |
" { \\\n", | |
" \"x2\": [0.3] \\\n", | |
" } \\\n", | |
" ] \\\n", | |
"}' \\" | |
] | |
}, | |
{ | |
"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.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment