{"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":"## Test submission.zip model with TFlite runtime 2.14.0\n\nThis notebook uses https://www.kaggle.com/code/irohith/aslfr-transformer \n\nAdd Data the url for this notebook to get submission.zip as input for testing","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n# comment out any tf imports - conflicts with TFlite runtime import or vice versa\n#    ImportError: generic_type: type \"InterpreterWrapper\" is already registered!\n#import tensorflow as tf   \n#from tensorflow import keras\n#from tensorflow.keras import layers\nfrom skimage.transform import resize\nfrom sklearn.model_selection import train_test_split\nimport json\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-03T08:45:14.161638Z","iopub.execute_input":"2023-06-03T08:45:14.162009Z","iopub.status.idle":"2023-06-03T08:45:16.166421Z","shell.execute_reply.started":"2023-06-03T08:45:14.161977Z","shell.execute_reply":"2023-06-03T08:45:16.165186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To see TFlite runtime 2.14.0 prerelease dev versions \n\nhttps://pypi.org/project/tflite-runtime-nightly/#history\n\nTo get whls \n\nhttps://pypi.org/project/tflite-runtime-nightly/#files\n\nlatest 2 june 2023 whl from dataset in notebook add data\n\nhttps://www.kaggle.com/datasets/something4kag/tflite-runtime-nightly-2140dev20230602-whl\n\nThis notebook Environment has always use latest for Python 3.10","metadata":{}},{"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-03T08:45:21.934772Z","iopub.execute_input":"2023-06-03T08:45:21.936128Z","iopub.status.idle":"2023-06-03T08:45:38.433937Z","shell.execute_reply.started":"2023-06-03T08:45:21.936087Z","shell.execute_reply":"2023-06-03T08:45:38.432558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open (\"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\", \"r\") as f:\n    char_to_num = json.load(f)\n\nnum_to_char = {j:i for i,j in char_to_num.items()}\n\ninpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/train.csv')\n\nLIP = [\n    61, 185, 40, 39, 37, 267, 269, 270, 409,\n    291, 146, 91, 181, 84, 17, 314, 405, 321, 375,\n    78, 191, 80, 81, 82, 13, 312, 311, 310, 415,\n    95, 88, 178, 87, 14, 317, 402, 318, 324, 308,\n]\n\nFACE = [f'x_face_{i}' for i in LIP] + [f'y_face_{i}' for i in LIP] + [f'z_face_{i}' for i in LIP]\nLHAND = [f'x_left_hand_{i}' for i in range(21)] + [f'y_left_hand_{i}' for i in range(21)] + [f'z_left_hand_{i}' for i in range(21)]\nRHAND = [f'x_right_hand_{i}' for i in range(21)] + [f'y_right_hand_{i}' for i in range(21)] + [f'z_right_hand_{i}' for i in range(21)]\nPOSE = [f'x_pose_{i}' for i in range(33)] + [f'y_pose_{i}' for i in range(33)] + [f'z_pose_{i}' for i in range(33)]\n\nX = [f'x_face_{i}' for i in LIP] + [f'x_left_hand_{i}' for i in range(21)] + [f'x_right_hand_{i}' for i in range(21)] + [f'x_pose_{i}' for i in range(33)]\nY = [f'y_face_{i}' for i in LIP] + [f'y_left_hand_{i}' for i in range(21)] + [f'y_right_hand_{i}' for i in range(21)] + [f'y_pose_{i}' for i in range(33)]\nZ = [f'z_face_{i}' for i in LIP] + [f'z_left_hand_{i}' for i in range(21)] + [f'z_right_hand_{i}' for i in range(21)] + [f'z_pose_{i}' for i in range(33)]\n\n#SEL_COLS = FACE + LHAND + RHAND + POSE\nSEL_COLS = X + Y + Z\nFRAME_LEN = 128","metadata":{"execution":{"iopub.status.busy":"2023-06-03T08:45:43.813698Z","iopub.execute_input":"2023-06-03T08:45:43.814112Z","iopub.status.idle":"2023-06-03T08:45:44.003239Z","shell.execute_reply.started":"2023-06-03T08:45:43.814075Z","shell.execute_reply":"2023-06-03T08:45:44.001988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training and model code is removed - refer to original notebook if needed\n\n# when submitted, the model will come from submission.zip  unpack it here using shutil","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/aslfr-transformer/submission.zip', '/kaggle/working')","metadata":{},"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_count":null,"outputs":[]},{"cell_type":"code","source":"\nREQUIRED_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()}\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T08:46:15.785183Z","iopub.execute_input":"2023-06-03T08:46:15.785567Z","iopub.status.idle":"2023-06-03T08:46:15.793134Z","shell.execute_reply.started":"2023-06-03T08:46:15.785537Z","shell.execute_reply":"2023-06-03T08:46:15.791902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tflite_runtime.interpreter as tflite","metadata":{"execution":{"iopub.status.busy":"2023-06-03T08:46:55.265579Z","iopub.execute_input":"2023-06-03T08:46:55.266707Z","iopub.status.idle":"2023-06-03T08:46:55.277843Z","shell.execute_reply.started":"2023-06-03T08:46:55.26667Z","shell.execute_reply":"2023-06-03T08:46:55.276672Z"},"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-03T08:47:53.17518Z","iopub.execute_input":"2023-06-03T08:47:53.175717Z","iopub.status.idle":"2023-06-03T08:47:53.195205Z","shell.execute_reply.started":"2023-06-03T08:47:53.175675Z","shell.execute_reply":"2023-06-03T08:47:53.193899Z"},"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\"]    \njson.dumps(slc,skipkeys=True)    # if you want to check the json or comment out    ","metadata":{"execution":{"iopub.status.busy":"2023-06-03T09:01:58.824648Z","iopub.execute_input":"2023-06-03T09:01:58.825072Z","iopub.status.idle":"2023-06-03T09:01:58.835786Z","shell.execute_reply.started":"2023-06-03T09:01:58.825041Z","shell.execute_reply":"2023-06-03T09:01:58.834681Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T08:58:53.579298Z","iopub.execute_input":"2023-06-03T08:58:53.57976Z","iopub.status.idle":"2023-06-03T08:58:53.585727Z","shell.execute_reply.started":"2023-06-03T08:58:53.579726Z","shell.execute_reply":"2023-06-03T08:58:53.584384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 500)   # to see all selected columns","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/input/asl-fingerspelling/' \ndf_train = pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-03T09:00:31.014697Z","iopub.execute_input":"2023-06-03T09:00:31.015161Z","iopub.status.idle":"2023-06-03T09:00:31.128077Z","shell.execute_reply.started":"2023-06-03T09:00:31.015113Z","shell.execute_reply":"2023-06-03T09:00:31.126928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test model using any row from train as sample path -\nrow = 13008 #  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-03T09:31:06.435162Z","iopub.execute_input":"2023-06-03T09:31:06.435603Z","iopub.status.idle":"2023-06-03T09:31:06.442506Z","shell.execute_reply.started":"2023-06-03T09:31:06.435573Z","shell.execute_reply":"2023-06-03T09:31:06.4412Z"},"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-03T09:03:12.931815Z","iopub.execute_input":"2023-06-03T09:03:12.932604Z","iopub.status.idle":"2023-06-03T09:03:15.353838Z","shell.execute_reply.started":"2023-06-03T09:03:12.932566Z","shell.execute_reply":"2023-06-03T09:03:15.352477Z"},"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))","metadata":{"execution":{"iopub.status.busy":"2023-06-03T09:31:15.342062Z","iopub.execute_input":"2023-06-03T09:31:15.342472Z","iopub.status.idle":"2023-06-03T09:31:15.349776Z","shell.execute_reply.started":"2023-06-03T09:31:15.342442Z","shell.execute_reply":"2023-06-03T09:31:15.348705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seq.head()  # to view sequence id ","metadata":{"execution":{"iopub.status.busy":"2023-06-03T09:31:18.61874Z","iopub.execute_input":"2023-06-03T09:31:18.619175Z","iopub.status.idle":"2023-06-03T09:31:18.887835Z","shell.execute_reply.started":"2023-06-03T09:31:18.619128Z","shell.execute_reply":"2023-06-03T09:31:18.88658Z"},"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-03T09:31:23.031987Z","iopub.execute_input":"2023-06-03T09:31:23.032423Z","iopub.status.idle":"2023-06-03T09:31:23.358886Z","shell.execute_reply.started":"2023-06-03T09:31:23.032388Z","shell.execute_reply":"2023-06-03T09:31:23.357702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}