{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-31T18:22:53.391496Z","iopub.execute_input":"2023-05-31T18:22:53.391909Z","iopub.status.idle":"2023-05-31T18:22:53.43834Z","shell.execute_reply.started":"2023-05-31T18:22:53.391876Z","shell.execute_reply":"2023-05-31T18:22:53.437573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import path\nimport json","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:37:48.485038Z","iopub.execute_input":"2023-05-31T18:37:48.48551Z","iopub.status.idle":"2023-05-31T18:37:48.491724Z","shell.execute_reply.started":"2023-05-31T18:37:48.485475Z","shell.execute_reply":"2023-05-31T18:37:48.489918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n\nNUM_CHARACTERS = 59","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:37:49.509544Z","iopub.execute_input":"2023-05-31T18:37:49.510022Z","iopub.status.idle":"2023-05-31T18:37:50.546139Z","shell.execute_reply.started":"2023-05-31T18:37:49.509987Z","shell.execute_reply":"2023-05-31T18:37:50.5446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:37:50.65615Z","iopub.execute_input":"2023-05-31T18:37:50.65657Z","iopub.status.idle":"2023-05-31T18:37:50.662488Z","shell.execute_reply.started":"2023-05-31T18:37:50.656532Z","shell.execute_reply":"2023-05-31T18:37:50.661115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"working_dir = '/kaggle/working'\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:37:51.333069Z","iopub.execute_input":"2023-05-31T18:37:51.333421Z","iopub.status.idle":"2023-05-31T18:37:51.339044Z","shell.execute_reply.started":"2023-05-31T18:37:51.333395Z","shell.execute_reply":"2023-05-31T18:37:51.337655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_columns = ['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                    ]\n\nselected_columns_dict = {\"selected_columns\": selected_columns}\ninference_args_path = path.join(working_dir, 'inference_args.json')\nwith open(inference_args_path, \"w\") as f:\n    json.dump(selected_columns_dict, f)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:37:52.263244Z","iopub.execute_input":"2023-05-31T18:37:52.263636Z","iopub.status.idle":"2023-05-31T18:37:52.27373Z","shell.execute_reply.started":"2023-05-31T18:37:52.263606Z","shell.execute_reply":"2023-05-31T18:37:52.272203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Save the tf model","metadata":{}},{"cell_type":"code","source":"\n\nimport tensorflow as tf\n\ninput_dim = len(selected_columns)\noutput_dim = 59\n\ninputs = tf.keras.Input(shape=(input_dim,), dtype=tf.float32, name='inputs')\nx = tf.where(tf.math.is_nan(inputs), tf.zeros_like(inputs), inputs)\noutputs = tf.keras.layers.Dense(output_dim, activation=tf.nn.relu, name='outputs')(x)\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\nmodel.compile(loss=\"sparse_categorical_crossentropy\",\n                            metrics=\"accuracy\")\n#model.summary()\n\ntf_model_path = path.join(working_dir, 'tf_model')\nmodel.save(tf_model_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:51:32.009638Z","iopub.execute_input":"2023-05-31T18:51:32.010209Z","iopub.status.idle":"2023-05-31T18:51:32.461547Z","shell.execute_reply.started":"2023-05-31T18:51:32.010165Z","shell.execute_reply":"2023-05-31T18:51:32.460173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Conver to tf lite","metadata":{}},{"cell_type":"code","source":"# Convert the model\ntflite_model_path = path.join(working_dir, 'model.tflite')\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the model.\nwith open(tflite_model_path, 'wb') as f:\n    f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:51:32.739679Z","iopub.execute_input":"2023-05-31T18:51:32.740121Z","iopub.status.idle":"2023-05-31T18:51:33.403608Z","shell.execute_reply.started":"2023-05-31T18:51:32.740085Z","shell.execute_reply":"2023-05-31T18:51:33.402365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_path = path.join(working_dir, 'submission.zip')\n\n! zip {submission_path}  {tflite_model_path} {inference_args_path}","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:51:33.405476Z","iopub.execute_input":"2023-05-31T18:51:33.406832Z","iopub.status.idle":"2023-05-31T18:51:34.45093Z","shell.execute_reply.started":"2023-05-31T18:51:33.405822Z","shell.execute_reply":"2023-05-31T18:51:34.449519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport tensorflow.lite as tflite\ninterpreter = tflite.Interpreter(tflite_model_path)\nprediction_fn = interpreter.get_signature_runner(\"serving_default\")\n\ndata_path = '/kaggle/input/asl-fingerspelling/train_landmarks/1358493307.parquet'\ninputs = pd.read_parquet(data_path, columns = selected_columns).values\n\nwith open (\"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\", \"r\") as f:\n    character_map = json.load(f)\nrev_character_map = {j:i for i,j in character_map.items()}\n","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:51:34.454047Z","iopub.execute_input":"2023-05-31T18:51:34.454927Z","iopub.status.idle":"2023-05-31T18:51:34.629352Z","shell.execute_reply.started":"2023-05-31T18:51:34.454876Z","shell.execute_reply":"2023-05-31T18:51:34.628358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = prediction_fn(inputs=inputs)\nprediction_str = \"\".join([rev_character_map.get(s, \"\") for s in np.argmax(output['outputs'], axis=1)])\nprint(prediction_str)","metadata":{"execution":{"iopub.status.busy":"2023-05-31T18:51:34.630694Z","iopub.execute_input":"2023-05-31T18:51:34.631082Z","iopub.status.idle":"2023-05-31T18:51:34.925003Z","shell.execute_reply.started":"2023-05-31T18:51:34.631052Z","shell.execute_reply":"2023-05-31T18:51:34.923708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}