{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom scipy import ndimage\nfrom IPython.display import Image\nfrom keras.preprocessing import image\nfrom tqdm import tqdm\n\ndata_train = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/train.csv\")\n\ntrain_image = [ ]\nfor i in tqdm(range(data_train.shape[0])):\n    img = image.load_img('../input/nnfl-2021-assignment-1/fire_videos/train/'+data_train['File'][i], target_size=(28,28,1), grayscale=True)\n    img = image.img_to_array(img)\n    img = img/255\n    train_image.append(img)\nx_train = np.array(train_image)\ny_train = data_train.True_Label\n\n\n\n\n# Reshaping the array to 4-dims so that it can work with the Keras API\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-27T08:57:05.773177Z","iopub.execute_input":"2021-10-27T08:57:05.773734Z","iopub.status.idle":"2021-10-27T09:12:56.433755Z","shell.execute_reply.started":"2021-10-27T08:57:05.773697Z","shell.execute_reply":"2021-10-27T09:12:56.431757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/test.csv\")\ntest_image = [ ]\nfor i in tqdm(range(data_test.shape[0])):\n    test_img = image.load_img('../input/nnfl-2021-assignment-1/fire_videos/test/'+data_test['File'][i], target_size=(28,28,1), grayscale=True)\n    test_img = image.img_to_array(test_img)\n    test_img = test_img/255\n    test_image.append(test_img)\nx_test = np.array(test_image)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:21:56.300318Z","iopub.execute_input":"2021-10-27T09:21:56.300670Z","iopub.status.idle":"2021-10-27T09:22:24.599658Z","shell.execute_reply.started":"2021-10-27T09:21:56.300629Z","shell.execute_reply":"2021-10-27T09:22:24.598464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\n\ninput_shape = (28, 28, 1)\nprint('x_train shape:', x_train.shape)\nprint('Number of images in x_train', x_train.shape[0])\nprint('Number of images in x_test', x_test.shape[0])","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:22:28.366455Z","iopub.execute_input":"2021-10-27T09:22:28.366769Z","iopub.status.idle":"2021-10-27T09:22:28.377512Z","shell.execute_reply.started":"2021-10-27T09:22:28.366738Z","shell.execute_reply":"2021-10-27T09:22:28.376308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nearlystop = EarlyStopping(monitor='loss',\n                          min_delta=0.0001,\n                          patience=3,\n                          verbose=0,\n                          mode='auto')\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='accuracy', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.2, \n                                            min_lr=0.0001)\n\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:14:12.327662Z","iopub.execute_input":"2021-10-27T09:14:12.327968Z","iopub.status.idle":"2021-10-27T09:14:12.336351Z","shell.execute_reply.started":"2021-10-27T09:14:12.327935Z","shell.execute_reply":"2021-10-27T09:14:12.335279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(y_train.shape[0]):\n    if  y_train[i] == 'not_fire':\n        y_train[i] = 0.0\n    else:\n        y_train[i] = 1.0\nprint(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:22:34.119962Z","iopub.execute_input":"2021-10-27T09:22:34.120842Z","iopub.status.idle":"2021-10-27T09:22:35.477043Z","shell.execute_reply.started":"2021-10-27T09:22:34.120791Z","shell.execute_reply":"2021-10-27T09:22:35.476238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train*255\nx_test = x_test*255\n\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\n\ny_train = y_train.astype('float32')\n\nx_train /= 255\nx_test /= 255","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:22:37.966410Z","iopub.execute_input":"2021-10-27T09:22:37.967022Z","iopub.status.idle":"2021-10-27T09:22:38.217269Z","shell.execute_reply.started":"2021-10-27T09:22:37.966960Z","shell.execute_reply":"2021-10-27T09:22:38.216325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/nnfl-2021-assignment-1/Sample Submission.csv\")\ny_test = test_data.Label\nfor i in range(y_test.shape[0]):\n    if  y_test[i] == 'not_fire':\n        y_test[i] = 0.0\n    else:\n        y_test[i] = 1.0\nprint(y_test)\ny_test = y_test.astype('float32')\n","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:22:43.834329Z","iopub.execute_input":"2021-10-27T09:22:43.834685Z","iopub.status.idle":"2021-10-27T09:22:44.031478Z","shell.execute_reply.started":"2021-10-27T09:22:43.834643Z","shell.execute_reply":"2021-10-27T09:22:44.030363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing the required Keras modules containing model and layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\n# Creating a Sequential Model and adding the layers\nmodel = Sequential()\nmodel.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(layers.Conv2D(28, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(28, (3, 3), activation='relu'))\nmodel.add(Flatten()) # Flattening the 2D arrays for fully connected layers\nmodel.add(Dense(128, activation=tf.nn.relu))\nmodel.add(Dropout(0.2))\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation=tf.nn.softmax))\n\nmodel.compile(optimizer='adam', \n              loss='sparse_categorical_crossentropy', \n              metrics=['accuracy'])\nmodel.fit(x=x_train,y=y_train, epochs=10, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T09:24:00.071367Z","iopub.execute_input":"2021-10-27T09:24:00.071679Z"},"trusted":true},"execution_count":null,"outputs":[]}]}