{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os, warnings\nfrom PIL import Image\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\nimport tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nfrom scipy import ndimage\nfrom IPython.display import Image\nfrom tqdm import tqdm\nimport random\nfrom pathlib import Path\nfrom matplotlib.image import imread\nimport keras\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-30T04:56:35.542285Z","iopub.execute_input":"2021-10-30T04:56:35.542608Z","iopub.status.idle":"2021-10-30T04:56:35.550695Z","shell.execute_reply.started":"2021-10-30T04:56:35.542560Z","shell.execute_reply":"2021-10-30T04:56:35.549516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = []\ntrain_labels = []\nDATADIR = '../input/nnfl-2021-assignment-1/fire_videos/train'\ntrain = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/train.csv\")\nfor i in range(len(train.File)):\n  if i%11 == 0:\n    imagepath = os.path.join(DATADIR,train.File[i])\n    image = cv2.imread(imagepath)\n    image = cv2.resize(image,(128,128))\n    train_images.append(image)\n    if train.True_Label[i] == 'fire':\n      train_labels.append([1,0])\n    else:\n      train_labels.append([0,1])\n  else:\n    continue\ntrain_images = np.array(train_images, dtype=\"float\")/255.0\ntrain_labels = np.array(train_labels)\n(x_train, x_test, y_train, y_test) = train_test_split(train_images, train_labels, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T04:56:38.864963Z","iopub.execute_input":"2021-10-30T04:56:38.865265Z","iopub.status.idle":"2021-10-30T04:58:00.071046Z","shell.execute_reply.started":"2021-10-30T04:56:38.865235Z","shell.execute_reply":"2021-10-30T04:58:00.070092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = []\nDATADIR = '../input/nnfl-2021-assignment-1/fire_videos/test'\ntest_data = pd.read_csv(\"../input/nnfl-2021-assignment-1/fire_videos/test.csv\")\nfor i in range(len(test_data.File)):\n    imagepath = os.path.join(DATADIR,test_data.File[i])\n    image = cv2.imread(imagepath)\n    image = cv2.resize(image,(128,128))\n    test_images.append(image)\ntest_images = np.array(test_images, dtype=\"float\")/255.0","metadata":{"execution":{"iopub.status.busy":"2021-10-30T04:58:16.993836Z","iopub.execute_input":"2021-10-30T04:58:16.994377Z","iopub.status.idle":"2021-10-30T04:58:55.956029Z","shell.execute_reply.started":"2021-10-30T04:58:16.994341Z","shell.execute_reply":"2021-10-30T04:58:55.954794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n                             tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n                             tf.keras.layers.MaxPool2D((2, 2)),\n                             tf.keras.layers.Conv2D(32, (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(64, (3,3), activation='relu'),\n                             tf.keras.layers.MaxPooling2D(2,2),\n                             tf.keras.layers.Flatten(),\n                             tf.keras.layers.Dense(512, activation='relu'),\n                             tf.keras.layers.Dense(2, activation='sigmoid')\n])\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:01:36.783675Z","iopub.execute_input":"2021-10-30T05:01:36.783961Z","iopub.status.idle":"2021-10-30T05:01:36.933048Z","shell.execute_reply.started":"2021-10-30T05:01:36.783928Z","shell.execute_reply":"2021-10-30T05:01:36.931758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=10, batch_size=256, validation_data=(x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:01:43.668572Z","iopub.execute_input":"2021-10-30T05:01:43.668828Z","iopub.status.idle":"2021-10-30T05:10:29.213962Z","shell.execute_reply.started":"2021-10-30T05:01:43.668800Z","shell.execute_reply":"2021-10-30T05:10:29.213052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels=[]\ntest_labels = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:10:44.759498Z","iopub.execute_input":"2021-10-30T05:10:44.760041Z","iopub.status.idle":"2021-10-30T05:10:52.671903Z","shell.execute_reply.started":"2021-10-30T05:10:44.759981Z","shell.execute_reply":"2021-10-30T05:10:52.671252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_prediction = []\nfor i in range(len(test_labels)):\n    class_prediction.append(np.argmax(test_labels[i]))","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:12:07.092139Z","iopub.execute_input":"2021-10-30T05:12:07.092443Z","iopub.status.idle":"2021-10-30T05:12:07.111677Z","shell.execute_reply.started":"2021-10-30T05:12:07.092406Z","shell.execute_reply":"2021-10-30T05:12:07.110352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []\ncount = 0\nfor i in class_prediction :\n    if i == 1:\n        pred.append('not_fire')\n        count = count + 1\n    else:\n        pred.append('fire')\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:12:09.958270Z","iopub.execute_input":"2021-10-30T05:12:09.958541Z","iopub.status.idle":"2021-10-30T05:12:09.968052Z","shell.execute_reply.started":"2021-10-30T05:12:09.958512Z","shell.execute_reply":"2021-10-30T05:12:09.967074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict = {'File': test_data.File, 'Label': pred} \n     \ntest_file = pd.DataFrame(dict)\n  \n# saving the dataframe\ntest_file.to_csv('./submfile16.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T05:12:48.299416Z","iopub.execute_input":"2021-10-30T05:12:48.299691Z","iopub.status.idle":"2021-10-30T05:12:48.315037Z","shell.execute_reply.started":"2021-10-30T05:12:48.299663Z","shell.execute_reply":"2021-10-30T05:12:48.313884Z"},"trusted":true},"execution_count":null,"outputs":[]}]}