diff --git a/docs/tutorials/text_classification_with_tf_hub.ipynb b/docs/tutorials/text_classification_with_tf_hub.ipynb index ebbb538d7..c5a9381b9 100644 --- a/docs/tutorials/text_classification_with_tf_hub.ipynb +++ b/docs/tutorials/text_classification_with_tf_hub.ipynb @@ -46,16 +46,30 @@ "id": "ok9PfyoQ2rH_" }, "source": [ - "# How to build a simple text classifier with TF-Hub\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# How to build a simple text classifier with TF-Hub\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -96,7 +110,7 @@ "id": "xOATihhH1IxS" }, "source": [ - "## Preparing the environment" + "## Setup" ] }, { @@ -111,8 +125,6 @@ "outputs": [], "source": [ "# Install TF-Hub.\n", - "!pip install tensorflow==2.1.0\n", - "!pip install tensorflow-hub\n", "!pip install seaborn" ] }, @@ -557,7 +569,9 @@ "N6ZDpd9XzFeN" ], "name": "Text classification with TF-Hub", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/action_recognition_with_tf_hub.ipynb b/examples/colab/action_recognition_with_tf_hub.ipynb index 816dc65a3..e75b526fd 100644 --- a/examples/colab/action_recognition_with_tf_hub.ipynb +++ b/examples/colab/action_recognition_with_tf_hub.ipynb @@ -45,16 +45,30 @@ "id": "cDq0CIKc1vO_" }, "source": [ - "# TF-Hub Action Recognition Model\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/action_recognition_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/action_recognition_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e" + "# TF-Hub Action Recognition Model\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/action_recognition_with_tf_hub\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/action_recognition_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/action_recognition_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/action_recognition_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -94,7 +108,7 @@ "id": "R_0xc2jyNGRp" }, "source": [ - "# Setting up the environment" + "## Setup" ] }, { @@ -107,7 +121,8 @@ }, "outputs": [], "source": [ - "!pip install -q imageio" + "!pip install -q imageio\n", + "!pip install python-opencv" ] }, { @@ -123,15 +138,9 @@ "source": [ "#@title Import the necessary modules\n", "# TensorFlow and TF-Hub modules.\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "from absl import logging\n", "\n", - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", "import tensorflow_hub as hub\n", "\n", "logging.set_verbosity(logging.ERROR)\n", @@ -330,11 +339,12 @@ "accelerator": "GPU", "colab": { "collapsed_sections": [ - "x8Q7Un821X1A", - "R_0xc2jyNGRp" + "x8Q7Un821X1A" ], "name": "Action Recognition on the UCF101 Dataset", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/bangla_article_classifier.ipynb b/examples/colab/bangla_article_classifier.ipynb index b5eb89a94..abfc13557 100644 --- a/examples/colab/bangla_article_classifier.ipynb +++ b/examples/colab/bangla_article_classifier.ipynb @@ -54,35 +54,45 @@ "cell_type": "markdown", "metadata": { "colab_type": "text", - "id": "GhN2WtIrBQ4y" + "id": "MfBg1C5NB3X0" }, "source": [ - "This colab is a demonstration of using [Tensorflow Hub](https://www.tensorflow.org/hub/) for text classification in non-English/local languages. Here we choose [Bangla](https://en.wikipedia.org/wiki/Bengali_language) as the local language and use pretrained word embeddings to solve a multiclass classification task where we classify Bangla news articles in 5 categories. The pretrained embeddings for Bangla comes from [fastText](https://fasttext.cc/docs/en/crawl-vectors.html) which is a library by Facebook with released pretrained word vectors for 157 languages. \n", - "\n", - "We'll use TF-Hub's pretrained embedding exporter for converting the word embeddings to a text embedding module first and then use the module to train a classifier with [tf.keras](https://www.tensorflow.org/api_docs/python/tf/keras), Tensorflow's high level user friendly API to build deep learning models. Even if we are using fastText embeddings here, it's possible to export any other embeddings pretrained from other tasks and quickly get results with Tensorflow hub. " + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/bangla_article_classifier\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/bangla_article_classifier.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/bangla_article_classifier.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/bangla_article_classifier.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", - "id": "Q4DN769E2O_R" + "id": "GhN2WtIrBQ4y" }, "source": [ - "# Prepare Environment" + "This colab is a demonstration of using [Tensorflow Hub](https://www.tensorflow.org/hub/) for text classification in non-English/local languages. Here we choose [Bangla](https://en.wikipedia.org/wiki/Bengali_language) as the local language and use pretrained word embeddings to solve a multiclass classification task where we classify Bangla news articles in 5 categories. The pretrained embeddings for Bangla comes from [fastText](https://fasttext.cc/docs/en/crawl-vectors.html) which is a library by Facebook with released pretrained word vectors for 157 languages. \n", + "\n", + "We'll use TF-Hub's pretrained embedding exporter for converting the word embeddings to a text embedding module first and then use the module to train a classifier with [tf.keras](https://www.tensorflow.org/api_docs/python/tf/keras), Tensorflow's high level user friendly API to build deep learning models. Even if we are using fastText embeddings here, it's possible to export any other embeddings pretrained from other tasks and quickly get results with Tensorflow hub. " ] }, { - "cell_type": "code", - "execution_count": 0, + "cell_type": "markdown", "metadata": { - "colab": {}, - "colab_type": "code", - "id": "zA07b51AGF5l" + "colab_type": "text", + "id": "Q4DN769E2O_R" }, - "outputs": [], "source": [ - "!pip install -q tensorflow-gpu==2.0.0-beta1" + "## Setup" ] }, { @@ -426,8 +436,9 @@ " model.add(embedding_layer)\n", " model.add(tf.keras.layers.Dense(64, activation=\"relu\"))\n", " model.add(tf.keras.layers.Dense(16, activation=\"relu\"))\n", - " model.add(tf.keras.layers.Dense(5, activation=\"softmax\"))\n", - " model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=['accuracy'])\n", + " model.add(tf.keras.layers.Dense(5))\n", + " model.compile(loss=tf.losses.CategoricalCrossentropy(from_logits=True),\n", + " optimizer=\"adam\", metrics=['accuracy'])\n", " return model" ] }, @@ -663,8 +674,9 @@ "colab": { "collapsed_sections": [], "name": "Bangla article classifier.ipynb", + "private_outputs": true, "provenance": [], - "version": "0.3.2" + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/bigbigan_with_tf_hub.ipynb b/examples/colab/bigbigan_with_tf_hub.ipynb index e32b95363..e58e1cec6 100644 --- a/examples/colab/bigbigan_with_tf_hub.ipynb +++ b/examples/colab/bigbigan_with_tf_hub.ipynb @@ -45,8 +45,39 @@ "id": "-1NTVIH6ABK-" }, "source": [ - "# BigBiGAN Demo\n", - "\n", + "# BigBiGAN Demo\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/bigbigan_with_tf_hub\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/bigbigan_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/bigbigan_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/bigbigan_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "AVvOoEhswyZg" + }, + "source": [ "This notebook is a demo for the *BigBiGAN* models available on [TF Hub](https://tfhub.dev/s?publisher=deepmind\u0026q=bigbigan).\n", "\n", "BigBiGAN extends standard (Big)GANs by adding an *encoder* module which can be used for unsupervised representation learning. Roughly speaking, the encoder inverts the generator by predicting latents `z` given real data `x`. See the [BigBiGAN paper on arXiv](https://arxiv.org/abs/1907.02544) [1] for more information about these models.\n", @@ -94,7 +125,7 @@ "id": "Lr01cszC_vcC" }, "source": [ - "# Setup" + "## Setup" ] }, { @@ -107,19 +138,16 @@ }, "outputs": [], "source": [ - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "import io\n", "import IPython.display\n", "import PIL.Image\n", "from pprint import pformat\n", "\n", "import numpy as np\n", - "import tensorflow as tf\n", + "\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub" ] }, @@ -716,11 +744,8 @@ "collapsed_sections": [ "9v58CTfl8jTc" ], - "last_runtime": { - "build_target": "", - "kind": "local" - }, "name": "BigBiGAN TF Hub Demo", + "private_outputs": true, "provenance": [], "toc_visible": true }, diff --git a/examples/colab/biggan_generation_with_tf_hub.ipynb b/examples/colab/biggan_generation_with_tf_hub.ipynb index ac5616edd..94d221fc7 100644 --- a/examples/colab/biggan_generation_with_tf_hub.ipynb +++ b/examples/colab/biggan_generation_with_tf_hub.ipynb @@ -45,8 +45,39 @@ "id": "Cd1dhL4Ykbm7" }, "source": [ - "# BigGAN Demo\n", - "\n", + "# BigGAN Demo\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/biggan_generation_with_tf_hub\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/biggan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/biggan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/biggan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "-1NTVIH6ABK-" + }, + "source": [ "This notebook is a demo for the *BigGAN* image generators available on [TF Hub](https://tfhub.dev/s?publisher=deepmind\u0026q=biggan).\n", "\n", "See the [BigGAN paper on arXiv](https://arxiv.org/abs/1809.11096) [1] for more information about these models.\n", @@ -103,7 +134,7 @@ "id": "JJrTM6hAi0CJ" }, "source": [ - "# Setup" + "## Setup" ] }, { @@ -116,11 +147,8 @@ }, "outputs": [], "source": [ - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", "\n", "import io\n", "import IPython.display\n", @@ -402,10 +430,10 @@ "accelerator": "GPU", "colab": { "collapsed_sections": [ - "pLOYL1PJAAtK", - "JJrTM6hAi0CJ" + "pLOYL1PJAAtK" ], "name": "BigGAN TF Hub Demo", + "private_outputs": true, "provenance": [], "toc_visible": true }, diff --git a/examples/colab/cord_19_embeddings.ipynb b/examples/colab/cord_19_embeddings.ipynb index a37aad5db..287b16e27 100644 --- a/examples/colab/cord_19_embeddings.ipynb +++ b/examples/colab/cord_19_embeddings.ipynb @@ -45,35 +45,59 @@ "id": "ORy-KvWXGXBo" }, "source": [ - "# Exploring the TF-Hub CORD-19 Swivel Embeddings\n", - "\n", - "The CORD-19 Swivel text embedding module from TF-Hub (https://tfhub.dev/tensorflow/cord-19/swivel-128d/1)\n", - " was built to support researchers analyzing natural languages text related to COVID-19.\n", - "These embeddings were trained on the titles, authors, abstracts, body texts, and\n", - "reference titles of articles in the [CORD-19 dataset](https://pages.semanticscholar.org/coronavirus-research).\n", - "\n", - "In this colab we will:\n", - "- Analyze semantically similar words in the embedding space\n", - "- Train a classifier on the SciCite dataset using the CORD-19 embeddings\n" + "# Exploring the TF-Hub CORD-19 Swivel Embeddings\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", - "id": "O4WKcsh8DH3H" + "id": "MfBg1C5NB3X0" }, "source": [ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/cord_19_embeddings\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/cord_19_embeddings.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/cord_19_embeddings.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/cord_19_embeddings.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "The CORD-19 Swivel text embedding module from TF-Hub (https://tfhub.dev/tensorflow/cord-19/swivel-128d/1)\n", + " was built to support researchers analyzing natural languages text related to COVID-19.\n", + "These embeddings were trained on the titles, authors, abstracts, body texts, and\n", + "reference titles of articles in the [CORD-19 dataset](https://pages.semanticscholar.org/coronavirus-research).\n", + "\n", + "In this colab we will:\n", + "- Analyze semantically similar words in the embedding space\n", + "- Train a classifier on the SciCite dataset using the CORD-19 embeddings\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "## Setup\n" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -530,6 +554,7 @@ "5wFF5JFyD2Ki" ], "name": "Exploring the TF-Hub CORD-19 Swivel Embeddings", + "private_outputs": true, "provenance": [], "toc_visible": true }, diff --git a/examples/colab/cord_19_embeddings_keras.ipynb b/examples/colab/cord_19_embeddings_keras.ipynb index 89bc7dfca..03ef7f433 100644 --- a/examples/colab/cord_19_embeddings_keras.ipynb +++ b/examples/colab/cord_19_embeddings_keras.ipynb @@ -45,35 +45,59 @@ "id": "ORy-KvWXGXBo" }, "source": [ - "# Exploring the TF-Hub CORD-19 Swivel Embeddings\n", - "\n", - "The CORD-19 Swivel text embedding module from TF-Hub (https://tfhub.dev/tensorflow/cord-19/swivel-128d/2)\n", - " was built to support researchers analyzing natural languages text related to COVID-19.\n", - "These embeddings were trained on the titles, authors, abstracts, body texts, and\n", - "reference titles of articles in the [CORD-19 dataset](https://pages.semanticscholar.org/coronavirus-research).\n", - "\n", - "In this colab we will:\n", - "- Analyze semantically similar words in the embedding space\n", - "- Train a classifier on the SciCite dataset using the CORD-19 embeddings\n" + "# Exploring the TF-Hub CORD-19 Swivel Embeddings\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", - "id": "O4WKcsh8DH3H" + "id": "MfBg1C5NB3X0" }, "source": [ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/cord_19_embeddings_keras\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/cord_19_embeddings_keras.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/cord_19_embeddings_keras.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/cord_19_embeddings_keras.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "The CORD-19 Swivel text embedding module from TF-Hub (https://tfhub.dev/tensorflow/cord-19/swivel-128d/2)\n", + " was built to support researchers analyzing natural languages text related to COVID-19.\n", + "These embeddings were trained on the titles, authors, abstracts, body texts, and\n", + "reference titles of articles in the [CORD-19 dataset](https://pages.semanticscholar.org/coronavirus-research).\n", + "\n", + "In this colab we will:\n", + "- Analyze semantically similar words in the embedding space\n", + "- Train a classifier on the SciCite dataset using the CORD-19 embeddings\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "## Setup\n" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -249,7 +273,7 @@ "\n", "model = tf.keras.Sequential()\n", "model.add(hub_layer)\n", - "model.add(tf.keras.layers.Dense(3, activation='softmax'))\n", + "model.add(tf.keras.layers.Dense(3))\n", "model.summary()\n", "model.compile(optimizer='adam',\n", " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", @@ -424,6 +448,7 @@ "5wFF5JFyD2Ki" ], "name": "Exploring the TF-Hub CORD-19 Swivel Embeddings", + "private_outputs": true, "provenance": [], "toc_visible": true }, diff --git a/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb b/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb index 496641474..5d53bdd9a 100644 --- a/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb +++ b/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb @@ -46,17 +46,30 @@ "id": "co7MV6sX7Xto" }, "source": [ - "# Cross-Lingual Similarity and Semantic Search Engine with Multilingual Universal Sentence Encoder\n", - "\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# Cross-Lingual Similarity and Semantic Search Engine with Multilingual Universal Sentence Encoder\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/cross_lingual_similarity_with_tf_hub_multilingual_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -97,21 +110,11 @@ "id": "pOTzp8O36CyQ" }, "source": [ - "# Getting Started\n", + "## Setup\n", "\n", "This section sets up the environment for access to the Multilingual Universal Sentence Encoder Module and also prepares a set of English sentences and their translations. In the following sections, the multilingual module will be used to compute similarity *across languages*." ] }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "63Pd3nJnTl-i" - }, - "source": [ - "**IMPORTANT**Note: Pleaseelect \"**Python 3**\" _and_ \"**GPU**\" in the ***Runtime-\u003eChange Runtime type*** dropdown menu above _before_ running this notebook for faster execution." - ] - }, { "cell_type": "code", "execution_count": 0, @@ -126,9 +129,7 @@ "%%capture\n", "#@title Setup Environment\n", "# Install the latest Tensorflow version.\n", - "!pip3 install tensorflow_text\n", - "!pip3 install --upgrade tensorflow-gpu\n", - "!pip install tensorflow-hub\n", + "!pip install tensorflow_text\n", "!pip install bokeh\n", "!pip install simpleneighbors\n", "!pip install tqdm" @@ -711,8 +712,7 @@ "1. Semantic-search capabilities: retrieving sentences from the corpus that are semantically similar to the given query.\n", "2. Multilingual capabilities: doing so in multiple languages when they query language and index language match\n", "3. Cross-lingual capabilities: issuing queries in a distinct language than the indexed corpus\n", - "4. Mixed-language corpus: all of the above on a single index containing entries from all languages\n", - "\n" + "4. Mixed-language corpus: all of the above on a single index containing entries from all languages\n" ] }, { @@ -728,8 +728,7 @@ "\n", "* Try a few different sample sentences\n", "* Try changing the number of returned results (they are returned in order of similarity)\n", - "* Try cross-lingual capabilities by returning results in different languages (might want to use [Google Translate](http://translate.google.com) on some results to your native language for sanity check)\n", - "\n" + "* Try cross-lingual capabilities by returning results in different languages (might want to use [Google Translate](http://translate.google.com) on some results to your native language for sanity check)\n" ] }, { @@ -873,8 +872,9 @@ "collapsed_sections": [], "machine_shape": "hm", "name": "Cross-Lingual Similarity and Semantic Search Engine with TF-Hub Multilingual Universal Encoder", + "private_outputs": true, "provenance": [], - "version": "0.3.2" + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/image_feature_vector.ipynb b/examples/colab/image_feature_vector.ipynb index 3fb79bb58..086a10aec 100644 --- a/examples/colab/image_feature_vector.ipynb +++ b/examples/colab/image_feature_vector.ipynb @@ -45,18 +45,29 @@ "id": "9Z_ZvMk5JPFV" }, "source": [ - "# Transfer Learning with TensorFlow\n", - "\n", - "\u003ctable align=\"left\"\u003e\n", - "\u003ctd align=\"center\"\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/image_feature_vector.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003e\u003cbr\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\n", - "\u003ctd align=\"center\"\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/image_feature_vector.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003e\u003cbr\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\n", + "# Transfer Learning with TensorFlow\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/image_feature_vector\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/image_feature_vector.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/image_feature_vector.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/image_feature_vector.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, @@ -71,13 +82,17 @@ "\n", "For classifying images, a particular type of *deep neural network*, called a *convolutional neural network* has proved to be particularly powerful. However, modern convolutional neural networks have millions of parameters. Training them from scratch requires a lot of labeled training data and a lot of computing power (hundreds of GPU-hours or more). We only have about three thousand labeled photos and want to spend much less time, so we need to be more clever.\n", "\n", - "We will use a technique called *transfer learning* where we take a pre-trained network (trained on about a million general images), use it to extract features, and train a new layer on top for our own task of classifying images of flowers.\n", - "\n", - "## Colab Setup\n", - "\n", - "Click on the \"Connect\" dropdown on the top right and click \"Connect to hosted runtime\"\n", - "\n", - "If you haven't worked with Colab before, then use `Ctrl+Shift+P` to show the command palette. The most important command is `Shift+Enter`, which runs the current cell and moves to the next one. Try it on the following cell with imports:" + "We will use a technique called *transfer learning* where we take a pre-trained network (trained on about a million general images), use it to extract features, and train a new layer on top for our own task of classifying images of flowers.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "## Setup\n" ] }, { @@ -90,12 +105,6 @@ }, "outputs": [], "source": [ - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "import collections\n", "import io\n", "import math\n", @@ -105,7 +114,9 @@ "\n", "from IPython.display import clear_output, Image, display, HTML\n", "\n", - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "\n", "import numpy as np\n", @@ -577,12 +588,10 @@ "collapsed_sections": [ "ScitaPqhKtuW" ], - "last_runtime": { - "build_target": "", - "kind": "local" - }, "name": "Transfer Learning with TensorFlow", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/object_detection.ipynb b/examples/colab/object_detection.ipynb index 1de2dfd5f..29db19759 100644 --- a/examples/colab/object_detection.ipynb +++ b/examples/colab/object_detection.ipynb @@ -1,23 +1,4 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "accelerator": "GPU", - "colab": { - "name": "Object detection", - "provenance": [], - "private_outputs": true, - "collapsed_sections": [ - "N6ZDpd9XzFeN", - "vlA3CftFpRiW" - ] - }, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - } - }, "cells": [ { "cell_type": "markdown", @@ -33,12 +14,14 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { "cellView": "both", + "colab": {}, "colab_type": "code", - "id": "KUu4vOt5zI9d", - "colab": {} + "id": "KUu4vOt5zI9d" }, + "outputs": [], "source": [ "# Copyright 2018 The TensorFlow Hub Authors. All Rights Reserved.\n", "#\n", @@ -54,9 +37,7 @@ "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "# ==============================================================================" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -65,16 +46,30 @@ "id": "CxmDMK4yupqg" }, "source": [ - "# Object detection\n", - "\n", - "
\n", - " \n", - " Run in Google Colab\n", - " \n", - "\n", - " \n", - " View source on GitHub\n", - "
\n" + "# Object detection\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/object_detection\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/object_detection.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -94,24 +89,22 @@ "id": "v4XGxDrCkeip" }, "source": [ - "## Imports and function definitions\n" + "## Setup\n" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { "cellView": "both", + "colab": {}, "colab_type": "code", - "id": "6cPY9Ou4sWs_", - "colab": {} + "id": "6cPY9Ou4sWs_" }, + "outputs": [], "source": [ "#@title Imports and function definitions\n", "\n", - "# Runs with stable version tensorflow 2.1.0.\n", - "\n", - "!pip install tensorflow==2.1.0\n", - "\n", "# For running inference on the TF-Hub module.\n", "import tensorflow as tf\n", "\n", @@ -139,9 +132,7 @@ "\n", "# Check available GPU devices.\n", "print(\"The following GPU devices are available: %s\" % tf.test.gpu_device_name())" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -167,11 +158,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "D9IwDpOtpIHW", - "colab": {} + "id": "D9IwDpOtpIHW" }, + "outputs": [], "source": [ "def display_image(image):\n", " fig = plt.figure(figsize=(20, 15))\n", @@ -221,7 +214,7 @@ " # Each display_str has a top and bottom margin of 0.05x.\n", " total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)\n", "\n", - " if top > total_display_str_height:\n", + " if top \u003e total_display_str_height:\n", " text_bottom = top\n", " else:\n", " text_bottom = top + total_display_str_height\n", @@ -251,7 +244,7 @@ " font = ImageFont.load_default()\n", "\n", " for i in range(min(boxes.shape[0], max_boxes)):\n", - " if scores[i] >= min_score:\n", + " if scores[i] \u003e= min_score:\n", " ymin, xmin, ymax, xmax = tuple(boxes[i])\n", " display_str = \"{}: {}%\".format(class_names[i].decode(\"ascii\"),\n", " int(100 * scores[i]))\n", @@ -268,9 +261,7 @@ " display_str_list=[display_str])\n", " np.copyto(image, np.array(image_pil))\n", " return image" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -286,18 +277,18 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { "cellView": "form", + "colab": {}, "colab_type": "code", - "id": "YLWNhjUY1mhg", - "colab": {} + "id": "YLWNhjUY1mhg" }, + "outputs": [], "source": [ "image_url = \"https://farm1.staticflickr.com/4032/4653948754_c0d768086b_o.jpg\" #@param\n", "downloaded_image_path = download_and_resize_image(image_url, 1280, 856, True)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -313,42 +304,44 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "uazJ5ASc2_QE", - "colab": {} + "id": "uazJ5ASc2_QE" }, + "outputs": [], "source": [ "module_handle = \"https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1\" #@param [\"https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1\", \"https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1\"]\n", "\n", "detector = hub.load(module_handle).signatures['default']" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "znW8Fq1EC0x7", - "colab": {} + "id": "znW8Fq1EC0x7" }, + "outputs": [], "source": [ "def load_img(path):\n", " img = tf.io.read_file(path)\n", " img = tf.image.decode_jpeg(img, channels=3)\n", " return img" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "kwGJV96WWBLH", - "colab": {} + "id": "kwGJV96WWBLH" }, + "outputs": [], "source": [ "def run_detector(detector, path):\n", " img = load_img(path)\n", @@ -368,22 +361,20 @@ " result[\"detection_class_entities\"], result[\"detection_scores\"])\n", "\n", " display_image(image_with_boxes)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "vchaUW1XDodD", - "colab": {} + "id": "vchaUW1XDodD" }, + "outputs": [], "source": [ "run_detector(detector, downloaded_image_path)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -398,11 +389,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { + "colab": {}, "colab_type": "code", - "id": "rubdr2JXfsa1", - "colab": {} + "id": "rubdr2JXfsa1" }, + "outputs": [], "source": [ "image_urls = [\"https://farm7.staticflickr.com/8092/8592917784_4759d3088b_o.jpg\",\n", " \"https://farm6.staticflickr.com/2598/4138342721_06f6e177f3_o.jpg\",\n", @@ -414,22 +407,27 @@ " run_detector(detector, image_path)\n", " end_time = time.time()\n", " print(\"Inference time:\",start_time-end_time)" + ] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "collapsed_sections": [ + "N6ZDpd9XzFeN", + "vlA3CftFpRiW" ], - "execution_count": 0, - "outputs": [] + "name": "Object detection", + "private_outputs": true, + "provenance": [], + "toc_visible": true }, - { - "cell_type": "code", - "metadata": { - "id": "RiAgzYQJ-yXe", - "colab_type": "code", - "colab": {} - }, - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" } - ] + }, + "nbformat": 4, + "nbformat_minor": 0 } diff --git a/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb b/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb index 99109affd..af4ffde71 100644 --- a/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb +++ b/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb @@ -46,15 +46,30 @@ "id": "0fO2R2BBKx3l" }, "source": [ - "# Multilingual Universal Sentence Encoder Q\u0026A model Retrieval Demo\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e" + "# Multilingual Universal Sentence Encoder Q\u0026A model Retrieval Demo\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/retrieval_with_tf_hub_universal_encoder_qa\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/retrieval_with_tf_hub_universal_encoder_qa.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -72,6 +87,16 @@ "to enable faster processing, select Hardware Accelerator \"**GPU**\". Estimated indexing time of the SQuAD train 2.0 dataset with ~94,000 sentences with GPU is about 3 mins." ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ORy-KvWXGXBo" + }, + "source": [ + "## Setup\n" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -86,11 +111,9 @@ "%%capture\n", "#@title Setup Environment\n", "# Install the latest Tensorflow version.\n", - "!pip3 install tensorflow_text\n", - "!pip3 install --upgrade tensorflow-gpu\n", - "!pip3 install tensorflow-hub\n", - "!pip3 install simpleneighbors\n", - "!pip3 install nltk" + "!pip install tensorflow_text\n", + "!pip install simpleneighbors\n", + "!pip install nltk" ] }, { @@ -252,7 +275,6 @@ "outputs": [], "source": [ "#@title Load model from tensorflow hub\n", - "%%time\n", "module_url = \"https://tfhub.dev/google/universal-sentence-encoder-multilingual-qa/3\" #@param [\"https://tfhub.dev/google/universal-sentence-encoder-multilingual-qa/3\", \"https://tfhub.dev/google/universal-sentence-encoder-qa/3\"]\n", "model = hub.load(module_url)\n" ] @@ -278,7 +300,6 @@ "outputs": [], "source": [ "#@title Compute embeddings and build simpleneighbors index\n", - "%%time\n", "from google.colab import output\n", "\n", "batch_size = 100\n", @@ -344,8 +365,10 @@ "collapsed_sections": [ "VFMCdVJIIraw" ], + "name": "Universal Encoder Q\u0026A Model Retrieval Demo", + "private_outputs": true, "provenance": [], - "name": "Universal Encoder Q\u0026A Model Retrieval Demo" + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/s3gan_generation_with_tf_hub.ipynb b/examples/colab/s3gan_generation_with_tf_hub.ipynb index 4624939b5..3e11fa383 100644 --- a/examples/colab/s3gan_generation_with_tf_hub.ipynb +++ b/examples/colab/s3gan_generation_with_tf_hub.ipynb @@ -45,8 +45,39 @@ "id": "AqBuuwrIxlGs" }, "source": [ - "# S3GAN Demo\n", - "\n", + "# S3GAN Demo\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/s3gan_generation_with_tf_hub\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/s3gan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/s3gan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/s3gan_generation_with_tf_hub.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "p5AWAusyySDA" + }, + "source": [ "This notebook is a demo of Generative Adversarial Networks trained on ImageNet with as little as 2.5% labeled data using self- and semi-supervised learning techniques. Both generator and discrimantor models are available on [TF Hub](https://tfhub.dev/s?publisher=google\u0026q=compare_gan).\n", "\n", "For more information about the models and the training procedure see our [blogpost](https://ai.googleblog.com/2019/03/reducing-need-for-labeled-data-in.html) and the [paper](https://arxiv.org/abs/1903.02271) [1]. \n", @@ -86,13 +117,6 @@ "outputs": [], "source": [ "# @title Imports and utility functions\n", - "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "import os\n", "\n", "import IPython\n", @@ -101,7 +125,10 @@ "import PIL.Image\n", "import pandas as pd\n", "import six\n", - "import tensorflow as tf\n", + "\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "\n", "def imgrid(imarray, cols=8, pad=1):\n", @@ -407,12 +434,10 @@ "collapsed_sections": [ "BhN1AplL0Hpv" ], - "last_runtime": { - "build_target": "", - "kind": "local" - }, "name": "S3GAN_Demo.ipynb", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/semantic_approximate_nearest_neighbors.ipynb b/examples/colab/semantic_approximate_nearest_neighbors.ipynb index 90627d3b7..09f03341e 100644 --- a/examples/colab/semantic_approximate_nearest_neighbors.ipynb +++ b/examples/colab/semantic_approximate_nearest_neighbors.ipynb @@ -45,8 +45,39 @@ "id": "9qOVy-_vmuUP" }, "source": [ - "# Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub\n", - "\n", + "# Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/semantic_approximate_nearest_neighbors\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "7Hks9F5qq6m2" + }, + "source": [ "This tutorial illustrates how to generate embeddings from a [TensorFlow Hub](https://tfhub.dev) (TF-Hub) module given input data, and build an approximate nearest neighbours (ANN) index using the extracted embeddings. The index can then be used for real-time similarity matching and retrieval. \n", "\n", "When dealing with a large corpus of data, it's not efficient to perform exact matching by scanning the whole repository to find the most similar items to a given query in real-time. Thus, we use an approximate similarity matching algorithm which allows us to trade off a little bit of accuracy in finding exact nearest neighbor matches for a significant boost in speed. \n", @@ -71,7 +102,7 @@ "id": "Q0jr0QK9qO5P" }, "source": [ - "## Getting Started" + "## Setup" ] }, { @@ -94,9 +125,7 @@ }, "outputs": [], "source": [ - "!pip install -q tensorflow==1.15\n", "!pip install -q tensorflow_transform\n", - "!pip install -q tensorflow_hub\n", "!pip install -q apache_beam\n", "!pip install -q sklearn\n", "!pip install -q annoy" @@ -124,17 +153,20 @@ "source": [ "import os\n", "import sys\n", + "import pathlib\n", "import pickle\n", "from collections import namedtuple\n", "from datetime import datetime\n", + "\n", "import numpy as np\n", "import apache_beam as beam\n", - "import tensorflow as tf\n", + "import annoy\n", + "from sklearn.random_projection import gaussian_random_matrix\n", + "\n", + "import tensorflow.compat.v1 as tf\n", "import tensorflow_transform as tft\n", "import tensorflow_hub as hub\n", - "import tensorflow_transform.beam as tft_beam\n", - "import annoy\n", - "from sklearn.random_projection import gaussian_random_matrix" + "import tensorflow_transform.beam as tft_beam" ] }, { @@ -507,7 +539,6 @@ "id": "On-MbzD922kb" }, "source": [ - "\n", "### Run pipeline" ] }, @@ -521,14 +552,19 @@ }, "outputs": [], "source": [ - "output_dir = '/embeds'\n", - "temporary_dir = '/temp'\n", - "original_dim = load_module(module_url)(['']).shape[1]\n", - "random_projection_matrix = None\n", + "import tempfile\n", + "\n", + "output_dir = pathlib.Path(tempfile.mkdtemp())\n", + "temporary_dir = pathlib.Path(tempfile.mkdtemp())\n", + "\n", + "g = tf.Graph()\n", + "with g.as_default():\n", + " original_dim = load_module(module_url)(['']).shape[1]\n", + " random_projection_matrix = None\n", "\n", - "if projected_dim:\n", - " random_projection_matrix = generate_random_projection_weights(\n", - " original_dim, projected_dim)\n", + " if projected_dim:\n", + " random_projection_matrix = generate_random_projection_weights(\n", + " original_dim, projected_dim)\n", "\n", "args = {\n", " 'job_name': 'hub2emb-{}'.format(datetime.utcnow().strftime('%y%m%d-%H%M%S')),\n", @@ -805,7 +841,9 @@ "source": [ "# Load the TF-Hub module\n", "print(\"Loading the TF-Hub module...\")\n", - "%time embed_fn = load_module(module_url)\n", + "g = tf.Graph()\n", + "with g.as_default():\n", + " embed_fn = load_module(module_url)\n", "print(\"TF-Hub module is loaded.\")\n", "\n", "random_projection_matrix = None\n", @@ -896,12 +934,13 @@ "SQ492LN7A-NZ" ], "name": "Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub [TF1]", + "private_outputs": true, "provenance": [], "toc_visible": true }, "kernelspec": { - "display_name": "Python 2", - "name": "python2" + "display_name": "Python 3", + "name": "python3" } }, "nbformat": 4, diff --git a/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb b/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb index c9435306c..5909034df 100644 --- a/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb +++ b/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb @@ -17,12 +17,7 @@ "execution_count": 0, "metadata": { "cellView": "code", - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "JMyTNwSJGGWg" }, @@ -51,17 +46,30 @@ "id": "co7MV6sX7Xto" }, "source": [ - "# Universal Sentence Encoder\n", - "\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# Universal Sentence Encoder\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/semantic_similarity_with_tf_hub_universal_encoder.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -83,7 +91,7 @@ "id": "pOTzp8O36CyQ" }, "source": [ - "# Getting Started\n", + "## Setup\n", "\n", "This section sets up the environment for access to the Universal Sentence Encoder on TF Hub and provides examples of applying the encoder to words, sentences, and paragraphs." ] @@ -92,22 +100,13 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "lVjNK8shFKOC" }, "outputs": [], "source": [ "%%capture\n", - "# Install the latest Tensorflow version.\n", - "!pip3 install --upgrade tensorflow-gpu\n", - "# Install TF-Hub.\n", - "!pip3 install tensorflow-hub\n", "!pip3 install seaborn" ] }, @@ -125,12 +124,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "zwty8Z6mAkdV" }, @@ -139,7 +133,9 @@ "#@title Load the Universal Sentence Encoder's TF Hub module\n", "from absl import logging\n", "\n", - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -203,12 +199,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "h1FFCTKm7ba4" }, @@ -247,12 +238,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "cPMCaxrZwp7t" }, @@ -309,12 +295,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "VOs8ZfOnJeBF" }, @@ -361,12 +342,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, + "colab": {}, "colab_type": "code", "id": "W-q2r7jyZGb7" }, @@ -400,11 +376,10 @@ "collapsed_sections": [ "RUymE2l9GZfO" ], - "default_view": {}, "name": "Semantic Similarity with TF-Hub Universal Encoder", + "private_outputs": true, "provenance": [], - "version": "0.3.2", - "views": {} + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb b/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb index d8fb0c79c..2aeff4b81 100644 --- a/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb +++ b/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb @@ -45,16 +45,30 @@ "id": "MlHqSdgSEwPE" }, "source": [ - "# Universal Sentence Encoder-Lite demo\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# Universal Sentence Encoder-Lite demo\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder_lite\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/semantic_similarity_with_tf_hub_universal_encoder_lite.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -86,7 +100,7 @@ "id": "rWeEjoO5M0Cx" }, "source": [ - "## Install required package for TF-Hub" + "## Setup" ] }, { @@ -117,15 +131,11 @@ }, "outputs": [], "source": [ - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "from absl import logging\n", "\n", - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "import sentencepiece as spm\n", "import matplotlib.pyplot as plt\n", @@ -554,7 +564,9 @@ "IJhWonqQN7u0" ], "name": "Semantic Similarity with TF-Hub Universal Encoder Lite", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/text_classification_with_tf_hub.ipynb b/examples/colab/text_classification_with_tf_hub.ipynb deleted file mode 100644 index b3229766f..000000000 --- a/examples/colab/text_classification_with_tf_hub.ipynb +++ /dev/null @@ -1,96 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "N6ZDpd9XzFeN" - }, - "source": [ - "##### Copyright 2018 The TensorFlow Hub Authors.\n", - "\n", - "Licensed under the Apache License, Version 2.0 (the \"License\");" - ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "cellView": "form", - "colab": { - "autoexec": { - "startup": false, - "wait_interval": 0 - } - }, - "colab_type": "code", - "id": "KUu4vOt5zI9d" - }, - "outputs": [], - "source": [ - "# Copyright 2018 The TensorFlow Hub Authors. All Rights Reserved.\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# 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, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License.\n", - "# ==============================================================================" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "ok9PfyoQ2rH_" - }, - "source": [ - "# How to build a simple text classifier with TF-Hub\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "OnEKneiGuZMS" - }, - "source": [ - "This tutorial has moved. Use the above links to access it at it's new location." - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [ - "N6ZDpd9XzFeN" - ], - "default_view": {}, - "name": "Text classification with TF-Hub", - "provenance": [], - "version": "0.3.2", - "views": {} - }, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb b/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb index 40d2773f1..c774877b5 100644 --- a/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb +++ b/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb @@ -46,17 +46,30 @@ "id": "ok9PfyoQ2rH_" }, "source": [ - "# How to solve a problem on Kaggle with TF-Hub\n", - "\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# How to solve a problem on Kaggle with TF-Hub\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/text_classification_with_tf_hub_on_kaggle\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/text_classification_with_tf_hub_on_kaggle.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -71,6 +84,16 @@ "For more detailed tutorial on text classification with TF-Hub and further steps for improving the accuracy, take a look at [Text classification with TF-Hub](https://colab.research.google.com/github/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb)." ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Q4DN769E2O_R" + }, + "source": [ + "## Setup" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -366,7 +389,9 @@ "colab": { "collapsed_sections": [], "name": "How to solve a problem on Kaggle with TF-Hub", + "private_outputs": true, "provenance": [], + "toc_visible": true, "version": "0.3.2" }, "kernelspec": { diff --git a/examples/colab/tf2_arbitrary_image_stylization.ipynb b/examples/colab/tf2_arbitrary_image_stylization.ipynb index b430003ea..9a2bc2de2 100644 --- a/examples/colab/tf2_arbitrary_image_stylization.ipynb +++ b/examples/colab/tf2_arbitrary_image_stylization.ipynb @@ -45,8 +45,39 @@ "id": "oXlcl8lqBgAD" }, "source": [ - "## TF-Hub Demo for Fast Style Transfer for Arbitrary Styles\n", - "\n", + "# TF-Hub Demo for Fast Style Transfer for Arbitrary Styles\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf2_arbitrary_image_stylization.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "oXlcl8lqBgAD" + }, + "source": [ "Based on the model code in [magenta](https://github.com/tensorflow/magenta/tree/master/magenta/models/arbitrary_image_stylization) and the publication:\n", "\n", "[Exploring the structure of a real-time, arbitrary neural artistic stylization\n", @@ -63,21 +94,17 @@ "id": "TaM8BVxrCA2E" }, "source": [ - "Let's start with importing TF-2 and all relevant dependencies." + "## Setup" ] }, { - "cell_type": "code", - "execution_count": 0, + "cell_type": "markdown", "metadata": { - "colab": {}, - "colab_type": "code", - "id": "PDuryq3dD_W5" + "colab_type": "text", + "id": "TaM8BVxrCA2E" }, - "outputs": [], "source": [ - "# We want to use TensorFlow 2.0 in the Eager mode for this demonstration. But this module works as well with the Graph mode.\n", - "!pip install tensorflow --quiet" + "Let's start with importing TF-2 and all relevant dependencies." ] }, { @@ -410,6 +437,7 @@ "colab": { "collapsed_sections": [], "name": "TF-Hub: Fast Style Transfer for Arbitrary Styles.ipynb", + "private_outputs": true, "provenance": [], "toc_visible": true, "version": "0.3.2" diff --git a/examples/colab/tf2_image_retraining.ipynb b/examples/colab/tf2_image_retraining.ipynb index cadfbcb85..ffddf5970 100644 --- a/examples/colab/tf2_image_retraining.ipynb +++ b/examples/colab/tf2_image_retraining.ipynb @@ -45,18 +45,29 @@ "id": "oYM61xrTsP5d" }, "source": [ - "# TF Hub for TF2: Retraining an image classifier\n", - "\n", - "\u003ctable align=\"left\"\u003e\n", - "\u003ctd align=\"center\"\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003e\u003cbr\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\n", - "\u003ctd align=\"center\"\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003e\u003cbr\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\n", + "# TF Hub for TF2: Retraining an image classifier\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf2_image_retraining\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_image_retraining.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf2_image_retraining.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, @@ -86,7 +97,7 @@ "id": "bL54LWCHt5q5" }, "source": [ - "## Set up TensorFlow 2 and other libraries" + "## Setup" ] }, { @@ -246,7 +257,7 @@ "model = tf.keras.Sequential([\n", " hub.KerasLayer(MODULE_HANDLE, trainable=do_fine_tuning),\n", " tf.keras.layers.Dropout(rate=0.2),\n", - " tf.keras.layers.Dense(train_generator.num_classes, activation='softmax',\n", + " tf.keras.layers.Dense(train_generator.num_classes,\n", " kernel_regularizer=tf.keras.regularizers.l2(0.0001))\n", "])\n", "model.build((None,)+IMAGE_SIZE+(3,))\n", @@ -275,7 +286,7 @@ "source": [ "model.compile(\n", " optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), \n", - " loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n", + " loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),\n", " metrics=['accuracy'])" ] }, @@ -459,6 +470,7 @@ "ScitaPqhKtuW" ], "name": "TF Hub for TF2: Retraining an image classifier", + "private_outputs": true, "provenance": [], "toc_visible": true }, diff --git a/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb b/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb index 506626005..f22329b94 100644 --- a/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb +++ b/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb @@ -45,8 +45,39 @@ "id": "9qOVy-_vmuUP" }, "source": [ - "# Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub\n", - "\n", + "# Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf2_semantic_approximate_nearest_neighbors\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf2_semantic_approximate_nearest_neighbors.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "9qOVy-_vmuUP" + }, + "source": [ "This tutorial illustrates how to generate embeddings from a [TensorFlow Hub](https://tfhub.dev) (TF-Hub) module given input data, and build an approximate nearest neighbours (ANN) index using the extracted embeddings. The index can then be used for real-time similarity matching and retrieval.\n", "\n", "When dealing with a large corpus of data, it's not efficient to perform exact matching by scanning the whole repository to find the most similar items to a given query in real-time. Thus, we use an approximate similarity matching algorithm which allows us to trade off a little bit of accuracy in finding exact nearest neighbor matches for a significant boost in speed.\n", @@ -69,7 +100,7 @@ "id": "Q0jr0QK9qO5P" }, "source": [ - "## Getting Started" + "## Setup" ] }, { @@ -92,8 +123,6 @@ }, "outputs": [], "source": [ - "!pip install --upgrade tensorflow\n", - "!pip install --upgrade tensorflow_hub\n", "!pip install apache_beam\n", "!pip install sklearn\n", "!pip install annoy" @@ -159,8 +188,7 @@ "\n", "[A Million News Headlines](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/SYBGZL#) dataset contains news headlines published over a period of 15 years sourced from the reputable Australian Broadcasting Corp. (ABC). This news dataset has a summarised historical record of noteworthy events in the globe from early-2003 to end-2017 with a more granular focus on Australia. \n", "\n", - "**Format**: Tab-separated two-column data: 1) publication date and 2) headline text. We are only interested in the headline text.\n", - "\n" + "**Format**: Tab-separated two-column data: 1) publication date and 2) headline text. We are only interested in the headline text.\n" ] }, { @@ -173,7 +201,7 @@ }, "outputs": [], "source": [ - "!wget 'https://dataverse.harvard.edu/api/access/datafile/3450625?format=tab\u0026gbrecs=true' -O raw.tsv\n", + "!wget 'https://dataverse.harvard.edu/api/access/datafile/3450625?format=tab&gbrecs=true' -O raw.tsv\n", "!wc -l raw.tsv\n", "!head raw.tsv" ] @@ -809,12 +837,13 @@ "g6pXBVxsVUbm" ], "name": "Semantic Search with Approximate Nearest Neighbors and Text Embeddings from TF-Hub", + "private_outputs": true, "provenance": [], "toc_visible": true }, "kernelspec": { - "display_name": "Python 2", - "name": "python2" + "display_name": "Python 3", + "name": "python3" } }, "nbformat": 4, diff --git a/examples/colab/tf2_text_classification.ipynb b/examples/colab/tf2_text_classification.ipynb index 6b4726ef8..664eeeec4 100644 --- a/examples/colab/tf2_text_classification.ipynb +++ b/examples/colab/tf2_text_classification.ipynb @@ -87,16 +87,22 @@ "cell_type": "markdown", "metadata": { "colab_type": "text", - "id": "hKY4XMc9o8iB" + "id": "MfBg1C5NB3X0" }, "source": [ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf2_text_classification\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf2_text_classification.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, @@ -114,6 +120,16 @@ "This notebook uses [tf.keras](https://www.tensorflow.org/guide/keras), a high-level API to build and train models in TensorFlow, and [TensorFlow Hub](https://www.tensorflow.org/hub), a library and platform for transfer learning. For a more advanced text classification tutorial using `tf.keras`, see the [MLCC Text Classification Guide](https://developers.google.com/machine-learning/guides/text-classification/)." ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Q4DN769E2O_R" + }, + "source": [ + "## Setup" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -316,7 +332,7 @@ "model = tf.keras.Sequential()\n", "model.add(hub_layer)\n", "model.add(tf.keras.layers.Dense(16, activation='relu'))\n", - "model.add(tf.keras.layers.Dense(1, activation='sigmoid'))\n", + "model.add(tf.keras.layers.Dense(1))\n", "\n", "model.summary()" ] @@ -332,7 +348,7 @@ "\n", "1. The first layer is a TensorFlow Hub layer. This layer uses a pre-trained Saved Model to map a sentence into its embedding vector. The model that we are using ([google/tf2-preview/gnews-swivel-20dim/1](https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1)) splits the sentence into tokens, embeds each token and then combines the embedding. The resulting dimensions are: `(num_examples, embedding_dimension)`.\n", "2. This fixed-length output vector is piped through a fully-connected (`Dense`) layer with 16 hidden units.\n", - "3. The last layer is densely connected with a single output node. Using the `sigmoid` activation function, this value is a float between 0 and 1, representing a probability, or confidence level." + "3. The last layer is densely connected with a single output node. This outputs logits: the log-odds of the true class, according to the model." ] }, { @@ -378,7 +394,7 @@ "outputs": [], "source": [ "model.compile(optimizer='adam',\n", - " loss='binary_crossentropy',\n", + " loss=tf.losses.BinaryCrossentropy(from_logits=True),\n", " metrics=['accuracy'])" ] }, diff --git a/examples/colab/tf_hub_delf_module.ipynb b/examples/colab/tf_hub_delf_module.ipynb index 7b1f88a78..959fc86c8 100644 --- a/examples/colab/tf_hub_delf_module.ipynb +++ b/examples/colab/tf_hub_delf_module.ipynb @@ -46,17 +46,30 @@ "id": "0DmDwGPOGfaQ" }, "source": [ - "# How to match images using DELF and TensorFlow Hub\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf_hub_delf_module.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf_hub_delf_module.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n", - "\n" + "# How to match images using DELF and TensorFlow Hub\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf_hub_delf_module\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf_hub_delf_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf_hub_delf_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf_hub_delf_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -71,6 +84,16 @@ "In this colab, we will use a module that packages the [DELF](https://github.com/tensorflow/models/tree/master/research/delf) neural network and logic for processing images to identify keypoints and their descriptors. The weights of the neural network were trained on images of landmarks as described in [this paper](https://arxiv.org/abs/1612.06321)." ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Q4DN769E2O_R" + }, + "source": [ + "## Setup" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -81,7 +104,8 @@ }, "outputs": [], "source": [ - "from absl import logging\n\n", + "from absl import logging\n", + "\n", "import matplotlib.image as mpimg\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -91,8 +115,10 @@ "from skimage.measure import ransac\n", "from skimage.transform import AffineTransform\n", "from six import BytesIO\n", - "%tensorflow_version 1.x\n", - "import tensorflow as tf\n", + "\n", + "import tensorflow.compat.v2 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "from six.moves.urllib.request import urlopen" ] @@ -374,11 +400,10 @@ "collapsed_sections": [ "RUymE2l9GZfO" ], - "default_view": {}, "name": "TF-Hub Delf module", + "private_outputs": true, "provenance": [], - "version": "0.3.2", - "views": {} + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/tf_hub_generative_image_module.ipynb b/examples/colab/tf_hub_generative_image_module.ipynb index acc25d594..986e9e162 100644 --- a/examples/colab/tf_hub_generative_image_module.ipynb +++ b/examples/colab/tf_hub_generative_image_module.ipynb @@ -46,16 +46,30 @@ "id": "CxmDMK4yupqg" }, "source": [ - "# TF-Hub generative image model\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf_hub_generative_image_module.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf_hub_generative_image_module.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e\n" + "# TF-Hub generative image model\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tf_hub_generative_image_module\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf_hub_generative_image_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tf_hub_generative_image_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf_hub_generative_image_module.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" ] }, { @@ -86,6 +100,16 @@ "* Paper on Progressive GANs: [Progressive Growing of GANs for Improved Quality, Stability, and Variation](https://arxiv.org/abs/1710.10196)." ] }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Q4DN769E2O_R" + }, + "source": [ + "## Setup" + ] + }, { "cell_type": "code", "execution_count": 0, @@ -105,7 +129,7 @@ "cell_type": "code", "execution_count": 0, "metadata": { - "cellView": "form", + "cellView": "both", "colab": {}, "colab_type": "code", "id": "6cPY9Ou4sWs_" @@ -113,19 +137,15 @@ "outputs": [], "source": [ "#@title Imports and function definitions\n", - "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 1.x\n", - "except Exception:\n", - " pass\n", - "\n", "from absl import logging\n", "\n", "import imageio\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import tensorflow as tf\n", + "\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "\n", "import tensorflow_hub as hub\n", "import time\n", "\n", @@ -373,7 +393,9 @@ "N6ZDpd9XzFeN" ], "name": "TF-Hub generative image module", - "provenance": [] + "private_outputs": true, + "provenance": [], + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", diff --git a/examples/colab/tweening_conv3d.ipynb b/examples/colab/tweening_conv3d.ipynb index 9130aa0fe..857173661 100644 --- a/examples/colab/tweening_conv3d.ipynb +++ b/examples/colab/tweening_conv3d.ipynb @@ -7,7 +7,7 @@ "id": "wC0PtNm3Sa_T" }, "source": [ - "**bold text**##### Copyright 2019 The TensorFlow Hub Authors.\n", + "##### Copyright 2019 The TensorFlow Hub Authors.\n", "\n", "Licensed under the Apache License, Version 2.0 (the \"License\");" ] @@ -45,8 +45,39 @@ "id": "oKAkxAYuONU6" }, "source": [ - "# Demo TF-Hub for video generation using Inbetweening 3d Convolutions\n", - "\n", + "# Demo TF-Hub for video generation using Inbetweening 3d Convolutions\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MfBg1C5NB3X0" + }, + "source": [ + "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/hub/tutorials/tweening_conv3d\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tweening_conv3d.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/hub/blob/master/examples/colab/tweening_conv3d.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", + " \u003c/td\u003e\n", + " \u003ctd\u003e\n", + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tweening_conv3d.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", + " \u003c/td\u003e\n", + "\u003c/table\u003e" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "oKAkxAYuONU6" + }, + "source": [ "Yunpeng Li, Dominik Roblek, and Marco Tagliasacchi. From Here to There: Video Inbetweening Using Direct 3D Convolutions, 2019.\n", "\n", "https://arxiv.org/abs/1905.10240\n", @@ -61,6 +92,16 @@ "\n", "IMPORTANT NOTE: If an error of \"Not enough disk space\" appears, it's likely due to the downloading of the full dataset (~30GB). In that case, we recommend either connecting via a local runtime or downloading the dataset to Google Drive and loading it manually instead of calling tfds.load()." ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Q4DN769E2O_R" + }, + "source": [ + "## Setup" + ] }, { "cell_type": "code", @@ -236,6 +277,7 @@ "colab": { "collapsed_sections": [], "name": "Inbetweening TF-Hub Module.ipynb", + "private_outputs": true, "provenance": [], "toc_visible": true },