{"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":"This notebook prepares the tfrecords for my simple transformer notebook and is almost an identical copy of Rohith Ingilela's [notebook](https://www.kaggle.com/code/irohith/aslfr-preprocess-dataset), with two small changes according to his latest update and remark:\n1. Only hands landmarks + ten pose landmarks.\n2. Only movies with a number of frames > twice the length of the phrase.","metadata":{}},{"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\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-12T14:02:27.071021Z","iopub.execute_input":"2023-10-12T14:02:27.071422Z","iopub.status.idle":"2023-10-12T14:02:38.272715Z","shell.execute_reply.started":"2023-10-12T14:02:27.07139Z","shell.execute_reply":"2023-10-12T14:02:38.271375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1","metadata":{}},{"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\"))\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T14:53:54.032538Z","iopub.execute_input":"2023-10-12T14:53:54.033306Z","iopub.status.idle":"2023-10-12T14:53:54.167548Z","shell.execute_reply.started":"2023-10-12T14:53:54.03327Z","shell.execute_reply":"2023-10-12T14:53:54.166186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LPOSE = [13, 15, 17, 19, 21]\nRPOSE = [14, 16, 18, 20, 22]\nPOSE = LPOSE + RPOSE\n\nRHAND_LBLS = [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)]\nLHAND_LBLS = [ 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)]\nPOSE_LBLS = [f'x_pose_{i}' for i in POSE] + [f'y_pose_{i}' for i in POSE] + [f'z_pose_{i}' for i in POSE]\n\nX = [f'x_right_hand_{i}' for i in range(21)] + [f'x_left_hand_{i}' for i in range(21)] + [f'x_pose_{i}' for i in POSE]\nY = [f'y_right_hand_{i}' for i in range(21)] + [f'y_left_hand_{i}' for i in range(21)] + [f'y_pose_{i}' for i in POSE]\nZ = [f'z_right_hand_{i}' for i in range(21)] + [f'z_left_hand_{i}' for i in range(21)] + [f'z_pose_{i}' for i in POSE]\n\nSEL_COLS = X + Y + Z\nFRAME_LEN = 128\n\nX_IDX = [i for i, col in enumerate(SEL_COLS)  if \"x_\" in col]\nY_IDX = [i for i, col in enumerate(SEL_COLS)  if \"y_\" in col]\nZ_IDX = [i for i, col in enumerate(SEL_COLS)  if \"z_\" in col]\n\nRHAND_IDX = [i for i, col in enumerate(SEL_COLS)  if \"right\" in col]\nLHAND_IDX = [i for i, col in enumerate(SEL_COLS)  if  \"left\" in col]\nRPOSE_IDX = [i for i, col in enumerate(SEL_COLS)  if  \"pose\" in col and int(col[-2:]) in RPOSE]\nLPOSE_IDX = [i for i, col in enumerate(SEL_COLS)  if  \"pose\" in col and int(col[-2:]) in LPOSE]\n\nprint('SEL_COLS size:' + str(len(SEL_COLS)))","metadata":{"execution":{"iopub.status.busy":"2023-10-12T14:53:54.1701Z","iopub.execute_input":"2023-10-12T14:53:54.170436Z","iopub.status.idle":"2023-10-12T14:53:54.185307Z","shell.execute_reply.started":"2023-10-12T14:53:54.170409Z","shell.execute_reply":"2023-10-12T14:53:54.183748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_landmarks = pd.read_parquet('/kaggle/input/asl-fingerspelling/train_landmarks/1019715464.parquet')\nkeys = train_landmarks.keys()[1:]\ntrain_landmarks.head()","metadata":{"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\ncounter = 0\nfor file_id in tqdm(df.file_id.unique()): ##68個\n    \n    print(counter)\n    counter+=1\n    \n    pqfile = f\"{inpdir}/train_landmarks/{file_id}.parquet\" ##'/kaggle/input/asl-fingerspelling/train_landmarks/5414471.parquet'\n    if not os.path.isdir(\"tfds\"): os.mkdir(\"tfds\")\n    tffile = f\"tfds/{file_id}.tfrecord\" ##'tfds/5414471.tfrecord'\n    seq_refs = df.loc[df.file_id == file_id] ##1000 rows × 6 columns\n    seqs = load_relevant_data_subset(pqfile) ##162699 rows × 156 columns\n    seqs_numpy = seqs.to_numpy() ##array\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_numpy[seqs.index == seq_id] ##(127, 156)\n            \n            r_nonan = np.sum(np.sum(np.isnan(frames[:, RHAND_IDX]), axis = 1) == 0)\n            l_nonan = np.sum(np.sum(np.isnan(frames[:, LHAND_IDX]), axis = 1) == 0)\n            no_nan = max(r_nonan, l_nonan) ##幀數\n            \n            if 2*len(phrase)<no_nan: ##幀數要大才記錄\n                features = {SEL_COLS[i]: tf.train.Feature(\n                    float_list=tf.train.FloatList(value=frames[:, i])) for i in range(len(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-10-12T14:53:54.18766Z","iopub.execute_input":"2023-10-12T14:53:54.188207Z","iopub.status.idle":"2023-10-12T15:04:34.927168Z","shell.execute_reply.started":"2023-10-12T14:53:54.188165Z","shell.execute_reply":"2023-10-12T15:04:34.925791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2","metadata":{}},{"cell_type":"code","source":"inpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/supplemental_metadata.csv')\ndf[\"phrase_bytes\"] = df[\"phrase\"].map(lambda x: x.encode(\"utf-8\"))\ndf.head()","metadata":{"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\ncounter = 0\nfor file_id in tqdm(df.file_id.unique()): ##68個\n    \n    print(counter)\n    counter+=1\n    \n    pqfile = f\"{inpdir}/supplemental_landmarks/{file_id}.parquet\" ##'/kaggle/input/asl-fingerspelling/supplemental_landmarks/5414471.parquet'\n    if not os.path.isdir(\"tfds\"): os.mkdir(\"tfds\")\n    tffile = f\"tfds/{file_id}.tfrecord\" ##'tfds/5414471.tfrecord'\n    seq_refs = df.loc[df.file_id == file_id] ##1000 rows × 6 columns\n    seqs = load_relevant_data_subset(pqfile) ##162699 rows × 156 columns\n    seqs_numpy = seqs.to_numpy() ##array\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_numpy[seqs.index == seq_id] ##(127, 156)\n            \n            r_nonan = np.sum(np.sum(np.isnan(frames[:, RHAND_IDX]), axis = 1) == 0)\n            l_nonan = np.sum(np.sum(np.isnan(frames[:, LHAND_IDX]), axis = 1) == 0)\n            no_nan = max(r_nonan, l_nonan) ##幀數\n            \n            if 2*len(phrase)<no_nan: ##幀數要大才記錄\n                features = {SEL_COLS[i]: tf.train.Feature(\n                    float_list=tf.train.FloatList(value=frames[:, i])) for i in range(len(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-10-12T14:45:32.198234Z","iopub.execute_input":"2023-10-12T14:45:32.198759Z","iopub.status.idle":"2023-10-12T14:53:53.789492Z","shell.execute_reply.started":"2023-10-12T14:45:32.198725Z","shell.execute_reply":"2023-10-12T14:53:53.787028Z"},"trusted":true},"execution_count":null,"outputs":[]}]}