{"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 numpy as np\nimport matplotlib.pyplot as plt \nimport pandas as pd \nimport cv2\n\nimport tensorflow as tf\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization,Conv2D, MaxPool2D\nfrom keras.models import Model\nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nimport keras\nfrom keras import regularizers, optimizers\nfrom keras.models import Sequential","metadata":{"id":"qgcXAiMMjeOx","execution":{"iopub.status.busy":"2021-10-28T16:35:32.271847Z","iopub.execute_input":"2021-10-28T16:35:32.272724Z","iopub.status.idle":"2021-10-28T16:35:38.316656Z","shell.execute_reply.started":"2021-10-28T16:35:32.272596Z","shell.execute_reply":"2021-10-28T16:35:38.315776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings \nwarnings.filterwarnings(\"ignore\")","metadata":{"id":"njAePHwDjkpF","outputId":"5b19996b-bdfd-475d-ae68-5463457f13f6","execution":{"iopub.status.busy":"2021-10-28T16:35:58.785654Z","iopub.execute_input":"2021-10-28T16:35:58.785939Z","iopub.status.idle":"2021-10-28T16:35:58.790501Z","shell.execute_reply.started":"2021-10-28T16:35:58.785911Z","shell.execute_reply":"2021-10-28T16:35:58.789631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/train.csv\")\ntest = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/test.csv\")\n\n\n\ntrain_folder = \"../input/nnfl-2021-assignment-1/fire_videos/train\"\ntest_folder = \"../input/nnfl-2021-assignment-1/fire_videos/test\"\n\n\nfire_map = {0: 'fire', 1: 'not_fire'}\ntrain[\"True_Label\"].value_counts()\n\n#split into fire and non-fire\nnot_fire_data=train[train['True_Label']=='not_fire']\nfire_data=train[train['True_Label']=='fire']\n\n#20% of validation data; random state is seed value\nnot_fire_val_data=not_fire_data.sample(frac=0.2,random_state=1)\nfire_val_data=fire_data.sample(frac=0.2,random_state=1)\n\n#getting the training data; i.e. removing the validation rows from the bigger data\nnot_fire_train_data=pd.concat([not_fire_data,not_fire_val_data,not_fire_val_data]).drop_duplicates(keep=False)\nfire_train_data=pd.concat([fire_data,fire_val_data,fire_val_data]).drop_duplicates(keep=False)\n\n#creating final pandas file\ntrain_data=pd.concat([not_fire_train_data,fire_train_data])\nval_data=pd.concat([not_fire_val_data,fire_val_data])\ntrain_final=pd.concat([train_data,val_data])","metadata":{"id":"_Q4zHy7rjzQO","execution":{"iopub.status.busy":"2021-10-28T16:36:01.288809Z","iopub.execute_input":"2021-10-28T16:36:01.289643Z","iopub.status.idle":"2021-10-28T16:36:01.492128Z","shell.execute_reply.started":"2021-10-28T16:36:01.2896Z","shell.execute_reply":"2021-10-28T16:36:01.491478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'Count': [50732, 18215]}, index=['Not_Fire', 'Fire'])\nplot = df.plot.pie(y='Count', figsize=(5, 5))","metadata":{"execution":{"iopub.status.busy":"2021-10-28T16:36:14.009021Z","iopub.execute_input":"2021-10-28T16:36:14.009337Z","iopub.status.idle":"2021-10-28T16:36:14.235952Z","shell.execute_reply.started":"2021-10-28T16:36:14.009284Z","shell.execute_reply":"2021-10-28T16:36:14.235365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Preporcess data using Data generator\n# \ntest_datagen=ImageDataGenerator(rescale=1./255.)\n\n\ntest_generator=test_datagen.flow_from_dataframe(dataframe=test,\n                                                directory=test_folder,\n                                                x_col=\"File\",\n                                                y_col=None,\n                                                batch_size=32,\n                                                seed=42,\n                                                shuffle=False,\n                                                class_mode=None,\n                                                target_size=(224,224))\n\n","metadata":{"id":"ly2FMsjTj9TX","outputId":"b5953aa5-b0a2-4936-b627-2cbaa028219e","execution":{"iopub.status.busy":"2021-10-28T16:36:58.863872Z","iopub.execute_input":"2021-10-28T16:36:58.864437Z","iopub.status.idle":"2021-10-28T16:37:03.308132Z","shell.execute_reply.started":"2021-10-28T16:36:58.864403Z","shell.execute_reply":"2021-10-28T16:37:03.307028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen=ImageDataGenerator(rescale=1./255., validation_split=0.2)\n\ntrain_generator=datagen.flow_from_dataframe(dataframe=train_final,\n                                            directory=train_folder,\n                                            x_col=\"File\",\n                                            y_col=\"True_Label\",\n                                            subset=\"training\",\n                                            batch_size=32,\n                                            seed=42,\n                                            shuffle=True,\n                                            class_mode=\"binary\",\n                                            target_size=(224,224))\ndatagen1=ImageDataGenerator(rescale=1./255.)\n\nvalid_generator=datagen.flow_from_dataframe(dataframe=train_final,\n                                            directory=train_folder,\n                                            x_col=\"File\",\n                                            y_col=\"True_Label\",\n                                            subset=\"validation\",\n                                            batch_size=32,\n                                            seed=42,\n                                            shuffle=True,\n                                            class_mode=\"binary\",\n                                            target_size=(224,224))\n\n","metadata":{"id":"Dhlb-S5alGks","outputId":"fca039c1-1f3f-4f5d-b06d-42fd9036c288","execution":{"iopub.status.busy":"2021-10-28T16:37:03.30993Z","iopub.execute_input":"2021-10-28T16:37:03.310171Z","iopub.status.idle":"2021-10-28T16:39:29.698406Z","shell.execute_reply.started":"2021-10-28T16:37:03.310144Z","shell.execute_reply":"2021-10-28T16:39:29.697195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_img,label = train_generator.next()","metadata":{"execution":{"iopub.status.busy":"2021-10-28T16:39:29.700443Z","iopub.execute_input":"2021-10-28T16:39:29.700772Z","iopub.status.idle":"2021-10-28T16:39:30.086352Z","shell.execute_reply.started":"2021-10-28T16:39:29.70073Z","shell.execute_reply":"2021-10-28T16:39:30.085452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotImages(img_arr,label):\n  \"\"\"\n  input: image array \n  output: plot images \n  \"\"\"\n  for idx,img in enumerate(img_arr):\n    if idx <= 10:\n      plt.figure(figsize = (2,2))\n      plt.imshow(img)\n      plt.title(img.shape)\n      plt.axis = False\n      plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-28T16:39:34.321099Z","iopub.execute_input":"2021-10-28T16:39:34.321431Z","iopub.status.idle":"2021-10-28T16:39:34.327821Z","shell.execute_reply.started":"2021-10-28T16:39:34.321384Z","shell.execute_reply":"2021-10-28T16:39:34.326861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(t_img,label)","metadata":{"execution":{"iopub.status.busy":"2021-10-28T16:39:39.603178Z","iopub.execute_input":"2021-10-28T16:39:39.603475Z","iopub.status.idle":"2021-10-28T16:39:41.772752Z","shell.execute_reply.started":"2021-10-28T16:39:39.603446Z","shell.execute_reply":"2021-10-28T16:39:41.771636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model ","metadata":{"id":"bthp-pQQq_Hm"}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=8,kernel_size = (2,2),activation='relu',input_shape = (224,224,3)))\nmodel.add(MaxPool2D(pool_size=(3, 3),strides=4))\nmodel.add(Conv2D(filters=16,kernel_size=(2,2),activation='relu'))\nmodel.add(MaxPool2D(pool_size=(5, 5),strides=1))\nmodel.add(Conv2D(filters=32,kernel_size=(2,2),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dense(64,activation='relu',kernel_regularizer='l2'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(32,activation = 'relu',kernel_regularizer='l2'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(16,activation = 'relu',kernel_regularizer='l2'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1,activation = 'sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', keras.metrics.Precision(), keras.metrics.Recall()])\n\nmodel.summary()","metadata":{"id":"ymKagTomq92W","outputId":"e44c99ab-8f23-46ea-a1dd-9d1a335a5e1d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\ncheckpoint = ModelCheckpoint('a_result1.h5', monitor='val_accuracy',mode='max', save_best_only=True)\nearly_stop = EarlyStopping(monitor=\"val_loss\", patience=3,verbose=1)\ncallbacks_list = [checkpoint, early_stop]\nmodel.fit(x=train_generator,batch_size=32,epochs=15,validation_data=valid_generator,callbacks = callbacks_list)","metadata":{"id":"gviTdefBdg5f","outputId":"db479917-e2c6-41e1-e567-75cf2d779887"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"a_result3.h5\")","metadata":{"id":"y3Bid5vbrEif"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1,2, figsize=(18, 6))\n# Plot training & validation accuracy values\naxes[0].plot(history.history['accuracy'])\naxes[0].plot(history.history['val_accuracy'])\naxes[0].set_title('Model accuracy')\naxes[0].set_ylabel('Accuracy')\naxes[0].set_xlabel('Epoch')\naxes[0].legend(['Train', 'Validation'], loc='upper left')\n\n# Plot training & validation loss values\naxes[1].plot(history.history['loss'])\naxes[1].plot(history.history['val_loss'])\naxes[1].set_title('Model loss')\naxes[1].set_ylabel('Loss')\naxes[1].set_xlabel('Epoch')\naxes[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=model.predict_generator(test_generator)\npredict=(predict >= 0.5).astype(\"int32\")","metadata":{"id":"j_N4-4n0oksl"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices=predict.ravel()","metadata":{"id":"mb0qU0I_kj1c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices","metadata":{"id":"D1UqdUuRklFn","outputId":"5d95ef45-3ecf-4479-d53b-393fbcf33f1c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels={}\nlabels[0]='fire'\nlabels[1]='not_fire'\npredictions = [labels[k] for k in predicted_class_indices]\n","metadata":{"id":"O5CI_5oQlSPY"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"File\":filenames,\n                      \"Label\":predictions})","metadata":{"id":"WXdoTq79lp7F"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head(10)","metadata":{"id":"lh8UHHuemI_G","outputId":"c0a52dba-963e-49a2-fa23-41b2eb46a38d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RESULT CSV\nresults.to_csv(\"resultsa3.csv\", index= False)","metadata":{"id":"S95j_hFtmKdV"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"1dKQJ-PqJ0Go","outputId":"f511cd71-d55b-42f2-9d20-03357b06d5bb"},"execution_count":null,"outputs":[]}]}