{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np  # linear algebra\nimport pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom pathlib import Path\nimport os\nimport json\nfrom tqdm import tqdm\nfrom matplotlib import pyplot as plt\n\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-24T10:48:23.238536Z","iopub.execute_input":"2023-05-24T10:48:23.239587Z","iopub.status.idle":"2023-05-24T10:48:30.643651Z","shell.execute_reply.started":"2023-05-24T10:48:23.239537Z","shell.execute_reply":"2023-05-24T10:48:30.642697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# !pip install tflite_runtime\n# import tflite_runtime.interpreter as tflite\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:48:37.032361Z","iopub.execute_input":"2023-05-24T10:48:37.032742Z","iopub.status.idle":"2023-05-24T10:48:39.037718Z","shell.execute_reply.started":"2023-05-24T10:48:37.03271Z","shell.execute_reply":"2023-05-24T10:48:39.036519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = '/kaggle/input/asl-fingerspelling'","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:32:46.453686Z","iopub.status.idle":"2023-05-24T10:32:46.454144Z","shell.execute_reply.started":"2023-05-24T10:32:46.453934Z","shell.execute_reply":"2023-05-24T10:32:46.453958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(keras.layers.Layer):\n    def __init__(self):\n        super().__init__()\n\n    def call(self, x):\n        S, _ = x.shape\n        out = tf.zeros([1, 59])\n        return out\n    \ninputs = tf.keras.Input(shape=(1), name=\"inputs\")\nmodel = Model()\nx = tf.expand_dims(inputs,0)\nmodel(inputs)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:35:13.535897Z","iopub.execute_input":"2023-05-24T10:35:13.536365Z","iopub.status.idle":"2023-05-24T10:35:13.674688Z","shell.execute_reply.started":"2023-05-24T10:35:13.536316Z","shell.execute_reply":"2023-05-24T10:35:13.673054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_inference_model(model):\n    inputs = tf.keras.Input(shape=(1), name=\"inputs\")\n    \n#     x = tf.expand_dims(inputs,0)\n    # call trained model\n    \n    out = model(inputs)\n\n    # explicitly name the final (identity) layer for the submission format\n    out = layers.Activation(\"linear\", name=\"outputs\")(out)\n\n    inference_model = tf.keras.Model(inputs=inputs, outputs=out)\n    inference_model.compile(loss=\"sparse_categorical_crossentropy\",\n                            metrics=\"accuracy\")\n    return inference_model\n\ninference_model = get_inference_model(model)\ninference_model.summary(expand_nested=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:37:18.346786Z","iopub.execute_input":"2023-05-24T10:37:18.348218Z","iopub.status.idle":"2023-05-24T10:37:18.48161Z","shell.execute_reply.started":"2023-05-24T10:37:18.348153Z","shell.execute_reply":"2023-05-24T10:37:18.48055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inference_model(tf.random.normal([23,1])).shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:38:02.469304Z","iopub.execute_input":"2023-05-24T10:38:02.469788Z","iopub.status.idle":"2023-05-24T10:38:02.503325Z","shell.execute_reply.started":"2023-05-24T10:38:02.469739Z","shell.execute_reply":"2023-05-24T10:38:02.501896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"converter = tf.lite.TFLiteConverter.from_keras_model(inference_model)\n\nconverter.optimizations = [tf.lite.Optimize.DEFAULT]\nconverter.experimental_new_converter=True\nconverter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,\n                                       tf.lite.OpsSet.SELECT_TF_OPS]\n\ntflite_model = converter.convert()\n\nmodel_path = \"model.tflite\"\n\n# Save the model.\nwith open(model_path, 'wb') as f:\n    f.write(tflite_model)\n!zip submission.zip $model_path","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:38:13.749723Z","iopub.execute_input":"2023-05-24T10:38:13.750164Z","iopub.status.idle":"2023-05-24T10:38:15.839172Z","shell.execute_reply.started":"2023-05-24T10:38:13.750133Z","shell.execute_reply":"2023-05-24T10:38:15.837742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_path","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tflite_runtime.interpreter as tflite\n# interpreter = tflite.Interpreter(model_path)\n\n\n# with open (\"/kaggle/input/fingerspelling-character-map/character_to_prediction_index.json\", \"r\") as f:\n#     character_map = json.load(f)\n# rev_character_map = {j:i for i,j in character_map.items()}\n\n# found_signatures = list(interpreter.get_signature_list().keys())\n\n# if REQUIRED_SIGNATURE not in found_signatures:\n#     raise KernelEvalException('Required input signature not found.')\n\n# prediction_fn = interpreter.get_signature_runner(\"serving_default\")\n# output = prediction_fn(inputs=frames)\n# prediction_str = \"\".join([rev_character_map.get(s, \"\") for s in np.argmax(output[REQUIRED_OUTPUT], axis=1)])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T10:42:11.014352Z","iopub.execute_input":"2023-05-24T10:42:11.014806Z","iopub.status.idle":"2023-05-24T10:42:11.056648Z","shell.execute_reply.started":"2023-05-24T10:42:11.014737Z","shell.execute_reply":"2023-05-24T10:42:11.05485Z"},"trusted":true},"execution_count":null,"outputs":[]}]}