Last active
September 20, 2021 09:57
-
-
Save ebraraktas/69995d036a35a8d7f744c845a163863e to your computer and use it in GitHub Desktop.
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": "tflite_conv1d_conversion_bug.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Jzl22GNGbDt7" | |
}, | |
"source": [ | |
"# TF Lite `Conv1D` conversion bug demo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "L4RoUpQkjuk5" | |
}, | |
"source": [ | |
"Conversion of `Conv1D` layer seems to have changed in TF Lite `2.4.0`. Minimal example below demonstrates interpreter `RESHAPE` error if we try to resize and allocate input." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "PwQ5iEETanLq" | |
}, | |
"source": [ | |
"## Prepare" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Dh5dvbl6arr5" | |
}, | |
"source": [ | |
"use_tf_2_3 = False # Using TF Lite 2.3.0 gives no error\n", | |
"\n", | |
"if use_tf_2_3:\n", | |
" !pip install tensorflow==2.3.0\n", | |
"else:\n", | |
" # 2.5.0 and 2.4.0 fail, too\n", | |
" !pip install tensorflow==2.6.0" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "l9vk-50PZkSj" | |
}, | |
"source": [ | |
"import tensorflow as tf\n", | |
"print(tf.__version__)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XVyhL41jaIFs" | |
}, | |
"source": [ | |
"def generate_model(num_hidden_units: int = 500) -> tf.keras.models.Sequential:\n", | |
" model = tf.keras.models.Sequential()\n", | |
" model.add(tf.keras.layers.Conv1D(filters=num_hidden_units, kernel_size=3, strides=1, padding='SAME', activation='relu'))\n", | |
" optimizer = tf.keras.optimizers.Adam()\n", | |
" model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n", | |
" return model" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7WdyoJQ6aj3Q" | |
}, | |
"source": [ | |
"## Create model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dh-uhJw6aKIU" | |
}, | |
"source": [ | |
"model = generate_model()\n", | |
"_ = model.predict(tf.random.normal((1, 100, 80)))" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "pCZubKlIafJb" | |
}, | |
"source": [ | |
"## Conversion" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "9sIGNMdraXW6" | |
}, | |
"source": [ | |
"converter = tf.lite.TFLiteConverter.from_keras_model(model)\n", | |
"converter.optimizations = [tf.lite.Optimize.DEFAULT]\n", | |
"\n", | |
"model_path = f\"model_{tf.__version__}.tflite\"\n", | |
"with open(model_path, \"wb\") as f:\n", | |
" f.write(converter.convert())" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "LdqKlzo0acqt" | |
}, | |
"source": [ | |
"## Interpreter test" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qZQ9ja83cf5-" | |
}, | |
"source": [ | |
"interpreter = tf.lite.Interpreter(model_path)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JF-k3FbTZV5A" | |
}, | |
"source": [ | |
"interpreter.resize_tensor_input(0, [1, 50, 80])\n", | |
"interpreter.allocate_tensors()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "wjkVCwgYoz4u" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment