Skip to content

Commit

Permalink
notebook update 1
Browse files Browse the repository at this point in the history
  • Loading branch information
solasolo committed May 25, 2022
1 parent 2551f99 commit 744f724
Showing 1 changed file with 18 additions and 181 deletions.
199 changes: 18 additions & 181 deletions Host/tinyml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,142 +7,12 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"\n",
"\n",
"import math\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras import layers\n",
"\n",
"SAMPLES_PER_GESTURE = 200\n",
"\n",
"Model = None\n"
"from tinyml import *\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "5b7c63f7",
"metadata": {},
"outputs": [],
"source": [
"def CreateModel():\n",
" # create a NN with 2 layers of 16 neurons\n",
" model = tf.keras.Sequential()\n",
" model.add(layers.Dense(16, activation='relu', input_shape=(6, SAMPLES_PER_GESTURE, )))\n",
" model.add(layers.Dense(16, activation='relu'))\n",
" model.add(layers.Dense(1))\n",
" model.compile(optimizer='rmsprop', loss='mse', metrics=['mae']) \n",
" \n",
" model.summary()\n",
" \n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "d531a817",
"metadata": {},
"outputs": [],
"source": [
"def PrepareModel(model):\n",
" SAMPLES = 1000\n",
" np.random.seed(1337)\n",
" \n",
" x_values = np.random.uniform(low=0, high=2*math.pi, size=(6, SAMPLES_PER_GESTURE, SAMPLES))\n",
" # shuffle and add noise\n",
" np.random.shuffle(x_values)\n",
" y_values = np.sin(x_values)\n",
" y_values += 0.1 * np.random.randn(*y_values.shape)\n",
"\n",
" # split into train, validation, test\n",
" TRAIN_SPLIT = int(0.6 * SAMPLES)\n",
" TEST_SPLIT = int(0.2 * SAMPLES + TRAIN_SPLIT)\n",
" x_train, x_test, x_validate = np.split(x_values, [TRAIN_SPLIT, TEST_SPLIT])\n",
" y_train, y_test, y_validate = np.split(y_values, [TRAIN_SPLIT, TEST_SPLIT])\n",
" \n",
" model.fit(x_train, y_train, epochs=200, batch_size=16, validation_data=(x_validate, y_validate))\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "8b7151ac",
"metadata": {},
"outputs": [],
"source": [
"def ConvertModel(model):\n",
" converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
" tflite_model = converter.convert()\n",
"\n",
" with open(\"model.tflite\", \"wb\") as f:\n",
" f.write(tflite_model)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "2d92833b",
"metadata": {},
"outputs": [],
"source": [
"# Function: Convert some hex value into an array for C programming\n",
"def Hex2H(model, h_model_name):\n",
" converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
" tflite_model = converter.convert()\n",
" \n",
" c_str = ''\n",
" model_len = len(tflite_model)\n",
"\n",
" # Create header guard\n",
" c_str += '#ifndef ' + h_model_name.upper() + '_H\\n'\n",
" c_str += '#define ' + h_model_name.upper() + '_H\\n'\n",
"\n",
" # Add array length at top of file\n",
" c_str += '\\nunsigned int ' + h_model_name + '_len = ' + str(model_len) + ';\\n'\n",
"\n",
" # Declare C variable\n",
" c_str += 'unsigned char ' + h_model_name + '[] = {'\n",
" hex_array = []\n",
" for i, val in enumerate(tflite_model) :\n",
" # Construct string from hex\n",
" hex_str = format(val, '#04x')\n",
"\n",
" # Add formatting so each line stays within 80 characters\n",
" if (i + 1) < model_len:\n",
" hex_str += ','\n",
" if (i + 1) % 12 == 0:\n",
" hex_str += '\\n '\n",
" hex_array.append(hex_str)\n",
"\n",
" # Add closing brace\n",
" c_str += '\\n ' + format(' '.join(hex_array)) + '\\n};\\n\\n'\n",
"\n",
" # Close out header guard\n",
" c_str += '#endif //' + h_model_name.upper() + '_H\\n'\n",
" \n",
" # Write TFLite model to a C source (or header) file\n",
" with open(h_model_name + '.h', 'w') as file:\n",
" file.write(c_str) \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "277f0b99",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 2,
"id": "3aa59cd7",
"metadata": {
"scrolled": true
Expand All @@ -152,19 +22,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential_1\"\n",
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" dense_3 (Dense) (None, 6, 16) 3216 \n",
" dense (Dense) (None, 32) 23072 \n",
" \n",
" dense_4 (Dense) (None, 6, 16) 272 \n",
" dense_1 (Dense) (None, 16) 528 \n",
" \n",
" dense_5 (Dense) (None, 6, 1) 17 \n",
" dense_2 (Dense) (None, 2) 34 \n",
" \n",
"=================================================================\n",
"Total params: 3,505\n",
"Trainable params: 3,505\n",
"Total params: 23,634\n",
"Trainable params: 23,634\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
Expand All @@ -176,31 +46,22 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 3,
"id": "43bcd7b1",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/200\n"
]
},
{
"ename": "ValueError",
"evalue": "in user code:\n\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1021, in train_function *\n return step_function(self, iterator)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1010, in step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1000, in run_step **\n outputs = model.train_step(data)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 859, in train_step\n y_pred = self(x, training=True)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n raise e.with_traceback(filtered_tb) from None\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/input_spec.py\", line 264, in assert_input_compatibility\n raise ValueError(f'Input {input_index} of layer \"{layer_name}\" is '\n\n ValueError: Input 0 of layer \"sequential_1\" is incompatible with the layer: expected shape=(None, 6, 200), found shape=(None, 200, 1000)\n",
"ename": "NameError",
"evalue": "name 'Train' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_995/983186460.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mPrepareModel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mModel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/tmp/ipykernel_995/498254193.py\u001b[0m in \u001b[0;36mPrepareModel\u001b[0;34m(model)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_validate\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mTRAIN_SPLIT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTEST_SPLIT\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m16\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalidation_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_validate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_validate\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/utils/traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=broad-except\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwith_traceback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfiltered_tb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mfiltered_tb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/tinyml/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py\u001b[0m in \u001b[0;36mautograph_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 1145\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint:disable=broad-except\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1146\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"ag_error_metadata\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1147\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mag_error_metadata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_exception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1148\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1149\u001b[0m \u001b[0;32mraise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: in user code:\n\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1021, in train_function *\n return step_function(self, iterator)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1010, in step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 1000, in run_step **\n outputs = model.train_step(data)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/training.py\", line 859, in train_step\n y_pred = self(x, training=True)\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n raise e.with_traceback(filtered_tb) from None\n File \"/root/miniconda3/envs/tinyml/lib/python3.7/site-packages/keras/engine/input_spec.py\", line 264, in assert_input_compatibility\n raise ValueError(f'Input {input_index} of layer \"{layer_name}\" is '\n\n ValueError: Input 0 of layer \"sequential_1\" is incompatible with the layer: expected shape=(None, 6, 200), found shape=(None, 200, 1000)\n"
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-ec048e20789d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mPrepareModel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mModel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mD:\\Work\\MushroomCloud\\TinyML\\Code\\Host\\tinyml.py\u001b[0m in \u001b[0;36mPrepareModel\u001b[1;34m(model)\u001b[0m\n\u001b[0;32m 38\u001b[0m \u001b[1;31m#y_values = np.random.randn(*y_values.shape)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 39\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 40\u001b[1;33m \u001b[0mTrain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx_values\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_values\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 41\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'Train' is not defined"
]
}
],
Expand Down Expand Up @@ -229,38 +90,14 @@
"source": [
"Hex2H(Model, \"model\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43ed0bce",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9652c769",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "179a0033",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "065a7fd65857af9b0d7d7760ab62099efda22ea2fccd7cdd83105fb1a789eaf5"
},
"kernelspec": {
"display_name": "Python 3.7.13 64-bit ('tinyml': conda)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -274,7 +111,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 744f724

Please sign in to comment.