{"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)\nimport os\n#import tensorflow as tf  not reqd\nimport json\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-04T12:00:26.106581Z","iopub.execute_input":"2023-06-04T12:00:26.106957Z","iopub.status.idle":"2023-06-04T12:00:26.149002Z","shell.execute_reply.started":"2023-06-04T12:00:26.106924Z","shell.execute_reply":"2023-06-04T12:00:26.147628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! python --version\n#tf.__version__","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:26.151292Z","iopub.execute_input":"2023-06-04T12:00:26.15196Z","iopub.status.idle":"2023-06-04T12:00:26.429013Z","shell.execute_reply.started":"2023-06-04T12:00:26.151907Z","shell.execute_reply":"2023-06-04T12:00:26.427717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /tmp/pip/cache/\n!cp /kaggle/input/tflite-runtime-nightly-2140dev20230602-whl/tflite_runtime_nightly-2.14.0.dev20230602-cp310-cp310-manylinux2014_x86_64.whl /tmp/pip/cache/\n!pip install --no-index --find-links /tmp/pip/cache/ tflite-runtime-nightly","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:26.430426Z","iopub.execute_input":"2023-06-04T12:00:26.431865Z","iopub.status.idle":"2023-06-04T12:00:39.396878Z","shell.execute_reply.started":"2023-06-04T12:00:26.431802Z","shell.execute_reply":"2023-06-04T12:00:39.396087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unpack the submission.zip to test before submitting \nshutil.unpack_archive('/kaggle/input/asl-f-preprocessing-example/submission.zip', '/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.398416Z","iopub.execute_input":"2023-06-04T12:00:39.398861Z","iopub.status.idle":"2023-06-04T12:00:39.41051Z","shell.execute_reply.started":"2023-06-04T12:00:39.398823Z","shell.execute_reply":"2023-06-04T12:00:39.408398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"REQUIRED_SIGNATURE = \"serving_default\"\nREQUIRED_OUTPUT = \"outputs\"\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()}","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.415486Z","iopub.execute_input":"2023-06-04T12:00:39.41598Z","iopub.status.idle":"2023-06-04T12:00:39.431503Z","shell.execute_reply.started":"2023-06-04T12:00:39.415943Z","shell.execute_reply":"2023-06-04T12:00:39.430429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verify TFLite model can be loaded and used for prediction with TFlite 2.14.0\n\n# N.B. if Tensorflow has been imported already get\n#      ImportError: generic_type: type \"InterpreterWrapper\" is already registered!\n\n# to just test model Tensorflow is not required ","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.433307Z","iopub.execute_input":"2023-06-04T12:00:39.433709Z","iopub.status.idle":"2023-06-04T12:00:39.441835Z","shell.execute_reply.started":"2023-06-04T12:00:39.43368Z","shell.execute_reply":"2023-06-04T12:00:39.440487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tflite_runtime.interpreter as tflite","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.443766Z","iopub.execute_input":"2023-06-04T12:00:39.444228Z","iopub.status.idle":"2023-06-04T12:00:39.467256Z","shell.execute_reply.started":"2023-06-04T12:00:39.444197Z","shell.execute_reply":"2023-06-04T12:00:39.464964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interpreter = tflite.Interpreter(\"/kaggle/working/model.tflite\")\nfound_signatures = list(interpreter.get_signature_list().keys())\nif REQUIRED_SIGNATURE not in found_signatures:\n    raise KernelEvalException('Required input signature not found.')\n    \nprediction_fn = interpreter.get_signature_runner(\"serving_default\")    ","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.468745Z","iopub.execute_input":"2023-06-04T12:00:39.469247Z","iopub.status.idle":"2023-06-04T12:00:39.476844Z","shell.execute_reply.started":"2023-06-04T12:00:39.469214Z","shell.execute_reply":"2023-06-04T12:00:39.475213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get zip file inference_args.json for selected columns \nwith open (\"/kaggle/working/inference_args.json\", \"r\") as f:\n    slc = json.load(f)\nuse_columns = slc[\"selected_columns\"]    \n#json.dumps(slc,skipkeys=True)    # if you want to check the json or comment out  ","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.477857Z","iopub.execute_input":"2023-06-04T12:00:39.478668Z","iopub.status.idle":"2023-06-04T12:00:39.489518Z","shell.execute_reply.started":"2023-06-04T12:00:39.478637Z","shell.execute_reply":"2023-06-04T12:00:39.487766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# per Evaluation how data is loaded \ndef load_relevant_data_subset(pq_path):\n    return pd.read_parquet(pq_path, columns=use_columns)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.491072Z","iopub.execute_input":"2023-06-04T12:00:39.491608Z","iopub.status.idle":"2023-06-04T12:00:39.500872Z","shell.execute_reply.started":"2023-06-04T12:00:39.491574Z","shell.execute_reply":"2023-06-04T12:00:39.499784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 500)   # to see all selected columns","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.502234Z","iopub.execute_input":"2023-06-04T12:00:39.502681Z","iopub.status.idle":"2023-06-04T12:00:39.515012Z","shell.execute_reply.started":"2023-06-04T12:00:39.502651Z","shell.execute_reply":"2023-06-04T12:00:39.513515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/input/asl-fingerspelling/' #'/kaggle/input/asl-signs/'\ndf_train = pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')\nprint(len(df_train))","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.517322Z","iopub.execute_input":"2023-06-04T12:00:39.517904Z","iopub.status.idle":"2023-06-04T12:00:39.687621Z","shell.execute_reply.started":"2023-06-04T12:00:39.517872Z","shell.execute_reply":"2023-06-04T12:00:39.685325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test model using any row from train as sample path -\nrow = 13005 #  30477   # for 13000... parquet only has 287 sequence ids, rest have 1000\nsample_path = PATH + df_train.path[row] \n\nsample_phrase = df_train.phrase[row] \nprint(sample_path, 'phrase',sample_phrase)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.689604Z","iopub.execute_input":"2023-06-04T12:00:39.690086Z","iopub.status.idle":"2023-06-04T12:00:39.705287Z","shell.execute_reply.started":"2023-06-04T12:00:39.690026Z","shell.execute_reply":"2023-06-04T12:00:39.702996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load train landmarks data for parquet sample path\nlandmark = load_relevant_data_subset(sample_path)\nprint(landmark.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:39.711598Z","iopub.execute_input":"2023-06-04T12:00:39.712275Z","iopub.status.idle":"2023-06-04T12:00:40.337847Z","shell.execute_reply.started":"2023-06-04T12:00:39.712218Z","shell.execute_reply":"2023-06-04T12:00:40.336965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seq = landmark.loc[[df_train.sequence_id[row]]]  # for testing use the sequence id for the row\nprint(len(seq))\nseq.head()  # to view sequence id ","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:40.339132Z","iopub.execute_input":"2023-06-04T12:00:40.34079Z","iopub.status.idle":"2023-06-04T12:00:40.425008Z","shell.execute_reply.started":"2023-06-04T12:00:40.340727Z","shell.execute_reply":"2023-06-04T12:00:40.42411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = prediction_fn(inputs=seq)\nprediction_str = \"\".join([rev_character_map.get(s, \"\") for s in np.argmax(output[REQUIRED_OUTPUT], axis=1)])\nprint('pred:' ,prediction_str, 'TRUE phrase:', sample_phrase)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T12:00:40.42819Z","iopub.execute_input":"2023-06-04T12:00:40.430638Z","iopub.status.idle":"2023-06-04T12:00:40.439349Z","shell.execute_reply.started":"2023-06-04T12:00:40.430604Z","shell.execute_reply":"2023-06-04T12:00:40.438355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}