{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":91048,"databundleVersionId":10710872,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Load the training data (You might have to change the file path based on how you're working)\ntrain_df = pd.read_csv('/kaggle/input/beginners-hypothesis-25/BH25/Training_Data/train.csv')\n\nprint(train_df.head())\n\n# For this notebook, we'll only train for 1000 video samples\n\ntrain_df = train_df[:3000]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:19:06.706658Z","iopub.execute_input":"2025-01-17T05:19:06.707035Z","iopub.status.idle":"2025-01-17T05:19:06.728717Z","shell.execute_reply.started":"2025-01-17T05:19:06.707003Z","shell.execute_reply":"2025-01-17T05:19:06.728006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2 \n\ndef process_video(video_path):\n    cap = cv2.VideoCapture(video_path)\n    frames = []\n    while True:\n        ret, frame = cap.read()\n        if not ret:\n            break\n        frame = cv2.resize(frame, (64, 64))\n        frames.append(frame)\n    cap.release()\n    frames = np.array(frames)  # Shape: (20, 64, 64, 3)\n    return frames.flatten()  # Flatten to 1D array\n    \nvideo_features = []\nfor idx, row in train_df.iterrows():\n    video_path = f\"/kaggle/input/beginners-hypothesis-25/BH25/Training_Data/Train_Videos/{row['video_id']}.mp4\"\n    video_features.append(process_video(video_path))\n\nvideo_features = np.array(video_features)  # Shape: (num_samples, 20*64*64*3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:19:06.729865Z","iopub.execute_input":"2025-01-17T05:19:06.730189Z","iopub.status.idle":"2025-01-17T05:19:23.402700Z","shell.execute_reply.started":"2025-01-17T05:19:06.730158Z","shell.execute_reply":"2025-01-17T05:19:23.401980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Encode categorical attributes using the Label Encoder\ndef encode_categorical_columns(df, columns):\n    label_encoders = {}\n    for col in columns:\n        le = LabelEncoder()\n        df[col] = le.fit_transform(df[col])\n        label_encoders[col] = le\n    return df, label_encoders\n\ncategorical_columns = ['element', 'motion', 'power']\ntrain_df, label_encoders = encode_categorical_columns(train_df, categorical_columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:19:23.404372Z","iopub.execute_input":"2025-01-17T05:19:23.404718Z","iopub.status.idle":"2025-01-17T05:19:23.412310Z","shell.execute_reply.started":"2025-01-17T05:19:23.404683Z","shell.execute_reply":"2025-01-17T05:19:23.411526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\ny_speed = train_df[['speed']]\ny_summary = train_df['video_summary'].apply(lambda x: eval(x))  # Convert string tuples to actual tuples\n\ny_categorical = train_df[categorical_columns]\n\n\ndef encode_categorical_columns(df, columns):\n    label_encoders = {}\n    for col in columns:\n        le = LabelEncoder()\n        df[col] = le.fit_transform(df[col].values.ravel())  # Use ravel() here\n        label_encoders[col] = le\n    return df, label_encoders\n\n\n\ncategorical_columns = ['element', 'motion', 'power']  # Example\ntrain_df, label_encoders = encode_categorical_columns(train_df, categorical_columns)\n\n# Split data for training and validation\nX_train_speed, X_val_speed, y_train_speed, y_val_speed = train_test_split(video_features, y_speed, test_size=0.2, random_state=42)\nX_train_cat, X_val_cat, y_train_cat, y_val_cat = train_test_split(video_features, y_categorical, test_size=0.2, random_state=42)\nX_train_summary, X_val_summary, y_train_summary, y_val_summary = train_test_split(video_features, y_summary, test_size=0.2, random_state=42)\n\n\n# Reshape the video features to (num_samples, frames, height, width, channels)\nnum_frames = 20  # Assuming 20 frames per video\nheight = 64\nwidth = 64\nchannels = 3\nX_train_speed = X_train_speed.reshape(-1, num_frames, height, width, channels)\nX_val_speed = X_val_speed.reshape(-1, num_frames, height, width, channels)\nX_train_cat = X_train_cat.reshape(-1, num_frames, height, width, channels)\nX_val_cat = X_val_cat.reshape(-1, num_frames, height, width, channels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:32:04.559534Z","iopub.execute_input":"2025-01-17T05:32:04.559876Z","iopub.status.idle":"2025-01-17T05:32:05.202260Z","shell.execute_reply.started":"2025-01-17T05:32:04.559851Z","shell.execute_reply":"2025-01-17T05:32:05.201553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\nimport tensorflow as tf\n\ndef create_3d_cnn_model():\n    model = tf.keras.models.Sequential([\n        tf.keras.layers.Conv3D(32, (3, 3, 3), activation='relu', input_shape=(num_frames, height, width, channels)),\n        tf.keras.layers.MaxPooling3D((2, 2, 2)),\n        tf.keras.layers.Conv3D(64, (3, 3, 3), activation='relu'),\n        tf.keras.layers.MaxPooling3D((2, 2, 2)),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(128, activation='relu'),\n        tf.keras.layers.Dense(1)  # Output layer for speed \n    ])\n    return model\n\n# Create and compile models for speed and categorical attributes\nmodel_continuous = create_3d_cnn_model()\nmodel_continuous.compile(optimizer='adam', loss='mse', metrics=['mae'])\n\nmodels_categorical = {}\nfor col in categorical_columns:\n    model = create_3d_cnn_model()  # You can adjust output layer for categorical predictions\n    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])  # Change loss function\n    models_categorical[col] = model\n\n\n\n\nmodel_continuous.fit(X_train_speed, y_train_speed, epochs=10, validation_data=(X_val_speed, y_val_speed))\n\nfor col in categorical_columns:\n    models_categorical[col].fit(X_train_cat, y_train_cat[col], epochs=10, validation_data=(X_val_cat, y_val_cat[col]))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:32:14.968401Z","iopub.execute_input":"2025-01-17T05:32:14.968703Z","iopub.status.idle":"2025-01-17T05:35:52.819244Z","shell.execute_reply.started":"2025-01-17T05:32:14.968681Z","shell.execute_reply":"2025-01-17T05:35:52.818306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training for video_summary, separate models for x and y here\n\nx_values, y_values = zip(*y_train_summary)\n\nmodel_summary_x = LinearRegression()\nmodel_summary_x.fit(X_train_summary, x_values)\n\nmodel_summary_y = LinearRegression()\nmodel_summary_y.fit(X_train_summary, y_values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:36:01.453626Z","iopub.execute_input":"2025-01-17T05:36:01.453968Z","iopub.status.idle":"2025-01-17T05:39:52.543689Z","shell.execute_reply.started":"2025-01-17T05:36:01.453939Z","shell.execute_reply":"2025-01-17T05:39:52.542936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ntest_folder = '/kaggle/input/beginners-hypothesis-25/BH25/Testing_Data'\n\ntest_features = []\nvideo_ids = []\n\n# The below function finds all the video ids in the test folder and sorts them, \n# and then stores their features in test_features\n\nfor video_id in sorted(\n    [f for f in os.listdir(test_folder) if f.endswith(\".mp4\")], key=lambda x: int(x.split('.')[0])):\n    video_path = os.path.join(test_folder, video_id)\n    video_ids.append(video_id.split('.')[0]) \n    test_features.append(process_video(video_path))\n\ntest_features = np.array(test_features)  # Shape: (num_test_samples, 20*64*64*3)\ntest_features1 = test_features.reshape(-1, num_frames, height, width, channels)  # Reshape test data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:40:35.648090Z","iopub.execute_input":"2025-01-17T05:40:35.648443Z","iopub.status.idle":"2025-01-17T05:40:48.451262Z","shell.execute_reply.started":"2025-01-17T05:40:35.648417Z","shell.execute_reply":"2025-01-17T05:40:48.450238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_test_speed = model_continuous.predict(test_features1)\ny_test_summary_x = model_summary_x.predict(test_features)\ny_test_summary_y = model_summary_y.predict(test_features)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:40:55.627085Z","iopub.execute_input":"2025-01-17T05:40:55.627447Z","iopub.status.idle":"2025-01-17T05:41:03.294484Z","shell.execute_reply.started":"2025-01-17T05:40:55.627419Z","shell.execute_reply":"2025-01-17T05:41:03.293658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_categorical = {}\n\nfor col in categorical_columns:\n    y_categorical[col] = models_categorical[col].predict(test_features1)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:51:46.131361Z","iopub.execute_input":"2025-01-17T05:51:46.131725Z","iopub.status.idle":"2025-01-17T05:51:58.060567Z","shell.execute_reply.started":"2025-01-17T05:51:46.131695Z","shell.execute_reply":"2025-01-17T05:51:58.059923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# As we are doing regression in this notebook, we need to label the numbers back to what they represent using\n# inverse label transform, i.e. 1 will get labelled back as circular for example.\n\ndecoded_categorical = {}\nfor col, predictions in y_categorical.items():\n    rounded_predictions = np.rint(predictions).astype(int)\n    valid_classes = range(len(label_encoders[col].classes_))\n    rounded_predictions = np.clip(rounded_predictions, min(valid_classes), max(valid_classes))\n    decoded_categorical[col] = label_encoders[col].inverse_transform(rounded_predictions.ravel())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T06:38:54.884994Z","iopub.execute_input":"2025-01-17T06:38:54.885433Z","iopub.status.idle":"2025-01-17T06:38:54.892705Z","shell.execute_reply.started":"2025-01-17T06:38:54.885400Z","shell.execute_reply":"2025-01-17T06:38:54.891921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output_df = pd.DataFrame({\n    'video_id': video_ids,                      \n    'element': decoded_categorical['element'],  \n    'motion': decoded_categorical['motion'],\n    'power': decoded_categorical['power'],\n    'speed':  [x[0] for x in y_test_speed],               \n    'video_summary': list(zip(y_test_summary_x, y_test_summary_y)) \n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:19:23.441640Z","iopub.status.idle":"2025-01-17T05:19:23.441974Z","shell.execute_reply":"2025-01-17T05:19:23.441838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output_df.to_csv(\"submission.csv\", index=False)\n\n# This is the final submission file to be submitted","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-17T05:19:23.442908Z","iopub.status.idle":"2025-01-17T05:19:23.443220Z","shell.execute_reply":"2025-01-17T05:19:23.443060Z"}},"outputs":[],"execution_count":null}]}