{"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 import tqdm\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-05-28T08:42:23.63563Z","iopub.execute_input":"2023-05-28T08:42:23.63637Z","iopub.status.idle":"2023-05-28T08:42:35.199711Z","shell.execute_reply.started":"2023-05-28T08:42:23.636276Z","shell.execute_reply":"2023-05-28T08:42:35.198217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/train.csv')\ndf[\"phrase_bytes\"] = df[\"phrase\"].map(lambda x: x.encode(\"utf-8\"))\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2023-05-28T08:42:38.230682Z","iopub.execute_input":"2023-05-28T08:42:38.231764Z","iopub.status.idle":"2023-05-28T08:42:38.51239Z","shell.execute_reply.started":"2023-05-28T08:42:38.231703Z","shell.execute_reply":"2023-05-28T08:42:38.510715Z"},"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] + [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\nSEL_COLS = FACE + LHAND + RHAND + POSE\nFRAME_LEN = 128","metadata":{"execution":{"iopub.status.busy":"2023-05-28T08:42:41.090317Z","iopub.execute_input":"2023-05-28T08:42:41.091355Z","iopub.status.idle":"2023-05-28T08:42:41.10279Z","shell.execute_reply.started":"2023-05-28T08:42:41.091304Z","shell.execute_reply":"2023-05-28T08:42:41.101385Z"},"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 in zip(seq_refs.sequence_id, seq_refs.phrase_bytes):\n            frames = seqs.iloc[seqs.index == seq_id]\n            frames128 = frames.fillna(-10).to_numpy()\n            frames128 = resize(frames128, (FRAME_LEN, len(SEL_COLS)))\n            frames = pd.DataFrame(data = frames128, columns=frames.columns)\n            \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]))\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-05-28T08:44:30.296686Z","iopub.execute_input":"2023-05-28T08:44:30.297168Z","iopub.status.idle":"2023-05-28T08:45:18.673651Z","shell.execute_reply.started":"2023-05-28T08:44:30.297134Z","shell.execute_reply":"2023-05-28T08:45:18.671319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_fn(record_bytes):\n    schema = {COL: tf.io.FixedLenFeature([FRAME_LEN], dtype=tf.float32) for COL in SEL_COLS}\n    schema[\"phrase\"] = tf.io.FixedLenFeature([], dtype=tf.string)\n    return tf.io.parse_single_example(record_bytes, schema)\n\nfor file_id in df.file_id:\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    for batch in tf.data.TFRecordDataset([tffile]).map(decode_fn).take(2):\n        print(list(batch.keys())[0])\n    break\n","metadata":{"execution":{"iopub.status.busy":"2023-05-28T08:46:24.271617Z","iopub.execute_input":"2023-05-28T08:46:24.272117Z","iopub.status.idle":"2023-05-28T08:46:25.61712Z","shell.execute_reply.started":"2023-05-28T08:46:24.272079Z","shell.execute_reply":"2023-05-28T08:46:25.615552Z"},"trusted":true},"execution_count":null,"outputs":[]}]}