Skip to content

Commit

Permalink
Merge pull request #366 from vespa-engine/tgm/tensorflow-via-onnx
Browse files Browse the repository at this point in the history
add title and include clean up section
  • Loading branch information
kkraune authored Aug 3, 2022
2 parents b11bd8f + 0fe067d commit 5bf2694
Showing 1 changed file with 88 additions and 53 deletions.
141 changes: 88 additions & 53 deletions docs/sphinx/source/use_cases/tensorflow-via-onnx.ipynb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "45d00d06",
"cell_type": "markdown",
"id": "1b9c76a1",
"metadata": {},
"outputs": [],
"source": [
"!pip3 install -Uqq pyvespa ir_datasets numpy pandas tensorflow tensorflow_ranking onnx tf2onnx"
"# TensorFlow: Deploy model to Vespa through ONNX"
]
},
{
Expand All @@ -27,6 +25,24 @@
"8. Ensure that prediction from the model deployed to Vespa match those obtained from the model directly."
]
},
{
"cell_type": "markdown",
"id": "d5af8b0d",
"metadata": {},
"source": [
"## Install packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "45d00d06",
"metadata": {},
"outputs": [],
"source": [
"!pip3 install -Uqq pyvespa ir_datasets numpy pandas tensorflow tensorflow_ranking onnx tf2onnx"
]
},
{
"cell_type": "markdown",
"id": "f16ae8a4",
Expand Down Expand Up @@ -481,45 +497,45 @@
"output_type": "stream",
"text": [
"Epoch 1/20\n",
"304/304 [==============================] - 4s 2ms/step - loss: -0.5089 - ndcg_stateless: 0.5346\n",
"304/304 [==============================] - 4s 2ms/step - loss: -0.6808 - ndcg_stateless: 0.7160\n",
"Epoch 2/20\n",
"304/304 [==============================] - 1s 975us/step - loss: -0.7020 - ndcg_stateless: 0.7352\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.6967 - ndcg_stateless: 0.7168\n",
"Epoch 3/20\n",
"304/304 [==============================] - 1s 902us/step - loss: -0.7267 - ndcg_stateless: 0.7621\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7051 - ndcg_stateless: 0.7232\n",
"Epoch 4/20\n",
"304/304 [==============================] - 1s 930us/step - loss: -0.7357 - ndcg_stateless: 0.7654\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7281 - ndcg_stateless: 0.7605\n",
"Epoch 5/20\n",
"304/304 [==============================] - 1s 905us/step - loss: -0.7399 - ndcg_stateless: 0.7669\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7355 - ndcg_stateless: 0.7646\n",
"Epoch 6/20\n",
"304/304 [==============================] - 1s 948us/step - loss: -0.7426 - ndcg_stateless: 0.7675\n",
"304/304 [==============================] - 1s 984us/step - loss: -0.7399 - ndcg_stateless: 0.7662\n",
"Epoch 7/20\n",
"304/304 [==============================] - 1s 921us/step - loss: -0.7448 - ndcg_stateless: 0.7678\n",
"304/304 [==============================] - 1s 955us/step - loss: -0.7424 - ndcg_stateless: 0.7674\n",
"Epoch 8/20\n",
"304/304 [==============================] - 1s 963us/step - loss: -0.7463 - ndcg_stateless: 0.7675\n",
"304/304 [==============================] - 1s 933us/step - loss: -0.7445 - ndcg_stateless: 0.7682\n",
"Epoch 9/20\n",
"304/304 [==============================] - 1s 923us/step - loss: -0.7474 - ndcg_stateless: 0.7680\n",
"304/304 [==============================] - 1s 947us/step - loss: -0.7460 - ndcg_stateless: 0.7687\n",
"Epoch 10/20\n",
"304/304 [==============================] - 1s 925us/step - loss: -0.7484 - ndcg_stateless: 0.7700\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7470 - ndcg_stateless: 0.7678\n",
"Epoch 11/20\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7491 - ndcg_stateless: 0.7679\n",
"304/304 [==============================] - 1s 971us/step - loss: -0.7480 - ndcg_stateless: 0.7676\n",
"Epoch 12/20\n",
"304/304 [==============================] - 1s 953us/step - loss: -0.7500 - ndcg_stateless: 0.7693\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7487 - ndcg_stateless: 0.7684\n",
"Epoch 13/20\n",
"304/304 [==============================] - 1s 915us/step - loss: -0.7504 - ndcg_stateless: 0.7697\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7495 - ndcg_stateless: 0.7686\n",
"Epoch 14/20\n",
"304/304 [==============================] - 1s 1ms/step - loss: -0.7511 - ndcg_stateless: 0.7702\n",
"304/304 [==============================] - 1s 906us/step - loss: -0.7502 - ndcg_stateless: 0.7694\n",
"Epoch 15/20\n",
"304/304 [==============================] - 1s 909us/step - loss: -0.7515 - ndcg_stateless: 0.7689\n",
"304/304 [==============================] - 1s 932us/step - loss: -0.7506 - ndcg_stateless: 0.7685\n",
"Epoch 16/20\n",
"304/304 [==============================] - 1s 935us/step - loss: -0.7519 - ndcg_stateless: 0.7696\n",
"304/304 [==============================] - 1s 925us/step - loss: -0.7511 - ndcg_stateless: 0.7693\n",
"Epoch 17/20\n",
"304/304 [==============================] - 1s 902us/step - loss: -0.7525 - ndcg_stateless: 0.7686\n",
"304/304 [==============================] - 1s 985us/step - loss: -0.7516 - ndcg_stateless: 0.7699\n",
"Epoch 18/20\n",
"304/304 [==============================] - 1s 972us/step - loss: -0.7524 - ndcg_stateless: 0.7689\n",
"304/304 [==============================] - 1s 899us/step - loss: -0.7519 - ndcg_stateless: 0.7685\n",
"Epoch 19/20\n",
"304/304 [==============================] - 1s 946us/step - loss: -0.7530 - ndcg_stateless: 0.7691\n",
"304/304 [==============================] - 1s 920us/step - loss: -0.7524 - ndcg_stateless: 0.7694\n",
"Epoch 20/20\n",
"304/304 [==============================] - 1s 922us/step - loss: -0.7532 - ndcg_stateless: 0.7688\n"
"304/304 [==============================] - 1s 933us/step - loss: -0.7527 - ndcg_stateless: 0.7681\n"
]
}
],
Expand Down Expand Up @@ -598,26 +614,26 @@
"text": [
"/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py:127: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour\n",
" warn(RuntimeWarning(msg))\n",
"2022-08-02 05:21:29,786 - WARNING - '--tag' not specified for saved_model. Using --tag serve\n",
"2022-08-02 05:21:29,853 - INFO - Signatures found in model: [serving_default].\n",
"2022-08-02 05:21:29,853 - WARNING - '--signature_def' not specified, using first signature: serving_default\n",
"2022-08-02 05:21:29,853 - INFO - Output names: ['dense']\n",
"2022-08-02 14:42:39,245 - WARNING - '--tag' not specified for saved_model. Using --tag serve\n",
"2022-08-02 14:42:39,319 - INFO - Signatures found in model: [serving_default].\n",
"2022-08-02 14:42:39,319 - WARNING - '--signature_def' not specified, using first signature: serving_default\n",
"2022-08-02 14:42:39,319 - INFO - Output names: ['dense']\n",
"WARNING:tensorflow:From /Users/tmartins/.local/share/virtualenvs/source-bd9eBXOq/lib/python3.9/site-packages/tf2onnx/tf_loader.py:711: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.graph_util.extract_sub_graph`\n",
"2022-08-02 05:21:29,874 - WARNING - From /Users/tmartins/.local/share/virtualenvs/source-bd9eBXOq/lib/python3.9/site-packages/tf2onnx/tf_loader.py:711: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.\n",
"2022-08-02 14:42:39,337 - WARNING - From /Users/tmartins/.local/share/virtualenvs/source-bd9eBXOq/lib/python3.9/site-packages/tf2onnx/tf_loader.py:711: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use `tf.compat.v1.graph_util.extract_sub_graph`\n",
"2022-08-02 05:21:29,881 - INFO - Using tensorflow=2.9.1, onnx=1.12.0, tf2onnx=1.12.0/a58786\n",
"2022-08-02 05:21:29,881 - INFO - Using opset <onnx, 13>\n",
"2022-08-02 05:21:29,885 - INFO - Computed 0 values for constant folding\n",
"2022-08-02 05:21:29,891 - INFO - Optimizing ONNX model\n",
"2022-08-02 05:21:29,906 - INFO - After optimization: Identity -5 (5->0)\n",
"2022-08-02 05:21:29,907 - INFO - \n",
"2022-08-02 05:21:29,907 - INFO - Successfully converted TensorFlow model simpler_keras_model to ONNX\n",
"2022-08-02 05:21:29,907 - INFO - Model inputs: ['input']\n",
"2022-08-02 05:21:29,907 - INFO - Model outputs: ['dense']\n",
"2022-08-02 05:21:29,907 - INFO - ONNX model is saved at simpler_keras_model.onnx\n"
"2022-08-02 14:42:39,342 - INFO - Using tensorflow=2.9.1, onnx=1.12.0, tf2onnx=1.12.0/a58786\n",
"2022-08-02 14:42:39,342 - INFO - Using opset <onnx, 13>\n",
"2022-08-02 14:42:39,345 - INFO - Computed 0 values for constant folding\n",
"2022-08-02 14:42:39,351 - INFO - Optimizing ONNX model\n",
"2022-08-02 14:42:39,363 - INFO - After optimization: Identity -5 (5->0)\n",
"2022-08-02 14:42:39,363 - INFO - \n",
"2022-08-02 14:42:39,364 - INFO - Successfully converted TensorFlow model simpler_keras_model to ONNX\n",
"2022-08-02 14:42:39,364 - INFO - Model inputs: ['input']\n",
"2022-08-02 14:42:39,364 - INFO - Model outputs: ['dense']\n",
"2022-08-02 14:42:39,364 - INFO - ONNX model is saved at simpler_keras_model.onnx\n"
]
}
],
Expand Down Expand Up @@ -1008,14 +1024,14 @@
"text": [
"Waiting for configuration server, 0/300 seconds...\n",
"Waiting for configuration server, 5/300 seconds...\n",
"Waiting for configuration server, 10/300 seconds...\n",
"Waiting for application status, 0/300 seconds...\n",
"Waiting for application status, 5/300 seconds...\n",
"Waiting for application status, 10/300 seconds...\n",
"Waiting for application status, 15/300 seconds...\n",
"Waiting for application status, 20/300 seconds...\n",
"Waiting for application status, 25/300 seconds...\n",
"Waiting for application status, 30/300 seconds...\n",
"Waiting for application status, 35/300 seconds...\n",
"Finished deployment.\n"
]
}
Expand Down Expand Up @@ -1349,14 +1365,14 @@
" <td>0.2</td>\n",
" <td>0.134791</td>\n",
" <td>0.016299</td>\n",
" <td>-0.655775</td>\n",
" <td>-0.643021</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.4</td>\n",
" <td>0.275639</td>\n",
" <td>0.038767</td>\n",
" <td>-1.030758</td>\n",
" <td>-1.011910</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand All @@ -1368,8 +1384,8 @@
"1 0.4 0.275639 \n",
"\n",
" nativeRank(text) vespa_prediction \n",
"0 0.016299 -0.655775 \n",
"1 0.038767 -1.030758 "
"0 0.016299 -0.643021 \n",
"1 0.038767 -1.011910 "
]
},
"execution_count": 37,
Expand Down Expand Up @@ -1400,8 +1416,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 75ms/step\n",
"1/1 [==============================] - 0s 25ms/step\n"
"1/1 [==============================] - 0s 72ms/step\n",
"1/1 [==============================] - 0s 27ms/step\n"
]
}
],
Expand Down Expand Up @@ -1451,16 +1467,16 @@
" <td>0.2</td>\n",
" <td>0.134791</td>\n",
" <td>0.016299</td>\n",
" <td>-0.655775</td>\n",
" <td>-0.655775</td>\n",
" <td>-0.643021</td>\n",
" <td>-0.643021</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.4</td>\n",
" <td>0.275639</td>\n",
" <td>0.038767</td>\n",
" <td>-1.030758</td>\n",
" <td>-1.030758</td>\n",
" <td>-1.011910</td>\n",
" <td>-1.011911</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand All @@ -1472,8 +1488,8 @@
"1 0.4 0.275639 \n",
"\n",
" nativeRank(text) vespa_prediction tf_prediction \n",
"0 0.016299 -0.655775 -0.655775 \n",
"1 0.038767 -1.030758 -1.030758 "
"0 0.016299 -0.643021 -0.643021 \n",
"1 0.038767 -1.011910 -1.011911 "
]
},
"execution_count": 39,
Expand Down Expand Up @@ -1504,6 +1520,25 @@
"\n",
"assert_almost_equal(predictions[\"vespa_prediction\"].tolist(), predictions[\"tf_prediction\"].tolist(), 5)"
]
},
{
"cell_type": "markdown",
"id": "d3398800",
"metadata": {},
"source": [
"## Clean environment"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "c8ff88cf",
"metadata": {},
"outputs": [],
"source": [
"vespa_docker.container.stop(timeout=600)\n",
"vespa_docker.container.remove()"
]
}
],
"metadata": {
Expand Down

0 comments on commit 5bf2694

Please sign in to comment.