You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "riteshghorse (via GitHub)" <gi...@apache.org> on 2023/03/22 16:01:23 UTC

[GitHub] [beam] riteshghorse opened a new pull request, #25933: [Python] TF with TFHub Notebook Example

riteshghorse opened a new pull request, #25933:
URL: https://github.com/apache/beam/pull/25933

   This PR adds an example notebook for using tensorflow model handler with Tensorflow Hub.
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] riteshghorse commented on a diff in pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "riteshghorse (via GitHub)" <gi...@apache.org>.
riteshghorse commented on code in PR #25933:
URL: https://github.com/apache/beam/pull/25933#discussion_r1145278748


##########
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:
   Changed to use the same image used in this notebook.



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damccorm commented on a diff in pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "damccorm (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damccorm commented on a diff in pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #25933:
URL: https://github.com/apache/beam/pull/25933#discussion_r1145298847


##########
examples/notebooks/beam-ml/run_inference_tensorflow_with_tensorflowhub.ipynb:
##########
@@ -0,0 +1,693 @@
+{
+  "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": 1,
+      "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 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/). 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",
+        "\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.\n",
+        "\n",
+        "**Note:** Image used for prediction is licensed in CC-BY, creator in listed in the [LICENSE.txt](https://storage.googleapis.com/apache-beam-samples/image_captioning/LICENSE.txt) file."
+      ],
+      "metadata": {
+        "id": "HrCtxslBGK8Z"
+      }
+    },
+    {
+      "cell_type": "markdown",
+      "source": [
+        "## Before you begin\n",
+        "Complete the following setup steps.\n",
+        "\n",
+        "First, import `tensorflow`.\n",
+        "\n",
+        "To use RunInference with Tensorflow Model Handler, install Apache Beam version 2.46 or later."
+      ],
+      "metadata": {
+        "id": "HrCtxslBGK8A"
+      }
+    },
+    {
+      "cell_type": "code",
+      "metadata": {
+        "id": "jBakpNZnAhqk",
+        "colab": {
+          "base_uri": "https://localhost:8080/"
+        },
+        "outputId": "014a2ccf-5180-40ba-e8ff-358f7a8d5e7d"
+      },
+      "source": [
+        "!pip install tensorflow\n",
+        "!pip install apache_beam==2.46.0"
+      ],
+      "execution_count": 2,
+      "outputs": [
+        {
+          "output_type": "stream",
+          "name": "stdout",
+          "text": [
+            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
+            "Requirement already satisfied: tensorflow in /usr/local/lib/python3.9/dist-packages (2.11.0)\n",
+            "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (3.3.0)\n",
+            "Requirement already satisfied: flatbuffers>=2.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (23.3.3)\n",
+            "Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (15.0.6.1)\n",
+            "Requirement already satisfied: protobuf<3.20,>=3.9.2 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (3.19.6)\n",
+            "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.51.3)\n",
+            "Requirement already satisfied: packaging in /usr/local/lib/python3.9/dist-packages (from tensorflow) (23.0)\n",
+            "Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.4.0)\n",
+            "Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.15.0)\n",
+            "Requirement already satisfied: tensorflow-estimator<2.12,>=2.11.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (2.11.0)\n",
+            "Requirement already satisfied: tensorboard<2.12,>=2.11 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (2.11.2)\n",
+            "Requirement already satisfied: numpy>=1.20 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.22.4)\n",
+            "Requirement already satisfied: gast<=0.4.0,>=0.2.1 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (0.4.0)\n",
+            "Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (0.2.0)\n",
+            "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (0.31.0)\n",
+            "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (2.2.0)\n",
+            "Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (4.5.0)\n",
+            "Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.6.3)\n",
+            "Requirement already satisfied: setuptools in /usr/local/lib/python3.9/dist-packages (from tensorflow) (67.6.0)\n",
+            "Requirement already satisfied: keras<2.12,>=2.11.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (2.11.0)\n",
+            "Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (3.8.0)\n",
+            "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow) (1.16.0)\n",
+            "Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.9/dist-packages (from astunparse>=1.6.0->tensorflow) (0.40.0)\n",
+            "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (3.4.1)\n",
+            "Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (2.2.3)\n",
+            "Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (2.27.1)\n",
+            "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (0.6.1)\n",
+            "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (1.8.1)\n",
+            "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (0.4.6)\n",
+            "Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.12,>=2.11->tensorflow) (2.16.2)\n",
+            "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow) (4.9)\n",
+            "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow) (5.3.0)\n",
+            "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow) (0.2.8)\n",
+            "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.9/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow) (1.3.1)\n",
+            "Requirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.9/dist-packages (from markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow) (6.1.0)\n",
+            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow) (2.0.12)\n",
+            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow) (3.4)\n",
+            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow) (1.26.15)\n",
+            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow) (2022.12.7)\n",
+            "Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.9/dist-packages (from werkzeug>=1.0.1->tensorboard<2.12,>=2.11->tensorflow) (2.1.2)\n",
+            "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.9/dist-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow) (3.15.0)\n",
+            "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.9/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow) (0.4.8)\n",
+            "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.9/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow) (3.2.2)\n",
+            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
+            "Collecting apache_beam==2.46.0\n",
+            "  Downloading apache_beam-2.46.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.5 MB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.5/14.5 MB\u001b[0m \u001b[31m24.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25hRequirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (1.51.3)\n",
+            "Collecting objsize<0.7.0,>=0.6.1\n",
+            "  Downloading objsize-0.6.1-py3-none-any.whl (9.3 kB)\n",
+            "Requirement already satisfied: pyarrow<10.0.0,>=3.0.0 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (9.0.0)\n",
+            "Collecting orjson<4.0\n",
+            "  Downloading orjson-3.8.8-cp39-cp39-manylinux_2_28_x86_64.whl (143 kB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.5/143.5 KB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25hCollecting fastavro<2,>=0.23.6\n",
+            "  Downloading fastavro-1.7.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.7/2.7 MB\u001b[0m \u001b[31m15.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25hCollecting crcmod<2.0,>=1.7\n",
+            "  Downloading crcmod-1.7.tar.gz (89 kB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m89.7/89.7 KB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (2.2.1)\n",
+            "Collecting pymongo<4.0.0,>=3.8.0\n",
+            "  Downloading pymongo-3.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (515 kB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m515.5/515.5 KB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25hRequirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (2.8.2)\n",
+            "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (2.27.1)\n",
+            "Collecting fasteners<1.0,>=0.3\n",
+            "  Downloading fasteners-0.18-py3-none-any.whl (18 kB)\n",
+            "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (2022.7.1)\n",
+            "Requirement already satisfied: protobuf<4,>3.12.2 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (3.19.6)\n",
+            "Collecting dill<0.3.2,>=0.3.1.1\n",
+            "  Downloading dill-0.3.1.1.tar.gz (151 kB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m152.0/152.0 KB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "Collecting hdfs<3.0.0,>=2.1.0\n",
+            "  Downloading hdfs-2.7.0-py3-none-any.whl (34 kB)\n",
+            "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (4.5.0)\n",
+            "Requirement already satisfied: httplib2<0.22.0,>=0.8 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (0.21.0)\n",
+            "Collecting zstandard<1,>=0.18.0\n",
+            "  Downloading zstandard-0.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB)\n",
+            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.6/2.6 MB\u001b[0m \u001b[31m43.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
+            "\u001b[?25hRequirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (1.22.4)\n",
+            "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (1.22.2)\n",
+            "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (1.4.2)\n",
+            "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.9/dist-packages (from apache_beam==2.46.0) (2022.10.31)\n",
+            "Collecting docopt\n",
+            "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
+            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.9/dist-packages (from hdfs<3.0.0,>=2.1.0->apache_beam==2.46.0) (1.16.0)\n",
+            "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.9/dist-packages (from httplib2<0.22.0,>=0.8->apache_beam==2.46.0) (3.0.9)\n",
+            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests<3.0.0,>=2.24.0->apache_beam==2.46.0) (3.4)\n",
+            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests<3.0.0,>=2.24.0->apache_beam==2.46.0) (2022.12.7)\n",
+            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests<3.0.0,>=2.24.0->apache_beam==2.46.0) (2.0.12)\n",
+            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests<3.0.0,>=2.24.0->apache_beam==2.46.0) (1.26.15)\n",
+            "Building wheels for collected packages: crcmod, dill, docopt\n",
+            "  Building wheel for crcmod (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "  Created wheel for crcmod: filename=crcmod-1.7-cp39-cp39-linux_x86_64.whl size=36916 sha256=aa40fd93320e110f397ba0630711ab173fca93b44babcef50c797f13f9f120a2\n",
+            "  Stored in directory: /root/.cache/pip/wheels/4a/6c/a6/ffdd136310039bf226f2707a9a8e6857be7d70a3fc061f6b36\n",
+            "  Building wheel for dill (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "  Created wheel for dill: filename=dill-0.3.1.1-py3-none-any.whl size=78545 sha256=aea972f29e1a09d4820d06619ea30fd946c2acb65f23ade451b73291c55d51c3\n",
+            "  Stored in directory: /root/.cache/pip/wheels/4f/0b/ce/75d96dd714b15e51cb66db631183ea3844e0c4a6d19741a149\n",
+            "  Building wheel for docopt (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
+            "  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13721 sha256=99281120e89574df1c1a6e486487563f4325793119b163fa90cb3c93216dcf6e\n",
+            "  Stored in directory: /root/.cache/pip/wheels/70/4a/46/1309fc853b8d395e60bafaf1b6df7845bdd82c95fd59dd8d2b\n",
+            "Successfully built crcmod dill docopt\n",
+            "Installing collected packages: docopt, crcmod, zstandard, pymongo, orjson, objsize, fasteners, fastavro, dill, hdfs, apache_beam\n",
+            "Successfully installed apache_beam-2.46.0 crcmod-1.7 dill-0.3.1.1 docopt-0.6.2 fastavro-1.7.3 fasteners-0.18 hdfs-2.7.0 objsize-0.6.1 orjson-3.8.8 pymongo-3.13.0 zstandard-0.20.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": 4,
+      "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 = \"google.com:clouddfe\"\n",
+        "bucket = \"clouddfe-riteshghorse\"\n",
+        "\n",
+        "save_model_dir_multiply = f'gs://{bucket}/tfx-inference/model/multiply_five/v1/'\n"
+      ],
+      "execution_count": 5,
+      "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": "175dc68d-6147-464f-95d9-e7e4c91ff403"
+      },
+      "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": 6,
+      "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": "5444b1ab-7198-4798-abb3-62dd7e848c26"
+      },
+      "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": 7,
+      "outputs": [
+        {
+          "output_type": "stream",
+          "name": "stdout",
+          "text": [
+            "1/1 [==============================] - 0s 118ms/step\n",
+            "Test Examples [20, 40, 60, 90]\n",
+            "Predictions [[ 42.615337]\n",
+            " [ 83.2313  ]\n",
+            " [123.84727 ]\n",
+            " [184.77121 ]]\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"
+      },
+      "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": []
+    },
+    {
+      "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",
+        "from typing import Tuple\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": {
+        "id": "KPtE3fmdJQry"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "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."

Review Comment:
   ```suggestion
           "To use TensorFlow Hub's trained model URL, pass it to the `model_uri` field of TFModelHandler class."
   ```



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] riteshghorse merged pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "riteshghorse (via GitHub)" <gi...@apache.org>.
riteshghorse merged PR #25933:
URL: https://github.com/apache/beam/pull/25933


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25933:
URL: https://github.com/apache/beam/pull/25933#issuecomment-1479939343

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] riteshghorse commented on pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "riteshghorse (via GitHub)" <gi...@apache.org>.
riteshghorse commented on PR #25933:
URL: https://github.com/apache/beam/pull/25933#issuecomment-1479937115

   R: @damccorm 


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] riteshghorse commented on pull request #25933: [Python] TF with TFHub Notebook Example

Posted by "riteshghorse (via GitHub)" <gi...@apache.org>.
riteshghorse commented on PR #25933:
URL: https://github.com/apache/beam/pull/25933#issuecomment-1481181821

   Addressed the comments, merging!


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org