{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":46105,"databundleVersionId":5087314,"sourceType":"competition"},{"sourceId":128386331,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for 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-11-20T13:56:20.92333Z","iopub.execute_input":"2023-11-20T13:56:20.923832Z","iopub.status.idle":"2023-11-20T13:56:20.930122Z","shell.execute_reply.started":"2023-11-20T13:56:20.923785Z","shell.execute_reply":"2023-11-20T13:56:20.928671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Download Packages","metadata":{}},{"cell_type":"code","source":"# !pip install tflite-runtime","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:20.962553Z","iopub.execute_input":"2023-11-20T13:56:20.962954Z","iopub.status.idle":"2023-11-20T13:56:20.967535Z","shell.execute_reply.started":"2023-11-20T13:56:20.962922Z","shell.execute_reply":"2023-11-20T13:56:20.966523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:20.970268Z","iopub.execute_input":"2023-11-20T13:56:20.970769Z","iopub.status.idle":"2023-11-20T13:56:20.978778Z","shell.execute_reply.started":"2023-11-20T13:56:20.970723Z","shell.execute_reply":"2023-11-20T13:56:20.977564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Paths","metadata":{}},{"cell_type":"code","source":"path = os.path.join(*[ \"/\" , \"kaggle\" , \"input\" , \"asl-signs\"])\nmodel_path = os.path.join(*[ \"/\" , \"kaggle\" , \"input\" , \"1st-place-solution-inference\" , \"model.tflite\"])\ntrain_file = 'train.csv'\njson_file = 'sign_to_prediction_index_map.json'\nlandmark = 'train_landmark_files'\ntest_particiment_id = '36257'\n# test_sequence_id = '3762317508'\ntest_sequence_id = '4030256468'\ntest_parquet_path = os.path.join( path , landmark , test_particiment_id , test_sequence_id+'.parquet')\ntrain_data_path = os.path.join( path , train_file )\nos.listdir( os.path.join( path , landmark , test_particiment_id ) )[:10]","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:20.980859Z","iopub.execute_input":"2023-11-20T13:56:20.981691Z","iopub.status.idle":"2023-11-20T13:56:20.996728Z","shell.execute_reply.started":"2023-11-20T13:56:20.981655Z","shell.execute_reply":"2023-11-20T13:56:20.995532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xyz = pd.read_parquet( test_parquet_path )\nxyz.query('type == \"face\"' )","metadata":{"execution":{"iopub.status.busy":"2023-11-20T14:40:58.665014Z","iopub.execute_input":"2023-11-20T14:40:58.666155Z","iopub.status.idle":"2023-11-20T14:40:58.764197Z","shell.execute_reply.started":"2023-11-20T14:40:58.666112Z","shell.execute_reply":"2023-11-20T14:40:58.763079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = []\nfor mark in ['\"face\"' , '\"pose\"' , '\"left_hand\"' , '\"right_hand\"']:\n    start_index = xyz.query('type == '+ mark )['landmark_index'].iloc[0]\n#     print(start_index)\n    end_index = xyz.query('type == ' + mark )['landmark_index'].iloc[-1]\n#     print(end_index)\n    size.append( end_index - start_index + 1 )\n\nsize","metadata":{"execution":{"iopub.status.busy":"2023-11-20T14:42:29.449878Z","iopub.execute_input":"2023-11-20T14:42:29.450292Z","iopub.status.idle":"2023-11-20T14:42:29.537587Z","shell.execute_reply.started":"2023-11-20T14:42:29.450258Z","shell.execute_reply":"2023-11-20T14:42:29.536489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv( train_data_path )\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.209143Z","iopub.execute_input":"2023-11-20T13:56:21.209941Z","iopub.status.idle":"2023-11-20T13:56:21.377708Z","shell.execute_reply.started":"2023-11-20T13:56:21.209907Z","shell.execute_reply":"2023-11-20T13:56:21.37688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lable = train_data['sign'].unique()\nlable.sort()","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.37898Z","iopub.execute_input":"2023-11-20T13:56:21.379487Z","iopub.status.idle":"2023-11-20T13:56:21.394064Z","shell.execute_reply.started":"2023-11-20T13:56:21.379456Z","shell.execute_reply":"2023-11-20T13:56:21.392967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_true_sign( sequence_id , data = train_data):\n    return data.query('sequence_id == '+ sequence_id)['sign'].values[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.397954Z","iopub.execute_input":"2023-11-20T13:56:21.398424Z","iopub.status.idle":"2023-11-20T13:56:21.404296Z","shell.execute_reply.started":"2023-11-20T13:56:21.398379Z","shell.execute_reply":"2023-11-20T13:56:21.402874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROWS_PER_FRAME = 543  # number of landmarks per frame\n\ndef load_relevant_data_subset(pq_path):\n    data_columns = ['x', 'y', 'z']\n    data = pd.read_parquet(pq_path, columns=data_columns)\n    n_frames = int(len(data) / ROWS_PER_FRAME)\n    data = data.values.reshape(n_frames, ROWS_PER_FRAME, len(data_columns))\n    return data.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.406083Z","iopub.execute_input":"2023-11-20T13:56:21.406458Z","iopub.status.idle":"2023-11-20T13:56:21.414638Z","shell.execute_reply.started":"2023-11-20T13:56:21.406419Z","shell.execute_reply":"2023-11-20T13:56:21.41363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xyz_np = load_relevant_data_subset( test_parquet_path )\nxyz_np.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.416152Z","iopub.execute_input":"2023-11-20T13:56:21.416958Z","iopub.status.idle":"2023-11-20T13:56:21.441365Z","shell.execute_reply.started":"2023-11-20T13:56:21.416886Z","shell.execute_reply":"2023-11-20T13:56:21.440239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tflite_runtime.interpreter as tflite\ninterpreter = tflite.Interpreter(model_path)\n\nfound_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\nprediction_fn = interpreter.get_signature_runner(\"serving_default\")","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.442648Z","iopub.execute_input":"2023-11-20T13:56:21.443229Z","iopub.status.idle":"2023-11-20T13:56:21.455797Z","shell.execute_reply.started":"2023-11-20T13:56:21.443196Z","shell.execute_reply":"2023-11-20T13:56:21.454208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = prediction_fn(inputs=xyz_np)\n\n# pd.Series( output['outputs'] ).plot()\n\nsign_index = np.argmax(output[\"outputs\"])\nlable[ sign_index ]","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.458058Z","iopub.execute_input":"2023-11-20T13:56:21.459176Z","iopub.status.idle":"2023-11-20T13:56:21.907434Z","shell.execute_reply.started":"2023-11-20T13:56:21.459124Z","shell.execute_reply":"2023-11-20T13:56:21.906228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if get_true_sign( test_sequence_id ) == lable[ sign_index ]:\n    print( \"O -> Prediction Currect .\" )\nelse:\n    print( \"X -> Prediction Incurrect !\" )\n    \n        ","metadata":{"execution":{"iopub.status.busy":"2023-11-20T13:56:21.909008Z","iopub.execute_input":"2023-11-20T13:56:21.909382Z","iopub.status.idle":"2023-11-20T13:56:21.920903Z","shell.execute_reply.started":"2023-11-20T13:56:21.909349Z","shell.execute_reply":"2023-11-20T13:56:21.919526Z"},"trusted":true},"execution_count":null,"outputs":[]}]}