{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":130287,"databundleVersionId":15633993}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install transformers\n!pip install tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:00.245194Z","iopub.execute_input":"2026-02-19T11:01:00.245531Z","iopub.status.idle":"2026-02-19T11:01:07.673990Z","shell.execute_reply.started":"2026-02-19T11:01:00.245507Z","shell.execute_reply":"2026-02-19T11:01:07.673285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\n\nfrom transformers import BertTokenizer, BertModel","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:07.675935Z","iopub.execute_input":"2026-02-19T11:01:07.676198Z","iopub.status.idle":"2026-02-19T11:01:24.626370Z","shell.execute_reply.started":"2026-02-19T11:01:07.676171Z","shell.execute_reply":"2026-02-19T11:01:24.625764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/test.csv\")\n\ntrain_df[\"id\"] = train_df[\"id\"].astype(str)\ntest_df[\"id\"] = test_df[\"id\"].astype(str)\n\nprint(\"Train shape:\", train_df.shape)\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:24.627168Z","iopub.execute_input":"2026-02-19T11:01:24.627576Z","iopub.status.idle":"2026-02-19T11:01:25.303967Z","shell.execute_reply.started":"2026-02-19T11:01:24.627525Z","shell.execute_reply":"2026-02-19T11:01:25.303365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nmotion_feature_path = \"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/Motion-Features\"\n\n# Get all available npy files\navailable_files = set(\n    f.replace(\".npy\", \"\")\n    for f in os.listdir(motion_feature_path)\n)\n\nprint(\"Available feature files:\", len(available_files))\n\n# Filter train_df\ntrain_df = train_df[train_df[\"id\"].isin(available_files)].reset_index(drop=True)\n\nprint(\"Filtered train shape:\", train_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:25.304890Z","iopub.execute_input":"2026-02-19T11:01:25.305140Z","iopub.status.idle":"2026-02-19T11:01:26.126952Z","shell.execute_reply.started":"2026-02-19T11:01:25.305118Z","shell.execute_reply":"2026-02-19T11:01:26.126143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MotionDataset(Dataset):\n    def __init__(self, df, motion_feature_path, tokenizer, max_motion_len=500):\n        self.df = df\n        self.motion_feature_path = motion_feature_path\n        self.tokenizer = tokenizer\n        self.max_motion_len = max_motion_len\n\n    def pad_motion(self, motion):\n        T, D = motion.shape\n        mask = np.ones(self.max_motion_len)\n\n        if T > self.max_motion_len:\n            motion = motion[:self.max_motion_len]\n        else:\n            pad = np.zeros((self.max_motion_len - T, D))\n            motion = np.concatenate([motion, pad], axis=0)\n            mask[T:] = 0\n\n        return motion, mask\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        row = self.df.iloc[idx]\n        sentence = row[\"sentence\"]\n        sample_id = row[\"id\"]\n\n        motion_path = os.path.join(\n            self.motion_feature_path,\n            f\"{sample_id}.npy\"\n        )\n\n        motion = np.load(motion_path)\n        motion, mask = self.pad_motion(motion)\n\n        tokenized = self.tokenizer(\n            sentence,\n            padding=\"max_length\",\n            truncation=True,\n            max_length=40,\n            return_tensors=\"pt\"\n        )\n\n        return {\n            \"input_ids\": tokenized[\"input_ids\"].squeeze(0),\n            \"attention_mask\": tokenized[\"attention_mask\"].squeeze(0),\n            \"motion\": torch.tensor(motion, dtype=torch.float32),\n            \"motion_mask\": torch.tensor(mask, dtype=torch.float32)\n        }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:26.128005Z","iopub.execute_input":"2026-02-19T11:01:26.128573Z","iopub.status.idle":"2026-02-19T11:01:26.135184Z","shell.execute_reply.started":"2026-02-19T11:01:26.128527Z","shell.execute_reply":"2026-02-19T11:01:26.134623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n\ntrain_dataset = MotionDataset(\n    train_df,\n    motion_feature_path=\"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/Motion-Features\",\n    tokenizer=tokenizer\n)\n\ntrain_loader = DataLoader(train_dataset, batch_size=8, shuffle=True)\n\nprint(\"Dataset ready.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:26.135995Z","iopub.execute_input":"2026-02-19T11:01:26.136255Z","iopub.status.idle":"2026-02-19T11:01:27.489930Z","shell.execute_reply.started":"2026-02-19T11:01:26.136219Z","shell.execute_reply":"2026-02-19T11:01:27.489331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = MotionDataset(\n    train_df,\n    motion_feature_path=motion_feature_path,\n    tokenizer=tokenizer\n)\n\ntrain_loader = DataLoader(train_dataset, batch_size=8, shuffle=True)\n\nprint(\"Dataset rebuilt safely.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.492254Z","iopub.execute_input":"2026-02-19T11:01:27.492869Z","iopub.status.idle":"2026-02-19T11:01:27.497115Z","shell.execute_reply.started":"2026-02-19T11:01:27.492843Z","shell.execute_reply":"2026-02-19T11:01:27.496431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = train_dataset[0]\nmotion = sample[\"motion\"].numpy()\nmask = sample[\"motion_mask\"].numpy()\n\nprint(\"Motion shape:\", motion.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.497995Z","iopub.execute_input":"2026-02-19T11:01:27.498503Z","iopub.status.idle":"2026-02-19T11:01:27.559586Z","shell.execute_reply.started":"2026-02-19T11:01:27.498480Z","shell.execute_reply":"2026-02-19T11:01:27.558969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Feature 0 Stats:\")\nprint(\"Min:\", motion[:, 0].min())\nprint(\"Max:\", motion[:, 0].max())\nprint(\"Mean:\", motion[:, 0].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.560404Z","iopub.execute_input":"2026-02-19T11:01:27.560838Z","iopub.status.idle":"2026-02-19T11:01:27.565445Z","shell.execute_reply.started":"2026-02-19T11:01:27.560804Z","shell.execute_reply":"2026-02-19T11:01:27.564903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"variances = motion.var(axis=0)\ntop_feature = np.argmax(variances)\n\nprint(\"Most dynamic feature index:\", top_feature)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.566244Z","iopub.execute_input":"2026-02-19T11:01:27.566442Z","iopub.status.idle":"2026-02-19T11:01:27.576528Z","shell.execute_reply.started":"2026-02-19T11:01:27.566422Z","shell.execute_reply":"2026-02-19T11:01:27.575913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure()\nplt.plot(motion[:350, top_feature])\nplt.title(f\"Most Dynamic Feature ({top_feature})\")\nplt.xlabel(\"Frame\")\nplt.ylabel(\"Value\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.577449Z","iopub.execute_input":"2026-02-19T11:01:27.578053Z","iopub.status.idle":"2026-02-19T11:01:27.746961Z","shell.execute_reply.started":"2026-02-19T11:01:27.578028Z","shell.execute_reply":"2026-02-19T11:01:27.746401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"top5 = np.argsort(variances)[-5:]\n\nplt.figure()\nfor idx in top5:\n    plt.plot(motion[:350, idx])\n\nplt.title(\"Top 5 Most Dynamic Features\")\nplt.xlabel(\"Frame\")\nplt.ylabel(\"Value\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.747721Z","iopub.execute_input":"2026-02-19T11:01:27.748013Z","iopub.status.idle":"2026-02-19T11:01:27.871869Z","shell.execute_reply.started":"2026-02-19T11:01:27.747991Z","shell.execute_reply":"2026-02-19T11:01:27.871334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"motion_energy = np.linalg.norm(motion, axis=1)\n\nplt.figure()\nplt.plot(motion_energy)\nplt.title(\"Motion Energy Over Time\")\nplt.xlabel(\"Frame\")\nplt.ylabel(\"L2 Norm\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.872636Z","iopub.execute_input":"2026-02-19T11:01:27.872876Z","iopub.status.idle":"2026-02-19T11:01:27.979480Z","shell.execute_reply.started":"2026-02-19T11:01:27.872854Z","shell.execute_reply":"2026-02-19T11:01:27.978755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure()\nplt.plot(mask)\nplt.title(\"Motion Mask (1=Real, 0=Padding)\")\nplt.xlabel(\"Frame\")\nplt.ylabel(\"Mask\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:27.980406Z","iopub.execute_input":"2026-02-19T11:01:27.980706Z","iopub.status.idle":"2026-02-19T11:01:28.086862Z","shell.execute_reply.started":"2026-02-19T11:01:27.980677Z","shell.execute_reply":"2026-02-19T11:01:28.086308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"real_length = int(mask.sum())\n\nplt.figure()\nplt.plot(motion_energy)\nplt.axvline(real_length, linestyle='--')\nplt.title(\"Motion Energy with Padding Boundary\")\nplt.xlabel(\"Frame\")\nplt.ylabel(\"L2 Norm\")\nplt.show()\n\nprint(\"Real motion length:\", real_length)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:28.087644Z","iopub.execute_input":"2026-02-19T11:01:28.087904Z","iopub.status.idle":"2026-02-19T11:01:28.199608Z","shell.execute_reply.started":"2026-02-19T11:01:28.087876Z","shell.execute_reply":"2026-02-19T11:01:28.198791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class PositionalEncoding(nn.Module):\n    def __init__(self, d_model, max_len=500):\n        super().__init__()\n\n        pe = torch.zeros(max_len, d_model)\n        position = torch.arange(0, max_len).unsqueeze(1)\n\n        div_term = torch.exp(\n            torch.arange(0, d_model, 2) *\n            (-np.log(10000.0) / d_model)\n        )\n\n        pe[:, 0::2] = torch.sin(position * div_term)\n        pe[:, 1::2] = torch.cos(position * div_term)\n\n        pe = pe.unsqueeze(0)\n        self.register_buffer(\"pe\", pe)\n\n    def forward(self, x):\n        return x + self.pe[:, :x.size(1)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:28.200574Z","iopub.execute_input":"2026-02-19T11:01:28.200881Z","iopub.status.idle":"2026-02-19T11:01:28.208245Z","shell.execute_reply.started":"2026-02-19T11:01:28.200840Z","shell.execute_reply":"2026-02-19T11:01:28.207439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Text2Motion(nn.Module):\n    def __init__(self, motion_dim=668, hidden=512):\n        super().__init__()\n\n        self.text_encoder = BertModel.from_pretrained(\"bert-base-uncased\")\n        self.text_proj = nn.Linear(768, hidden)\n\n        self.motion_proj = nn.Linear(motion_dim, hidden)\n        self.pos_enc = PositionalEncoding(hidden)\n\n        decoder_layer = nn.TransformerDecoderLayer(\n            d_model=hidden,\n            nhead=8,\n            batch_first=True\n        )\n\n        self.decoder = nn.TransformerDecoder(\n            decoder_layer,\n            num_layers=4\n        )\n\n        self.output_layer = nn.Linear(hidden, motion_dim)\n\n    def forward(self, input_ids, attention_mask, motion):\n\n        text_out = self.text_encoder(\n            input_ids=input_ids,\n            attention_mask=attention_mask\n        )\n\n        memory = self.text_proj(text_out.last_hidden_state)\n\n        tgt = self.motion_proj(motion)\n        tgt = self.pos_enc(tgt)\n\n        decoded = self.decoder(tgt=tgt, memory=memory)\n\n        output = self.output_layer(decoded)\n\n        return output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:28.209345Z","iopub.execute_input":"2026-02-19T11:01:28.209706Z","iopub.status.idle":"2026-02-19T11:01:28.223954Z","shell.execute_reply.started":"2026-02-19T11:01:28.209683Z","shell.execute_reply":"2026-02-19T11:01:28.223174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nmodel = Text2Motion().to(device)\n\noptimizer = optim.AdamW(model.parameters(), lr=1e-4)\ncriterion = nn.MSELoss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:28.225022Z","iopub.execute_input":"2026-02-19T11:01:28.225422Z","iopub.status.idle":"2026-02-19T11:01:30.954854Z","shell.execute_reply.started":"2026-02-19T11:01:28.225375Z","shell.execute_reply":"2026-02-19T11:01:30.954039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs = 5\nloss_history = []\n\nfor epoch in range(epochs):\n    model.train()\n    total_loss = 0\n\n    for batch in tqdm(train_loader):\n\n        input_ids = batch[\"input_ids\"].to(device)\n        attention_mask = batch[\"attention_mask\"].to(device)\n        motion = batch[\"motion\"].to(device)\n        mask = batch[\"motion_mask\"].to(device)\n\n        optimizer.zero_grad()\n\n        output = model(input_ids, attention_mask, motion)\n\n        loss = ((output - motion) ** 2)\n        loss = loss.mean(dim=2)\n        loss = (loss * mask).sum() / mask.sum()\n\n        loss.backward()\n        optimizer.step()\n\n        total_loss += loss.item()\n\n    epoch_loss = total_loss / len(train_loader)\n    loss_history.append(epoch_loss)\n\n    print(f\"Epoch {epoch+1} Loss: {epoch_loss}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:01:30.955876Z","iopub.execute_input":"2026-02-19T11:01:30.956196Z","iopub.status.idle":"2026-02-19T11:45:50.994737Z","shell.execute_reply.started":"2026-02-19T11:01:30.956163Z","shell.execute_reply":"2026-02-19T11:45:50.993131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure()\nplt.plot(loss_history)\nplt.title(\"Training Loss Curve\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:45:50.999761Z","iopub.execute_input":"2026-02-19T11:45:51.000002Z","iopub.status.idle":"2026-02-19T11:45:51.171355Z","shell.execute_reply.started":"2026-02-19T11:45:50.999979Z","shell.execute_reply":"2026-02-19T11:45:51.170775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\n\nbatch = next(iter(train_loader))\n\ninput_ids = batch[\"input_ids\"].to(device)\nattention_mask = batch[\"attention_mask\"].to(device)\nmotion = batch[\"motion\"].to(device)\n\nwith torch.no_grad():\n    pred = model(input_ids, attention_mask, motion)\n\ngt = motion[0].cpu().numpy()\npred = pred[0].cpu().numpy()\n\nplt.figure()\nplt.plot(gt[:200, 0], label=\"Ground Truth\")\nplt.plot(pred[:200, 0], label=\"Prediction\")\nplt.legend()\nplt.title(\"GT vs Predicted Motion Feature 0\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:45:51.172242Z","iopub.execute_input":"2026-02-19T11:45:51.172491Z","iopub.status.idle":"2026-02-19T11:45:51.664996Z","shell.execute_reply.started":"2026-02-19T11:45:51.172468Z","shell.execute_reply":"2026-02-19T11:45:51.664283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"variances = gt.var(axis=0)\ntop_feature = np.argmax(variances)\n\nplt.figure()\nplt.plot(gt[:200, top_feature], label=\"GT\")\nplt.plot(pred[:200, top_feature], label=\"Prediction\")\nplt.legend()\nplt.title(f\"GT vs Predicted (Feature {top_feature})\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:45:51.667578Z","iopub.execute_input":"2026-02-19T11:45:51.667889Z","iopub.status.idle":"2026-02-19T11:45:51.811186Z","shell.execute_reply.started":"2026-02-19T11:45:51.667863Z","shell.execute_reply":"2026-02-19T11:45:51.810496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language\"\n\ntest_df = pd.read_csv(f\"{BASE_PATH}/test.csv\")\nsample_sub = pd.read_csv(f\"{BASE_PATH}/sample_submission.csv\")\n\nprint(\"Test shape:\", test_df.shape)\nprint(\"Sample submission shape:\", sample_sub.shape)\n\nsample_sub.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:46:41.534454Z","iopub.execute_input":"2026-02-19T11:46:41.535042Z","iopub.status.idle":"2026-02-19T11:46:41.585265Z","shell.execute_reply.started":"2026-02-19T11:46:41.535009Z","shell.execute_reply":"2026-02-19T11:46:41.584701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/sample_submission.csv\")\n\nfor col in sample_sub.columns:\n    if col != \"id\":\n        print(col, \"token count:\",\n              len(sample_sub.iloc[0][col].split()))\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Copy sample submission structure\nsubmission = sample_sub.copy()\n\n# Fill prediction column with dummy values (safe baseline)\n\nif \"motion\" in submission.columns:\n    submission[\"motion\"] = \"0\"\n\nif \"layered_motion_tokens\" in submission.columns:\n    submission[\"layered_motion_tokens\"] = \"0\"\n\nprint(\"Submission preview:\")\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:46:44.862702Z","iopub.execute_input":"2026-02-19T11:46:44.863288Z","iopub.status.idle":"2026-02-19T11:46:44.873083Z","shell.execute_reply.started":"2026-02-19T11:46:44.863260Z","shell.execute_reply":"2026-02-19T11:46:44.872319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/motion-s-hierarchical-text-to-motion-generation-for-sign-language/sample_submission.csv\")\n\nsubmission = sample_sub.copy()\n\n# Replace all token strings with same-length zeros\nfor col in submission.columns:\n    if col != \"id\":\n        token_count = len(submission.iloc[0][col].split())\n        zero_string = \" \".join([\"0\"] * token_count)\n        submission[col] = zero_string\n\nsubmission.to_csv(\"submission.csv\", index=False)\n\nprint(\"Fixed submission.csv created.\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n\nprint(\"submission.csv created successfully.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:47:06.916892Z","iopub.execute_input":"2026-02-19T11:47:06.917541Z","iopub.status.idle":"2026-02-19T11:47:06.928029Z","shell.execute_reply.started":"2026-02-19T11:47:06.917502Z","shell.execute_reply":"2026-02-19T11:47:06.927270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/working\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-19T11:47:34.473135Z","iopub.execute_input":"2026-02-19T11:47:34.473465Z","iopub.status.idle":"2026-02-19T11:47:34.477932Z","shell.execute_reply.started":"2026-02-19T11:47:34.473436Z","shell.execute_reply":"2026-02-19T11:47:34.477255Z"}},"outputs":[],"execution_count":null}]}