{"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":"class CFG:\n    data_path = \"../input/asl-signs/\"\n","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:23:32.725910Z","iopub.execute_input":"2023-02-26T15:23:32.726266Z","iopub.status.idle":"2023-02-26T15:23:32.750082Z","shell.execute_reply.started":"2023-02-26T15:23:32.726236Z","shell.execute_reply":"2023-02-26T15:23:32.749216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tqdm import tqdm\nimport json\nimport os\nfrom sklearn.model_selection import train_test_split\n\nROWS_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    data.replace(np.nan, 0, inplace=True)\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)\n\ndef read_dict(file_path):\n    path = os.path.expanduser(file_path)\n    with open(path, \"r\") as f:\n        dic = json.load(f)\n    return dic\n\ntrain = pd.read_csv(f\"{CFG.data_path}train.csv\")\n\nlabel_index = read_dict(f\"{CFG.data_path}sign_to_prediction_index_map.json\")\nindex_label = dict([(label_index[key], key) for key in label_index])\n\ntrain[\"label\"] = train[\"sign\"].map(lambda sign: label_index[sign])","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:23:59.758670Z","iopub.execute_input":"2023-02-26T15:23:59.759119Z","iopub.status.idle":"2023-02-26T15:24:08.248033Z","shell.execute_reply.started":"2023-02-26T15:23:59.759070Z","shell.execute_reply":"2023-02-26T15:24:08.247058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xs = []\nys = []\n\nmaxFrames = 50\n\nfor i in tqdm(range(len(train))):\n\n    path = f\"{CFG.data_path}{train.iloc[i].path}\"\n    data = load_relevant_data_subset(path)\n\n    #NaN to ZERO\n    data = np.nan_to_num(data)\n\n    #Convert each frame from (543,3) to (543 * 3)\n    data = data.reshape(data.shape[0], -1)\n\n    n_frames = data.shape[0]\n\n    if n_frames > maxFrames:\n        # Extract maxFrames items spaced evenly from the list\n        indices = np.linspace(0, n_frames - 1, num=maxFrames, dtype=int)\n        data = data[indices]\n\n    # Apply padding to the example sequence with a maximum length of 50 time steps, using a padding value of -1\n    data = tf.keras.utils.pad_sequences([data], maxlen=maxFrames, padding='post', truncating='post', value=0.0, dtype='float64').squeeze(axis=0)\n\n    xs.append(data)\n\n    class_id = train.iloc[i].label\n    label = tf.keras.utils.to_categorical(class_id, num_classes=250)\n    ys.append(label)\n\n\n    if i == 20000:\n        break\n    \n## Save number of frames of each training sample for data analysis\n\nX = np.array(xs)\ny = np.array(ys)\nprint(X.shape, y.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:47:03.034365Z","iopub.execute_input":"2023-02-26T15:47:03.035472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# print the shapes of the resulting datasets\nprint('X_train shape:', X_train.shape)\nprint('X_test shape:', X_test.shape)\nprint('y_train shape:', y_train.shape)\nprint('y_test shape:', y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:27:43.732251Z","iopub.execute_input":"2023-02-26T15:27:43.733017Z","iopub.status.idle":"2023-02-26T15:27:46.344663Z","shell.execute_reply.started":"2023-02-26T15:27:43.732974Z","shell.execute_reply":"2023-02-26T15:27:46.343351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define LSTM model architecture\nmodel = tf.keras.Sequential([\n    tf.keras.layers.LSTM(units=64, return_sequences=True, input_shape=(None, 1629)),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.LSTM(units=32, return_sequences=False),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(units=250, activation='softmax')\n])\n\n# Define the Adam optimizer with a learning rate of 0.0001\nadam = tf.keras.optimizers.Adam(learning_rate=0.0001)\n\n# Compile the model\nmodel.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])\n\n# Print the model summary\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:27:54.039587Z","iopub.execute_input":"2023-02-26T15:27:54.040032Z","iopub.status.idle":"2023-02-26T15:27:55.040102Z","shell.execute_reply.started":"2023-02-26T15:27:54.039977Z","shell.execute_reply":"2023-02-26T15:27:55.038964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nmodel.fit(X_train, y_train, batch_size=32, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:28:01.624028Z","iopub.execute_input":"2023-02-26T15:28:01.624477Z","iopub.status.idle":"2023-02-26T15:31:09.104890Z","shell.execute_reply.started":"2023-02-26T15:28:01.624439Z","shell.execute_reply":"2023-02-26T15:31:09.103958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on new data\n# Here, X_test is the test set of input images of shape (num_samples, 100, 543, 3)\ny_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:31:49.138025Z","iopub.execute_input":"2023-02-26T15:31:49.138469Z","iopub.status.idle":"2023-02-26T15:31:53.808152Z","shell.execute_reply.started":"2023-02-26T15:31:49.138433Z","shell.execute_reply":"2023-02-26T15:31:53.807160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(y_pred.shape[0]):\n    signPred = np.argmax(y_pred[i])\n    signReal = np.argmax(y_test[i])\n    mark = \"Prediction\"\n    if signPred == signReal:\n        mark = \"*************** MATCH\"\n    print(f\"{mark}:{index_label[signPred]} vs {index_label[signReal]}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:40:51.062203Z","iopub.execute_input":"2023-02-26T15:40:51.063041Z","iopub.status.idle":"2023-02-26T15:40:51.096326Z","shell.execute_reply.started":"2023-02-26T15:40:51.062996Z","shell.execute_reply":"2023-02-26T15:40:51.095108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(y_test.shape[0]):\n    if np.argmax(y_test[i]) == np.argmax(y_pred[i]):\n        print(np.argmax(y_test[i]))\n\n        sign = np.argmax(y_test[i])\n        print(f\"Match: {index_label[sign]}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-02-26T15:36:43.591562Z","iopub.execute_input":"2023-02-26T15:36:43.592901Z","iopub.status.idle":"2023-02-26T15:36:43.611330Z","shell.execute_reply.started":"2023-02-26T15:36:43.592855Z","shell.execute_reply":"2023-02-26T15:36:43.610219Z"},"trusted":true},"execution_count":null,"outputs":[]}]}