{"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":"markdown","source":"This is my starter code for the Kaggle competition. This could may evolve a bit. This is a basic implementation that should get around 11-ish RMSE on the leaderboard. \n\nFirst, we build a dataframe that contains the filenames and the clip_count. You will need to modify the paths below to match where you put train.csv.","metadata":{}},{"cell_type":"code","source":"!nvidia-smi --query-gpu=name --format=csv,noheader","metadata":{"execution":{"iopub.status.busy":"2023-04-16T14:08:44.434667Z","iopub.execute_input":"2023-04-16T14:08:44.435650Z","iopub.status.idle":"2023-04-16T14:08:45.509440Z","shell.execute_reply.started":"2023-04-16T14:08:44.435604Z","shell.execute_reply":"2023-04-16T14:08:45.507958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2023-04-16T14:08:48.333061Z","iopub.execute_input":"2023-04-16T14:08:48.337741Z","iopub.status.idle":"2023-04-16T14:08:49.575610Z","shell.execute_reply.started":"2023-04-16T14:08:48.337675Z","shell.execute_reply":"2023-04-16T14:08:49.574375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\n\nPATH = \"/kaggle/input/applications-of-deep-learning-wustlspring-2023/beach/beach/\"\n\n!conda install -y -c conda-forge keras-preprocessing\n\ndf = pd.read_csv(\n    os.path.join(PATH, \"train.csv\"), \n    na_values=['NA', '?'])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T14:08:54.443985Z","iopub.execute_input":"2023-04-16T14:08:54.445052Z","iopub.status.idle":"2023-04-16T14:12:57.102370Z","shell.execute_reply.started":"2023-04-16T14:08:54.444997Z","shell.execute_reply":"2023-04-16T14:12:57.101196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Separate into a training and validation (for early stopping)","metadata":{}},{"cell_type":"code","source":"TRAIN_PCT = 0.9\nTRAIN_CUT = int(len(df) * TRAIN_PCT)\n\ndf_train = df[0:TRAIN_CUT]\ndf_validate = df[TRAIN_CUT:]\n\nprint(f\"Training size: {len(df_train)}\")\nprint(f\"Validate size: {len(df_validate)}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T16:26:51.137306Z","iopub.execute_input":"2023-04-16T16:26:51.137695Z","iopub.status.idle":"2023-04-16T16:26:51.144898Z","shell.execute_reply.started":"2023-04-16T16:26:51.137661Z","shell.execute_reply":"2023-04-16T16:26:51.143588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-04-16T16:26:52.618638Z","iopub.execute_input":"2023-04-16T16:26:52.619349Z","iopub.status.idle":"2023-04-16T16:26:52.632791Z","shell.execute_reply.started":"2023-04-16T16:26:52.619308Z","shell.execute_reply":"2023-04-16T16:26:52.631640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create DataGenerators for training and validation. This is what links the .csv with the images. This part might take a few minutes to run.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport keras_preprocessing\nfrom keras_preprocessing import image\nfrom keras_preprocessing.image import ImageDataGenerator\n\ntraining_datagen = ImageDataGenerator(\n  rescale = 1./255,\n  horizontal_flip=True,\n  vertical_flip=True,\n  fill_mode='nearest')\n\ntrain_generator = training_datagen.flow_from_dataframe(\n        dataframe=df_train,\n        directory=PATH,\n        x_col=\"filename\",\n        y_col=\"count\",\n        target_size=(256, 256),\n        batch_size=32,\n        class_mode='other')\n\nvalidation_datagen = ImageDataGenerator(rescale = 1./255)\n\nval_generator = validation_datagen.flow_from_dataframe(\n        dataframe=df_validate,\n        directory=PATH,\n        x_col=\"filename\",\n        y_col=\"count\",\n        target_size=(256, 256),\n        class_mode='other')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T16:26:55.922752Z","iopub.execute_input":"2023-04-16T16:26:55.923480Z","iopub.status.idle":"2023-04-16T16:27:03.821272Z","shell.execute_reply.started":"2023-04-16T16:26:55.923439Z","shell.execute_reply":"2023-04-16T16:27:03.820102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Neural Network","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nmodel = tf.keras.models.Sequential([\n    # Note the input shape is the desired size of the image 150x150 with 3 bytes color\n    # This is the first convolution\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(256, 256, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    # The second convolution\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    # 512 neuron hidden layer\n    tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(1, activation='linear')\n])\n\n\nmodel.summary()\nepoch_steps = 250 # needed for 2.2\nvalidation_steps = len(df_validate)\nmodel.compile(loss = 'mean_squared_error', optimizer='adam')\nmonitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto',\n        restore_best_weights=True)\nhistory = model.fit(train_generator,  \n  verbose = 1, \n  validation_data=val_generator, callbacks=[monitor], epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.712414Z","iopub.status.idle":"2023-04-15T13:20:19.713343Z","shell.execute_reply.started":"2023-04-15T13:20:19.713035Z","shell.execute_reply":"2023-04-15T13:20:19.713068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_test = pd.read_csv(\n#     os.path.join(PATH,\"test.csv\"),\n#     na_values=['NA', '?'])\n\n# test_datagen = ImageDataGenerator(rescale = 1./255)\n\n# test_generator = validation_datagen.flow_from_dataframe(\n#         dataframe=df_test,\n#         directory=PATH,\n#         x_col=\"filename\",\n#         batch_size=1,\n#         shuffle=False,\n#         target_size=(256, 256),\n#         class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.715119Z","iopub.status.idle":"2023-04-15T13:20:19.716040Z","shell.execute_reply.started":"2023-04-15T13:20:19.715762Z","shell.execute_reply":"2023-04-15T13:20:19.715792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_generator.reset()\n# pred = model.predict(test_generator,steps=len(df_test))\n\n# df_submit = pd.DataFrame({'id':df_test['id'],'count':pred.flatten()})\n# df_submit.to_csv(\"/kaggle/working/submit.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.717597Z","iopub.status.idle":"2023-04-15T13:20:19.718486Z","shell.execute_reply.started":"2023-04-15T13:20:19.718205Z","shell.execute_reply":"2023-04-15T13:20:19.718233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.callbacks import EarlyStopping\n\n# model = tf.keras.models.Sequential([\n#     # Note the input shape is the desired size of the image 150x150 with 3 bytes color\n#     # This is the first convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(256, 256, 3)),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n#     # The second convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Flatten(),\n#     # 512 neuron hidden layer\n#     tf.keras.layers.Dense(512, activation='relu'),\n#     tf.keras.layers.Dense(64, activation='relu'),\n#     tf.keras.layers.Dense(1, activation='linear')\n# ])\n\n\n# model.summary()\n# epoch_steps = 250 # needed for 2.2\n# validation_steps = len(df_validate)\n# model.compile(loss = 'mean_squared_error', optimizer='adam')\n# monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=4, verbose=1, mode='auto',\n#         restore_best_weights=True)\n# history = model.fit(train_generator,  \n#   verbose = 1, \n#   validation_data=val_generator, callbacks=[monitor], epochs=100)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.720007Z","iopub.status.idle":"2023-04-15T13:20:19.720887Z","shell.execute_reply.started":"2023-04-15T13:20:19.720569Z","shell.execute_reply":"2023-04-15T13:20:19.720596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.callbacks import EarlyStopping\n\n# model = tf.keras.models.Sequential([\n#     # Note the input shape is the desired size of the image 150x150 with 3 bytes color\n#     # This is the first convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(256, 256, 3)),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n#     # The second convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Flatten(),\n#     # 512 neuron hidden layer\n#     tf.keras.layers.Dense(512, activation='relu'),\n#     tf.keras.layers.Dense(64, activation='relu'),\n#     tf.keras.layers.Dense(1, activation='linear')\n# ])\n\n# model.summary()\n# epoch_steps = 250 # needed for 2.2\n# validation_steps = len(df_validate)\n# model.compile(loss = 'mean_squared_error', optimizer='adam')\n# monitor = EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=10, verbose=1, mode='auto',\n#         restore_best_weights=True)\n# history = model.fit(train_generator,  \n#   verbose = 1, \n#   validation_data=val_generator, callbacks=[monitor], epochs=150)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T16:52:29.568301Z","iopub.execute_input":"2023-04-16T16:52:29.568984Z","iopub.status.idle":"2023-04-16T20:06:52.172750Z","shell.execute_reply.started":"2023-04-16T16:52:29.568943Z","shell.execute_reply":"2023-04-16T20:06:52.171623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.callbacks import EarlyStopping\n\n# model = tf.keras.models.Sequential([\n#     # Note the input shape is the desired size of the image 150x150 with 3 bytes color\n#     # This is the first convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(256, 256, 3)),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n#     # The second convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Flatten(),\n#     # 512 neuron hidden layer\n#     tf.keras.layers.Dense(512, activation='relu'),\n#     tf.keras.layers.Dense(64, activation='relu'),\n#     tf.keras.layers.Dense(1, activation='linear')\n# ])\n\n\n# model.summary()\n# epoch_steps = 250 # needed for 2.2\n# validation_steps = len(df_validate)\n# model.compile(loss = 'mean_squared_error', optimizer='adam')\n# monitor = EarlyStopping(monitor='val_loss', min_delta=1e-4, patience=4, verbose=1, mode='auto',\n#         restore_best_weights=True)\n# history = model.fit(train_generator,  \n#   verbose = 1, \n#   validation_data=val_generator, callbacks=[monitor], epochs=100)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T14:49:02.123369Z","iopub.execute_input":"2023-04-15T14:49:02.124157Z","iopub.status.idle":"2023-04-15T16:34:14.736557Z","shell.execute_reply.started":"2023-04-15T14:49:02.124119Z","shell.execute_reply":"2023-04-15T16:34:14.735483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.callbacks import EarlyStopping\n\n# model = tf.keras.models.Sequential([\n#     # Note the input shape is the desired size of the image 150x150 with 3 bytes color\n#     # This is the first convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(256, 256, 3)),\n#     tf.keras.layers.MaxPooling2D(2, 2),\n#     # The second convolution\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n#     tf.keras.layers.MaxPooling2D(2,2),\n#     tf.keras.layers.Flatten(),\n#     # 512 neuron hidden layer\n#     tf.keras.layers.Dense(512, activation='relu'),\n#     tf.keras.layers.Dense(64, activation='relu'),\n#     tf.keras.layers.Dense(1, activation='linear')\n# ])\n\n\n# model.summary()\n# epoch_steps = 250 # needed for 2.2\n# validation_steps = len(df_validate)\n# model.compile(loss = 'mean_squared_error', optimizer='adam')\n# monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=8, verbose=1, mode='auto',\n#         restore_best_weights=True)\n# history = model.fit(train_generator,  \n#   verbose = 1, \n#   validation_data=val_generator, callbacks=[monitor], epochs=100)\n\n# df_test = pd.read_csv(\n#     os.path.join(PATH,\"test.csv\"),\n#     na_values=['NA', '?'])\n\n# test_datagen = ImageDataGenerator(rescale = 1./255)\n\n# test_generator = validation_datagen.flow_from_dataframe(\n#         dataframe=df_test,\n#         directory=PATH,\n#         x_col=\"filename\",\n#         batch_size=1,\n#         shuffle=False,\n#         target_size=(256, 256),\n#         class_mode=None)\n\n# test_generator.reset()\n# pred = model.predict(test_generator,steps=len(df_test))\n\n# df_submit = pd.DataFrame({'id':df_test['id'],'count':pred.flatten()})\n# df_submit.to_csv(\"/kaggle/working/submit.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T16:55:50.494544Z","iopub.execute_input":"2023-04-15T16:55:50.495235Z","iopub.status.idle":"2023-04-15T18:55:49.670721Z","shell.execute_reply.started":"2023-04-15T16:55:50.495197Z","shell.execute_reply":"2023-04-15T18:55:49.669698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# # Step 1: Create an augmentation pipeline for the test data\n# test_datagen = ImageDataGenerator(\n#     rescale=1./255,\n#     rotation_range=20,\n#     width_shift_range=0.1,\n#     height_shift_range=0.1,\n#     shear_range=0.1,\n#     zoom_range=0.1,\n#     horizontal_flip=True,\n#     fill_mode='nearest'\n# )\n\n# test_generator = test_datagen.flow_from_directory(\n#     test_dir,\n#     target_size=(256, 256),\n#     batch_size=32,\n#     class_mode='binary'\n# )\n\n# # Step 2: Run the model on the augmented test data multiple times and average the predictions\n# tta_steps = 10\n# predictions = []\n\n# for i in range(tta_steps):\n#     test_generator.reset()\n#     preds = model.predict(test_generator, steps=len(test_generator))\n#     predictions.append(preds)\n\n# avg_preds = np.mean(predictions, axis=0)\n\n# # Step 3: Evaluate the model's performance using the averaged predictions\n# # Assuming you have true labels for your test data\n# true_labels = ...\n# accuracy = np.mean((true_labels - avg_preds) ** 2)\n\n# print(\"TTA Mean Squared Error:\", accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.727071Z","iopub.status.idle":"2023-04-15T13:20:19.727964Z","shell.execute_reply.started":"2023-04-15T13:20:19.727663Z","shell.execute_reply":"2023-04-15T13:20:19.727696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Step 2: Run the model on the augmented test data multiple times and average the predictions\n# tta_steps = 10\n# predictions = []\n\n# for i in range(tta_steps):\n#     test_generator.reset()\n#     preds = model.predict(test_generator, steps=len(test_generator))\n#     predictions.append(preds)\n\n# avg_preds = np.mean(predictions, axis=0)\n\n# # Step 3: Evaluate the model's performance using the averaged predictions\n# # Assuming you have true labels for your test data\n# true_labels = ...\n# accuracy = np.mean((true_labels - avg_preds) ** 2)\n\n# print(\"TTA Mean Squared Error:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T13:20:19.729581Z","iopub.status.idle":"2023-04-15T13:20:19.730490Z","shell.execute_reply.started":"2023-04-15T13:20:19.730202Z","shell.execute_reply":"2023-04-15T13:20:19.730232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Submission File","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv(\n    os.path.join(PATH,\"test.csv\"),\n    na_values=['NA', '?'])\n\ntest_datagen = ImageDataGenerator(rescale = 1./255)\n\ntest_generator = validation_datagen.flow_from_dataframe(\n        dataframe=df_test,\n        directory=PATH,\n        x_col=\"filename\",\n        batch_size=1,\n        shuffle=False,\n        target_size=(256, 256),\n        class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T20:25:44.200703Z","iopub.execute_input":"2023-04-16T20:25:44.201447Z","iopub.status.idle":"2023-04-16T20:25:50.889176Z","shell.execute_reply.started":"2023-04-16T20:25:44.201409Z","shell.execute_reply":"2023-04-16T20:25:50.888049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\npred = model.predict(test_generator,steps=len(df_test))\n\ndf_submit = pd.DataFrame({'id':df_test['id'],'count':pred.flatten()})\ndf_submit.to_csv(\"/kaggle/working/submit.csv\",index=False)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-16T20:25:54.399688Z","iopub.execute_input":"2023-04-16T20:25:54.400295Z","iopub.status.idle":"2023-04-16T20:26:45.931096Z","shell.execute_reply.started":"2023-04-16T20:25:54.400257Z","shell.execute_reply":"2023-04-16T20:26:45.929838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After training bunch of MCNN models, try to merge all the high-score result together, and use mean to submit.","metadata":{}}]}