{"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-05-06T10:01:19.602308Z","iopub.execute_input":"2023-05-06T10:01:19.602810Z","iopub.status.idle":"2023-05-06T10:05:18.380833Z","shell.execute_reply.started":"2023-05-06T10:01:19.602767Z","shell.execute_reply":"2023-05-06T10:05:18.379649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:28:20.462696Z","iopub.execute_input":"2023-05-06T11:28:20.463682Z","iopub.status.idle":"2023-05-06T11:28:20.470876Z","shell.execute_reply.started":"2023-05-06T11:28:20.463626Z","shell.execute_reply":"2023-05-06T11:28:20.469807Z"},"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\n\nprint(f\"Training size: {len(df_train)}\")\nprint(f\"Validate size: {len(df_validate)}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:05:18.383597Z","iopub.execute_input":"2023-05-06T10:05:18.384606Z","iopub.status.idle":"2023-05-06T10:05:18.393757Z","shell.execute_reply.started":"2023-05-06T10:05:18.384538Z","shell.execute_reply":"2023-05-06T10:05:18.391488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:05:18.395119Z","iopub.execute_input":"2023-05-06T10:05:18.396102Z","iopub.status.idle":"2023-05-06T10:05:18.436410Z","shell.execute_reply.started":"2023-05-06T10:05:18.396063Z","shell.execute_reply":"2023-05-06T10:05:18.435331Z"},"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-05-06T10:05:18.437894Z","iopub.execute_input":"2023-05-06T10:05:18.438528Z","iopub.status.idle":"2023-05-06T10:06:46.335450Z","shell.execute_reply.started":"2023-05-06T10:05:18.438491Z","shell.execute_reply":"2023-05-06T10:06:46.334341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:06:46.338837Z","iopub.execute_input":"2023-05-06T10:06:46.339539Z","iopub.status.idle":"2023-05-06T10:06:49.008236Z","shell.execute_reply.started":"2023-05-06T10:06:46.339509Z","shell.execute_reply":"2023-05-06T10:06:49.007254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:06:49.009453Z","iopub.execute_input":"2023-05-06T10:06:49.009832Z","iopub.status.idle":"2023-05-06T10:06:49.087710Z","shell.execute_reply.started":"2023-05-06T10:06:49.009794Z","shell.execute_reply":"2023-05-06T10:06:49.085659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we construct and train the neural network.","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_generator,  \n  verbose = 1, \n  validation_data=val_generator, callbacks=[monitor], epochs=25)# epochs=25","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:06:49.089167Z","iopub.execute_input":"2023-05-06T10:06:49.089768Z","iopub.status.idle":"2023-05-06T11:16:10.623544Z","shell.execute_reply.started":"2023-05-06T10:06:49.089729Z","shell.execute_reply":"2023-05-06T11:16:10.622500Z"},"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-05-06T11:16:10.624984Z","iopub.execute_input":"2023-05-06T11:16:10.625320Z","iopub.status.idle":"2023-05-06T11:16:32.684829Z","shell.execute_reply.started":"2023-05-06T11:16:10.625291Z","shell.execute_reply":"2023-05-06T11:16:32.683660Z"},"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-05-06T11:16:32.686469Z","iopub.execute_input":"2023-05-06T11:16:32.686872Z","iopub.status.idle":"2023-05-06T11:18:08.031775Z","shell.execute_reply.started":"2023-05-06T11:16:32.686833Z","shell.execute_reply":"2023-05-06T11:18:08.030240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submit","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:45:43.920698Z","iopub.execute_input":"2023-05-06T11:45:43.921164Z","iopub.status.idle":"2023-05-06T11:45:43.936719Z","shell.execute_reply.started":"2023-05-06T11:45:43.921121Z","shell.execute_reply":"2023-05-06T11:45:43.935645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:49:25.904314Z","iopub.execute_input":"2023-05-06T11:49:25.904795Z","iopub.status.idle":"2023-05-06T11:49:25.920764Z","shell.execute_reply.started":"2023-05-06T11:49:25.904753Z","shell.execute_reply":"2023-05-06T11:49:25.919528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"al=0\ntp = 0\ntdf = df[\"count\"]\nrdf = df_submit[\"clip_count\"]\nfor i in range (TRAIN_CUT,len(df)):\n    al+=1\n    val = int(rdf[i])\n    if(tdf[i]==val):\n        tp+=1\n    else:\n        print(f\"id = {i} \\n prediction = {rdf[i]}\\nreal = {tdf[i]}\\n\\n\\n\")\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:48:41.019730Z","iopub.execute_input":"2023-05-06T11:48:41.020767Z","iopub.status.idle":"2023-05-06T11:48:41.051050Z","shell.execute_reply.started":"2023-05-06T11:48:41.020727Z","shell.execute_reply":"2023-05-06T11:48:41.048533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nmodel.save('beach_count')\n\n#model_loaded = keras.models.load_model('beach_count')","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:18:08.033713Z","iopub.execute_input":"2023-05-06T11:18:08.034377Z","iopub.status.idle":"2023-05-06T11:18:12.640054Z","shell.execute_reply.started":"2023-05-06T11:18:08.034333Z","shell.execute_reply":"2023-05-06T11:18:12.638668Z"},"trusted":true},"execution_count":null,"outputs":[]}]}