{"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":"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-03-20T17:51:07.541509Z","iopub.execute_input":"2023-03-20T17:51:07.542076Z","iopub.status.idle":"2023-03-20T17:55:04.480267Z","shell.execute_reply.started":"2023-03-20T17:51:07.542034Z","shell.execute_reply":"2023-03-20T17:55:04.478296Z"},"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-03-20T17:55:13.911774Z","iopub.execute_input":"2023-03-20T17:55:13.913472Z","iopub.status.idle":"2023-03-20T17:55:13.921066Z","shell.execute_reply.started":"2023-03-20T17:55:13.913401Z","shell.execute_reply":"2023-03-20T17:55:13.919569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-03-20T17:55:17.171351Z","iopub.execute_input":"2023-03-20T17:55:17.171874Z","iopub.status.idle":"2023-03-20T17:55:17.205282Z","shell.execute_reply.started":"2023-03-20T17:55:17.171831Z","shell.execute_reply":"2023-03-20T17:55:17.204259Z"},"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-03-20T17:55:20.621670Z","iopub.execute_input":"2023-03-20T17:55:20.622122Z","iopub.status.idle":"2023-03-20T17:55:51.174479Z","shell.execute_reply.started":"2023-03-20T17:55:20.622076Z","shell.execute_reply":"2023-03-20T17:55:51.173269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we construct and train the 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.Flatten(),\n    # 512 neuron hidden layer\n    tf.keras.layers.Dense(512, 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=25)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T17:56:06.449297Z","iopub.execute_input":"2023-03-20T17:56:06.450370Z","iopub.status.idle":"2023-03-20T19:04:14.422275Z","shell.execute_reply.started":"2023-03-20T17:56:06.450328Z","shell.execute_reply":"2023-03-20T19:04:14.421287Z"},"trusted":true},"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-03-20T19:06:48.628465Z","iopub.execute_input":"2023-03-20T19:06:48.628995Z","iopub.status.idle":"2023-03-20T19:06:58.963755Z","shell.execute_reply.started":"2023-03-20T19:06:48.628955Z","shell.execute_reply":"2023-03-20T19:06:58.962609Z"},"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'],'clip_count':pred.flatten()})\ndf_submit.to_csv(\"/kaggle/working/submit.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-20T19:07:07.076592Z","iopub.execute_input":"2023-03-20T19:07:07.076956Z","iopub.status.idle":"2023-03-20T19:08:19.628505Z","shell.execute_reply.started":"2023-03-20T19:07:07.076925Z","shell.execute_reply":"2023-03-20T19:08:19.627468Z"},"trusted":true},"execution_count":null,"outputs":[]}]}