{"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":"import os\nimport random\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import OneHotEncoder\n\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Dense, Concatenate","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-01T12:10:13.089347Z","iopub.execute_input":"2023-04-01T12:10:13.090110Z","iopub.status.idle":"2023-04-01T12:10:23.038433Z","shell.execute_reply.started":"2023-04-01T12:10:13.090066Z","shell.execute_reply":"2023-04-01T12:10:23.036892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reduce Memory Usage\n# reference : https://www.kaggle.com/code/arjanso/reducing-dataframe-memory-size-by-65 @ARJANGROEN\n\ndef reduce_memory_usage(df):\n    \n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype.name\n        if ((col_type != 'datetime64[ns]') & (col_type != 'category')):\n            if (col_type != 'object'):\n                c_min = df[col].min()\n                c_max = df[col].max()\n\n                if str(col_type)[:3] == 'int':\n                    if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                        df[col] = df[col].astype(np.int8)\n                    elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                        df[col] = df[col].astype(np.int16)\n                    elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                        df[col] = df[col].astype(np.int32)\n                    elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                        df[col] = df[col].astype(np.int64)\n\n                else:\n                    if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                        df[col] = df[col].astype(np.float16)\n                    elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                        df[col] = df[col].astype(np.float32)\n                    else:\n                        pass\n            else:\n                df[col] = df[col].astype('category')\n    mem_usg = df.memory_usage().sum() / 1024**2 \n    print(\"Memory usage became: \",mem_usg,\" MB\")\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:10:23.040883Z","iopub.execute_input":"2023-04-01T12:10:23.041633Z","iopub.status.idle":"2023-04-01T12:10:23.058948Z","shell.execute_reply.started":"2023-04-01T12:10:23.041591Z","shell.execute_reply":"2023-04-01T12:10:23.057665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reference: https://www.kaggle.com/code/ghrangel/read-data-and-merge\n#Defog\nDATA_ROOT_DEFOG = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/'\ndefog = pd.DataFrame()\nfor root, dirs, files in os.walk(DATA_ROOT_DEFOG):\n    for name in files:       \n        f = os.path.join(root, name)\n        df_list= pd.read_csv(f)\n        words = name.split('.')[0]\n        df_list['file']= name.split('.')[0]\n        defog = pd.concat([defog, df_list], axis=0)\n\n#Reduce Memory        \ndefog = reduce_memory_usage(defog)\ngc.collect()\n\n#Using Valid Data only\ndefog = defog[(defog['Task']==1)&(defog['Valid']==1)]\n\n#Combine With MetaData\ndefog_metadata = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv\")\ndefog_m= defog_metadata.merge(defog, how = 'inner', left_on = 'Id', right_on = 'file')\ndefog_m.drop(['file','Valid','Task'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:10:23.060730Z","iopub.execute_input":"2023-04-01T12:10:23.061181Z","iopub.status.idle":"2023-04-01T12:11:20.411715Z","shell.execute_reply.started":"2023-04-01T12:10:23.061138Z","shell.execute_reply":"2023-04-01T12:11:20.409565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Tdcsfog \nDATA_ROOT_TDCSFOG = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog/'\ntdcsfog = pd.DataFrame()\nfor root, dirs, files in os.walk(DATA_ROOT_TDCSFOG):\n    for name in files:       \n        f = os.path.join(root, name)\n        df_list= pd.read_csv(f)\n        words = name.split('.')[0]\n        df_list['file']= name.split('.')[0]\n        tdcsfog = pd.concat([tdcsfog, df_list], axis=0)\n\ntdcsfog = reduce_memory_usage(tdcsfog)\ngc.collect()\n        \n#Combine with metadata        \ntdcsfog_metadata = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv\")\ntdcsfog_m= tdcsfog_metadata.merge(tdcsfog, how = 'inner', left_on = 'Id', right_on = 'file')\ntdcsfog_m.drop(['file'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:11:20.416504Z","iopub.execute_input":"2023-04-01T12:11:20.419435Z","iopub.status.idle":"2023-04-01T12:13:42.579680Z","shell.execute_reply.started":"2023-04-01T12:11:20.419344Z","shell.execute_reply":"2023-04-01T12:13:42.578532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.optimizers import Adam\n\"\"\"\n# Define input layer\ninputs = Input(shape=(3,))\n\n# Define shared hidden layers\nx = Dense(64, activation='relu')(inputs)\nx = Dense(32, activation='relu')(x)\n\n# Define task-specific output layers\nout1 = Dense(1, activation='sigmoid', name='output1')(x)\nout2 = Dense(1, activation='sigmoid', name='output2')(x)\nout3 = Dense(1, activation='sigmoid', name='output3')(x)\n\n# Define the model with the input and output layers\nmodel = Model(inputs=inputs, outputs=[out1, out2, out3])\n\n# Compile the model with binary crossentropy loss and Adam optimizer\nmodel.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])\n\n# Concatenate the two datasets vertically\ndf = pd.concat([defog_m, tdcsfog_m], axis=0)\n\n# Split the dataset into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(df[['AccV','AccML','AccAP']],\n                                                    df[['StartHesitation','Turn','Walking']],\n                                                    test_size=0.2,\n                                                    random_state=42)\n\n\nimport time\nstart = time.time()\n# Fit the model with the training data\nhistory = model.fit(X_train, [y_train['StartHesitation'], y_train['Turn'], y_train['Walking']],\n                    validation_data=(X_test, [y_test['StartHesitation'], y_test['Turn'], y_test['Walking']]),\n                    epochs=10,\n                    batch_size=32,\n                    verbose=1)\nend = time.time()\nprint(end - start)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:13:42.581239Z","iopub.execute_input":"2023-04-01T12:13:42.581681Z","iopub.status.idle":"2023-04-01T12:13:47.214205Z","shell.execute_reply.started":"2023-04-01T12:13:42.581647Z","shell.execute_reply":"2023-04-01T12:13:47.212870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport glob\np = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/'\ntest = glob.glob(p+'test/**/**')\n\nk = 0\nfor f in test:\n    test_defog_path = f\n    test_defog = pd.read_csv(test_defog_path)\n    name = os.path.basename(test_defog_path)\n    id_value = name.split('.')[0]\n    test_defog['Id_value'] = id_value\n    test_defog['Id'] = test_defog['Id_value'].astype(str) + '_' + test_defog['Time'].astype(str)\n    test_defog = test_defog[['Id','AccV','AccML','AccAP']]\n    test_defog.set_index('Id',inplace=True)\n    \n    if(k==1):\n        print('k = 1')\n        test_tdcsfog_pred = model.predict(test_defog)\n        d2 = {'Id':test_defog.index,'StartHesitation':test_tdcsfog_pred[0].flatten(),'Turn':test_tdcsfog_pred[1].flatten(),'Walking':test_tdcsfog_pred[2].flatten()}\n        tdcsfog_pred = pd.DataFrame(d2)\n        final_pred = tdcsfog_pred.append(defog_pred)\n        continue\n        \n    test_defog_pred = model.predict(test_defog)\n    d1 = {'Id':test_defog.index,'StartHesitation':test_defog_pred[0].flatten(),'Turn':test_defog_pred[1].flatten(),'Walking':test_defog_pred[2].flatten()}\n    defog_pred = pd.DataFrame(d1)\n    k = k + 1\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T06:41:22.227000Z","iopub.execute_input":"2023-04-01T06:41:22.227458Z","iopub.status.idle":"2023-04-01T06:41:49.076944Z","shell.execute_reply.started":"2023-04-01T06:41:22.227416Z","shell.execute_reply":"2023-04-01T06:41:49.075517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n#final_pred = tdcsfog_pred.append(defog_pred)\ncols = ['StartHesitation', 'Turn', 'Walking']\nmax_col = final_pred[cols].idxmax(axis=1)\nbinary_matrix = (max_col.values[:, None] == cols).astype(int)\nvalues_with_precision = (final_pred[cols].values * binary_matrix)\n\n# Update the dataframe with the new values\nfinal_pred[cols] = values_with_precision\nfinal_pred = final_pred.reset_index(drop=True)\n#final_pred.to_csv('submission4.csv',index = False)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T06:41:49.079953Z","iopub.execute_input":"2023-04-01T06:41:49.080652Z","iopub.status.idle":"2023-04-01T06:41:49.485123Z","shell.execute_reply.started":"2023-04-01T06:41:49.080598Z","shell.execute_reply":"2023-04-01T06:41:49.483823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nsub = pd.read_csv(p+'sample_submission.csv')\nsub['t'] = 0\n#submission = pd.concat(final_pred)\nsubmission = pd.merge(sub[['Id','t']], final_pred, how='left', on='Id').fillna(0.0)\nsubmission[['Id','StartHesitation', 'Turn' , 'Walking']].to_csv('submission.csv', index=False)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T06:42:00.399738Z","iopub.execute_input":"2023-04-01T06:42:00.400199Z","iopub.status.idle":"2023-04-01T06:42:01.875976Z","shell.execute_reply.started":"2023-04-01T06:42:00.400160Z","shell.execute_reply":"2023-04-01T06:42:01.874687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import average_precision_score\n\n# Define the number of folds for cross-validation\nnum_folds = 5\n\n# Load the concatenated dataset\ndf = pd.concat([defog_m, tdcsfog_m], axis=0)\n\n# Split the data into inputs and targets\nX = df[['AccV','AccML','AccAP']].values\ny = df[['StartHesitation','Turn','Walking']].values\n\n# Define the cross-validation splitter\nkf = KFold(n_splits=num_folds, shuffle=True)\n\n# Initialize the list to store the average precision scores for each target\nap_scores = [[] for i in range(y.shape[1])]\n\n# Loop over the folds\nfor fold, (train_index, test_index) in enumerate(kf.split(X)):\n    print(\"Training and evaluating fold {}/{}...\".format(fold+1, num_folds))\n\n    # Split the data into training and testing sets\n    X_train, X_test = X[train_index], X[test_index]\n    y_train, y_test = y[train_index], y[test_index]\n\n    # Fit the model with the training data\n    history = model.fit(X_train, [y_train[:,0], y_train[:,1], y_train[:,2]],\n                        epochs=10,\n                        batch_size=128,\n                        verbose=1)\n\n    # Make predictions on the testing data\n    y_pred = model.predict(X_test)\n\n    # Calculate the average precision score for each target\n    for i in range(y.shape[1]):\n        ap = average_precision_score(y_test[:,i], y_pred[i])\n        ap_scores[i].append(ap)\n\n# Calculate the mean average precision score for each target\nmap_scores = [np.mean(ap) for ap in ap_scores]\n\n# Calculate the overall mean average precision score\noverall_map_score = np.mean(map_scores)\n\n# Print the mean average precision score for each target and the overall score\nprint(\"Start Hesitation MAP: {:.4f}\".format(map_scores[0]))\nprint(\"Turn MAP: {:.4f}\".format(map_scores[1]))\nprint(\"Walking MAP: {:.4f}\".format(map_scores[2]))\nprint(\"Overall MAP: {:.4f}\".format(overall_map_score))\n\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.optimizers import Adam\n\n# Define input layer\ninputs = Input(shape=(3,))\n\n# Define shared hidden layers\nx = Dense(64, activation='relu')(inputs)\nx = Dense(32, activation='relu')(x)\n\n# Define task-specific output layers\nout1 = Dense(1, activation='relu', name='output1')(x)\nout2 = Dense(1, activation='relu', name='output2')(x)\nout3 = Dense(1, activation='relu', name='output3')(x)\n\n# Define the model with the input and output layers\nmodel = Model(inputs=inputs, outputs=[out1, out2, out3])\n\n# Compile the model with binary crossentropy loss and Adam optimizer\nmodel.compile(loss='mse', optimizer=Adam(lr=0.001), metrics='RootMeanSquaredError')\n\n# Concatenate the two datasets vertically\ndf = pd.concat([defog_m, tdcsfog_m], axis=0).fillna(0.0)\n\n# Split the dataset into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(df[['AccV','AccML','AccAP']],\n                                                    df[['StartHesitation','Turn','Walking']],\n                                                    test_size=0.2,\n                                                    random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:13:47.215850Z","iopub.execute_input":"2023-04-01T12:13:47.216221Z","iopub.status.idle":"2023-04-01T12:13:55.507978Z","shell.execute_reply.started":"2023-04-01T12:13:47.216187Z","shell.execute_reply":"2023-04-01T12:13:55.506896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport time\nstart = time.time()\n# Fit the model with the training data\nhistory = model.fit(X_train, [y_train['StartHesitation'], y_train['Turn'], y_train['Walking']],\n                    validation_data=(X_test, [y_test['StartHesitation'], y_test['Turn'], y_test['Walking']]),\n                    epochs=100,\n                    batch_size=256,\n                    verbose=1)\nend = time.time()\nprint(end - start)\n\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n#model.save('Reg_100_256.h5')\nmodel = load_model('/kaggle/input/reg100265/Reg_100_256.h5')","metadata":{"execution":{"iopub.status.busy":"2023-04-01T15:11:56.071827Z","iopub.execute_input":"2023-04-01T15:11:56.072308Z","iopub.status.idle":"2023-04-01T15:11:56.224169Z","shell.execute_reply.started":"2023-04-01T15:11:56.072265Z","shell.execute_reply":"2023-04-01T15:11:56.222826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport matplotlib.pyplot as plt\n\n# Plot training & validation loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper right')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-04-01T15:05:43.951676Z","iopub.execute_input":"2023-04-01T15:05:43.953090Z","iopub.status.idle":"2023-04-01T15:05:44.183734Z","shell.execute_reply.started":"2023-04-01T15:05:43.953035Z","shell.execute_reply":"2023-04-01T15:05:44.182263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\np = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/'\ntest = glob.glob(p+'test/**/**')\n\nk = 0\nfor f in test:\n    test_defog_path = f\n    test_defog = pd.read_csv(test_defog_path)\n    name = os.path.basename(test_defog_path)\n    id_value = name.split('.')[0]\n    test_defog['Id_value'] = id_value\n    test_defog['Id'] = test_defog['Id_value'].astype(str) + '_' + test_defog['Time'].astype(str)\n    test_defog = test_defog[['Id','AccV','AccML','AccAP']]\n    test_defog.set_index('Id',inplace=True)\n    \n    if(k==1):\n        print('k = 1')\n        test_tdcsfog_pred = model.predict(test_defog)\n        d2 = {'Id':test_defog.index,'StartHesitation':test_tdcsfog_pred[0].flatten(),'Turn':test_tdcsfog_pred[1].flatten(),'Walking':test_tdcsfog_pred[2].flatten()}\n        tdcsfog_pred = pd.DataFrame(d2)\n        final_pred = tdcsfog_pred.append(defog_pred)\n        continue\n        \n    test_defog_pred = model.predict(test_defog)\n    d1 = {'Id':test_defog.index,'StartHesitation':test_defog_pred[0].flatten(),'Turn':test_defog_pred[1].flatten(),'Walking':test_defog_pred[2].flatten()}\n    defog_pred = pd.DataFrame(d1)\n    k = k + 1","metadata":{"execution":{"iopub.status.busy":"2023-04-01T15:06:44.912359Z","iopub.execute_input":"2023-04-01T15:06:44.912834Z","iopub.status.idle":"2023-04-01T15:07:27.582065Z","shell.execute_reply.started":"2023-04-01T15:06:44.912791Z","shell.execute_reply":"2023-04-01T15:07:27.580797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_pred = final_pred.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T15:07:37.747827Z","iopub.execute_input":"2023-04-01T15:07:37.748236Z","iopub.status.idle":"2023-04-01T15:07:37.761227Z","shell.execute_reply.started":"2023-04-01T15:07:37.748202Z","shell.execute_reply":"2023-04-01T15:07:37.759722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(p+'sample_submission.csv')\nsub['t'] = 0\n#submission = pd.concat(final_pred)\nsubmission = pd.merge(sub[['Id','t']], final_pred, how='left', on='Id').fillna(0.0)\nsubmission[['Id','StartHesitation', 'Turn' , 'Walking']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T15:07:38.023154Z","iopub.execute_input":"2023-04-01T15:07:38.023599Z","iopub.status.idle":"2023-04-01T15:07:39.544384Z","shell.execute_reply.started":"2023-04-01T15:07:38.023560Z","shell.execute_reply":"2023-04-01T15:07:39.543331Z"},"trusted":true},"execution_count":null,"outputs":[]}]}