{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "_-GR0EDHM1SO" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "R3yYtBPkM2qZ" }, "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": "6Y8E0lw5eYWm" }, "source": [ "# 训练后动态范围量化" ] }, { "cell_type": "markdown", "metadata": { "id": "CIGrZZPTZVeO" }, "source": [ "\n", " \n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org 上查看在 Google Colab 中运行 在 Github 上查看源代码 下载笔记本 查看 TF Hub 模型 \n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "BTC1rDAuei_1" }, "source": [ "## 概述\n", "\n", "[TensorFlow Lite](https://tensorflow.google.cn/lite/) 现在支持将权重转换为 8 位精度,作为从 TensorFlow GraphDef 到 TensorFlow Lite FlatBuffer 格式的模型转换的一部分。动态范围量化能使模型大小缩减至原来的四分之一。此外,TFLite 支持对激活进行实时量化和反量化以实现以下效果:\n", "\n", "1. 在可用时使用量化内核加快实现速度。\n", "2. 将计算图不同部分的浮点内核与量化内核混合。\n", "\n", "激活始终以浮点进行存储。对于支持量化内核的算子,激活会在处理前动态量化为 8 位精度,并在处理后反量化为浮点精度。根据被转换的模型,这可以提供比纯浮点计算更快的速度。\n", "\n", "与[量化感知训练](https://github.com/tensorflow/tensorflow/tree/r1.14/tensorflow/contrib/quantize)相比,在此方法中,权重会在训练后量化,激活会在推断时动态量化。因此,不会重新训练模型权重以补偿量化引起的误差。请务必检查量化模型的准确率,以确保下降程度可以接受。\n", "\n", "本教程将从头开始训练一个 MNIST 模型,在 TensorFlow 中检查其准确率,然后使用动态范围量化将此模型转换为 Tensorflow Lite FlatBuffer 格式。最后,检查转换后模型的准确率,并将其与原始浮点模型进行比较。" ] }, { "cell_type": "markdown", "metadata": { "id": "2XsEP17Zelz9" }, "source": [ "## 构建 MNIST 模型" ] }, { "cell_type": "markdown", "metadata": { "id": "dDqqUIZjZjac" }, "source": [ "### 设置" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "gyqAw1M9lyab" }, "outputs": [], "source": [ "import logging\n", "logging.getLogger(\"tensorflow\").setLevel(logging.DEBUG)\n", "\n", "import tensorflow as tf\n", "from tensorflow import keras\n", "import numpy as np\n", "import pathlib" ] }, { "cell_type": "markdown", "metadata": { "id": "eQ6Q0qqKZogR" }, "source": [ "### 训练 TensorFlow 模型" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hWSAjQWagIHl" }, "outputs": [], "source": [ "# Load MNIST dataset\n", "mnist = keras.datasets.mnist\n", "(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n", "\n", "# Normalize the input image so that each pixel value is between 0 to 1.\n", "train_images = train_images / 255.0\n", "test_images = test_images / 255.0\n", "\n", "# Define the model architecture\n", "model = keras.Sequential([\n", " keras.layers.InputLayer(input_shape=(28, 28)),\n", " keras.layers.Reshape(target_shape=(28, 28, 1)),\n", " keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation=tf.nn.relu),\n", " keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", " keras.layers.Flatten(),\n", " keras.layers.Dense(10)\n", "])\n", "\n", "# Train the digit classification model\n", "model.compile(optimizer='adam',\n", " loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", " metrics=['accuracy'])\n", "model.fit(\n", " train_images,\n", " train_labels,\n", " epochs=1,\n", " validation_data=(test_images, test_labels)\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "5NMaNZQCkW9X" }, "source": [ "在此示例中,由于您只对模型进行了一个周期的训练,因此只训练到约 96% 的准确率。\n" ] }, { "cell_type": "markdown", "metadata": { "id": "xl8_fzVAZwOh" }, "source": [ "### 转换为 TensorFlow Lite 模型\n", "\n", "现在,您可以使用 TensorFlow Lite [Converter](https://tensorflow.google.cn/lite/models/convert) 将训练后的模型转换为 TensorFlow Lite 模型。\n", "\n", "现在使用 `TFLiteConverter` 加载模型:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_i8B2nDZmAgQ" }, "outputs": [], "source": [ "converter = tf.lite.TFLiteConverter.from_keras_model(model)\n", "tflite_model = converter.convert()" ] }, { "cell_type": "markdown", "metadata": { "id": "F2o2ZfF0aiCx" }, "source": [ "将其写入 TFLite 文件:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vptWZq2xnclo" }, "outputs": [], "source": [ "tflite_models_dir = pathlib.Path(\"/tmp/mnist_tflite_models/\")\n", "tflite_models_dir.mkdir(exist_ok=True, parents=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Ie9pQaQrn5ue" }, "outputs": [], "source": [ "tflite_model_file = tflite_models_dir/\"mnist_model.tflite\"\n", "tflite_model_file.write_bytes(tflite_model)" ] }, { "cell_type": "markdown", "metadata": { "id": "7BONhYtYocQY" }, "source": [ "要在导出时量化模型,请设置 `optimizations` 标记以优化大小:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "g8PUvLWDlmmz" }, "outputs": [], "source": [ "converter.optimizations = [tf.lite.Optimize.DEFAULT]\n", "tflite_quant_model = converter.convert()\n", "tflite_model_quant_file = tflite_models_dir/\"mnist_model_quant.tflite\"\n", "tflite_model_quant_file.write_bytes(tflite_quant_model)" ] }, { "cell_type": "markdown", "metadata": { "id": "PhMmUTl4sbkz" }, "source": [ "请注意,生成文件的大小约为 `1/4`。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "JExfcfLDscu4" }, "outputs": [], "source": [ "!ls -lh {tflite_models_dir}" ] }, { "cell_type": "markdown", "metadata": { "id": "L8lQHMp_asCq" }, "source": [ "## 运行 TFLite 模型\n", "\n", "使用 Python TensorFlow Lite 解释器运行 TensorFlow Lite 模型。\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Ap_jE7QRvhPf" }, "source": [ "### 将模型加载到解释器中" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Jn16Rc23zTss" }, "outputs": [], "source": [ "interpreter = tf.lite.Interpreter(model_path=str(tflite_model_file))\n", "interpreter.allocate_tensors()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "J8Pztk1mvNVL" }, "outputs": [], "source": [ "interpreter_quant = tf.lite.Interpreter(model_path=str(tflite_model_quant_file))\n", "interpreter_quant.allocate_tensors()" ] }, { "cell_type": "markdown", "metadata": { "id": "2opUt_JTdyEu" }, "source": [ "### 在单个图像上测试模型" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "AKslvo2kwWac" }, "outputs": [], "source": [ "test_image = np.expand_dims(test_images[0], axis=0).astype(np.float32)\n", "\n", "input_index = interpreter.get_input_details()[0][\"index\"]\n", "output_index = interpreter.get_output_details()[0][\"index\"]\n", "\n", "interpreter.set_tensor(input_index, test_image)\n", "interpreter.invoke()\n", "predictions = interpreter.get_tensor(output_index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "XZClM2vo3_bm" }, "outputs": [], "source": [ "import matplotlib.pylab as plt\n", "\n", "plt.imshow(test_images[0])\n", "template = \"True:{true}, predicted:{predict}\"\n", "_ = plt.title(template.format(true= str(test_labels[0]),\n", " predict=str(np.argmax(predictions[0]))))\n", "plt.grid(False)" ] }, { "cell_type": "markdown", "metadata": { "id": "LwN7uIdCd8Gw" }, "source": [ "### 评估模型" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "05aeAuWjvjPx" }, "outputs": [], "source": [ "# A helper function to evaluate the TF Lite model using \"test\" dataset.\n", "def evaluate_model(interpreter):\n", " input_index = interpreter.get_input_details()[0][\"index\"]\n", " output_index = interpreter.get_output_details()[0][\"index\"]\n", "\n", " # Run predictions on every image in the \"test\" dataset.\n", " prediction_digits = []\n", " for test_image in test_images:\n", " # Pre-processing: add batch dimension and convert to float32 to match with\n", " # the model's input data format.\n", " test_image = np.expand_dims(test_image, axis=0).astype(np.float32)\n", " interpreter.set_tensor(input_index, test_image)\n", "\n", " # Run inference.\n", " interpreter.invoke()\n", "\n", " # Post-processing: remove batch dimension and find the digit with highest\n", " # probability.\n", " output = interpreter.tensor(output_index)\n", " digit = np.argmax(output()[0])\n", " prediction_digits.append(digit)\n", "\n", " # Compare prediction results with ground truth labels to calculate accuracy.\n", " accurate_count = 0\n", " for index in range(len(prediction_digits)):\n", " if prediction_digits[index] == test_labels[index]:\n", " accurate_count += 1\n", " accuracy = accurate_count * 1.0 / len(prediction_digits)\n", "\n", " return accuracy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "DqXBnDfJ7qxL" }, "outputs": [], "source": [ "print(evaluate_model(interpreter))" ] }, { "cell_type": "markdown", "metadata": { "id": "Km3cY9ry8ZlG" }, "source": [ "在动态范围量化模型上重复评估,以获得如下结果:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "-9cnwiPp6EGm" }, "outputs": [], "source": [ "print(evaluate_model(interpreter_quant))" ] }, { "cell_type": "markdown", "metadata": { "id": "L7lfxkor8pgv" }, "source": [ "在此示例中,压缩后的模型在准确率方面没有差别。" ] }, { "cell_type": "markdown", "metadata": { "id": "M0o1FtmWeKZm" }, "source": [ "## 优化现有模型\n", "\n", "带有预激活层的 ResNet (ResNet-v2) 被广泛用于视觉应用。用于 ResNet-v2-101 的预训练冻结计算图可在 [Tensorflow Hub](https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4) 上获得。\n", "\n", "您可以通过执行以下代码,使用量化将冻结计算图转换为 TensorFLow Lite FlatBuffer 格式:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "jrXZxSJiJfYN" }, "outputs": [], "source": [ "import tensorflow_hub as hub\n", "\n", "resnet_v2_101 = tf.keras.Sequential([\n", " keras.layers.InputLayer(input_shape=(224, 224, 3)),\n", " hub.KerasLayer(\"https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4\")\n", "])\n", "\n", "converter = tf.lite.TFLiteConverter.from_keras_model(resnet_v2_101)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "LwnV4KxwVEoG" }, "outputs": [], "source": [ "# Convert to TF Lite without quantization\n", "resnet_tflite_file = tflite_models_dir/\"resnet_v2_101.tflite\"\n", "resnet_tflite_file.write_bytes(converter.convert())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2qkZD0VoVExe" }, "outputs": [], "source": [ "# Convert to TF Lite with quantization\n", "converter.optimizations = [tf.lite.Optimize.DEFAULT]\n", "resnet_quantized_tflite_file = tflite_models_dir/\"resnet_v2_101_quantized.tflite\"\n", "resnet_quantized_tflite_file.write_bytes(converter.convert())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vhOjeg1x9Knp" }, "outputs": [], "source": [ "!ls -lh {tflite_models_dir}/*.tflite" ] }, { "cell_type": "markdown", "metadata": { "id": "qqHLaqFMCjRZ" }, "source": [ "模型大小从 171 MB 减小到 43 MB。可以使用为 [TFLite 准确率测量](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/evaluation/tasks/imagenet_image_classification)提供的脚本来评估此模型在 ImageNet 上的准确率。\n", "\n", "优化后模型的 Top-1 准确率为 76.8,与浮点模型相同。" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "post_training_quant.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }