Created
June 27, 2024 18:55
-
-
Save jphdotam/7bb4e97099b9fefd718e789ceddabc23 to your computer and use it in GitHub Desktop.
Copy of ONNX-to-TF-to-TFJS-Colab.ipynb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jphdotam/7bb4e97099b9fefd718e789ceddabc23/copy-of-onnx-to-tf-to-tfjs-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3333ebe2", | |
"metadata": { | |
"id": "3333ebe2" | |
}, | |
"source": [ | |
"# **Setup Steps** - Required\n", | |
"1. Uncomment the first code cell (enter the code cell and press `Ctrl a` to highlight all and `Ctrl /` to uncomment the whole cell)\n", | |
"2. Run the first code cell to install the packages to convert the ONNX model to TensorFlow.js.\n", | |
"3. Restart the notebook runtime before continuing. To do so, open the `Runtime` menu and click `Restart runtime` in the drop-down menu as shown in the image below. Once the notebook kernel restarts do not run the first code cell again.\n", | |
"![google-colab-restart-notebook-runtime](https://github.com/cj-mills/christianjmills/raw/master/images/tfjs-yolox-unity-tutorial/google-colab-restart-notebook-runtime.png)\n", | |
"4. Re-comment the first code cell (enter the code cell and press `Ctrl a` to highlight all and `Ctrl /` to comment the whole cell).\n", | |
"5. Execute the notebook starting from the **Import dependencies for TensorFlow.js conversion** cell." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bd98bd08", | |
"metadata": { | |
"id": "bd98bd08" | |
}, | |
"source": [ | |
"**Install Tensorflow.js conversion dependencies**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%%capture\n", | |
"# !pip install onnxruntime\n", | |
"# !pip install onnx-tf\n", | |
"# !pip install tensorflowjs\n", | |
"# !pip install onnx-simplifier\n", | |
"# !pip install simple-onnx-processing-tools\n", | |
"# !pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com\n", | |
"\n", | |
"!pip uninstall -y tensorflow tensorflow-probability keras onnx-tf tf-keras\n", | |
"\n", | |
"# # Reinstall specific compatible versions\n", | |
"!pip install tensorflow==2.15.0 keras==2.15.0 tf-keras==2.15.0 tensorflow-probability==0.23 onnx-tf onnxruntime tensorflowjs onnx-simplifier simple-onnx-processing-tools # Downgrade TensorFlow to a more stable earlier version\n", | |
"\n", | |
"# # Reinstall other necessary packages\n", | |
"# !pip install onnx-tf\n", | |
"# !pip install onnxruntime\n", | |
"# !pip install tensorflowjs\n", | |
"# !pip install onnx-simplifier\n", | |
"# !pip install simple-onnx-processing-tools\n", | |
"!pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com" | |
], | |
"metadata": { | |
"id": "pWNbb8NPRh-_" | |
}, | |
"id": "pWNbb8NPRh-_", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"USE_UINT8 = True" | |
], | |
"metadata": { | |
"id": "fdBiKy6c55gV" | |
}, | |
"id": "fdBiKy6c55gV", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2b2b866c", | |
"metadata": { | |
"id": "2b2b866c" | |
}, | |
"source": [ | |
"**Import dependencies for TensorFlow.js conversion**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3337887c", | |
"metadata": { | |
"id": "3337887c", | |
"outputId": "578c40c8-258c-4a27-c445-3398fce74cc8", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"/usr/local/lib/python3.10/dist-packages/tensorflow_addons/utils/tfa_eol_msg.py:23: UserWarning: \n", | |
"\n", | |
"TensorFlow Addons (TFA) has ended development and introduction of new features.\n", | |
"TFA has entered a minimal maintenance and release mode until a planned end of life in May 2024.\n", | |
"Please modify downstream libraries to take dependencies from other repositories in our TensorFlow community (e.g. Keras, Keras-CV, and Keras-NLP). \n", | |
"\n", | |
"For more information see: https://github.com/tensorflow/addons/issues/2807 \n", | |
"\n", | |
" warnings.warn(\n" | |
] | |
} | |
], | |
"source": [ | |
"from pathlib import Path\n", | |
"import onnx\n", | |
"from scc4onnx import order_conversion\n", | |
"from onnxsim import simplify\n", | |
"from onnx_tf.backend import prepare" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a6d6b979", | |
"metadata": { | |
"id": "a6d6b979" | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"pd.set_option('max_colwidth', None)\n", | |
"pd.set_option('display.max_rows', None)\n", | |
"pd.set_option('display.max_columns', None)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "60d68be6", | |
"metadata": { | |
"id": "60d68be6" | |
}, | |
"source": [ | |
"**Download YOLOX model**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "68c43833", | |
"metadata": { | |
"id": "68c43833", | |
"outputId": "875c4b0b-546d-4618-9637-390fc5ddd3c8", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"--2024-06-11 07:00:01-- https://aid.mr/models/aifs/104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx\n", | |
"Resolving aid.mr (aid.mr)... 178.79.143.113\n", | |
"Connecting to aid.mr (aid.mr)|178.79.143.113|:443... connected.\n", | |
"HTTP request sent, awaiting response... 200 OK\n", | |
"Length: 20557248 (20M) [application/octet-stream]\n", | |
"Saving to: ‘104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx’\n", | |
"\n", | |
"104.yaml__ked7ywcs_ 100%[===================>] 19.60M 11.2MB/s in 1.8s \n", | |
"\n", | |
"2024-06-11 07:00:03 (11.2 MB/s) - ‘104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx’ saved [20557248/20557248]\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# !wget https://james.dev/103.yaml__6rp5565l__epoch=75__val_loss=0.013__iou=0.886.ckpt_cpu.onnx\n", | |
"# !wget https://james.dev/104.yaml__epoch=152__loss_val=0.181.ckpt.onnx\n", | |
"!wget https://aid.mr/models/aifs/104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6b0e890a", | |
"metadata": { | |
"id": "6b0e890a" | |
}, | |
"source": [ | |
"**Define data directory**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "aab07131", | |
"metadata": { | |
"id": "aab07131" | |
}, | |
"outputs": [], | |
"source": [ | |
"data_dir = Path('./')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "459f836f", | |
"metadata": { | |
"id": "459f836f" | |
}, | |
"source": [ | |
"**Get available ONNX files**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "8fc19b17", | |
"metadata": { | |
"id": "8fc19b17", | |
"outputId": "21ce6518-a61b-4e43-86b9-b67434d0257a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 81 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
" 0\n", | |
"0 104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx" | |
], | |
"text/html": [ | |
"\n", | |
" <div id=\"df-d26deaf1-c4e3-45dc-8d06-7b90ed422287\" class=\"colab-df-container\">\n", | |
" <div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>0</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>\n", | |
" <div class=\"colab-df-buttons\">\n", | |
"\n", | |
" <div class=\"colab-df-container\">\n", | |
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d26deaf1-c4e3-45dc-8d06-7b90ed422287')\"\n", | |
" title=\"Convert this dataframe to an interactive table.\"\n", | |
" style=\"display:none;\">\n", | |
"\n", | |
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n", | |
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n", | |
" </svg>\n", | |
" </button>\n", | |
"\n", | |
" <style>\n", | |
" .colab-df-container {\n", | |
" display:flex;\n", | |
" gap: 12px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert {\n", | |
" background-color: #E8F0FE;\n", | |
" border: none;\n", | |
" border-radius: 50%;\n", | |
" cursor: pointer;\n", | |
" display: none;\n", | |
" fill: #1967D2;\n", | |
" height: 32px;\n", | |
" padding: 0 0 0 0;\n", | |
" width: 32px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert:hover {\n", | |
" background-color: #E2EBFA;\n", | |
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
" fill: #174EA6;\n", | |
" }\n", | |
"\n", | |
" .colab-df-buttons div {\n", | |
" margin-bottom: 4px;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert {\n", | |
" background-color: #3B4455;\n", | |
" fill: #D2E3FC;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert:hover {\n", | |
" background-color: #434B5C;\n", | |
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
" fill: #FFFFFF;\n", | |
" }\n", | |
" </style>\n", | |
"\n", | |
" <script>\n", | |
" const buttonEl =\n", | |
" document.querySelector('#df-d26deaf1-c4e3-45dc-8d06-7b90ed422287 button.colab-df-convert');\n", | |
" buttonEl.style.display =\n", | |
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
"\n", | |
" async function convertToInteractive(key) {\n", | |
" const element = document.querySelector('#df-d26deaf1-c4e3-45dc-8d06-7b90ed422287');\n", | |
" const dataTable =\n", | |
" await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
" [key], {});\n", | |
" if (!dataTable) return;\n", | |
"\n", | |
" const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
" + ' to learn more about interactive tables.';\n", | |
" element.innerHTML = '';\n", | |
" dataTable['output_type'] = 'display_data';\n", | |
" await google.colab.output.renderOutput(dataTable, element);\n", | |
" const docLink = document.createElement('div');\n", | |
" docLink.innerHTML = docLinkHtml;\n", | |
" element.appendChild(docLink);\n", | |
" }\n", | |
" </script>\n", | |
" </div>\n", | |
"\n", | |
" </div>\n", | |
" </div>\n" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "dataframe", | |
"summary": "{\n \"name\": \"pd\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": 0,\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
], | |
"source": [ | |
"onnx_files = [p for p in Path( data_dir ).iterdir() if p.suffix == '.onnx']\n", | |
"pd.DataFrame([file.name for file in onnx_files])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e47e3230", | |
"metadata": { | |
"id": "e47e3230" | |
}, | |
"source": [ | |
"**Select ONNX model**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "8cd47288", | |
"metadata": { | |
"id": "8cd47288", | |
"outputId": "69957a50-4d25-4608-e412-a1d64ecac96a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 12 | |
} | |
], | |
"source": [ | |
"onnx_model_path = onnx_files[0]\n", | |
"onnx_model_path.name" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b1a6a79d", | |
"metadata": { | |
"id": "b1a6a79d" | |
}, | |
"source": [ | |
"**Load ONNX model**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b9fff192", | |
"metadata": { | |
"id": "b9fff192" | |
}, | |
"outputs": [], | |
"source": [ | |
"onnx_model = onnx.load(onnx_model_path)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f10c8f1d", | |
"metadata": { | |
"id": "f10c8f1d" | |
}, | |
"source": [ | |
"**Inspect model input**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "543cb69b", | |
"metadata": { | |
"id": "543cb69b", | |
"outputId": "820c5b68-8a3f-4b72-ddd3-d014ba4c145f", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"name: \"input\"\n", | |
"type {\n", | |
" tensor_type {\n", | |
" elem_type: 1\n", | |
" shape {\n", | |
" dim {\n", | |
" dim_param: \"batch_size\"\n", | |
" }\n", | |
" dim {\n", | |
" dim_value: 3\n", | |
" }\n", | |
" dim {\n", | |
" dim_param: \"h\"\n", | |
" }\n", | |
" dim {\n", | |
" dim_param: \"w\"\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"}" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 14 | |
} | |
], | |
"source": [ | |
"onnx_model.graph.input[0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c48f6775", | |
"metadata": { | |
"id": "c48f6775" | |
}, | |
"source": [ | |
"**Get input name**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "7aacd3e5", | |
"metadata": { | |
"id": "7aacd3e5", | |
"outputId": "0f69dbfe-ba9e-4a2e-b909-2df1df7f52d3", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'input'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 15 | |
} | |
], | |
"source": [ | |
"input_name = onnx_model.graph.input[0].name\n", | |
"input_name" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f112378c", | |
"metadata": { | |
"id": "f112378c" | |
}, | |
"source": [ | |
"**Convert model input to channels-last format**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "82153dfd", | |
"metadata": { | |
"id": "82153dfd" | |
}, | |
"outputs": [], | |
"source": [ | |
"onnx_model = order_conversion(\n", | |
" onnx_graph=onnx_model,\n", | |
" input_op_names_and_order_dims={f\"{input_name}\": [0,2,3,1]},\n", | |
" non_verbose=True\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9ae0237d", | |
"metadata": { | |
"id": "9ae0237d" | |
}, | |
"source": [ | |
"**Inspect updated model input**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d1ceb6b0", | |
"metadata": { | |
"id": "d1ceb6b0", | |
"outputId": "35f9794c-71fc-4cdf-8c27-8dc1b36188b0", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"name: \"input\"\n", | |
"type {\n", | |
" tensor_type {\n", | |
" elem_type: 1\n", | |
" shape {\n", | |
" dim {\n", | |
" dim_param: \"batch_size\"\n", | |
" }\n", | |
" dim {\n", | |
" dim_param: \"h\"\n", | |
" }\n", | |
" dim {\n", | |
" dim_param: \"w\"\n", | |
" }\n", | |
" dim {\n", | |
" dim_value: 3\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"}" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 17 | |
} | |
], | |
"source": [ | |
"onnx_model.graph.input[0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5a812ade", | |
"metadata": { | |
"id": "5a812ade" | |
}, | |
"source": [ | |
"**Simplify ONNX model**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "92de2c08", | |
"metadata": { | |
"id": "92de2c08", | |
"outputId": "954f8227-403a-4efe-aa80-a47618064b86", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 18 | |
} | |
], | |
"source": [ | |
"onnx_model, check = simplify(onnx_model)\n", | |
"check" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "50d13053", | |
"metadata": { | |
"id": "50d13053" | |
}, | |
"source": [ | |
"**Prepare ONNX model for Tensorflow Backend**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3f810568", | |
"metadata": { | |
"id": "3f810568" | |
}, | |
"outputs": [], | |
"source": [ | |
"tf_rep = prepare(onnx_model)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4d94eadc", | |
"metadata": { | |
"id": "4d94eadc" | |
}, | |
"source": [ | |
"**Define path for TensorFlow saved model directory**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "da9707bb", | |
"metadata": { | |
"id": "da9707bb", | |
"outputId": "05e6c843-4f73-4510-aacb-06962bbb7b4b", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 28 | |
} | |
], | |
"source": [ | |
"tf_model_dir = f\"./{onnx_model_path.name.rsplit('.',1)[0]}\"\n", | |
"tf_model_dir" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7c838290", | |
"metadata": { | |
"id": "7c838290" | |
}, | |
"source": [ | |
"**Export backend representation to a Tensorflow proto file**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ec5930f8", | |
"metadata": { | |
"id": "ec5930f8" | |
}, | |
"outputs": [], | |
"source": [ | |
"tf_rep.export_graph(tf_model_dir)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bbcfc09b", | |
"metadata": { | |
"id": "bbcfc09b" | |
}, | |
"source": [ | |
"**Define directory path to store tfjs model files**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2f40a9d1", | |
"metadata": { | |
"id": "2f40a9d1", | |
"outputId": "7468f0b2-f405-4b46-c12b-d802c7c5dfd4", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 30 | |
} | |
], | |
"source": [ | |
"tfjs_model_dir = f\"{tf_model_dir}-tfjs-{'uint8' if USE_UINT8 else 'native'}\"\n", | |
"tfjs_model_dir" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "cc25b9ac", | |
"metadata": { | |
"id": "cc25b9ac" | |
}, | |
"source": [ | |
"**Define arguments for tfjs converter script**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f9a3c322", | |
"metadata": { | |
"id": "f9a3c322" | |
}, | |
"outputs": [], | |
"source": [ | |
"from IPython.display import Markdown, display" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "69f00536", | |
"metadata": { | |
"id": "69f00536", | |
"outputId": "73360cf8-76cf-4255-8f7e-5540aac20f95", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 54 | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
], | |
"text/markdown": "```bash\ntensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model --signature_name=serving_default --saved_model_tags=serve \"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda\" \"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8\" --quantize_uint8\n```" | |
}, | |
"metadata": {} | |
} | |
], | |
"source": [ | |
"tfjs_convert_command = f\"\"\"tensorflowjs_converter\n", | |
" --input_format=tf_saved_model\n", | |
" --output_format=tfjs_graph_model\n", | |
" --signature_name=serving_default\n", | |
" --saved_model_tags=serve\n", | |
" \"{tf_model_dir}\"\n", | |
" \"{tfjs_model_dir}\"\n", | |
" \"\"\"\n", | |
"tfjs_convert_command = \" \".join(tfjs_convert_command.split())\n", | |
"\n", | |
"if USE_UINT8:\n", | |
" tfjs_convert_command += \" --quantize_uint8\"\n", | |
"\n", | |
"display(Markdown(f\"```bash\\n{tfjs_convert_command}\\n```\"))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ed4a703e", | |
"metadata": { | |
"id": "ed4a703e" | |
}, | |
"source": [ | |
"**Export SavedModel to TFJS format**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4c76310c", | |
"metadata": { | |
"id": "4c76310c", | |
"outputId": "cea02d5d-25ca-48d2-eecb-7d653984da08", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Exporting TensorFlow SavedModel to TensorFlow.js Graph model...\n", | |
"2024-06-11 07:02:15.105125: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", | |
"2024-06-11 07:02:15.105185: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", | |
"2024-06-11 07:02:15.106652: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", | |
"2024-06-11 07:02:16.345362: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n", | |
"weight StatefulPartitionedCall/Where_8 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_36 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_8 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_16 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_17 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_17 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_35 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Shape_59 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_18 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_34 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/ExpandDims_4 with shape (1, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_38 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_36 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_9 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_39 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_9 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_18 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_19 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_6 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_28 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_6 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_12 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_13 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_13 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_24 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Shape_36 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_14 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_23 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/ExpandDims_3 with shape (1, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_30 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_25 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_7 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_31 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_7 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_14 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_15 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_4 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_20 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_4 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_8 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_9 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_9 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_16 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Shape_22 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_10 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_15 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/ExpandDims_2 with shape (1, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_22 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_17 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_5 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_23 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_5 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_10 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_11 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_2 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_12 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_2 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_4 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_5 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_5 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_9 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Shape_11 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_6 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_8 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/ExpandDims_1 with shape (1, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_14 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_10 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_3 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_15 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_3 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_6 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_7 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight unknown_4 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight unknown_5 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_4 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_1 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_1 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_2 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Shape_2 with shape (2,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/SelectV2_2 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_1 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/ExpandDims with shape (1, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_6 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Const_3 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight unknown_7 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Where_1 with shape (0, 1) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/Cast_7 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/GatherV2_1 with shape (0,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_2 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/zeros_3 with shape (1,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_1 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_2 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_3 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_4 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_5 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_6 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond/else/_144/cond/range_1 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond/else/_144/cond/range with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_7 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_8 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/FloorMod_9 with shape () and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_1/else/_174/cond_1/range_1 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_1/else/_174/cond_1/range with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_2/else/_204/cond_2/range_1 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_2/else/_204/cond_2/range with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_3/else/_234/cond_3/range_1 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_3/else/_234/cond_3/range with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_4/else/_264/cond_4/range_1 with shape (4,) and dtype int64 was auto converted to the type int32\n", | |
"weight StatefulPartitionedCall/cond_4/else/_264/cond_4/range with shape (4,) and dtype int64 was auto converted to the type int32\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Exporting TensorFlow SavedModel to TensorFlow.js Graph model...\")\n", | |
"conversion_result = %sx $tfjs_convert_command\n", | |
"print(\"\\n\".join(conversion_result))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2ebcfae6", | |
"metadata": { | |
"id": "2ebcfae6", | |
"outputId": "3bf4980b-b8c3-42d6-97a9-054156f3f8f4", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
" 104\n", | |
" 104-tfjs-uint8\n", | |
" 104-tfjs-uint8.tar.gz\n", | |
"'104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda'\n", | |
"'104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda.onnx'\n", | |
"'104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8'\n", | |
" sample_data\n" | |
] | |
} | |
], | |
"source": [ | |
"!ls" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "8a090596", | |
"metadata": { | |
"id": "8a090596", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "bb787921-5ce7-45ad-84f3-4b9dff8d8656" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8/\n", | |
"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8/group1-shard2of2.bin\n", | |
"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8/model.json\n", | |
"./104.yaml__ked7ywcs__epoch=48__loss_val=0.004__iou_val=0.865.ckpt_cuda-tfjs-uint8/group1-shard1of2.bin\n" | |
] | |
} | |
], | |
"source": [ | |
"!tar vcfz {tfjs_model_dir}.tar.gz {tfjs_model_dir}" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.4" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment