{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"},{"sourceId":12139340,"sourceType":"datasetVersion","datasetId":7645099},{"sourceId":242954653,"sourceType":"kernelVersion"}],"dockerImageVersionId":31041,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nTrain&Inf Kernel\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"### **ℹ️INFO**\n* First, we would like to thank our participants for sharing their excellent baselines.\n    * [Two‑Branch Human‑Activity‑Recognition Pipeline (IMU + Thermopile/TOF  + SE‑CNN + BiLSTM + Attentio](https://www.kaggle.com/code/vonmainstein/imu-tof)\n \n### **ℹ️MyUpdate**\n* My main additions are\n    * Ensuring reproducibility with SeedFix\n    * Introducing a Gaussian Noise layer to suppress overfitting\n    * Adding a GRU layer, which is often used in conjunction with LSTM\n    * Tuning TTS as data split","metadata":{}},{"cell_type":"markdown","source":"### **ℹ️ModelDef**","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nim1 = Image.open('/kaggle/input/cmi25-imu-thmtof-tf-bilstm-gru-attentionlb-xx/CMI-Model.png')\ndef get_concat_h_multi_resize(im_list, resample=Image.BICUBIC):\n    min_height = min(im.height for im in im_list)\n    im_list_resize = [im.resize((int(im.width * min_height / im.height), min_height),resample=resample)\n                      for im in im_list]\n    total_width = sum(im.width for im in im_list_resize)\n    dst = Image.new('RGB', (total_width, min_height))\n    pos_x = 0\n    for im in im_list_resize:\n        dst.paste(im, (pos_x, 0))\n        pos_x += im.width\n    return dst\nget_concat_h_multi_resize([im1])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-12T20:01:39.208673Z","iopub.execute_input":"2025-06-12T20:01:39.208925Z","iopub.status.idle":"2025-06-12T20:01:42.708269Z","shell.execute_reply.started":"2025-06-12T20:01:39.208904Z","shell.execute_reply":"2025-06-12T20:01:42.707104Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nTrain&Inf Pipeline\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"### 》》》**Importing the necessary Libraries**","metadata":{}},{"cell_type":"code","source":"# Two‑Branch Human‑Activity‑Recognition Pipeline (IMU + Thermopile/TOF  + SE‑CNN + BiLSTM + Attention)\nimport 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 train_test_split\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, GRU, GaussianNoise\n)\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras import backend as K\nimport tensorflow as tf\nimport polars as pl\nfrom sklearn.metrics import classification_report\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T17:38:48.419052Z","iopub.execute_input":"2025-07-07T17:38:48.419231Z","iopub.status.idle":"2025-07-07T17:38:48.435885Z","shell.execute_reply.started":"2025-07-07T17:38:48.419217Z","shell.execute_reply":"2025-07-07T17:38:48.435229Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 》》》**Fix Seed**","metadata":{}},{"cell_type":"code","source":"import random\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    tf.experimental.numpy.random.seed(seed)\n    os.environ['TF_CUDNN_DETERMINISTIC'] = '1'\n    os.environ['TF_DETERMINISTIC_OPS'] = '1'\nseed_everything(seed=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T17:38:48.436665Z","iopub.execute_input":"2025-07-07T17:38:48.437225Z","iopub.status.idle":"2025-07-07T17:38:48.452283Z","shell.execute_reply.started":"2025-07-07T17:38:48.437203Z","shell.execute_reply":"2025-07-07T17:38:48.451771Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 》》》**Configuration**","metadata":{}},{"cell_type":"code","source":"#(Competition metric will only be imported when TRAINing)\nTRAIN = True                     # ← set to True when you want to train\nRAW_DIR = Path(\"/kaggle/input/cmi-detect-behavior-with-sensor-data\")\nPRETRAINED_DIR = Path(\"/kaggle/input/cmi25-imu-thmtof-tf-bilstm-gru-attentionlb-xx\")  # used when TRAIN=False\nEXPORT_DIR = Path(\"./\")                                    # artefacts will be saved here\nBATCH_SIZE = 64\nPAD_PERCENTILE = 99\nLR_INIT = 1e-4\nWD = 3e-3\nMIXUP_ALPHA = 0.5\nEPOCHS = 500\nPATIENCE = 10\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T17:38:48.454214Z","iopub.execute_input":"2025-07-07T17:38:48.454398Z","iopub.status.idle":"2025-07-07T17:38:48.469243Z","shell.execute_reply.started":"2025-07-07T17:38:48.454384Z","shell.execute_reply":"2025-07-07T17:38:48.468727Z"}},"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=4):  # Reduced from 8 to 4 for more capacity\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# Residual CNN Block with SE - Enhanced capacity\ndef residual_se_cnn_block(x, filters, kernel_size, pool_size=2, drop=0.015, wd=5e-5):  # Reduced dropout and weight decay\n    shortcut = x\n    # Increased to 3 conv layers for more capacity\n    for i in range(3):\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        # Add dilation for the middle layer to increase receptive field\n        if i == 1:\n            x = Conv1D(filters, kernel_size, padding='same', dilation_rate=2, \n                      use_bias=False, kernel_regularizer=l2(wd))(x)\n            x = BatchNormalization()(x)\n            x = Activation('relu')(x)\n    \n    x = se_block(x)\n    \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    \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    # Multi-head attention-like mechanism\n    attention_dim = inputs.shape[-1] // 2\n    \n    # Create multiple attention heads\n    heads = []\n    for _ in range(4):  # 4 attention heads\n        score = Dense(attention_dim, activation='tanh')(inputs)\n        score = Dense(1, activation='tanh')(score)\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        heads.append(context)\n    \n    # Combine attention heads\n    if len(heads) > 1:\n        combined = Concatenate()(heads)\n    else:\n        combined = heads[0]\n    \n    return combined","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T17:38:48.469823Z","iopub.execute_input":"2025-07-07T17:38:48.470052Z","iopub.status.idle":"2025-07-07T17:38:48.494112Z","shell.execute_reply.started":"2025-07-07T17:38:48.470031Z","shell.execute_reply":"2025-07-07T17:38:48.493623Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 》》》**Data Helpers**","metadata":{}},{"cell_type":"code","source":"# Normalizes and cleans the time series sequence with enhanced preprocessing\ndef preprocess_sequence(df_seq: pd.DataFrame, feature_cols: list[str], scaler: StandardScaler):\n    mat = df_seq[feature_cols].ffill().bfill().fillna(0).values\n    \n    # Add noise injection for data augmentation during preprocessing\n    if np.random.random() > 0.7:  # 30% chance of adding noise\n        noise = np.random.normal(0, 0.01, mat.shape)\n        mat = mat + noise\n    \n    return scaler.transform(mat).astype('float32')\n\n# # Enhanced MixUp with more aggressive augmentation\n# class MixupGenerator(Sequence):\n#     def __init__(self, X, y, batch_size, alpha=0.4):  # Increased alpha for stronger mixup\n#         self.X, self.y = X, y\n#         self.batch = batch_size\n#         self.alpha = alpha\n#         self.indices = np.arange(len(X))\n        \n#     def __len__(self):\n#         return int(np.ceil(len(self.X) / self.batch))\n    \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        \n#         # Enhanced mixup with multiple augmentations\n#         lam = np.random.beta(self.alpha, self.alpha)\n#         perm = np.random.permutation(len(Xb))\n        \n#         # Basic mixup\n#         X_mix = lam * Xb + (1-lam) * Xb[perm]\n#         y_mix = lam * yb + (1-lam) * yb[perm]\n        \n#         # Additional augmentations\n#         if np.random.random() > 0.5:\n#             # Time shifting\n#             shift = np.random.randint(-5, 6)\n#             X_mix = np.roll(X_mix, shift, axis=1)\n        \n#         if np.random.random() > 0.6:\n#             # Scaling augmentation\n#             scale = np.random.uniform(0.9, 1.1)\n#             X_mix = X_mix * scale\n            \n#         return X_mix, y_mix\n    \n#     def on_epoch_end(self):\n#         np.random.shuffle(self.indices)\nclass MixupGenerator(Sequence):\n    def __init__(self, X, y, batch_size, alpha=0.4):\n        self.X, self.y = X, y\n        self.batch = batch_size\n        self.alpha = alpha\n        self.indices = np.arange(len(X))\n        \n    def __len__(self):\n        return int(np.ceil(len(self.X) / self.batch))\n    \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        \n        lam = np.random.beta(self.alpha, self.alpha)\n        perm = np.random.permutation(len(Xb))\n        \n        X_mix = lam * Xb + (1-lam) * Xb[perm]\n        y_mix = lam * yb + (1-lam) * yb[perm]\n\n        # Time shift\n        if np.random.random() > 0.2:\n            shift = np.random.randint(-5, 6)\n            X_mix = np.roll(X_mix, shift, axis=1)\n        \n        # Scaling\n        if np.random.random() > 0.2:\n            scale = np.random.uniform(0.9, 1.1)\n            X_mix *= scale\n\n        # Cutout (mask a random segment)\n        if np.random.random() > 0.2:\n            cutout_len = np.random.randint(5, 15)\n            start = np.random.randint(0, X_mix.shape[1] - cutout_len)\n            X_mix[:, start:start+cutout_len, :] = 0\n\n        # Jitter (random noise)\n        if np.random.random() > 0.2:\n            jitter = np.random.normal(0, 0.005, X_mix.shape)\n            X_mix += jitter\n\n        # Random feature dropout\n        if np.random.random() > 0.2:\n            drop_idx = np.random.randint(0, X_mix.shape[2])\n            X_mix[:, :, drop_idx] = 0\n\n        return X_mix, y_mix\n\n    def on_epoch_end(self):\n        np.random.shuffle(self.indices)\n\n\n# class MixupGenerator(Sequence):\n#     def __init__(self, X, y, batch_size, alpha=0.4):\n#         self.X, self.y = X, y\n#         self.batch = batch_size\n#         self.alpha = alpha\n#         self.indices = np.arange(len(X))\n        \n#     def __len__(self):\n#         return int(np.ceil(len(self.X) / self.batch))\n    \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        \n#         lam = np.random.beta(self.alpha, self.alpha)\n#         perm = np.random.permutation(len(Xb))\n        \n#         X_mix = lam * Xb + (1-lam) * Xb[perm]\n#         y_mix = lam * yb + (1-lam) * yb[perm]\n\n#         # Time shift\n#         if np.random.random() > 0.7:\n#             shift = np.random.randint(-5, 6)\n#             X_mix = np.roll(X_mix, shift, axis=1)\n        \n#         # Scaling\n#         if np.random.random() > 0.7:\n#             scale = np.random.uniform(0.9, 1.1)\n#             X_mix *= scale\n\n#         # Cutout (mask a random segment)\n#         if np.random.random() > 0.7:\n#             cutout_len = np.random.randint(5, 15)\n#             start = np.random.randint(0, X_mix.shape[1] - cutout_len)\n#             X_mix[:, start:start+cutout_len, :] = 0\n\n#         # Jitter (random noise)\n#         if np.random.random() > 0.7:\n#             jitter = np.random.normal(0, 0.005, X_mix.shape)\n#             X_mix += jitter\n\n#         # Random feature dropout\n#         if np.random.random() > 0.7:\n#             drop_idx = np.random.randint(0, X_mix.shape[2])\n#             X_mix[:, :, drop_idx] = 0\n\n#         return X_mix, y_mix\n\n#     def on_epoch_end(self):\n#         np.random.shuffle(self.indices)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T17:38:48.496864Z","iopub.execute_input":"2025-07-07T17:38:48.497517Z","iopub.status.idle":"2025-07-07T17:38:48.518206Z","shell.execute_reply.started":"2025-07-07T17:38:48.497494Z","shell.execute_reply":"2025-07-07T17:38:48.517732Z"}},"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=5e-5):  # Reduced weight decay\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    # Enhanced IMU deep branch with more capacity\n    x1 = residual_se_cnn_block(imu, 96, 3, drop=0.1, wd=wd)  # Increased filters\n    x1 = residual_se_cnn_block(x1, 192, 5, drop=0.1, wd=wd)  # Increased filters\n    x1 = residual_se_cnn_block(x1, 256, 7, drop=0.15, wd=wd)  # Added third block\n    \n    # Enhanced TOF/Thermal branch with matching pooling operations\n    x2 = Conv1D(96, 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.1)(x2)\n    \n    x2 = Conv1D(192, 5, 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.1)(x2)\n    \n    # Additional conv layer with matching pooling to IMU branch\n    x2 = Conv1D(256, 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.15)(x2)  # Added third pooling to match IMU branch\n    \n    merged = Concatenate()([x1, x2])\n    \n    # Enhanced recurrent layers with more units\n    xa = Bidirectional(LSTM(192, return_sequences=True, kernel_regularizer=l2(wd)))(merged)\n    xb = Bidirectional(GRU(192, return_sequences=True, kernel_regularizer=l2(wd)))(merged)\n    \n    # Enhanced dense processing branch\n    xc = GaussianNoise(0.05)(merged)  # Reduced noise\n    xc = Dense(64, activation='elu')(xc)  # Increased units\n    xc = Dense(32, activation='elu')(xc)  # Added second dense layer\n    \n    x = Concatenate()([xa, xb, xc])\n    x = Dropout(0.02)(x)  # Reduced dropout\n    x = attention_layer(x)\n    \n    # Enhanced final dense layers with more capacity\n    dense_configs = [(512, 0.3), (256, 0.2), (128, 0.1)]  # Increased units, reduced dropout\n    for units, drop in dense_configs:\n        x = Dense(units, use_bias=False, kernel_regularizer=l2(wd))(x)\n        x = BatchNormalization()(x)\n        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-07-07T17:38:48.519314Z","iopub.execute_input":"2025-07-07T17:38:48.519506Z","iopub.status.idle":"2025-07-07T17:38:48.540947Z","shell.execute_reply.started":"2025-07-07T17:38:48.519492Z","shell.execute_reply":"2025-07-07T17:38:48.540423Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### **》》》ModelDef**\n","metadata":{}},{"cell_type":"code","source":"# from tensorflow.keras.utils import plot_model\n\n# tmp_model = build_two_branch_model(127,7,325,18)\n# plot_model(\n#     tmp_model, \n#     to_file='CMI-Model.png', \n#     show_shapes=True,\n#     show_layer_names=True\n# )","metadata":{"trusted":true,"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-06-17T18:23:39.873599Z","iopub.execute_input":"2025-06-17T18:23:39.874454Z","iopub.status.idle":"2025-06-17T18:23:39.877786Z","shell.execute_reply.started":"2025-06-17T18:23:39.874423Z","shell.execute_reply":"2025-06-17T18:23:39.876984Z"}},"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    X = pad_sequences(X_list, maxlen=pad_len, padding='post', truncating='post', dtype='float32')\n    y = to_categorical(y_list, num_classes=len(le.classes_))\n\n    # split\n    # X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y_list)\n    X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.1, random_state=82, stratify=y_list)\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_INIT),\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    es = EarlyStopping(patience=PATIENCE, restore_best_weights=True, verbose=1)\n\n    checkpoint_path = \"checkpoints/best_model.h5\"\n\n    cb = ModelCheckpoint(\n        checkpoint_path,\n        monitor='val_loss',\n        save_best_only=True,\n        save_weights_only=False, \n        mode='min', \n        verbose=1\n    )\n    \n    from tensorflow.keras.callbacks import ReduceLROnPlateau\n\n    reduce_lr_cb = ReduceLROnPlateau(\n        monitor='val_loss',\n        factor=0.5,\n        patience=3,\n        min_lr=1e-6,\n        verbose=1\n    )\n\n    model.fit(train_gen, epochs=EPOCHS, validation_data=(X_val, y_val),\n              class_weight=class_weight, callbacks=[cb, es ,reduce_lr_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    print(\"\\n📊 Classification Report:\")\n    print(classification_report(true, preds, target_names=le.classes_))\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        'AddPositionalEncoding': AddPositionalEncoding,\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-07-07T17:38:56.160310Z","iopub.execute_input":"2025-07-07T17:38:56.161000Z","iopub.status.idle":"2025-07-07T18:48:25.214911Z","shell.execute_reply.started":"2025-07-07T17:38:56.160977Z","shell.execute_reply":"2025-07-07T18:48:25.214103Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.metrics import CategoricalAccuracy, TopKCategoricalAccuracy, AUC\n\nmodel.compile(\n    optimizer=Adam(learning_rate=LR_INIT, beta_1=0.9, beta_2=0.999, epsilon=1e-7),  # Use kwargs for clarity\n    loss=CategoricalCrossentropy(label_smoothing=0.5),\n    metrics=[\n        CategoricalAccuracy(name='accuracy'),\n    ]\n)\n\n\n\ntrain_gen = MixupGenerator(X_tr, y_tr, batch_size=BATCH_SIZE, alpha=MIXUP_ALPHA)\ncb = EarlyStopping(patience=PATIENCE, restore_best_weights=True, verbose=1)\n\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\nreduce_lr_cb = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.5,\n    patience=3,\n    min_lr=1e-8,\n    verbose=1\n)\n\nmodel.fit(train_gen, epochs=EPOCHS, validation_data=(X_val, y_val),\n          class_weight=class_weight, callbacks=[cb,es, reduce_lr_cb], verbose=1)\n\nmodel.save(EXPORT_DIR / \"gesture_two_branch_mixup.h5\")\nprint(\"✔ Training done – artefacts saved in\", EXPORT_DIR)\n\n# quick metric\nfrom cmi_2025_metric_copy_for_import import CompetitionMetric\npreds = model.predict(X_val).argmax(1)\ntrue  = y_val.argmax(1)\nh_f1 = CompetitionMetric().calculate_hierarchical_f1(\n    pd.DataFrame({'gesture': le.classes_[true]}),\n    pd.DataFrame({'gesture': le.classes_[preds]}))\nprint(\"Hold‑out H‑F1 =\", round(h_f1, 4))\nprint(\"\\n📊 Classification Report:\")\nprint(classification_report(true, preds, target_names=le.classes_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T18:48:25.216204Z","iopub.execute_input":"2025-07-07T18:48:25.216421Z","iopub.status.idle":"2025-07-07T18:59:08.005489Z","shell.execute_reply.started":"2025-07-07T18:48:25.216404Z","shell.execute_reply":"2025-07-07T18:59:08.004092Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# quick metric\nfrom cmi_2025_metric_copy_for_import import CompetitionMetric\nfrom tensorflow.keras.models import load_model\n\ncustom_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\nmodel = load_model('checkpoints/best_model.h5',\n                       compile=False, custom_objects=custom_objs)\n\npreds = model.predict(X_val).argmax(1)\ntrue  = y_val.argmax(1)\n\nh_f1 = CompetitionMetric().calculate_hierarchical_f1(\n    pd.DataFrame({'gesture': le.classes_[true]}),\n    pd.DataFrame({'gesture': le.classes_[preds]}))\nprint(\"Hold‑out H‑F1 =\", round(h_f1, 4))\n\n# ⬇️ Classification report\nfrom sklearn.metrics import classification_report\nprint(\"\\n📊 Classification Report:\")\nprint(classification_report(true, preds, target_names=le.classes_))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:27:57.490652Z","iopub.execute_input":"2025-07-07T01:27:57.490962Z","iopub.status.idle":"2025-07-07T01:28:01.838045Z","shell.execute_reply.started":"2025-07-07T01:27:57.490931Z","shell.execute_reply":"2025-07-07T01:28:01.837397Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 》》》**Predict**","metadata":{}},{"cell_type":"code","source":"# make sure gesture_classes exists in both modes\nif TRAIN:\n    gesture_classes = le.classes_\n\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])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T18:59:12.904539Z","iopub.execute_input":"2025-07-07T18:59:12.905403Z","iopub.status.idle":"2025-07-07T18:59:12.910315Z","shell.execute_reply.started":"2025-07-07T18:59:12.905372Z","shell.execute_reply":"2025-07-07T18:59:12.909617Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 》》》**Submit Inference server**","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-07-07T18:59:15.108820Z","iopub.execute_input":"2025-07-07T18:59:15.109285Z","iopub.status.idle":"2025-07-07T18:59:19.052239Z","shell.execute_reply.started":"2025-07-07T18:59:15.109262Z","shell.execute_reply":"2025-07-07T18:59:19.051456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}