{"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":"from datetime import datetime, timedelta, timezone\n\nSHA_TZ = timezone(\n  timedelta(hours=8),\n  name='Asia/Shanghai',\n)\nutc_now = datetime.utcnow().replace(tzinfo=timezone.utc)\nbeijing_now = utc_now.astimezone(SHA_TZ)\nbeijing_now.strftime('%Y-%m-%d %H:%M:%S')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:21.89241Z","iopub.execute_input":"2023-08-27T05:45:21.892913Z","iopub.status.idle":"2023-08-27T05:45:21.902946Z","shell.execute_reply.started":"2023-08-27T05:45:21.892874Z","shell.execute_reply":"2023-08-27T05:45:21.901869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys, os\nimport json\ndef is_kaggle_interactive():\n  if 'KAGGLE_KERNEL_RUN_TYPE' in os.environ:\n    return os.environ['KAGGLE_KERNEL_RUN_TYPE'] == 'Interactive'\n  return True","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:21.90556Z","iopub.execute_input":"2023-08-27T05:45:21.906254Z","iopub.status.idle":"2023-08-27T05:45:21.91664Z","shell.execute_reply.started":"2023-08-27T05:45:21.906214Z","shell.execute_reply":"2023-08-27T05:45:21.915521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/aslfr-model/* ./","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-27T05:45:21.918306Z","iopub.execute_input":"2023-08-27T05:45:21.918727Z","iopub.status.idle":"2023-08-27T05:45:24.120093Z","shell.execute_reply.started":"2023-08-27T05:45:21.918672Z","shell.execute_reply":"2023-08-27T05:45:24.11884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat ./inference_args.json","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:24.122091Z","iopub.execute_input":"2023-08-27T05:45:24.122462Z","iopub.status.idle":"2023-08-27T05:45:25.171999Z","shell.execute_reply.started":"2023-08-27T05:45:24.122429Z","shell.execute_reply":"2023-08-27T05:45:25.170657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(json.load(open('./inference_args.json'))['selected_columns'])","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:25.175408Z","iopub.execute_input":"2023-08-27T05:45:25.17586Z","iopub.status.idle":"2023-08-27T05:45:25.185803Z","shell.execute_reply.started":"2023-08-27T05:45:25.175821Z","shell.execute_reply":"2023-08-27T05:45:25.184757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n  from icecream import ic\nexcept Exception:\n  !pip install /kaggle/input/tflite-runtime-nightly-2140dev20230602-whl/tflite_runtime_nightly-2.14.0.dev20230602-cp310-cp310-manylinux2014_x86_64.whl\n  !pip install -q icecream --no-index --find-links=file:///kaggle/input/icecream/ ","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:25.187128Z","iopub.execute_input":"2023-08-27T05:45:25.187453Z","iopub.status.idle":"2023-08-27T05:45:25.197174Z","shell.execute_reply.started":"2023-08-27T05:45:25.187427Z","shell.execute_reply":"2023-08-27T05:45:25.196148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#if is_kaggle_interactive():\nfrom icecream import ic\nimport tflite_runtime.interpreter as tflite\nimport tflite_runtime\nic(tflite_runtime.__version__)\nimport time\nimport json\nimport pandas as pd\nfrom tqdm.auto import tqdm\nfrom leven import levenshtein\n# import tensorflow as tf\nimport numpy as np\n\nmodel_dir = './'\nfold = 0\ntotal = 20\ntotal = 50\n\nSEL_FEATURES = json.load(\n    open(f'{model_dir}/inference_args.json'))['selected_columns']\n# ic(SEL_FEATURES)\n\ndef load_relevant_data_subset(pq_path):\n  return pd.read_parquet(pq_path, columns=SEL_FEATURES)  #selected_columns)\n\nwith open(\"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\",\n          \"r\") as f:\n  character_map = json.load(f)\nrev_character_map = {j: i for i, j in character_map.items()}\n\ndf = pd.read_csv('/kaggle/input/aslfr-input/train.csv')\ndf = df[df.fold == fold]\ndf = df.reset_index(drop=True)\n\nidx = 0\nsample = df.loc[idx]\nloaded = load_relevant_data_subset('/kaggle/input/asl-fingerspelling/' +\n                                   sample['path'])\nloaded = loaded[loaded.index == sample['sequence_id']].values\nprint(loaded.shape)\nframes = loaded\n\ninterpreter = tflite.Interpreter(f'{model_dir}/model.tflite')\n# interpreter = tf.lite.Interpreter(f'{model_dir}/model.tflite')\nfound_signatures = list(interpreter.get_signature_list().keys())\n\nREQUIRED_SIGNATURE = 'serving_default'\nREQUIRED_OUTPUT = 'outputs'\nif REQUIRED_SIGNATURE not in found_signatures:\n  raise KernelEvalException('Required input signature not found.')\n\nprediction_fn = interpreter.get_signature_runner(\"serving_default\")\noutput_lite = prediction_fn(inputs=frames)\nprediction_str = \"\".join([\n    rev_character_map.get(s, \"\")\n    for s in np.argmax(output_lite[REQUIRED_OUTPUT], axis=1)\n])\nic(sample['phrase'], prediction_str)\n\nst = time.time()\ncnt = 0\nmodel_time = 0\nic(total, len(df))\ntotal = min(len(df), total)\n\nl = []\nfor i in tqdm(range(total)):\n  sample = df.loc[i]\n  loaded = load_relevant_data_subset('/kaggle/input/asl-fingerspelling/' +\n                                     sample['path'])\n  loaded = loaded[loaded.index == sample['sequence_id']].values\n  # ic(loaded)\n  md_st = time.time()\n  output_ = prediction_fn(inputs=loaded)\n  model_time += time.time() - md_st\n\n  prediction_str = \"\".join([\n      rev_character_map.get(s, \"\")\n      for s in np.argmax(output_[REQUIRED_OUTPUT], axis=1)\n  ])\n  distance = levenshtein(sample['phrase'], prediction_str)\n  phrase_true = sample['phrase']\n  phrase_pred = prediction_str\n  l.append({\n    'sequence_id': sample['sequence_id'],\n    'phrase_true': phrase_true,\n    'phrase_pred': phrase_pred,\n    'phrase_len_true': len(phrase_true),\n    'phrase_len_pred': len(phrase_pred),\n    'distance': distance,\n    'score': max(len(phrase_true) - distance, 0.) / len(phrase_true),\n  })\n\ndf = pd.DataFrame(l)\nscore = max(df['phrase_len_true'].sum() - df['distance'].sum(), 0.) / df['phrase_len_true'].sum()\ndisplay(df.head(20))\nmean_time = model_time / total\nprint(beijing_now.strftime('%Y-%m-%d %H:%M:%S'))\nprint(open('./path.txt').readline().split('working/')[1].split('/ckpt')[0])\nos.system('du -h ./model.tflite')\nimport pandas as pd\nmetrics = pd.read_csv('./metrics.csv').iloc[-1]\nprint('score:', metrics['score'])\nprint('score/head', metrics['score/head'])\nprint('tflite_score/head:', score)\nprint('mean_time:', mean_time)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:45:25.19861Z","iopub.execute_input":"2023-08-27T05:45:25.199038Z","iopub.status.idle":"2023-08-27T05:46:34.970274Z","shell.execute_reply.started":"2023-08-27T05:45:25.19901Z","shell.execute_reply":"2023-08-27T05:46:34.969425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Package the TFLite model and inference args into a ZIP file for submission\n!zip submission.zip  model.tflite inference_args.json","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:46:34.971487Z","iopub.execute_input":"2023-08-27T05:46:34.972455Z","iopub.status.idle":"2023-08-27T05:46:38.226737Z","shell.execute_reply.started":"2023-08-27T05:46:34.972424Z","shell.execute_reply":"2023-08-27T05:46:38.225478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!du -h ./submission.zip","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:46:38.22835Z","iopub.execute_input":"2023-08-27T05:46:38.228677Z","iopub.status.idle":"2023-08-27T05:46:39.285567Z","shell.execute_reply.started":"2023-08-27T05:46:38.228649Z","shell.execute_reply":"2023-08-27T05:46:39.284458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}