{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"},{"sourceId":242954653,"sourceType":"kernelVersion"}],"dockerImageVersionId":31041,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Lib","metadata":{}},{"cell_type":"code","source":"import os, json, joblib, numpy as np, pandas as pd\nfrom pathlib import Path\nimport warnings \nwarnings.filterwarnings(\"ignore\")\n\n\nfrom sklearn.model_selection import GroupKFold\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\nfrom sklearn.utils.class_weight import compute_class_weight\n\nfrom tensorflow.keras.utils import Sequence, to_categorical, pad_sequences\nfrom tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.layers import (\n    Input, Conv1D, BatchNormalization, Activation, add, MaxPooling1D, Dropout,\n    Bidirectional, LSTM, GlobalAveragePooling1D, Dense, Multiply, Reshape,\n    Lambda, Concatenate\n)\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras import backend as K\nimport tensorflow as tf\nimport polars as pl\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:04:55.641674Z","iopub.execute_input":"2025-06-12T17:04:55.642382Z","iopub.status.idle":"2025-06-12T17:05:10.207775Z","shell.execute_reply.started":"2025-06-12T17:04:55.642356Z","shell.execute_reply":"2025-06-12T17:05:10.207176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN = True                 \nRAW_DIR = Path(\"/kaggle/input/cmi-detect-behavior-with-sensor-data\")\nPRETRAINED_DIR = Path(\"/kaggle/input/pretrained-model\") \nEXPORT_DIR = Path(\"./\")                           \nBATCH_SIZE = 128\nPAD_PERCENTILE = 97\nLR_INIT = 1e-3\nWD = 3e-4\nMIXUP_ALPHA = 0.4\nEPOCHS = 250\nPATIENCE = 100\n\nprint(\"▶ imports ready · tensorflow\", tf.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:05:10.208791Z","iopub.execute_input":"2025-06-12T17:05:10.209324Z","iopub.status.idle":"2025-06-12T17:05:10.214186Z","shell.execute_reply.started":"2025-06-12T17:05:10.209302Z","shell.execute_reply":"2025-06-12T17:05:10.213422Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Utility Functions","metadata":{}},{"cell_type":"code","source":"#Tensor Manipulations\ndef time_sum(x):\n    return K.sum(x, axis=1)\n\ndef squeeze_last_axis(x):\n    return tf.squeeze(x, axis=-1)\n\ndef expand_last_axis(x):\n    return tf.expand_dims(x, axis=-1)\n\ndef se_block(x, reduction=8):\n    ch = x.shape[-1]\n    se = GlobalAveragePooling1D()(x)\n    se = Dense(ch // reduction, activation='relu')(se)\n    se = Dense(ch, activation='sigmoid')(se)\n    se = Reshape((1, ch))(se)\n    return Multiply()([x, se])\n\n\n# Residual CNN Block with SE\ndef residual_se_cnn_block(x, filters, kernel_size, pool_size=2, drop=0.3, wd=1e-4):\n    shortcut = x\n    for _ in range(2):\n        x = Conv1D(filters, kernel_size, padding='same', use_bias=False,\n                   kernel_regularizer=l2(wd))(x)\n        x = BatchNormalization()(x)\n        x = Activation('relu')(x)\n    x = se_block(x)\n    if shortcut.shape[-1] != filters:\n        shortcut = Conv1D(filters, 1, padding='same', use_bias=False,\n                          kernel_regularizer=l2(wd))(shortcut)\n        shortcut = BatchNormalization()(shortcut)\n    x = add([x, shortcut])\n    x = Activation('relu')(x)\n    x = MaxPooling1D(pool_size)(x)\n    x = Dropout(drop)(x)\n    return x\n\ndef attention_layer(inputs):\n    score = Dense(1, activation='tanh')(inputs)\n    score = Lambda(squeeze_last_axis)(score)\n    weights = Activation('softmax')(score)\n    weights = Lambda(expand_last_axis)(weights)\n    context = Multiply()([inputs, weights])\n    context = Lambda(time_sum)(context)\n    return context\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:05:10.214850Z","iopub.execute_input":"2025-06-12T17:05:10.215035Z","iopub.status.idle":"2025-06-12T17:05:10.253362Z","shell.execute_reply.started":"2025-06-12T17:05:10.215020Z","shell.execute_reply":"2025-06-12T17:05:10.252636Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Helpers","metadata":{}},{"cell_type":"code","source":"def preprocess_sequence(df_seq: pd.DataFrame, feature_cols: list[str], scaler: StandardScaler):\n    mat = df_seq[feature_cols].ffill().bfill().fillna(0).values\n    return scaler.transform(mat).astype('float32')\n\n# MixUp the data argumentation in order to regularize the neural network. \nclass MixupGenerator(Sequence):\n    def __init__(self, X, y, batch_size, alpha=0.2):\n        self.X, self.y = X, y\n        self.batch = batch_size\n        self.alpha = alpha\n        self.indices = np.arange(len(X))\n    def __len__(self):\n        return int(np.ceil(len(self.X) / self.batch))\n    def __getitem__(self, i):\n        idx = self.indices[i*self.batch:(i+1)*self.batch]\n        Xb, yb = self.X[idx], self.y[idx]\n        lam = np.random.beta(self.alpha, self.alpha)\n        perm = np.random.permutation(len(Xb))\n        X_mix = lam * Xb + (1-lam) * Xb[perm]\n        y_mix = lam * yb + (1-lam) * yb[perm]\n        return X_mix, y_mix\n    def on_epoch_end(self):\n        np.random.shuffle(self.indices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:05:10.255044Z","iopub.execute_input":"2025-06-12T17:05:10.255283Z","iopub.status.idle":"2025-06-12T17:05:10.274052Z","shell.execute_reply.started":"2025-06-12T17:05:10.255264Z","shell.execute_reply":"2025-06-12T17:05:10.273367Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Definition - Two Branch Architecture","metadata":{}},{"cell_type":"code","source":"def build_two_branch_model(pad_len, imu_dim, tof_dim, n_classes, wd=1e-4):\n    inp = Input(shape=(pad_len, imu_dim+tof_dim))\n    imu = Lambda(lambda t: t[:, :, :imu_dim])(inp)\n    tof = Lambda(lambda t: t[:, :, imu_dim:])(inp)\n\n    # IMU deep branch\n    x1 = residual_se_cnn_block(imu, 64, 3, drop=0.3, wd=wd)\n    x1 = residual_se_cnn_block(x1, 128, 5, drop=0.3, wd=wd)\n\n    # TOF/Thermal lighter branch\n    x2 = Conv1D(64, 3, padding='same', use_bias=False, kernel_regularizer=l2(wd))(tof)\n    x2 = BatchNormalization()(x2); x2 = Activation('relu')(x2)\n    x2 = MaxPooling1D(2)(x2); x2 = Dropout(0.3)(x2)\n    x2 = Conv1D(128, 3, padding='same', use_bias=False, kernel_regularizer=l2(wd))(x2)\n    x2 = BatchNormalization()(x2); x2 = Activation('relu')(x2)\n    x2 = MaxPooling1D(2)(x2); x2 = Dropout(0.3)(x2)\n\n    merged = Concatenate()([x1, x2])\n\n    x = Bidirectional(LSTM(128, return_sequences=True, kernel_regularizer=l2(wd)))(merged)\n    x = Dropout(0.4)(x)\n    x = attention_layer(x)\n\n    for units, drop in [(256, 0.5), (128, 0.3)]:\n        x = Dense(units, use_bias=False, kernel_regularizer=l2(wd))(x)\n        x = BatchNormalization()(x); x = Activation('relu')(x)\n        x = Dropout(drop)(x)\n\n    out = Dense(n_classes, activation='softmax', kernel_regularizer=l2(wd))(x)\n    return Model(inp, out)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:05:10.274818Z","iopub.execute_input":"2025-06-12T17:05:10.275025Z","iopub.status.idle":"2025-06-12T17:05:10.293142Z","shell.execute_reply.started":"2025-06-12T17:05:10.275008Z","shell.execute_reply":"2025-06-12T17:05:10.292364Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training / Inference Pipeline","metadata":{}},{"cell_type":"code","source":"if TRAIN:\n    print(\"▶ TRAIN MODE – loading dataset …\")\n    df = pd.read_csv(RAW_DIR / \"train.csv\")\n\n    # label encoding\n    le = LabelEncoder(); df['gesture_int'] = le.fit_transform(df['gesture'])\n    np.save(EXPORT_DIR / \"gesture_classes.npy\", le.classes_)\n\n    # feature list\n    meta_cols = {'gesture', 'gesture_int', 'sequence_type', 'behavior', 'orientation',\n                 'row_id', 'subject', 'phase', 'sequence_id', 'sequence_counter'}\n    feature_cols = [c for c in df.columns if c not in meta_cols]\n\n    imu_cols  = [c for c in feature_cols if not (c.startswith('thm_') or c.startswith('tof_'))]\n    tof_cols  = [c for c in feature_cols if c.startswith('thm_') or c.startswith('tof_')]\n    print(f\"  IMU {len(imu_cols)} | TOF/THM {len(tof_cols)} | total {len(feature_cols)} features\")\n\n    # global scaler\n    scaler = StandardScaler().fit(df[feature_cols].ffill().bfill().fillna(0).values)\n    joblib.dump(scaler, EXPORT_DIR / \"scaler.pkl\")\n\n    # build sequences\n    seq_gp = df.groupby('sequence_id')\n    X_list, y_list, lens = [], [], []\n    for seq_id, seq in seq_gp:\n        mat = preprocess_sequence(seq, feature_cols, scaler)\n        X_list.append(mat)\n        y_list.append(seq['gesture_int'].iloc[0])\n        lens.append(len(mat))\n    pad_len = int(np.percentile(lens, PAD_PERCENTILE))\n    np.save(EXPORT_DIR / \"sequence_maxlen.npy\", pad_len)\n    np.save(EXPORT_DIR / \"feature_cols.npy\", np.array(feature_cols))\n\n    # Get gesture labels and groups\n    y_labels = np.array(y_list)\n    groups = np.array([seq_id for seq_id, _ in seq_gp])  # sequence_id per sample\n    \n    # Convert X and y to numpy arrays\n    X = pad_sequences(X_list, maxlen=pad_len, padding='post', truncating='post', dtype='float32')\n    y = to_categorical(y_labels, num_classes=len(le.classes_))\n    \n    # Group-aware split\n    gkf = GroupKFold(n_splits=5)\n    for fold, (train_idx, val_idx) in enumerate(gkf.split(X, y_labels, groups=groups)):\n        X_tr, X_val = X[train_idx], X[val_idx]\n        y_tr, y_val = y[train_idx], y[val_idx]\n        print(f\"✅ Fold {fold}: Train size = {len(train_idx)}, Val size = {len(val_idx)}\")\n        break  # Use only the first fold for now\n        \n    # class weights\n    cw_vals = compute_class_weight('balanced', classes=np.arange(len(le.classes_)), y=y_list)\n    class_weight = dict(enumerate(cw_vals))\n\n    # model\n    model = build_two_branch_model(pad_len, len(imu_cols), len(tof_cols), len(le.classes_), wd=WD)\n    steps = len(X_tr)//BATCH_SIZE\n    lr_sched = tf.keras.optimizers.schedules.CosineDecayRestarts(LR_INIT, first_decay_steps=5*steps)\n    model.compile(optimizer=Adam(lr_sched),\n                  loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n                  metrics=['accuracy'])\n\n    train_gen = MixupGenerator(X_tr, y_tr, batch_size=BATCH_SIZE, alpha=MIXUP_ALPHA)\n    cb = EarlyStopping(patience=PATIENCE, restore_best_weights=True, verbose=1)\n    model.fit(train_gen, epochs=EPOCHS, validation_data=(X_val, y_val),\n              class_weight=class_weight, callbacks=[cb], verbose=1)\n\n    model.save(EXPORT_DIR / \"gesture_two_branch_mixup.h5\")\n    print(\"✔ Training done – artefacts saved in\", EXPORT_DIR)\n\n    # quick metric\n    from cmi_2025_metric_copy_for_import import CompetitionMetric\n    preds = model.predict(X_val).argmax(1)\n    true  = y_val.argmax(1)\n    h_f1 = CompetitionMetric().calculate_hierarchical_f1(\n        pd.DataFrame({'gesture': le.classes_[true]}),\n        pd.DataFrame({'gesture': le.classes_[preds]}))\n    print(\"Hold‑out H‑F1 =\", round(h_f1, 4))\n\nelse:\n    print(\"▶ INFERENCE MODE – loading artefacts from\", PRETRAINED_DIR)\n    feature_cols   = np.load(PRETRAINED_DIR / \"feature_cols.npy\", allow_pickle=True).tolist()\n    pad_len        = int(np.load(PRETRAINED_DIR / \"sequence_maxlen.npy\"))\n    scaler         = joblib.load(PRETRAINED_DIR / \"scaler.pkl\")\n    gesture_classes = np.load(PRETRAINED_DIR / \"gesture_classes.npy\", allow_pickle=True)\n\n    imu_cols = [c for c in feature_cols if not (c.startswith('thm_') or c.startswith('tof_'))]\n    tof_cols = [c for c in feature_cols if c.startswith('thm_') or c.startswith('tof_')]\n\n    custom_objs = {\n        'time_sum': time_sum,\n        'squeeze_last_axis': squeeze_last_axis,\n        'expand_last_axis': expand_last_axis,\n        'se_block': se_block,\n        'residual_se_cnn_block': residual_se_cnn_block,\n        'attention_layer': attention_layer,\n    }\n    model = load_model(PRETRAINED_DIR / \"gesture_two_branch_mixup.h5\",\n                       compile=False, custom_objects=custom_objs)\n    print(\"  model, scaler, pads loaded – ready for evaluation\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:05:10.294013Z","iopub.execute_input":"2025-06-12T17:05:10.294293Z","iopub.status.idle":"2025-06-12T17:17:24.171491Z","shell.execute_reply.started":"2025-06-12T17:05:10.294269Z","shell.execute_reply":"2025-06-12T17:17:24.170795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Visualize sequence lengths\nplt.figure(figsize=(10, 5))\nsns.histplot(lens, bins=30, kde=True)\nplt.axvline(pad_len, color='red', linestyle='--', label=f'{PAD_PERCENTILE}th Percentile = {pad_len}')\nplt.title(\"Distribution of Sequence Lengths\")\nplt.xlabel(\"Sequence Length\")\nplt.ylabel(\"Frequency\")\nplt.legend()\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:17:24.172240Z","iopub.execute_input":"2025-06-12T17:17:24.172480Z","iopub.status.idle":"2025-06-12T17:17:24.895244Z","shell.execute_reply.started":"2025-06-12T17:17:24.172433Z","shell.execute_reply":"2025-06-12T17:17:24.894486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Visualize before and after scaling\n# sample_df = df[feature_cols].copy().ffill().bfill().fillna(0)\n# scaled_df = pd.DataFrame(scaler.transform(sample_df), columns=feature_cols)\n\n# fig, axes = plt.subplots(2, 1, figsize=(14, 8), sharex=True)\n# sns.kdeplot(sample_df[feature_cols].values.flatten(), ax=axes[0], fill=True)\n# axes[0].set_title(\"Original Feature Distribution\")\n# sns.kdeplot(scaled_df[feature_cols].values.flatten(), ax=axes[1], fill=True, color='orange')\n# axes[1].set_title(\"Scaled Feature Distribution\")\n# plt.tight_layout()\n# plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:19:05.332907Z","iopub.execute_input":"2025-06-12T17:19:05.333432Z","iopub.status.idle":"2025-06-12T17:19:05.336966Z","shell.execute_reply.started":"2025-06-12T17:19:05.333410Z","shell.execute_reply":"2025-06-12T17:19:05.336242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nsns.histplot(lens, bins=50, kde=True)\nplt.axvline(pad_len, color='red', linestyle='--', label=f'Pad Percentile ({PAD_PERCENTILE}%) = {pad_len}')\nplt.title(\"Sequence Lengths Distribution\")\nplt.xlabel(\"Sequence Length\")\nplt.ylabel(\"Count\")\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:19:06.615538Z","iopub.execute_input":"2025-06-12T17:19:06.616252Z","iopub.status.idle":"2025-06-12T17:19:06.984142Z","shell.execute_reply.started":"2025-06-12T17:19:06.616231Z","shell.execute_reply":"2025-06-12T17:19:06.983420Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen, epochs=EPOCHS, validation_data=(X_val, y_val),\n                    class_weight=class_weight, callbacks=[cb], verbose=1)\n\n# Plot training history\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Val Loss')\nplt.title(\"Loss Over Epochs\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Train Acc')\nplt.plot(history.history['val_accuracy'], label='Val Acc')\nplt.title(\"Accuracy Over Epochs\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:19:10.109778Z","iopub.execute_input":"2025-06-12T17:19:10.110336Z","iopub.status.idle":"2025-06-12T17:24:38.657619Z","shell.execute_reply.started":"2025-06-12T17:19:10.110312Z","shell.execute_reply":"2025-06-12T17:24:38.656778Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Predicting","metadata":{}},{"cell_type":"code","source":"if TRAIN:\n    gesture_classes = le.classes_\n\ndef predict(sequence: pl.DataFrame, demographics: pl.DataFrame) -> str:\n    global gesture_classes\n    if gesture_classes is None:\n        gesture_classes = np.load(PRETRAINED_DIR / \"gesture_classes.npy\", allow_pickle=True)\n\n    df_seq = sequence.to_pandas()\n    mat = preprocess_sequence(df_seq, feature_cols, scaler)\n    pad = pad_sequences([mat], maxlen=pad_len, padding='post', truncating='post', dtype='float32')\n    idx = int(model.predict(pad, verbose=0).argmax(1)[0])\n    return str(gesture_classes[idx])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:24:39.373750Z","iopub.execute_input":"2025-06-12T17:24:39.373999Z","iopub.status.idle":"2025-06-12T17:24:39.378986Z","shell.execute_reply.started":"2025-06-12T17:24:39.373981Z","shell.execute_reply":"2025-06-12T17:24:39.378390Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Integration with Kaggle Evaluation","metadata":{}},{"cell_type":"code","source":"import kaggle_evaluation.cmi_inference_server\ninference_server = kaggle_evaluation.cmi_inference_server.CMIInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv',\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T17:24:39.380326Z","iopub.execute_input":"2025-06-12T17:24:39.380543Z","iopub.status.idle":"2025-06-12T17:24:41.346830Z","shell.execute_reply.started":"2025-06-12T17:24:39.380527Z","shell.execute_reply":"2025-06-12T17:24:41.345999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}