{"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 pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom skimage.transform import resize\nimport json\nfrom tqdm.notebook import tqdm\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-30T06:16:48.655571Z","iopub.execute_input":"2023-06-30T06:16:48.656505Z","iopub.status.idle":"2023-06-30T06:17:01.472345Z","shell.execute_reply.started":"2023-06-30T06:16:48.656455Z","shell.execute_reply":"2023-06-30T06:17:01.471309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-30T06:17:01.474036Z","iopub.execute_input":"2023-06-30T06:17:01.475418Z","iopub.status.idle":"2023-06-30T06:17:01.717967Z","shell.execute_reply.started":"2023-06-30T06:17:01.475375Z","shell.execute_reply":"2023-06-30T06:17:01.716783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"phrase_bytes\"] = df[\"phrase\"].map(lambda x: x.encode(\"utf-8\"))\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2023-06-30T06:17:01.719385Z","iopub.execute_input":"2023-06-30T06:17:01.71975Z","iopub.status.idle":"2023-06-30T06:17:01.770437Z","shell.execute_reply.started":"2023-06-30T06:17:01.719717Z","shell.execute_reply":"2023-06-30T06:17:01.769182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LIP = [\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]\nLHAND = [f'x_left_hand_{i}' for i in range(21)] + [f'y_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)]\nPOSE = [f'x_pose_{i}' for i in range(33)] + [f'y_pose_{i}' for i in range(33)]\n\nSEL_COLS = FACE + LHAND + RHAND + POSE","metadata":{"execution":{"iopub.status.busy":"2023-06-30T06:17:01.772987Z","iopub.execute_input":"2023-06-30T06:17:01.773357Z","iopub.status.idle":"2023-06-30T06:17:01.785843Z","shell.execute_reply.started":"2023-06-30T06:17:01.773326Z","shell.execute_reply":"2023-06-30T06:17:01.784296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(SEL_COLS),len(FACE),len(LHAND),len(RHAND),len(POSE)","metadata":{"execution":{"iopub.status.busy":"2023-06-30T06:17:01.787407Z","iopub.execute_input":"2023-06-30T06:17:01.787753Z","iopub.status.idle":"2023-06-30T06:17:01.807214Z","shell.execute_reply.started":"2023-06-30T06:17:01.787725Z","shell.execute_reply":"2023-06-30T06:17:01.805418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_relevant_data_subset(pq_path):\n    return pd.read_parquet(pq_path, columns=SEL_COLS)\n\nfor file_id in tqdm(df.file_id.unique()):\n    pqfile = f\"{inpdir}/train_landmarks/{file_id}.parquet\"\n    if not os.path.isdir(\"tfds\"): os.mkdir(\"tfds\")\n    tffile = f\"tfds/{file_id}.tfrecord\"\n    seq_refs = df.loc[df.file_id == file_id]\n    seqs = load_relevant_data_subset(pqfile)\n    \n    with tf.io.TFRecordWriter(tffile) as file_writer:\n        for seq_id, phrase_bytes,phrase in zip(seq_refs.sequence_id, seq_refs.phrase_bytes,seq_refs.phrase):\n            frames = seqs.iloc[seqs.index == seq_id]\n            frames = pd.DataFrame(data = frames, columns=frames.columns)\n            features = {COL: tf.train.Feature(float_list=tf.train.FloatList(value=frames[COL])) for COL in SEL_COLS}\n            features[\"phrase\"] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[phrase_bytes]))\n            record_bytes = tf.train.Example(features=tf.train.Features(feature=features)).SerializeToString()\n            file_writer.write(record_bytes)","metadata":{"execution":{"iopub.status.busy":"2023-06-30T06:17:26.686873Z","iopub.execute_input":"2023-06-30T06:17:26.687387Z","iopub.status.idle":"2023-06-30T06:28:51.696509Z","shell.execute_reply.started":"2023-06-30T06:17:26.687349Z","shell.execute_reply":"2023-06-30T06:28:51.693618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}