{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bK-7g5sizhg5"
      },
      "source": [
        "## Install Tensorflow-Models packages\n",
        "\n",
        "The notebook is tested with Google Colab sandbox.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 9737,
          "status": "ok",
          "timestamp": 1650513863935,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "eTz93_P2dMty",
        "outputId": "d147b4b0-954f-4064-d179-bb82bc3ea4fe"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[K     |████████████████████████████████| 21.8 MB 1.6 MB/s \n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip3 install -q tf-models-nightly\n",
        "# Fix Colab default opencv problem\n",
        "!pip3 install -q opencv-python-headless==4.1.2.30\n",
        "\n",
        "## Colab environment setup. To use a stable TF release version\n",
        "##  because of the possible breakage in tf-nightly.\n",
        "# !pip3 install -U numpy\u003e=1.20\n",
        "# !pip3 install -q tensorflow==2.8.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 2,
          "status": "ok",
          "timestamp": 1650513867685,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "GHvGWdCcdQqG",
        "outputId": "863683b2-6b70-4de9-98bc-5df743619ad5"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1.21.6\n",
            "2.10.0-dev20220420\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "import tensorflow as tf\n",
        "print(np.__version__)\n",
        "print(tf.__version__)\n",
        "\n",
        "import tensorflow_models as tfm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eYSeQJniztc8"
      },
      "source": [
        "## Check out modules\n",
        "\n",
        "**Note: As the TensorFlow Models (NLP + Vision) 2.9 release which is tested for this notebook, we partially exported selected modules but the APIs are not stable. Also be aware that, the\n",
        "modeling libraries are advancing very fast, so we generally don't guarantee compatability between versions.** "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 206,
          "status": "ok",
          "timestamp": 1650513874596,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "Y1iEMMGTMrQu",
        "outputId": "f3da68d7-ecda-471c-c27b-915b80b55131"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Top-level modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'core', 'hyperparams', 'nlp', 'optimization', 'utils', 'vision']\n",
            "NLP modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'encoders', 'layers', 'losses', 'models', 'networks', 'ops', 'serving_modules', 'tasks']\n",
            "Vision modules:  ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'anchor', 'anchor_generator', 'augment', 'backbones', 'box_matcher', 'box_ops', 'classification_model', 'configs', 'decoders', 'factory', 'factory_3d', 'heads', 'iou_similarity', 'layers', 'mask_ops', 'maskrcnn_model', 'nms', 'preprocess_ops', 'preprocess_ops_3d', 'retinanet_model', 'sampling_ops', 'segmentation_model', 'spatial_transform_ops', 'target_gather', 'video_classification_model']\n"
          ]
        }
      ],
      "source": [
        "print(\"Top-level modules: \", dir(tfm))\n",
        "print(\"NLP modules: \", dir(tfm.nlp))\n",
        "print(\"Vision modules: \", dir(tfm.vision))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UMHeJmk_1yUf"
      },
      "source": [
        "## Quick Examples\n",
        "\n",
        "### 1. Use a tfm.nlp Keras layer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 400,
          "status": "ok",
          "timestamp": 1650514040957,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "XVWEUozQ1xQY",
        "outputId": "5de2aa91-8c38-438e-80b6-481617917c08"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "transformer_encoder_block_1\n",
            "tf.Tensor(\n",
            "[[[-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]]\n",
            "\n",
            " [[-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.79198956  0.4180589 ]\n",
            "  [-1.063648    1.4375787  -0.7919895   0.41805887]]], shape=(2, 3, 4), dtype=float32)\n"
          ]
        }
      ],
      "source": [
        "encoder_block = tfm.nlp.layers.TransformerEncoderBlock(\n",
        "        num_attention_heads=2, inner_dim=10, inner_activation='relu')\n",
        "\n",
        "batch, length, hidden_size = 2, 3, 4\n",
        "qkv_inputs = tf.ones((batch, length, hidden_size), tf.float32)\n",
        "attention_mask = None\n",
        "outputs = encoder_block([qkv_inputs, attention_mask])\n",
        "print(encoder_block.name)\n",
        "print(outputs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AOOrWjKkSYM0"
      },
      "source": [
        "### 2. Use a tfm.vision Backbone models"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 5979,
          "status": "ok",
          "timestamp": 1650514078414,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "xwD0UhUdSzNU",
        "outputId": "770d46c0-8c71-4f59-ee0b-67430c791380"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n",
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n",
            "WARNING:absl:SpineNet output level out of range [min_level, max_level] = [4, 6] will not be used for further processing.\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "spine_net\n",
            "{'4': \u003cKerasTensor: shape=(1, 8, 8, 128) dtype=float32 (created by layer 'spine_net')\u003e, '5': \u003cKerasTensor: shape=(1, 4, 4, 128) dtype=float32 (created by layer 'spine_net')\u003e, '6': \u003cKerasTensor: shape=(1, 2, 2, 128) dtype=float32 (created by layer 'spine_net')\u003e}\n"
          ]
        }
      ],
      "source": [
        "input_size = 128\n",
        "filter_size_scale, block_repeats, resample_alpha, endpoints_num_filters, min_level, max_level = 0.65, 1, 0.5, 128, 4, 6\n",
        "input_specs = tf.keras.layers.InputSpec(\n",
        "    shape=[None, input_size, input_size, 3])\n",
        "model = tfm.vision.backbones.SpineNet(\n",
        "    input_specs=input_specs,\n",
        "    min_level=min_level,\n",
        "    max_level=max_level,\n",
        "    endpoints_num_filters=endpoints_num_filters,\n",
        "    resample_alpha=resample_alpha,\n",
        "    block_repeats=block_repeats,\n",
        "    filter_size_scale=filter_size_scale,\n",
        "    init_stochastic_depth_rate=0.2,\n",
        ")\n",
        "\n",
        "inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)\n",
        "endpoints = model(inputs)\n",
        "print(model.name)\n",
        "print(endpoints)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HHJs4lRlTk8q"
      },
      "source": [
        "### 3. Use Orbit package for advanced training loops"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "executionInfo": {
          "elapsed": 215,
          "status": "ok",
          "timestamp": 1650514185283,
          "user": {
            "displayName": "Hongkun Yu",
            "userId": "12855578661733349593"
          },
          "user_tz": 420
        },
        "id": "X4ek9IrJTkP_",
        "outputId": "7b31de68-53a0-4b8a-d9ab-109e7c43c933"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Orbit modules:  ['AbstractEvaluator', 'AbstractTrainer', 'Action', 'Controller', 'StandardEvaluator', 'StandardEvaluatorOptions', 'StandardTrainer', 'StandardTrainerOptions', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'actions', 'controller', 'runner', 'standard_runner', 'utils']\n"
          ]
        }
      ],
      "source": [
        "import orbit\n",
        "print(\"Orbit modules: \", dir(orbit))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "tensorflow_models_pypi",
      "provenance": [
        {
          "file_id": "1dm1dUZ2Bo6S6Zom7GTQrIG78Xz7iFeZY",
          "timestamp": 1650514452505
        }
      ]
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
