<commit_before>{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MhoQ0WE77laV"
      },
      "source": [
        "##### Copyright 2019 The TensorFlow Hub Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "_ckMIh7O7s6D"
      },
      "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."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jZwnHZ70oUIM"
      },
      "source": [
        "# CropNet: Cassava Disease Detection"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6sg9wHP9oR3q"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td><a target=\"_blank\" href=\"https://tensorflow.google.cn/hub/tutorials/cropnet_cassava\"><img src=\"https://tensorflow.google.cn/images/tf_logo_32px.png\">在 TensorFlow.org 查看</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/colab_logo_32px.png\">在 Google Colab 中运行 </a></td>\n",
        "  <td>     <a target=\"_blank\" href=\"https://github.com/tensorflow/docs-l10n/blob/master/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/GitHub-Mark-32px.png\">查看上GitHub</a> </td>\n",
        "  <td><a href=\"https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/download_logo_32px.png\">下载笔记本</a></td>\n",
        "  <td><a href=\"https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2\"><img src=\"https://tensorflow.google.cn/images/hub_logo_32px.png\">查看 TF Hub 模型</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "grEgSWu2iTxm"
      },
      "source": [
        "此笔记本演示如何使用 **TensorFlow Hub** 中的 CropNet [木薯病虫害分类器](https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2)模型。该模型可将木薯叶的图像分为 6 类：*细菌性枯萎病、褐条病毒病、绿螨、花叶病、健康或未知*。\n",
        "\n",
        "此 Colab 演示了如何执行以下操作：\n",
        "\n",
        "- 从 **TensorFlow Hub** 加载 https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2 模型\n",
        "- 从 **TensorFlow Datasets (TFDS)** 加载[木薯](https://tensorflow.google.cn/datasets/catalog/cassava)数据集\n",
        "- 将木薯叶图像分为 4 种不同的木薯病虫害类别、健康或未知。\n",
        "- 评估分类器的*准确率*，并查看将模型在应用于域外图像时的*鲁棒性*。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bKn4Fiq2OD7u"
      },
      "source": [
        "## 导入和设置"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LMgjpSoYqJIz"
      },
      "outputs": [],
      "source": [
        "!pip install matplotlib==3.2.2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "both",
        "id": "FIP4rkjp45MG"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "import tensorflow as tf\n",
        "import tensorflow_datasets as tfds\n",
        "import tensorflow_hub as hub"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "mIqmq_qmWw78"
      },
      "outputs": [],
      "source": [
        "#@title Helper function for displaying examples\n",
        "def plot(examples, predictions=None):\n",
        "  # Get the images, labels, and optionally predictions\n",
        "  images = examples['image']\n",
        "  labels = examples['label']\n",
        "  batch_size = len(images)\n",
        "  if predictions is None:\n",
        "    predictions = batch_size * [None]\n",
        "\n",
        "  # Configure the layout of the grid\n",
        "  x = np.ceil(np.sqrt(batch_size))\n",
        "  y = np.ceil(batch_size / x)\n",
        "  fig = plt.figure(figsize=(x * 6, y * 7))\n",
        "\n",
        "  for i, (image, label, prediction) in enumerate(zip(images, labels, predictions)):\n",
        "    # Render the image\n",
        "    ax = fig.add_subplot(x, y, i+1)\n",
        "    ax.imshow(image, aspect='auto')\n",
        "    ax.grid(False)\n",
        "    ax.set_xticks([])\n",
        "    ax.set_yticks([])\n",
        "\n",
        "    # Display the label and optionally prediction\n",
        "    x_label = 'Label: ' + name_map[class_names[label]]\n",
        "    if prediction is not None:\n",
        "      x_label = 'Prediction: ' + name_map[class_names[prediction]] + '\\n' + x_label\n",
        "      ax.xaxis.label.set_color('green' if label == prediction else 'red')\n",
        "    ax.set_xlabel(x_label)\n",
        "\n",
        "  plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kwrg9yIlaUSb"
      },
      "source": [
        "## 数据集\n",
        "\n",
        "让我们从 TFDS 中加载*木薯*数据集"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0rTcnxoSkp31"
      },
      "outputs": [],
      "source": [
        "dataset, info = tfds.load('cassava', with_info=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GpC71TFDhJFO"
      },
      "source": [
        "我们来查看数据集信息以了解更多内容，例如描述和引用以及有关可用样本量的信息"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "btJBMovmbYtR"
      },
      "outputs": [],
      "source": [
        "info"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QT3XWAtR6BRy"
      },
      "source": [
        "*木薯*数据集包含涉及 4 种不同病虫害的木薯叶图像以及健康的木薯叶图像。模型可以预测上述五个类，当模型不确定其预测结果时，会将图像分为第六个类，即“未知”类。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9NT9q8yyXZfX"
      },
      "outputs": [],
      "source": [
        "# Extend the cassava dataset classes with 'unknown'\n",
        "class_names = info.features['label'].names + ['unknown']\n",
        "\n",
        "# Map the class names to human readable names\n",
        "name_map = dict(\n",
        "    cmd='Mosaic Disease',\n",
        "    cbb='Bacterial Blight',\n",
        "    cgm='Green Mite',\n",
        "    cbsd='Brown Streak Disease',\n",
        "    healthy='Healthy',\n",
        "    unknown='Unknown')\n",
        "\n",
        "print(len(class_names), 'classes:')\n",
        "print(class_names)\n",
        "print([name_map[name] for name in class_names])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "I6y_MGxgiW09"
      },
      "source": [
        "将数据馈送至模型之前，我们需要进行一些预处理。模型接受大小为 224 x 224，且 RGB 通道值范围为 [0, 1] 的图像。让我们归一化图像并调整图像大小。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UxtxvqRjh7Nm"
      },
      "outputs": [],
      "source": [
        "def preprocess_fn(data):\n",
        "  image = data['image']\n",
        "\n",
        "  # Normalize [0, 255] to [0, 1]\n",
        "  image = tf.cast(image, tf.float32)\n",
        "  image = image / 255.\n",
        "\n",
        "  # Resize the images to 224 x 224\n",
        "  image = tf.image.resize(image, (224, 224))\n",
        "\n",
        "  data['image'] = image\n",
        "  return data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qz27YrZahdvn"
      },
      "source": [
        "我们看一下数据集中的一些样本"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "j6LkAxv3f-aJ"
      },
      "outputs": [],
      "source": [
        "batch = dataset['validation'].map(preprocess_fn).batch(25).as_numpy_iterator()\n",
        "examples = next(batch)\n",
        "plot(examples)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eHlEAhL3hq2R"
      },
      "source": [
        "## 模型\n",
        "\n",
        "让我们从 TF-Hub 中加载分类器并获取一些预测结果，然后查看模型对一些样本的预测"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "b6eIWkTjIQhS"
      },
      "outputs": [],
      "source": [
        "classifier = hub.KerasLayer('https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2')\n",
        "probabilities = classifier(examples['image'])\n",
        "predictions = tf.argmax(probabilities, axis=-1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "MTQA1YAltfRZ"
      },
      "outputs": [],
      "source": [
        "plot(examples, predictions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MuFE8A5aZv9z"
      },
      "source": [
        "## 评估和鲁棒性\n",
        "\n",
        "我们来衡量分类器在拆分数据集上的*准确率*。我们还可以通过评估模型在非木薯数据集上的性能来评估其*鲁棒性*。对于 iNaturalist 或豆科植物等其他植物数据集中的图像，模型应几乎始终返回*未知*。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "0ERcNxs0kHd3"
      },
      "outputs": [],
      "source": [
        "#@title Parameters {run: \"auto\"}\n",
        "\n",
        "DATASET = 'cassava'  #@param {type:\"string\"} ['cassava', 'beans', 'i_naturalist2017']\n",
        "DATASET_SPLIT = 'test' #@param {type:\"string\"} ['train', 'test', 'validation']\n",
        "BATCH_SIZE =  32 #@param {type:\"integer\"}\n",
        "MAX_EXAMPLES = 1000 #@param {type:\"integer\"}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Mt0-IVmZplbb"
      },
      "outputs": [],
      "source": [
        "def label_to_unknown_fn(data):\n",
        "  data['label'] = 5  # Override label to unknown.\n",
        "  return data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cQYvY3IvY2Nx"
      },
      "outputs": [],
      "source": [
        "# Preprocess the examples and map the image label to unknown for non-cassava datasets.\n",
        "ds = tfds.load(DATASET, split=DATASET_SPLIT).map(preprocess_fn).take(MAX_EXAMPLES)\n",
        "dataset_description = DATASET\n",
        "if DATASET != 'cassava':\n",
        "  ds = ds.map(label_to_unknown_fn)\n",
        "  dataset_description += ' (labels mapped to unknown)'\n",
        "ds = ds.batch(BATCH_SIZE)\n",
        "\n",
        "# Calculate the accuracy of the model\n",
        "metric = tf.keras.metrics.Accuracy()\n",
        "for examples in ds:\n",
        "  probabilities = classifier(examples['image'])\n",
        "  predictions = tf.math.argmax(probabilities, axis=-1)\n",
        "  labels = examples['label']\n",
        "  metric.update_state(labels, predictions)\n",
        "\n",
        "print('Accuracy on %s: %.2f' % (dataset_description, metric.result().numpy()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rvS18sBExpdL"
      },
      "source": [
        "## 了解更多\n",
        "\n",
        "- 详细了解 TensorFlow Hub 上的模型：https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2\n",
        "- 了解如何使用[模型的 TensorFlow Lite 版本](https://tfhub.dev/google/lite-model/cropnet/classifier/cassava_disease_V1/1)通过 [ML Kit](https://developers.google.com/ml-kit/custom-models#tfhub) 构建在手机上运行的自定义图像分类器。"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "cropnet_cassava.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
<commit_msg>Translate cropnet_cassava.ipynb via GitLocalize<commit_after>{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vtNtfcHHoHNP"
      },
      "outputs": [],
      "source": [
        "# Copyright 2019 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": {
        "id": "jZwnHZ70oUIM"
      },
      "source": [
        "# CropNet: Cassava Disease Detection"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6sg9wHP9oR3q"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td><a target=\"_blank\" href=\"https://tensorflow.google.cn/hub/tutorials/cropnet_cassava\"><img src=\"https://tensorflow.google.cn/images/tf_logo_32px.png\">在 TensorFlow.org 查看</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/colab_logo_32px.png\">在 Google Colab 中运行 </a></td>\n",
        "  <td>     <a target=\"_blank\" href=\"https://github.com/tensorflow/docs-l10n/blob/master/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/GitHub-Mark-32px.png\">查看上GitHub</a> </td>\n",
        "  <td><a href=\"https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/zh-cn/hub/tutorials/cropnet_cassava.ipynb\"><img src=\"https://tensorflow.google.cn/images/download_logo_32px.png\">下载笔记本</a></td>\n",
        "  <td><a href=\"https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2\"><img src=\"https://tensorflow.google.cn/images/hub_logo_32px.png\">查看 TF Hub 模型</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "grEgSWu2iTxm"
      },
      "source": [
        "此笔记本演示如何使用 **TensorFlow Hub** 中的 CropNet [木薯病虫害分类器](https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2)模型。该模型可将木薯叶的图像分为 6 类：*细菌性枯萎病、褐条病毒病、绿螨、花叶病、健康或未知*。\n",
        "\n",
        "此 Colab 演示了如何执行以下操作：\n",
        "\n",
        "- 从 **TensorFlow Hub** 加载 https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2 模型\n",
        "- 从 **TensorFlow Datasets (TFDS)** 加载[木薯](https://tensorflow.google.cn/datasets/catalog/cassava)数据集\n",
        "- 将木薯叶图像分为 4 种不同的木薯病虫害类别、健康或未知。\n",
        "- 评估分类器的*准确率*，并查看将模型在应用于域外图像时的*鲁棒性*。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bKn4Fiq2OD7u"
      },
      "source": [
        "## 导入和设置"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LMgjpSoYqJIz"
      },
      "outputs": [],
      "source": [
        "!pip install matplotlib==3.2.2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "both",
        "id": "FIP4rkjp45MG"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "import tensorflow as tf\n",
        "import tensorflow_datasets as tfds\n",
        "import tensorflow_hub as hub"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "mIqmq_qmWw78"
      },
      "outputs": [],
      "source": [
        "#@title Helper function for displaying examples\n",
        "def plot(examples, predictions=None):\n",
        "  # Get the images, labels, and optionally predictions\n",
        "  images = examples['image']\n",
        "  labels = examples['label']\n",
        "  batch_size = len(images)\n",
        "  if predictions is None:\n",
        "    predictions = batch_size * [None]\n",
        "\n",
        "  # Configure the layout of the grid\n",
        "  x = np.ceil(np.sqrt(batch_size))\n",
        "  y = np.ceil(batch_size / x)\n",
        "  fig = plt.figure(figsize=(x * 6, y * 7))\n",
        "\n",
        "  for i, (image, label, prediction) in enumerate(zip(images, labels, predictions)):\n",
        "    # Render the image\n",
        "    ax = fig.add_subplot(x, y, i+1)\n",
        "    ax.imshow(image, aspect='auto')\n",
        "    ax.grid(False)\n",
        "    ax.set_xticks([])\n",
        "    ax.set_yticks([])\n",
        "\n",
        "    # Display the label and optionally prediction\n",
        "    x_label = 'Label: ' + name_map[class_names[label]]\n",
        "    if prediction is not None:\n",
        "      x_label = 'Prediction: ' + name_map[class_names[prediction]] + '\\n' + x_label\n",
        "      ax.xaxis.label.set_color('green' if label == prediction else 'red')\n",
        "    ax.set_xlabel(x_label)\n",
        "\n",
        "  plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kwrg9yIlaUSb"
      },
      "source": [
        "## 数据集\n",
        "\n",
        "让我们从 TFDS 中加载*木薯*数据集"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0rTcnxoSkp31"
      },
      "outputs": [],
      "source": [
        "dataset, info = tfds.load('cassava', with_info=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GpC71TFDhJFO"
      },
      "source": [
        "我们来查看数据集信息以了解更多内容，例如描述和引用以及有关可用样本量的信息"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "btJBMovmbYtR"
      },
      "outputs": [],
      "source": [
        "info"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QT3XWAtR6BRy"
      },
      "source": [
        "*木薯*数据集包含涉及 4 种不同病虫害的木薯叶图像以及健康的木薯叶图像。模型可以预测上述五个类，当模型不确定其预测结果时，会将图像分为第六个类，即“未知”类。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9NT9q8yyXZfX"
      },
      "outputs": [],
      "source": [
        "# Extend the cassava dataset classes with 'unknown'\n",
        "class_names = info.features['label'].names + ['unknown']\n",
        "\n",
        "# Map the class names to human readable names\n",
        "name_map = dict(\n",
        "    cmd='Mosaic Disease',\n",
        "    cbb='Bacterial Blight',\n",
        "    cgm='Green Mite',\n",
        "    cbsd='Brown Streak Disease',\n",
        "    healthy='Healthy',\n",
        "    unknown='Unknown')\n",
        "\n",
        "print(len(class_names), 'classes:')\n",
        "print(class_names)\n",
        "print([name_map[name] for name in class_names])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "I6y_MGxgiW09"
      },
      "source": [
        "将数据馈送至模型之前，我们需要进行一些预处理。模型接受大小为 224 x 224，且 RGB 通道值范围为 [0, 1] 的图像。让我们归一化图像并调整图像大小。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UxtxvqRjh7Nm"
      },
      "outputs": [],
      "source": [
        "def preprocess_fn(data):\n",
        "  image = data['image']\n",
        "\n",
        "  # Normalize [0, 255] to [0, 1]\n",
        "  image = tf.cast(image, tf.float32)\n",
        "  image = image / 255.\n",
        "\n",
        "  # Resize the images to 224 x 224\n",
        "  image = tf.image.resize(image, (224, 224))\n",
        "\n",
        "  data['image'] = image\n",
        "  return data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qz27YrZahdvn"
      },
      "source": [
        "我们看一下数据集中的一些样本"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "j6LkAxv3f-aJ"
      },
      "outputs": [],
      "source": [
        "batch = dataset['validation'].map(preprocess_fn).batch(25).as_numpy_iterator()\n",
        "examples = next(batch)\n",
        "plot(examples)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eHlEAhL3hq2R"
      },
      "source": [
        "## 模型\n",
        "\n",
        "让我们从 TF-Hub 中加载分类器并获取一些预测结果，然后查看模型对一些样本的预测"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "b6eIWkTjIQhS"
      },
      "outputs": [],
      "source": [
        "classifier = hub.KerasLayer('https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2')\n",
        "probabilities = classifier(examples['image'])\n",
        "predictions = tf.argmax(probabilities, axis=-1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "MTQA1YAltfRZ"
      },
      "outputs": [],
      "source": [
        "plot(examples, predictions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MuFE8A5aZv9z"
      },
      "source": [
        "## 评估和鲁棒性\n",
        "\n",
        "我们来衡量分类器在拆分数据集上的*准确率*。我们还可以通过评估模型在非木薯数据集上的性能来评估其*鲁棒性*。对于 iNaturalist 或豆科植物等其他植物数据集中的图像，模型应几乎始终返回*未知*。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "0ERcNxs0kHd3"
      },
      "outputs": [],
      "source": [
        "#@title Parameters {run: \"auto\"}\n",
        "\n",
        "DATASET = 'cassava'  #@param {type:\"string\"} ['cassava', 'beans', 'i_naturalist2017']\n",
        "DATASET_SPLIT = 'test' #@param {type:\"string\"} ['train', 'test', 'validation']\n",
        "BATCH_SIZE =  32 #@param {type:\"integer\"}\n",
        "MAX_EXAMPLES = 1000 #@param {type:\"integer\"}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Mt0-IVmZplbb"
      },
      "outputs": [],
      "source": [
        "def label_to_unknown_fn(data):\n",
        "  data['label'] = 5  # Override label to unknown.\n",
        "  return data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cQYvY3IvY2Nx"
      },
      "outputs": [],
      "source": [
        "# Preprocess the examples and map the image label to unknown for non-cassava datasets.\n",
        "ds = tfds.load(DATASET, split=DATASET_SPLIT).map(preprocess_fn).take(MAX_EXAMPLES)\n",
        "dataset_description = DATASET\n",
        "if DATASET != 'cassava':\n",
        "  ds = ds.map(label_to_unknown_fn)\n",
        "  dataset_description += ' (labels mapped to unknown)'\n",
        "ds = ds.batch(BATCH_SIZE)\n",
        "\n",
        "# Calculate the accuracy of the model\n",
        "metric = tf.keras.metrics.Accuracy()\n",
        "for examples in ds:\n",
        "  probabilities = classifier(examples['image'])\n",
        "  predictions = tf.math.argmax(probabilities, axis=-1)\n",
        "  labels = examples['label']\n",
        "  metric.update_state(labels, predictions)\n",
        "\n",
        "print('Accuracy on %s: %.2f' % (dataset_description, metric.result().numpy()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rvS18sBExpdL"
      },
      "source": [
        "## 了解更多\n",
        "\n",
        "- 详细了解 TensorFlow Hub 上的模型：https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2\n",
        "- 了解如何使用[模型的 TensorFlow Lite 版本](https://tfhub.dev/google/lite-model/cropnet/classifier/cassava_disease_V1/1)通过 [ML Kit](https://developers.google.com/ml-kit/custom-models#tfhub) 构建在手机上运行的自定义图像分类器。"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "cropnet_cassava.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.18803283864856332, 'fraction_numerical': 0.011682980738869593, 'mean_word_length': 2.003793716656787, 'pattern_counts': {'":': 299, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 35, 'lorem ipsum': 0, 'www.': 2, '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': '6263114', 'n_tokens_mistral': 10086, 'n_tokens_neox': 10017, 'n_words': 1476}