damccorm commented on code in PR #25933:
URL: https://github.com/apache/beam/pull/25933#discussion_r1145175601
##########
examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb:
##########
@@ -0,0 +1,620 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "fFjof1NgAJwu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "A8xNRyZMW1yK"
+ },
+ "source": [
+ "# Apache Beam RunInference with TensorFlow and TensorflowHub\n",
+ "\n",
+ "<table align=\"left\">\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"
/>Run in Google Colab</a>\n",
+ " </td>\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://github.com/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"
/>View source on GitHub</a>\n",
+ " </td>\n",
+ "</table>\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "This notebook demonstrates the use of the RunInference transform for
[TensorFlow](https://www.tensorflow.org/).\n",
+ "Beam
[RunInference](https://beam.apache.org/releases/pydoc/current/apache_beam.ml.inference.base.html#apache_beam.ml.inference.base.RunInference)
accepts a Tensorflow Model Handler either
[TFModelHandlerNumpy](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L91)
or
[TFModelHandlerTensor](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L184)
depending upon the type of the input.\n",
Review Comment:
```suggestion
"This notebook demonstrates the use of Beam's
[RunInference](https://beam.apache.org/releases/pydoc/current/apache_beam.ml.inference.base.html#apache_beam.ml.inference.base.RunInference)
transform for [TensorFlow](https://www.tensorflow.org/).\n",
"Beam has built in support for 2 Tensorflow Model Handlers:
[TFModelHandlerNumpy](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L91)
and
[TFModelHandlerTensor](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L184).\n",
"TFModelHandlerNumpy can be used to run inference on models
expecting a Numpy array as an input while TFModelHandlerTensor can be used to
run inference on models expecting a Tensor as an input.\n",
```
##########
examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb:
##########
@@ -0,0 +1,620 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "fFjof1NgAJwu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "A8xNRyZMW1yK"
+ },
+ "source": [
+ "# Apache Beam RunInference with TensorFlow and TensorflowHub\n",
+ "\n",
+ "<table align=\"left\">\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"
/>Run in Google Colab</a>\n",
+ " </td>\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://github.com/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"
/>View source on GitHub</a>\n",
+ " </td>\n",
+ "</table>\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "This notebook demonstrates the use of the RunInference transform for
[TensorFlow](https://www.tensorflow.org/).\n",
+ "Beam
[RunInference](https://beam.apache.org/releases/pydoc/current/apache_beam.ml.inference.base.html#apache_beam.ml.inference.base.RunInference)
accepts a Tensorflow Model Handler either
[TFModelHandlerNumpy](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L91)
or
[TFModelHandlerTensor](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L184)
depending upon the type of the input.\n",
+ "\n",
+ "The Apache Beam RunInference transform is used to make predictions
for\n",
+ "a variety of machine learning models. For more information about the
RunInference API, see [Machine
Learning](https://beam.apache.org/documentation/sdks/python-machine-learning)
in the Apache Beam documentation.\n",
+ "\n",
+ "This notebook demonstrates the following steps:\n",
+ "- Build a simple TensorFlow model.\n",
+ "- Set up example data.\n",
+ "- Run those examples and get a prediction inside an Apache Beam
pipeline.\n",
+ "- Run an inference pipeline by using a trained model from tensorflow
hub."
Review Comment:
```suggestion
"- Run an inference pipeline by using a trained model from
TensorFlow Hub."
```
##########
examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb:
##########
@@ -0,0 +1,620 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "fFjof1NgAJwu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "A8xNRyZMW1yK"
+ },
+ "source": [
+ "# Apache Beam RunInference with TensorFlow and TensorflowHub\n",
+ "\n",
+ "<table align=\"left\">\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"
/>Run in Google Colab</a>\n",
+ " </td>\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://github.com/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"
/>View source on GitHub</a>\n",
+ " </td>\n",
+ "</table>\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "This notebook demonstrates the use of the RunInference transform for
[TensorFlow](https://www.tensorflow.org/).\n",
+ "Beam
[RunInference](https://beam.apache.org/releases/pydoc/current/apache_beam.ml.inference.base.html#apache_beam.ml.inference.base.RunInference)
accepts a Tensorflow Model Handler either
[TFModelHandlerNumpy](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L91)
or
[TFModelHandlerTensor](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L184)
depending upon the type of the input.\n",
+ "\n",
+ "The Apache Beam RunInference transform is used to make predictions
for\n",
+ "a variety of machine learning models. For more information about the
RunInference API, see [Machine
Learning](https://beam.apache.org/documentation/sdks/python-machine-learning)
in the Apache Beam documentation.\n",
+ "\n",
+ "This notebook demonstrates the following steps:\n",
+ "- Build a simple TensorFlow model.\n",
+ "- Set up example data.\n",
+ "- Run those examples and get a prediction inside an Apache Beam
pipeline.\n",
+ "- Run an inference pipeline by using a trained model from tensorflow
hub."
+ ],
+ "metadata": {
+ "id": "HrCtxslBGK8Z"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Before you begin\n",
+ "Complete the following setup steps.\n",
+ "\n",
+ "First, import `tensorflow`."
+ ],
+ "metadata": {
+ "id": "HrCtxslBGK8A"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "jBakpNZnAhqk"
+ },
+ "source": [
+ "!pip install tensorflow\n",
+ "!pip install apache_beam==2.46.0"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "To use RunInference with Tensorflow Model Handler, install Apache
Beam version 2.46 or later."
Review Comment:
Could you collapse this step with the last one and remove the pip
freeze/grep check? As is, the check is redundant (the >=2.46 callout is not
though)
##########
examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb:
##########
@@ -0,0 +1,620 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF),
Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "fFjof1NgAJwu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "A8xNRyZMW1yK"
+ },
+ "source": [
+ "# Apache Beam RunInference with TensorFlow and TensorflowHub\n",
+ "\n",
+ "<table align=\"left\">\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://colab.research.google.com/github/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"
/>Run in Google Colab</a>\n",
+ " </td>\n",
+ " <td>\n",
+ " <a target=\"_blank\"
href=\"https://github.com/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb\"><img
src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"
/>View source on GitHub</a>\n",
+ " </td>\n",
+ "</table>\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "This notebook demonstrates the use of the RunInference transform for
[TensorFlow](https://www.tensorflow.org/).\n",
+ "Beam
[RunInference](https://beam.apache.org/releases/pydoc/current/apache_beam.ml.inference.base.html#apache_beam.ml.inference.base.RunInference)
accepts a Tensorflow Model Handler either
[TFModelHandlerNumpy](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L91)
or
[TFModelHandlerTensor](https://github.com/apache/beam/blob/ca0787642a6b3804a742326147281c99ae8d08d2/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L184)
depending upon the type of the input.\n",
+ "\n",
+ "The Apache Beam RunInference transform is used to make predictions
for\n",
+ "a variety of machine learning models. For more information about the
RunInference API, see [Machine
Learning](https://beam.apache.org/documentation/sdks/python-machine-learning)
in the Apache Beam documentation.\n",
+ "\n",
+ "This notebook demonstrates the following steps:\n",
+ "- Build a simple TensorFlow model.\n",
+ "- Set up example data.\n",
+ "- Run those examples and get a prediction inside an Apache Beam
pipeline.\n",
+ "- Run an inference pipeline by using a trained model from tensorflow
hub."
+ ],
+ "metadata": {
+ "id": "HrCtxslBGK8Z"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Before you begin\n",
+ "Complete the following setup steps.\n",
+ "\n",
+ "First, import `tensorflow`."
+ ],
+ "metadata": {
+ "id": "HrCtxslBGK8A"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "jBakpNZnAhqk"
+ },
+ "source": [
+ "!pip install tensorflow\n",
+ "!pip install apache_beam==2.46.0"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "To use RunInference with Tensorflow Model Handler, install Apache
Beam version 2.46 or later."
+ ],
+ "metadata": {
+ "id": "gVCtGOKTHMm4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "8hHP9wu98Ld4",
+ "outputId": "98d15e75-5c57-4e70-f80a-2f21d3717a17"
+ },
+ "source": [
+ "!pip freeze | grep beam"
+ ],
+ "execution_count": 3,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "apache-beam==2.46.0\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Authenticate with Google Cloud\n",
+ "This notebook relies on saving your model to Google Cloud. To use
your Google Cloud account, authenticate this notebook."
+ ],
+ "metadata": {
+ "id": "X80jy3FqHjK4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Kz9sccyGBqz3"
+ },
+ "source": [
+ "from google.colab import auth\n",
+ "auth.authenticate_user()"
+ ],
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Import dependencies and set up your bucket\n",
+ "Replace `PROJECT_ID` and `BUCKET_NAME` with the ID of your project
and the name of your bucket.\n",
+ "\n",
+ "**Important**: If an error occurs, restart your runtime."
+ ],
+ "metadata": {
+ "id": "40qtP6zJuMXm"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "eEle839_Akqx"
+ },
+ "source": [
+ "import argparse\n",
+ "\n",
+ "import tensorflow as tf\n",
+ "from tensorflow import keras\n",
+ "\n",
+ "import numpy\n",
+ "\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.ml.inference.base import RunInference\n",
+ "from apache_beam.options.pipeline_options import PipelineOptions\n",
+ "\n",
+ "project = \"PROJECT_ID\"\n",
+ "bucket = \"BUCKET_NAME\"\n",
+ "\n",
+ "save_model_dir_multiply =
f'gs://{bucket}/tfx-inference/model/multiply_five/v1/'\n"
+ ],
+ "execution_count": 4,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Create and test a simple model\n",
+ "\n",
+ "This step creates and tests a model that predicts the 5 times table."
+ ],
+ "metadata": {
+ "id": "YzvZWEv-1oiK"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "JiCniG0Ye2Wu"
+ },
+ "source": [
+ "### Create the model\n",
+ "Create training data and build a linear regression model."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "SH7iq3zeBBJ-",
+ "outputId": "e90e3b28-b428-40f0-c3b1-2c0c92c48864"
+ },
+ "source": [
+ "# Create training data that represents the 5 times multiplication
table for the numbers 0 to 99.\n",
+ "# x is the data and y is the labels.\n",
+ "x = numpy.arange(0, 100) # Examples\n",
+ "y = x * 5 # Labels\n",
+ "\n",
+ "# Build a simple linear regression model.\n",
+ "# Note that the model has a shape of (1) for its input layer and
expects a single int64 value.\n",
+ "input_layer = keras.layers.Input(shape=(1), dtype=tf.float32,
name='x')\n",
+ "output_layer= keras.layers.Dense(1)(input_layer)\n",
+ "\n",
+ "model = keras.Model(input_layer, output_layer)\n",
+ "model.compile(optimizer=tf.optimizers.Adam(),
loss='mean_absolute_error')\n",
+ "model.summary()"
+ ],
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Model: \"model\"\n",
+
"_________________________________________________________________\n",
+ " Layer (type) Output Shape Param #
\n",
+
"=================================================================\n",
+ " x (InputLayer) [(None, 1)] 0
\n",
+ "
\n",
+ " dense (Dense) (None, 1) 2
\n",
+ "
\n",
+
"=================================================================\n",
+ "Total params: 2\n",
+ "Trainable params: 2\n",
+ "Non-trainable params: 0\n",
+
"_________________________________________________________________\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Test the model\n",
+ "\n",
+ "This step tests the model that you created."
+ ],
+ "metadata": {
+ "id": "O_a0-4Gb19cy"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "5XkIYXhJBFmS",
+ "outputId": "f1c03b9f-afac-4a3b-cf4b-f77b79320a80"
+ },
+ "source": [
+ "model.fit(x, y, epochs=500, verbose=0)\n",
+ "test_examples =[20, 40, 60, 90]\n",
+ "value_to_predict = numpy.array(test_examples, dtype=numpy.float32)\n",
+ "predictions = model.predict(value_to_predict)\n",
+ "\n",
+ "print('Test Examples ' + str(test_examples))\n",
+ "print('Predictions ' + str(predictions))"
+ ],
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1/1 [==============================] - 0s 62ms/step\n",
+ "Test Examples [20, 40, 60, 90]\n",
+ "Predictions [[ 24.687366]\n",
+ " [ 47.377365]\n",
+ " [ 70.06737 ]\n",
+ " [104.10237 ]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Save the model\n",
+ "\n",
+ "This step shows how to save your model."
+ ],
+ "metadata": {
+ "id": "r4dpR6dQ4JwX"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "model.save(save_model_dir_multiply)"
+ ],
+ "metadata": {
+ "id": "7yVTY-hOhsgI"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Run the Pipeline\n",
+ "Use the following code to run the pipeline."
+ ],
+ "metadata": {
+ "id": "P2UMmbNW4YQV"
+ }
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "PzjmXM_KvqHY",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "e344cb7a-06a9-4c60-c1bd-11117141a550"
+ },
+ "source": [
+ "from apache_beam.ml.inference.tensorflow_inference import
TFModelHandlerNumpy\n",
+ "import apache_beam as beam\n",
+ "\n",
+ "class FormatOutput(beam.DoFn):\n",
+ " def process(self, element, *args, **kwargs):\n",
+ " yield \"example is {example} prediction is
{prediction}\".format(example=element.example, prediction=element.inference)\n",
+ "\n",
+ "\n",
+ "examples = numpy.array([20, 40, 60, 90], dtype=numpy.float32)\n",
+ "model_handler = TFModelHandlerNumpy(save_model_dir_multiply)\n",
+ "with beam.Pipeline() as p:\n",
+ " _ = (p | beam.Create(examples)\n",
+ " | RunInference(model_handler)\n",
+ " | beam.ParDo(FormatOutput())\n",
+ " | beam.Map(print)\n",
+ " )"
+ ],
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "example is 20.0 prediction is [24.687366]\n",
+ "example is 40.0 prediction is [47.377365]\n",
+ "example is 60.0 prediction is [70.06737]\n",
+ "example is 90.0 prediction is [104.10237]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## KeyedModelHandler with TensorFlow\n",
+ "\n",
+ "By default, the `ModelHandler` does not expect a key.\n",
+ "\n",
+ "* If you know that keys are associated with your examples, wrap the
model handler with `beam.KeyedModelHandler`.\n",
+ "* If you don't know whether keys are associated with your examples,
use `beam.MaybeKeyedModelHandler`."
+ ],
+ "metadata": {
+ "id": "IXikjkGdHm9n"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from apache_beam.ml.inference.base import KeyedModelHandler\n",
+ "from google.protobuf import text_format\n",
+ "import tensorflow as tf\n",
+ "\n",
+ "class FormatOutputKeyed(FormatOutput):\n",
+ " # To simplify, inherit from FormatOutput.\n",
+ " def process(self, tuple_in: Tuple):\n",
+ " key, element = tuple_in\n",
+ " output = super().process(element)\n",
+ " yield \"{} : {}\".format(key, output)\n",
+ "\n",
+ "examples = numpy.array([(1,20), (2,40), (3,60), (4,90)],
dtype=numpy.float32)\n",
+ "keyed_model_handler =
KeyedModelHandler(TFModelHandlerNumpy(save_model_dir_multiply))\n",
+ "with beam.Pipeline() as p:\n",
+ " _ = (p | 'CreateExamples' >> beam.Create(examples)\n",
+ " | RunInference(keyed_model_handler)\n",
+ " | beam.ParDo(FormatOutputKeyed())\n",
+ " | beam.Map(print)\n",
+ " )"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "KPtE3fmdJQry",
+ "outputId": "e1a4cb24-458c-4cd1-8f58-5e72351a71f6"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "1.0 : example is 20.0 prediction is [24.687366]\n",
+ "2.0 : example is 40.0 prediction is [47.377365]\n",
+ "3.0 : example is 60.0 prediction is [70.06737]\n",
+ "4.0 : example is 90.0 prediction is [104.10237]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## RunInference with Tensorflow Hub\n",
+ "\n",
+ "To use tensorflow hub's trained model URL, pass it to the `model_uri`
field of TFModelHandler class."
+ ],
+ "metadata": {
+ "id": "dVLTtFxDuJT_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import tensorflow_hub as hub"
+ ],
+ "metadata": {
+ "id": "H4-ZvkcTv7MO"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# URL of the trained model from tensorflow hub\n",
+ "CLASSIFIER_URL
=\"https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4\""
+ ],
+ "metadata": {
+ "id": "n3M6FNaUwBbl"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import numpy as np\n",
+ "import PIL.Image as Image\n",
+ "\n",
+ "IMAGE_RES = 224\n",
+ "img =
tf.keras.utils.get_file(origin='https://images-na.ssl-images-amazon.com/images/G/01/img15/pet-products/small-tiles/23695_pets_vertical_store_dogs_small_tile_8._CB312176604_.jpg')\n",
Review Comment:
Do you know how this image is licensed? Generally, we try to use images that
are available under creative commons licensing since that makes licensing
easier, we should also call that out here. See
https://github.com/apache/beam/blob/master/examples/notebooks/beam-ml/run_inference_multi_model.ipynb
for example
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]