{"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":"markdown","source":"# Dummy model with TfLite inference demo\n\nThis notebook uses the kernel configuration from the previous Kaggle competition (leave environment pinned if you want to keep this). It demonstrates how to create a \"dummy model\" submission (based on what we assume at present) using a subset of the keypoints. \n\nThe model uses all left and right hand keypoint coordinates as inputs and it has an untrained (random) dense layer that produces logits.\nIt preserves the length of the input sequence (it is currently not clear whether there are any requirements on this).\n\nAt the end of the notebook, there is code to test inference on the TfLite model, using the tflite-runtime required for this competition (active when setting CHECKING to True).\n","metadata":{}},{"cell_type":"markdown","source":"## Configuration","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, optimizers, constraints, regularizers\nimport numpy as np\nimport pandas as pd\nimport json\nimport matplotlib.pyplot as plt\nfrom datetime import datetime\n\nprint(\"Tensorflow\", tf.__version__)\n!python --version\n\n\nbasedir = \"/kaggle/working/\"\n\nNUM_CHARACTERS = 59","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:23:57.936359Z","iopub.execute_input":"2023-05-24T11:23:57.937737Z","iopub.status.idle":"2023-05-24T11:24:08.89325Z","shell.execute_reply.started":"2023-05-24T11:23:57.93766Z","shell.execute_reply":"2023-05-24T11:24:08.891641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Model for inference","metadata":{}},{"cell_type":"code","source":"basedir = \"/kaggle/working/\"\n\nSEL_FEATURES = ['x_right_hand_0','y_right_hand_0','z_right_hand_0',\n                'x_right_hand_1','y_right_hand_1','z_right_hand_1',\n                'x_right_hand_2','y_right_hand_2','z_right_hand_2',\n                'x_right_hand_3','y_right_hand_3','z_right_hand_3',\n                'x_right_hand_4','y_right_hand_4','z_right_hand_4',\n                'x_right_hand_5','y_right_hand_5','z_right_hand_5',\n                'x_right_hand_6','y_right_hand_6','z_right_hand_6',\n                'x_right_hand_7','y_right_hand_7','z_right_hand_7',\n                'x_right_hand_8','y_right_hand_8','z_right_hand_8',\n                'x_right_hand_9','y_right_hand_9','z_right_hand_9',\n                'x_right_hand_10','y_right_hand_10','z_right_hand_10',\n                'x_right_hand_11','y_right_hand_11','z_right_hand_11',\n                'x_right_hand_12','y_right_hand_12','z_right_hand_12',\n                'x_right_hand_13','y_right_hand_13','z_right_hand_13',\n                'x_right_hand_14','y_right_hand_14','z_right_hand_14',\n                'x_right_hand_15','y_right_hand_15','z_right_hand_15',\n                'x_right_hand_16','y_right_hand_16','z_right_hand_16',\n                'x_right_hand_17','y_right_hand_17','z_right_hand_17',\n                'x_right_hand_18','y_right_hand_18','z_right_hand_18',\n                'x_right_hand_19','y_right_hand_19','z_right_hand_19',\n                'x_right_hand_20','y_right_hand_20','z_right_hand_20',\n                'x_left_hand_0','y_left_hand_0','z_left_hand_0',\n                'x_left_hand_1','y_left_hand_1','z_left_hand_1',\n                'x_left_hand_2','y_left_hand_2','z_left_hand_2',\n                'x_left_hand_3','y_left_hand_3','z_left_hand_3',\n                'x_left_hand_4','y_left_hand_4','z_left_hand_4',\n                'x_left_hand_5','y_left_hand_5','z_left_hand_5',\n                'x_left_hand_6','y_left_hand_6','z_left_hand_6',\n                'x_left_hand_7','y_left_hand_7','z_left_hand_7',\n                'x_left_hand_8','y_left_hand_8','z_left_hand_8',\n                'x_left_hand_9','y_left_hand_9','z_left_hand_9',\n                'x_left_hand_10','y_left_hand_10','z_left_hand_10',\n                'x_left_hand_11','y_left_hand_11','z_left_hand_11',\n                'x_left_hand_12','y_left_hand_12','z_left_hand_12',\n                'x_left_hand_13','y_left_hand_13','z_left_hand_13',\n                'x_left_hand_14','y_left_hand_14','z_left_hand_14',\n                'x_left_hand_15','y_left_hand_15','z_left_hand_15',\n                'x_left_hand_16','y_left_hand_16','z_left_hand_16',\n                'x_left_hand_17','y_left_hand_17','z_left_hand_17',\n                'x_left_hand_18','y_left_hand_18','z_left_hand_18',\n                'x_left_hand_19','y_left_hand_19','z_left_hand_19',\n                'x_left_hand_20','y_left_hand_20','z_left_hand_20'\n                ]\nNUM_FEATURES = len(SEL_FEATURES)\n\n#print(\"number of used features:\",NUM_FEATURES)\n\nd = {\"selected_columns\":SEL_FEATURES}\n\nwith open(f\"{basedir}/inference_args.json\", \"w\") as f:\n    json.dump(d, f)\n\n    \ndef get_dummy_model():\n    inputs = tf.keras.Input(shape=(NUM_FEATURES), dtype=tf.float32, name=\"inputs\")\n    #print(inputs.shape)\n    # remove Nan's\n    x = tf.where(tf.math.is_nan(inputs), tf.zeros_like(inputs), inputs)\n    # Dummy submission: using an untrained dense layer\n    x = tf.keras.layers.Dense(NUM_CHARACTERS)(x)\n    out = tf.keras.layers.Activation(\"linear\", name=\"outputs\")(x)\n    #print(out.shape)\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\ndummy_model_test = get_dummy_model()\ndummy_model_test.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:24:08.896246Z","iopub.execute_input":"2023-05-24T11:24:08.897456Z","iopub.status.idle":"2023-05-24T11:24:09.140295Z","shell.execute_reply.started":"2023-05-24T11:24:08.897407Z","shell.execute_reply":"2023-05-24T11:24:09.13963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create submission file","metadata":{}},{"cell_type":"code","source":"converter = tf.lite.TFLiteConverter.from_keras_model(dummy_model_test)\n\ntflite_model = converter.convert()\nmodel_path = 'model.tflite'\n\nwith open(model_path, 'wb') as f:\n    f.write(tflite_model)\n\n!zip submission.zip  '/kaggle/working/model.tflite' '/kaggle/working/inference_args.json'","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:24:09.141435Z","iopub.execute_input":"2023-05-24T11:24:09.141763Z","iopub.status.idle":"2023-05-24T11:24:11.289467Z","shell.execute_reply.started":"2023-05-24T11:24:09.141731Z","shell.execute_reply":"2023-05-24T11:24:11.287796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making Prediction","metadata":{}},{"cell_type":"code","source":"CHECKING = False\n\nif CHECKING:\n    !pip install tflite-runtime==2.9.1\n    import tflite_runtime.interpreter as tflite\n\n    def load_relevant_data_subset(pq_path):\n        return pd.read_parquet(pq_path, columns=SEL_FEATURES) #selected_columns)\n    \n    data_path = \"/kaggle/input/asl-fingerspelling/train_landmarks/1019715464.parquet\"\n    frames = load_relevant_data_subset(data_path).values\n    \n    interpreter = tflite.Interpreter(model_path)\n    found_signatures = list(interpreter.get_signature_list().keys())\n    prediction_fn = interpreter.get_signature_runner(\"serving_default\")\n    \n    with open (\"/kaggle/input/asl-fingerspelling/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()}","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:24:11.293223Z","iopub.execute_input":"2023-05-24T11:24:11.293679Z","iopub.status.idle":"2023-05-24T11:24:11.307437Z","shell.execute_reply.started":"2023-05-24T11:24:11.293632Z","shell.execute_reply":"2023-05-24T11:24:11.306181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if CHECKING:\n    output = prediction_fn(inputs=frames)\n    prediction_str = \"\".join([rev_character_map.get(s, \"\") for s in np.argmax(output['outputs'], axis=1)])\n    print(\"\\n\\n\",prediction_str[:100])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T11:24:11.309232Z","iopub.execute_input":"2023-05-24T11:24:11.309773Z","iopub.status.idle":"2023-05-24T11:24:11.322552Z","shell.execute_reply.started":"2023-05-24T11:24:11.309716Z","shell.execute_reply":"2023-05-24T11:24:11.321026Z"},"trusted":true},"execution_count":null,"outputs":[]}]}