<commit_before>{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wdeKOEkv1Fe8"
      },
      "source": [
        "##### Copyright \u0026copy; 2020 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "both",
        "id": "c2jyGuiG1gHr"
      },
      "outputs": [],
      "source": [
        "#@title 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",
        "# https://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": {
        "id": "23R0Z9RojXYW"
      },
      "source": [
        "# Standalone Model Card Toolkit Demo\n",
        "\n",
        "This \"standalone\" notebook demonstrates using the Model Card Toolkit without the TFX/MLMD context. To learn how to use Model Card Toolkit with TFX/MLMD, please check [MLMD Model Card Toolkit Demo](./MLMD_Model_Card_Toolkit_Demo.ipynb)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sfSQ-kX-MLEr"
      },
      "source": [
        "## Objective\n",
        "\n",
        "This notebook demonstrates how to generate a Model Card using the Model Card Toolkit in a Jupyter/Colab environment. You can learn more about model cards at https://modelcards.withgoogle.com/about. \n",
        "\n",
        "We are using a Keras model in this demo. But the logic below also applies to other ML frameworks in general.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2GivNBNYjb3b"
      },
      "source": [
        "## Setup\n",
        "We first need to a) install and import the necessary packages, and b) download the data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Fmgi8ZvQkScg"
      },
      "source": [
        "### Upgrade Pip and install the Model Card Toolkit"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "as4OTe2ukSqm"
      },
      "outputs": [],
      "source": [
        "try:\n",
        "  import colab\n",
        "  !pip install --upgrade pip\n",
        "except:\n",
        "  pass\n",
        "\n",
        "!pip install 'model-card-toolkit\u003e=0.1.1,\u003c0.2'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "EwT0nov5QO1M"
      },
      "source": [
        "#### Did you restart the runtime?\n",
        "\n",
        "If you are using Google Colab, the first time that you run the cell above, you must restart the runtime (Runtime \u003e Restart runtime ...). This is because of the way that Colab loads packages."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7-JNjw8eUdrL"
      },
      "source": [
        "### Imports"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Nx4sE8cUUhF-"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "from model_card_toolkit import ModelCardToolkit\n",
        "from model_card_toolkit.documentation.examples import cats_vs_dogs\n",
        "from model_card_toolkit.utils.graphics import figure_to_base64str\n",
        "import tempfile\n",
        "import matplotlib.pyplot as plt\n",
        "from IPython import display\n",
        "import requests\n",
        "import os\n",
        "import zipfile"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jzNHy94JjfEJ"
      },
      "source": [
        "## Model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "u7UTf5FqXeQd"
      },
      "source": [
        "We will use a pretrained model with architecture based off [MobileNetV2](https://arxiv.org/abs/1801.04381), a popular 16-layer image classification model. Our model has been trained to distinguish between betweens cats and dogs using the [Cats vs Dogs](https://www.tensorflow.org/datasets/catalog/cats_vs_dogs) dataset. The model training was based on the [TensorFlow transfer learning tutorial](https://www.tensorflow.org/tutorials/images/transfer_learning). "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TJzHu_ZQCJ_z"
      },
      "outputs": [],
      "source": [
        "URL = 'https://storage.googleapis.com/cats_vs_dogs_model/cats_vs_dogs_model.zip'\n",
        "ZIPPATH = 'cats_vs_dogs_model.zip'\n",
        "DIRPATH = 'cats_vs_dogs_model'\n",
        "\n",
        "r = requests.get(URL, allow_redirects=True)\n",
        "open(ZIPPATH, 'wb').write(r.content)\n",
        "\n",
        "with zipfile.ZipFile(ZIPPATH, 'r') as zip_ref:\n",
        "    zip_ref.extractall('')\n",
        "\n",
        "model = tf.keras.models.load_model(DIRPATH)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7SUMYXTM3Cch"
      },
      "source": [
        "## Dataset"
      ]
    },
    {
      "cell_type": "markdown",<commit_msg>Write the model to tmp dir instead so that it can run on internal test infra.<commit_after>{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wdeKOEkv1Fe8"
      },
      "source": [
        "##### Copyright \u0026copy; 2020 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "c2jyGuiG1gHr"
      },
      "outputs": [],
      "source": [
        "#@title 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",
        "# https://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": {
        "id": "23R0Z9RojXYW"
      },
      "source": [
        "# Standalone Model Card Toolkit Demo\n",
        "\n",
        "This \"standalone\" notebook demonstrates using the Model Card Toolkit without the TFX/MLMD context. To learn how to use Model Card Toolkit with TFX/MLMD, please check [MLMD Model Card Toolkit Demo](./MLMD_Model_Card_Toolkit_Demo.ipynb)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sfSQ-kX-MLEr"
      },
      "source": [
        "## Objective\n",
        "\n",
        "This notebook demonstrates how to generate a Model Card using the Model Card Toolkit in a Jupyter/Colab environment. You can learn more about model cards at https://modelcards.withgoogle.com/about. \n",
        "\n",
        "We are using a Keras model in this demo. But the logic below also applies to other ML frameworks in general.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2GivNBNYjb3b"
      },
      "source": [
        "## Setup\n",
        "We first need to a) install and import the necessary packages, and b) download the data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Fmgi8ZvQkScg"
      },
      "source": [
        "### Upgrade Pip and install the Model Card Toolkit"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "as4OTe2ukSqm"
      },
      "outputs": [],
      "source": [
        "try:\n",
        "  import colab\n",
        "  !pip install --upgrade pip\n",
        "except:\n",
        "  pass\n",
        "\n",
        "!pip install 'model-card-toolkit\u003e=0.1.1,\u003c0.2'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "EwT0nov5QO1M"
      },
      "source": [
        "#### Did you restart the runtime?\n",
        "\n",
        "If you are using Google Colab, the first time that you run the cell above, you must restart the runtime (Runtime \u003e Restart runtime ...). This is because of the way that Colab loads packages."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7-JNjw8eUdrL"
      },
      "source": [
        "### Imports"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Nx4sE8cUUhF-"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "from model_card_toolkit import ModelCardToolkit\n",
        "from model_card_toolkit.documentation.examples import cats_vs_dogs\n",
        "from model_card_toolkit.utils.graphics import figure_to_base64str\n",
        "import tempfile\n",
        "import matplotlib.pyplot as plt\n",
        "from IPython import display\n",
        "import requests\n",
        "import os\n",
        "import zipfile"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jzNHy94JjfEJ"
      },
      "source": [
        "## Model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "u7UTf5FqXeQd"
      },
      "source": [
        "We will use a pretrained model with architecture based off [MobileNetV2](https://arxiv.org/abs/1801.04381), a popular 16-layer image classification model. Our model has been trained to distinguish between betweens cats and dogs using the [Cats vs Dogs](https://www.tensorflow.org/datasets/catalog/cats_vs_dogs) dataset. The model training was based on the [TensorFlow transfer learning tutorial](https://www.tensorflow.org/tutorials/images/transfer_learning). "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TJzHu_ZQCJ_z"
      },
      "outputs": [],
      "source": [
        "URL = 'https://storage.googleapis.com/cats_vs_dogs_model/cats_vs_dogs_model.zip'\n",
        "BASE_PATH = tempfile.mkdtemp()\n",
        "ZIP_PATH = os.path.join(BASE_PATH, 'cats_vs_dogs_model.zip')\n",
        "MODEL_PATH = os.path.join(BASE_PATH,'cats_vs_dogs_model')\n",
        "\n",
        "r = requests.get(URL, allow_redirects=True)\n",
        "open(ZIP_PATH, 'wb').write(r.content)\n",
        "\n",
        "with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:\n",
        "    zip_ref.extractall(BASE_PATH)\n",
        "\n",
        "model = tf.keras.models.load_model(MODEL_PATH)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7SUMYXTM3Cch"
      },
      "source": [
        "## Dataset"
      ]
    },
    {
      "cell_type": "markdown",
{'fraction_non_alphanumeric': 0.1477615802639524, 'fraction_numerical': 0.011386181316311568, 'mean_word_length': 1.9873743880443184, 'pattern_counts': {'":': 133, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943870', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3788, 'n_words': 941}