|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# tflite tensorflow lite" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 2, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# 把编译好的模型,可以转换成tflite的格式,适合在很弱的cpu的边缘设备上运行" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 2, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "## 模型的转化\n", |
| 28 | + "# 把h5模型文件转换成 tflite的类型\n", |
| 29 | + "import tensorflow as tf\n", |
| 30 | + "from tensorflow.keras.applications.mobilenet_v2 import *\n", |
| 31 | + "model = tf.keras.applications.mobilenet_v2.MobileNetV2(weights='imagenet')\n", |
| 32 | + "model.save(\"xx.h5\")" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 21, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [ |
| 40 | + { |
| 41 | + "name": "stdout", |
| 42 | + "output_type": "stream", |
| 43 | + "text": [ |
| 44 | + "66058900\n" |
| 45 | + ] |
| 46 | + } |
| 47 | + ], |
| 48 | + "source": [ |
| 49 | + "import cv2\n", |
| 50 | + "import numpy as np\n", |
| 51 | + "import time\n", |
| 52 | + "image = cv2.imread('ddd.jpg')\n", |
| 53 | + "x = cv2.resize(image,(224,224))\n", |
| 54 | + "## 模型预测\n", |
| 55 | + "decode_predictions(model.predict(np.array([x])/255.0))\n" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 5, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "name": "stdout", |
| 65 | + "output_type": "stream", |
| 66 | + "text": [ |
| 67 | + "WARNING:tensorflow:From C:\\Users\\Administrator\\Anaconda3\\envs\\tfenv\\lib\\site-packages\\tensorflow\\python\\training\\tracking\\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n", |
| 68 | + "Instructions for updating:\n", |
| 69 | + "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", |
| 70 | + "WARNING:tensorflow:From C:\\Users\\Administrator\\Anaconda3\\envs\\tfenv\\lib\\site-packages\\tensorflow\\python\\training\\tracking\\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n", |
| 71 | + "Instructions for updating:\n", |
| 72 | + "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", |
| 73 | + "INFO:tensorflow:Assets written to: C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpimh742dq\\assets\n" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "data": { |
| 78 | + "text/plain": [ |
| 79 | + "13988516" |
| 80 | + ] |
| 81 | + }, |
| 82 | + "execution_count": 5, |
| 83 | + "metadata": {}, |
| 84 | + "output_type": "execute_result" |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "## tflite \n", |
| 89 | + "converter = tf.lite.TFLiteConverter.from_keras_model(model)\n", |
| 90 | + "## 使用转换器进行模型的转化\n", |
| 91 | + "tflite_model = converter.convert()\n", |
| 92 | + "\n", |
| 93 | + "## 保存tflite的模型\n", |
| 94 | + "\n", |
| 95 | + "open(\"xxx.tflite\",\"wb\").write(tflite_model)" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 6, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "## 转换成tflite文件大小发生了变换,存储方式也发生了变换。\n" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "## 使用tflite的模型来预测数据" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 36, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "import cv2\n", |
| 121 | + "import numpy as np\n", |
| 122 | + "image = cv2.imread('ddd.jpg')\n", |
| 123 | + "x = cv2.resize(image,(224,224))\n", |
| 124 | + "x = np.array([x])/255.0" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 37, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "[{'name': 'input_1', 'index': 0, 'shape': array([ 1, 224, 224, 3]), 'shape_signature': array([ -1, 224, 224, 3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]\n", |
| 137 | + "[{'name': 'Identity', 'index': 178, 'shape': array([ 1, 1000]), 'shape_signature': array([ -1, 1000]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]\n", |
| 138 | + "[[('n02480855', 'gorilla', 0.66071), ('n02480495', 'orangutan', 0.17576733), ('n02481823', 'chimpanzee', 0.00764068), ('n02484975', 'guenon', 0.004435742), ('n02486410', 'baboon', 0.0038167273)]]\n" |
| 139 | + ] |
| 140 | + } |
| 141 | + ], |
| 142 | + "source": [ |
| 143 | + "interpreter = tf.lite.Interpreter(\"xxx.tflite\") # 得到tflite的模型解释器\n", |
| 144 | + "interpreter.allocate_tensors() ## 声明计算所需要的空间\n", |
| 145 | + "\n", |
| 146 | + "input_details = interpreter.get_input_details() # 输入\n", |
| 147 | + "output_details = interpreter.get_output_details() #输出\n", |
| 148 | + "\n", |
| 149 | + "print(input_details)\n", |
| 150 | + "print(output_details)\n", |
| 151 | + "\n", |
| 152 | + "interpreter.set_tensor(input_details[0]['index'],x.astype(np.float32)) #指定输入\n", |
| 153 | + "\n", |
| 154 | + "## 运行推理 \n", |
| 155 | + "interpreter.invoke()\n", |
| 156 | + "\n", |
| 157 | + "## 获取计算出来的结果\n", |
| 158 | + "results = interpreter.get_tensor(output_details[0]['index'])\n", |
| 159 | + "\n", |
| 160 | + "print(decode_predictions(results))" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "metadata": {}, |
| 166 | + "source": [ |
| 167 | + "# 树莓派 cpu,arm平台。\n", |
| 168 | + "\n", |
| 169 | + "* 链接:https://pan.baidu.com/s/1xPxepH6eLdlW3HNmZvVmUw \n", |
| 170 | + "* 提取码:1234 \n" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "sudo apt-get install libjpeg8-dev zlib1g-dev\n", |
| 180 | + "sudo apt-get install Pillow==7.2.0\n", |
| 181 | + "sudo apt-get install Cython\n", |
| 182 | + "pip3 install scikit-build\n", |
| 183 | + "\n", |
| 184 | + "pip3 install opencv-python 三个小时\n", |
| 185 | + "https://github.com/itheima1/tensorflow-on-aarch64" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "metadata": { |
| 190 | + "kernelspec": { |
| 191 | + "display_name": "Python 3", |
| 192 | + "language": "python", |
| 193 | + "name": "python3" |
| 194 | + }, |
| 195 | + "language_info": { |
| 196 | + "codemirror_mode": { |
| 197 | + "name": "ipython", |
| 198 | + "version": 3 |
| 199 | + }, |
| 200 | + "file_extension": ".py", |
| 201 | + "mimetype": "text/x-python", |
| 202 | + "name": "python", |
| 203 | + "nbconvert_exporter": "python", |
| 204 | + "pygments_lexer": "ipython3", |
| 205 | + "version": "3.7.9" |
| 206 | + } |
| 207 | + }, |
| 208 | + "nbformat": 4, |
| 209 | + "nbformat_minor": 4 |
| 210 | +} |
0 commit comments