Last active
June 29, 2021 07:10
-
-
Save Studentenfutter/ac48c41b0e5c4c43b3586008d4dc3877 to your computer and use it in GitHub Desktop.
yolov5-to-coral-tpu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "yolov5-to-coral-tpu", | |
"provenance": [], | |
"authorship_tag": "ABX9TyOrA0ElDFyaD0nxgHCazBwM", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "TPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Studentenfutter/ac48c41b0e5c4c43b3586008d4dc3877/untitled1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Rlos3A6TyDoE" | |
}, | |
"source": [ | |
"# Conversion of a Yolov5 Pytorch Model to tflite to Coral TPU" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "0JAeICOcyzEf" | |
}, | |
"source": [ | |
"Installation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "SDE8iFvAyWw9", | |
"outputId": "f182c64f-52ad-4e5c-d4ca-4c2c67e2d259" | |
}, | |
"source": [ | |
"!git clone https://github.com/ultralytics/yolov5 # clone repo" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Cloning into 'yolov5'...\n", | |
"remote: Enumerating objects: 7643, done.\u001b[K\n", | |
"remote: Counting objects: 100% (363/363), done.\u001b[K\n", | |
"remote: Compressing objects: 100% (171/171), done.\u001b[K\n", | |
"remote: Total 7643 (delta 225), reused 272 (delta 189), pack-reused 7280\u001b[K\n", | |
"Receiving objects: 100% (7643/7643), 9.03 MiB | 24.78 MiB/s, done.\n", | |
"Resolving deltas: 100% (5243/5243), done.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "I3DB85UWx164", | |
"outputId": "54770d87-3f75-42a8-c155-46a6783598c2" | |
}, | |
"source": [ | |
"!pip install -r yolov5/requirements.txt # base requirements\n", | |
"!pip install coremltools>=4.1 onnx>=1.9.0 scikit-learn>=0.19.2 # export requirements" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: matplotlib>=3.2.2 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 4)) (3.2.2)\n", | |
"Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 5)) (1.19.5)\n", | |
"Requirement already satisfied: opencv-python>=4.1.2 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 6)) (4.1.2.30)\n", | |
"Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 7)) (7.1.2)\n", | |
"Collecting PyYAML>=5.3.1\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/7a/a5/393c087efdc78091afa2af9f1378762f9821c9c1d7a22c5753fb5ac5f97a/PyYAML-5.4.1-cp37-cp37m-manylinux1_x86_64.whl (636kB)\n", | |
"\u001b[K |████████████████████████████████| 645kB 4.9MB/s \n", | |
"\u001b[?25hRequirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 9)) (1.4.1)\n", | |
"Requirement already satisfied: torch>=1.7.0 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 10)) (1.9.0+cu102)\n", | |
"Requirement already satisfied: torchvision>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 11)) (0.10.0+cu102)\n", | |
"Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 12)) (4.41.1)\n", | |
"Requirement already satisfied: tensorboard>=2.4.1 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 15)) (2.5.0)\n", | |
"Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 19)) (0.11.1)\n", | |
"Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from -r yolov5/requirements.txt (line 20)) (1.1.5)\n", | |
"Collecting thop\n", | |
" Downloading https://files.pythonhosted.org/packages/6c/8b/22ce44e1c71558161a8bd54471123cc796589c7ebbfc15a7e8932e522f83/thop-0.0.31.post2005241907-py3-none-any.whl\n", | |
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r yolov5/requirements.txt (line 4)) (2.4.7)\n", | |
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r yolov5/requirements.txt (line 4)) (1.3.1)\n", | |
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r yolov5/requirements.txt (line 4)) (0.10.0)\n", | |
"Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r yolov5/requirements.txt (line 4)) (2.8.1)\n", | |
"Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.7.0->-r yolov5/requirements.txt (line 10)) (3.7.4.3)\n", | |
"Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.6.1)\n", | |
"Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (57.0.0)\n", | |
"Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.12.0)\n", | |
"Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.34.1)\n", | |
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.8.0)\n", | |
"Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.31.0)\n", | |
"Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.36.2)\n", | |
"Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.0.1)\n", | |
"Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.4.4)\n", | |
"Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (3.12.4)\n", | |
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (2.23.0)\n", | |
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (3.3.4)\n", | |
"Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->-r yolov5/requirements.txt (line 20)) (2018.9)\n", | |
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from cycler>=0.10->matplotlib>=3.2.2->-r yolov5/requirements.txt (line 4)) (1.15.0)\n", | |
"Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (4.2.2)\n", | |
"Requirement already satisfied: rsa<5,>=3.1.4; python_version >= \"3.6\" in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (4.7.2)\n", | |
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.2.8)\n", | |
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.3.0)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (2021.5.30)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (1.24.3)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (2.10)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (3.0.4)\n", | |
"Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (4.5.0)\n", | |
"Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/dist-packages (from rsa<5,>=3.1.4; python_version >= \"3.6\"->google-auth<2,>=1.6.3->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (0.4.8)\n", | |
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (3.1.1)\n", | |
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < \"3.8\"->markdown>=2.6.8->tensorboard>=2.4.1->-r yolov5/requirements.txt (line 15)) (3.4.1)\n", | |
"Installing collected packages: PyYAML, thop\n", | |
" Found existing installation: PyYAML 3.13\n", | |
" Uninstalling PyYAML-3.13:\n", | |
" Successfully uninstalled PyYAML-3.13\n", | |
"Successfully installed PyYAML-5.4.1 thop-0.0.31.post2005241907\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "_twNr-KMzLP7" | |
}, | |
"source": [ | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zyGsXN9wy1L5" | |
}, | |
"source": [ | |
"# Model conversion\n", | |
"\n", | |
"Manually upload a generated weights.pt (maske.pt) from Yolov5 to the project and convert it\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "gWRZDG3Nyr6l", | |
"outputId": "54c4a7cf-e6f3-4242-c7f8-17838bdc7a19" | |
}, | |
"source": [ | |
"!python yolov5/export.py --weights maske.pt --img 640 --batch 1 # export at 640x640 with batch size 1" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"\u001b[34m\u001b[1mexport: \u001b[0mweights=maske.pt, img_size=[640], batch_size=1, device=cpu, include=['torchscript', 'onnx', 'coreml'], half=False, inplace=False, train=False, optimize=False, dynamic=False, simplify=False, opset_version=12\n", | |
"YOLOv5 🚀 v5.0-250-g02719dd torch 1.9.0+cu102 CPU\n", | |
"\n", | |
"Fusing layers... \n", | |
"/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)\n", | |
" return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)\n", | |
"Model Summary: 224 layers, 7056607 parameters, 0 gradients\n", | |
"\n", | |
"\u001b[34m\u001b[1mPyTorch:\u001b[0m starting from maske.pt (14.4 MB)\n", | |
"\n", | |
"\u001b[34m\u001b[1mTorchScript:\u001b[0m starting export with torch 1.9.0+cu102...\n", | |
"/content/yolov5/models/yolo.py:58: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n", | |
" if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic:\n", | |
"\u001b[34m\u001b[1mTorchScript:\u001b[0m export success, saved as maske.torchscript.pt (28.6 MB)\n", | |
"\u001b[34m\u001b[1mONNX:\u001b[0m starting export with onnx 1.9.0...\n", | |
"\u001b[34m\u001b[1mONNX:\u001b[0m export success, saved as maske.onnx (28.3 MB)\n", | |
"scikit-learn version 0.22.2.post1 is not supported. Minimum required version: 0.17. Maximum required version: 0.19.2. Disabling scikit-learn conversion API.\n", | |
"2021-06-28 14:48:37.070515: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n", | |
"TensorFlow version 2.5.0 detected. Last version known to be fully compatible is 2.3.1 .\n", | |
"Keras version 2.5.0 detected. Last version known to be fully compatible of Keras is 2.2.4 .\n", | |
"\u001b[34m\u001b[1mCoreML:\u001b[0m starting export with coremltools 4.1...\n", | |
"\u001b[34m\u001b[1mCoreML:\u001b[0m export failure: CoreML exports should be placed in model.train() mode with `python export.py --train`\n", | |
"\n", | |
"Export complete (11.16s). Visualize with https://github.com/lutzroeder/netron.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "lUSITFNezcVr" | |
}, | |
"source": [ | |
"## ONXX to tflite\n", | |
"\n", | |
"Once successful, we convert the ONXX-model to a tflite model using special parameters to use a older TensorFlow version" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "1TlXJ_AdGbJP", | |
"outputId": "4e28d619-9ae9-42bb-c4d6-52d3f086e788" | |
}, | |
"source": [ | |
"!pip install tensorflow-addons tensorflow onnx" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting tensorflow-addons\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/66/4b/e893d194e626c24b3df2253066aa418f46a432fdb68250cde14bf9bb0700/tensorflow_addons-0.13.0-cp37-cp37m-manylinux2010_x86_64.whl (679kB)\n", | |
"\u001b[K |████████████████████████████████| 686kB 5.1MB/s \n", | |
"\u001b[?25hRequirement already satisfied: tensorflow in /usr/local/lib/python3.7/dist-packages (2.5.0)\n", | |
"Requirement already satisfied: onnx in /usr/local/lib/python3.7/dist-packages (1.9.0)\n", | |
"Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.7/dist-packages (from tensorflow-addons) (2.7.1)\n", | |
"Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.1.0)\n", | |
"Requirement already satisfied: tensorflow-estimator<2.6.0,>=2.5.0rc0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (2.5.0)\n", | |
"Requirement already satisfied: astunparse~=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.6.3)\n", | |
"Requirement already satisfied: wrapt~=1.12.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.12.1)\n", | |
"Requirement already satisfied: gast==0.4.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (0.4.0)\n", | |
"Requirement already satisfied: h5py~=3.1.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (3.1.0)\n", | |
"Requirement already satisfied: numpy~=1.19.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.19.5)\n", | |
"Requirement already satisfied: typing-extensions~=3.7.4 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (3.7.4.3)\n", | |
"Requirement already satisfied: grpcio~=1.34.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.34.1)\n", | |
"Requirement already satisfied: wheel~=0.35 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (0.36.2)\n", | |
"Requirement already satisfied: keras-nightly~=2.5.0.dev in /usr/local/lib/python3.7/dist-packages (from tensorflow) (2.5.0.dev2021032900)\n", | |
"Requirement already satisfied: keras-preprocessing~=1.1.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.1.2)\n", | |
"Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (3.12.4)\n", | |
"Requirement already satisfied: flatbuffers~=1.12.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.12)\n", | |
"Requirement already satisfied: opt-einsum~=3.3.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (3.3.0)\n", | |
"Requirement already satisfied: google-pasta~=0.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (0.2.0)\n", | |
"Requirement already satisfied: six~=1.15.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (1.15.0)\n", | |
"Requirement already satisfied: tensorboard~=2.5 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (2.5.0)\n", | |
"Requirement already satisfied: absl-py~=0.10 in /usr/local/lib/python3.7/dist-packages (from tensorflow) (0.12.0)\n", | |
"Requirement already satisfied: cached-property; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from h5py~=3.1.0->tensorflow) (1.5.2)\n", | |
"Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from protobuf>=3.9.2->tensorflow) (57.0.0)\n", | |
"Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (0.6.1)\n", | |
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (1.8.0)\n", | |
"Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (0.4.4)\n", | |
"Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (1.0.1)\n", | |
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (3.3.4)\n", | |
"Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (1.31.0)\n", | |
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.5->tensorflow) (2.23.0)\n", | |
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.5->tensorflow) (1.3.0)\n", | |
"Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard~=2.5->tensorflow) (4.5.0)\n", | |
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (0.2.8)\n", | |
"Requirement already satisfied: rsa<5,>=3.1.4; python_version >= \"3.6\" in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (4.7.2)\n", | |
"Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (4.2.2)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.5->tensorflow) (2021.5.30)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.5->tensorflow) (1.24.3)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.5->tensorflow) (3.0.4)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.5->tensorflow) (2.10)\n", | |
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.5->tensorflow) (3.1.1)\n", | |
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < \"3.8\"->markdown>=2.6.8->tensorboard~=2.5->tensorflow) (3.4.1)\n", | |
"Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.7/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (0.4.8)\n", | |
"Installing collected packages: tensorflow-addons\n", | |
"Successfully installed tensorflow-addons-0.13.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ssKK59OYF2v8", | |
"outputId": "34a46aca-0e21-4f75-a1b7-cd7f4ad6f509" | |
}, | |
"source": [ | |
"# load tf\n", | |
"import tensorflow as tf\n", | |
"import tensorflow_addons as tfa\n", | |
"import sys\n", | |
"\n", | |
"print(tf.__version__)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"2.5.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "UgOSe2zFHG0l" | |
}, | |
"source": [ | |
"Install onnx from github" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "mOZZK2myHGiZ", | |
"outputId": "91790f85-55d7-4413-8ea3-9484e8417833" | |
}, | |
"source": [ | |
"%%bash\n", | |
"pip install onnx tensorflow-addons\n", | |
"git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow && pip install -e ." | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: onnx in /usr/local/lib/python3.7/dist-packages (1.9.0)\n", | |
"Requirement already satisfied: tensorflow-addons in /usr/local/lib/python3.7/dist-packages (0.13.0)\n", | |
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from onnx) (1.15.0)\n", | |
"Requirement already satisfied: numpy>=1.16.6 in /usr/local/lib/python3.7/dist-packages (from onnx) (1.19.5)\n", | |
"Requirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.7/dist-packages (from onnx) (3.7.4.3)\n", | |
"Requirement already satisfied: protobuf in /usr/local/lib/python3.7/dist-packages (from onnx) (3.12.4)\n", | |
"Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.7/dist-packages (from tensorflow-addons) (2.7.1)\n", | |
"Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from protobuf->onnx) (57.0.0)\n", | |
"Obtaining file:///content/onnx-tensorflow\n", | |
"Requirement already satisfied: onnx>=1.8.0 in /usr/local/lib/python3.7/dist-packages (from onnx-tf==1.8.0) (1.9.0)\n", | |
"Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from onnx-tf==1.8.0) (5.4.1)\n", | |
"Requirement already satisfied: tensorflow_addons in /usr/local/lib/python3.7/dist-packages (from onnx-tf==1.8.0) (0.13.0)\n", | |
"Requirement already satisfied: protobuf in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx-tf==1.8.0) (3.12.4)\n", | |
"Requirement already satisfied: numpy>=1.16.6 in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx-tf==1.8.0) (1.19.5)\n", | |
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx-tf==1.8.0) (1.15.0)\n", | |
"Requirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx-tf==1.8.0) (3.7.4.3)\n", | |
"Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.7/dist-packages (from tensorflow_addons->onnx-tf==1.8.0) (2.7.1)\n", | |
"Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from protobuf->onnx>=1.8.0->onnx-tf==1.8.0) (57.0.0)\n", | |
"Installing collected packages: onnx-tf\n", | |
" Running setup.py develop for onnx-tf\n", | |
"Successfully installed onnx-tf\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Cloning into 'onnx-tensorflow'...\n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kmC-HKcNICin" | |
}, | |
"source": [ | |
"**IMPORTANT**: In Google Colab, restart the Kernel after this chunk got executed, then import the onxx_tf.backend package." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_ARuMvjcHB4t" | |
}, | |
"source": [ | |
"import onnx\n", | |
"from onnx_tf.backend import prepare" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9iZBBrfJGO4z" | |
}, | |
"source": [ | |
"Convert model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "7bJp0pFsGPY0", | |
"outputId": "8f88b5b2-14b9-49ff-923d-7a29d5980dcd" | |
}, | |
"source": [ | |
"# Convert model.onnx to Tensorflow\n", | |
"onnx_model = onnx.load('maske.onnx')\n", | |
"onnx.checker.check_model(onnx_model) \n", | |
"tf_rep = prepare(onnx_model) \n", | |
"tf_rep.export_graph('maske') \n", | |
"\n", | |
"# Convert saved model to tflite\n", | |
"converter = tf.lite.TFLiteConverter.from_saved_model('maske')\n", | |
"#converter.experimental_new_converter = False # Turn off expermintal model conversion tf function!\n", | |
"tf_lite_model = converter.convert()\n", | |
"open('maske.tflite', 'wb').write(tf_lite_model)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"WARNING:absl:Found untraced functions such as gen_tensor_dict while saving (showing 1 of 1). These functions will not be directly callable after loading.\n" | |
], | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"INFO:tensorflow:Assets written to: maske/assets\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"INFO:tensorflow:Assets written to: maske/assets\n" | |
], | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"29101764" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "rvbnbUomL9Vm" | |
}, | |
"source": [ | |
"**TODO:** Experimental try to quantize the onnx model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pW2wvcEJMCay", | |
"outputId": "988aee4a-8d95-4336-c660-050c1e6f1895" | |
}, | |
"source": [ | |
"!git clone https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/python/tools/quantization" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Cloning into 'quantization'...\n", | |
"fatal: repository 'https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/python/tools/quantization/' not found\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "0lM_cY3DKkYu" | |
}, | |
"source": [ | |
"## tflite to colab\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "XaJRuTETKx1R" | |
}, | |
"source": [ | |
"Install the edgetpu compiler" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "p6ZpWgrk21Ad", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "04a9a409-cc54-4819-a041-d6454a1206e6" | |
}, | |
"source": [ | |
"! curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -\n", | |
"\n", | |
"! echo \"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list\n", | |
"\n", | |
"! sudo apt-get update\n", | |
"\n", | |
"! sudo apt-get install edgetpu-compiler\t" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
" % Total % Received % Xferd Average Speed Time Time Time Current\n", | |
" Dload Upload Total Spent Left Speed\n", | |
"\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\r100 2537 100 2537 0 0 123k 0 --:--:-- --:--:-- --:--:-- 123k\n", | |
"OK\n", | |
"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\n", | |
"Get:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]\n", | |
"Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]\n", | |
"Ign:3 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease\n", | |
"Get:4 https://packages.cloud.google.com/apt coral-edgetpu-stable InRelease [6,722 B]\n", | |
"Ign:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease\n", | |
"Get:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]\n", | |
"Get:7 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]\n", | |
"Hit:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release\n", | |
"Get:9 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]\n", | |
"Hit:10 http://archive.ubuntu.com/ubuntu bionic InRelease\n", | |
"Get:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n", | |
"Ign:12 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 Packages\n", | |
"Get:12 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 Packages [2,329 B]\n", | |
"Hit:13 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease\n", | |
"Get:14 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,220 kB]\n", | |
"Get:16 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]\n", | |
"Get:17 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [473 kB]\n", | |
"Get:18 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,418 kB]\n", | |
"Get:19 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]\n", | |
"Ign:20 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages\n", | |
"Get:20 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [599 kB]\n", | |
"Get:21 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease [21.3 kB]\n", | |
"Get:22 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,777 kB]\n", | |
"Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,188 kB]\n", | |
"Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,653 kB]\n", | |
"Get:25 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [909 kB]\n", | |
"Get:26 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [505 kB]\n", | |
"Get:27 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [40.9 kB]\n", | |
"Get:28 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main amd64 Packages [41.5 kB]\n", | |
"Fetched 13.1 MB in 4s (3,744 kB/s)\n", | |
"Reading package lists... Done\n", | |
"Reading package lists... Done\n", | |
"Building dependency tree \n", | |
"Reading state information... Done\n", | |
"The following NEW packages will be installed:\n", | |
" edgetpu-compiler\n", | |
"0 upgraded, 1 newly installed, 0 to remove and 81 not upgraded.\n", | |
"Need to get 7,865 kB of archives.\n", | |
"After this operation, 31.2 MB of additional disk space will be used.\n", | |
"Get:1 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 edgetpu-compiler amd64 15.0 [7,865 kB]\n", | |
"Fetched 7,865 kB in 0s (17.9 MB/s)\n", | |
"debconf: unable to initialize frontend: Dialog\n", | |
"debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 1.)\n", | |
"debconf: falling back to frontend: Readline\n", | |
"debconf: unable to initialize frontend: Readline\n", | |
"debconf: (This frontend requires a controlling tty.)\n", | |
"debconf: falling back to frontend: Teletype\n", | |
"dpkg-preconfigure: unable to re-open stdin: \n", | |
"Selecting previously unselected package edgetpu-compiler.\n", | |
"(Reading database ... 160772 files and directories currently installed.)\n", | |
"Preparing to unpack .../edgetpu-compiler_15.0_amd64.deb ...\n", | |
"Unpacking edgetpu-compiler (15.0) ...\n", | |
"Setting up edgetpu-compiler (15.0) ...\n", | |
"Processing triggers for libc-bin (2.27-3ubuntu1.2) ...\n", | |
"/sbin/ldconfig.real: /usr/local/lib/python3.7/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "GLnHVttbK50h" | |
}, | |
"source": [ | |
"Convert the model\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "joxrIB0I3cdi", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "1c3d22df-8eb4-4227-a6b8-86bbc6c8fa21" | |
}, | |
"source": [ | |
"! edgetpu_compiler maske.tflite" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Edge TPU Compiler version 15.0.340273435\n", | |
"Invalid model: maske.tflite\n", | |
"Model not quantized\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment