{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":41880,"databundleVersionId":5677426,"sourceType":"competition"},{"sourceId":10858021,"sourceType":"datasetVersion","datasetId":6744622}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.layers import Input, Conv1D, BatchNormalization, ReLU, LSTM, Bidirectional, Dense, Dropout, Attention\nfrom tensorflow.keras.optimizers import Adam\nimport pandas as pd\nimport os","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set Kaggle dataset paths\nDATASET_PATH = '/kaggle/input/multimodal-dataset-of-freezing-of-gait'  \nMODEL_PATH = '/kaggle/working/fog_detection_model.h5'\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load and preprocess dataset\ndef load_data(file_name):\n    data = pd.read_csv(os.path.join(DATASET_PATH, file_name))\n    \n    # Assuming columns: ['acc_x', 'acc_y', 'acc_z', 'gyro_x', 'gyro_y', 'gyro_z', 'label']\n    X = data[['acc_x', 'acc_y', 'acc_z', 'gyro_x', 'gyro_y', 'gyro_z']].values\n    y = data['label'].values\n    \n    # Normalize features\n    X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)\n    \n    # Reshape for LSTM (samples, timesteps, features)\n    X = X.reshape((len(X) // 50, 50, 6))  # Assuming window size of 50\n    y = y.reshape((len(y) // 50, 50))[:, 0]  # Taking one label per window\n    \n    return X, y","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model Architecture\ndef build_model(input_shape):\n    inputs = Input(shape=input_shape)\n    \n    # CNN Feature Extractor\n    x = Conv1D(filters=64, kernel_size=3, padding='same')(inputs)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    \n    # BiLSTM for Temporal Modeling\n    x = Bidirectional(LSTM(128, return_sequences=True))(x)\n    x = Bidirectional(LSTM(64, return_sequences=True))(x)\n    \n    # Attention Mechanism\n    attention = Attention()([x, x])\n    \n    # Classification Layer\n    x = Dense(64, activation='relu')(attention)\n    x = Dropout(0.5)(x)\n    x = Dense(1, activation='sigmoid')(x)\n    \n    model = Model(inputs, x)\n    model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Data\ntrain_file = 'sensor_data.csv'  \nX, y = load_data(train_file)\n\n# Build and Train Model\nmodel = build_model((50, 6))\nmodel.summary()\nmodel.fit(X, y, epochs=125, batch_size=32, validation_split=0.2)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save Model\nmodel.save(MODEL_PATH)\n\n# Load and Test on Unseen Data\ndef test_model(test_file):\n    X_test, y_test = load_data(test_file)\n    model = load_model(MODEL_PATH)\n    loss, accuracy = model.evaluate(X_test, y_test)\n    print(f'Test Accuracy: {accuracy * 100:.2f}%')\n    predictions = model.predict(X_test)\n    return predictions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Example Usage\ntest_file = /kaggle/input/multimodal-dataset-of-freezing-of-gait/Raw/012/Arm.csv \npredictions = test_model(test_file)\nprint(predictions)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}