{"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":"\n# importing all libraries \nimport numpy as np    \nimport pandas as pd\nimport os \nimport cv2 as cv\nfrom tqdm.notebook import tqdm\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Rescaling,Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-05-20T05:59:44.548847Z","iopub.execute_input":"2023-05-20T05:59:44.549255Z","iopub.status.idle":"2023-05-20T05:59:54.025492Z","shell.execute_reply.started":"2023-05-20T05:59:44.549217Z","shell.execute_reply":"2023-05-20T05:59:54.024102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting data into train data from image generator\ntrain_data = tf.keras.utils.image_dataset_from_directory(\n  '/kaggle/input/state-farm-distracted-driver-detection/imgs/train',\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(100, 100),\n  batch_size=128,label_mode='categorical',)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T05:59:54.028043Z","iopub.execute_input":"2023-05-20T05:59:54.030062Z","iopub.status.idle":"2023-05-20T06:00:16.336390Z","shell.execute_reply.started":"2023-05-20T05:59:54.030016Z","shell.execute_reply":"2023-05-20T06:00:16.335309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting data into validation data from image generator\nval_data = tf.keras.utils.image_dataset_from_directory(\n  '/kaggle/input/state-farm-distracted-driver-detection/imgs/train',\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(100, 100),\n  batch_size=128,label_mode='categorical',)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:00:16.337790Z","iopub.execute_input":"2023-05-20T06:00:16.338201Z","iopub.status.idle":"2023-05-20T06:00:22.310217Z","shell.execute_reply.started":"2023-05-20T06:00:16.338157Z","shell.execute_reply":"2023-05-20T06:00:22.309212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# showing some of the images\nclasses = train_data.class_names\nimport matplotlib.pyplot as plt\nplt.figure(figsize=(10,10))\nfor images,labels in train_data.take(1):\n    labels = labels.numpy()\n    for i in range(25):\n        ax = plt.subplot(5, 5, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(classes[labels[i].argmax()])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:00:22.312737Z","iopub.execute_input":"2023-05-20T06:00:22.313054Z","iopub.status.idle":"2023-05-20T06:00:28.073685Z","shell.execute_reply.started":"2023-05-20T06:00:22.313014Z","shell.execute_reply":"2023-05-20T06:00:28.069767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating our model\nmodel = tf.keras.models.Sequential([\n    Rescaling(scale = 1/255,input_shape=(100,100,3)),\n    Conv2D(32,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    Dropout(0.1),\n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    Conv2D(32,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    Dropout(0.1),\n    Flatten(),\n    Dense(1024,activation='relu'),\n    Dropout(0.1),\n    Dense(512,activation='relu'),\n    \n    Dense(256,activation='relu'),\n    Dropout(0.1),\n    Dense(10,activation='softmax'),\n])\n\n# compiling our model\nmodel.compile(optimizer = Adam(lr=0.01),loss = 'categorical_crossentropy',metrics=['acc'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:00:28.074738Z","iopub.execute_input":"2023-05-20T06:00:28.075064Z","iopub.status.idle":"2023-05-20T06:00:28.311876Z","shell.execute_reply.started":"2023-05-20T06:00:28.075030Z","shell.execute_reply":"2023-05-20T06:00:28.311029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# early stopping to stop overfitting\nes = EarlyStopping(monitor='val_acc',min_delta=0.01,patience=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:00:28.312948Z","iopub.execute_input":"2023-05-20T06:00:28.313307Z","iopub.status.idle":"2023-05-20T06:00:28.320642Z","shell.execute_reply.started":"2023-05-20T06:00:28.313268Z","shell.execute_reply":"2023-05-20T06:00:28.319688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting the model\nhistory = model.fit(train_data,epochs=10,validation_data=val_data,callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:00:28.322211Z","iopub.execute_input":"2023-05-20T06:00:28.322651Z","iopub.status.idle":"2023-05-20T06:03:50.746190Z","shell.execute_reply.started":"2023-05-20T06:00:28.322611Z","shell.execute_reply":"2023-05-20T06:03:50.745003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualizing accuracy and losses\nacc = history.history['acc']\nval_acc = history.history['val_acc']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = history.epoch\n\nplt.figure(figsize=(10,10))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:03:50.748589Z","iopub.execute_input":"2023-05-20T06:03:50.749450Z","iopub.status.idle":"2023-05-20T06:03:51.164424Z","shell.execute_reply.started":"2023-05-20T06:03:50.749382Z","shell.execute_reply":"2023-05-20T06:03:51.163325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting training data from image generator for prediction\ntest_data = image_dataset_from_directory(\n    '/kaggle/input/state-farm-distracted-driver-detection/imgs/test',\n    batch_size = 128,\n    image_size=(100,100),\n    labels = None,\n    label_mode=None,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:04:31.671073Z","iopub.execute_input":"2023-05-20T06:04:31.671611Z","iopub.status.idle":"2023-05-20T06:08:05.056127Z","shell.execute_reply.started":"2023-05-20T06:04:31.671569Z","shell.execute_reply":"2023-05-20T06:08:05.055047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function for predicting images\ndef predict_image(path):\n    img = tf.keras.utils.load_img(path).resize((100,100))\n    img = np.array(img).reshape((1,100,100,3))\n    y = model.predict(img,verbose=False)\n    return y\n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-19T19:27:31.846942Z","iopub.execute_input":"2023-05-19T19:27:31.848159Z","iopub.status.idle":"2023-05-19T19:27:31.855202Z","shell.execute_reply.started":"2023-05-19T19:27:31.848105Z","shell.execute_reply":"2023-05-19T19:27:31.854007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting some test images\ntest_path = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test'\nplt.figure(figsize=(10,10))\ni=1\nfor img_path in os.listdir(test_path)[:25]:\n    img_path = os.path.join(test_path,img_path)\n    img = tf.keras.utils.load_img(img_path)\n    ax = plt.subplot(5, 5, i)\n    plt.imshow(img)\n    plt.title('c'+str(predict_image(img_path).argmax()))\n    plt.axis(\"off\")\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2023-05-19T19:28:45.003914Z","iopub.execute_input":"2023-05-19T19:28:45.004331Z","iopub.status.idle":"2023-05-19T19:28:49.518636Z","shell.execute_reply.started":"2023-05-19T19:28:45.004294Z","shell.execute_reply":"2023-05-19T19:28:49.517710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# also use for predicting test data takes more time\n# y = np.zeros((79726,10))\n# test_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test'\n# count = 0\n# for i in tqdm(os.listdir(test_dir)):\n#     path = os.path.join(test_dir,i)\n#     y[count] = predict_image(path)\n#     count += 1\n","metadata":{"execution":{"iopub.status.busy":"2023-05-19T19:31:11.374966Z","iopub.execute_input":"2023-05-19T19:31:11.375962Z","iopub.status.idle":"2023-05-19T19:31:28.166505Z","shell.execute_reply.started":"2023-05-19T19:31:11.375918Z","shell.execute_reply":"2023-05-19T19:31:28.164819Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicting test data given\ny = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:34:24.472266Z","iopub.execute_input":"2023-05-20T06:34:24.473040Z","iopub.status.idle":"2023-05-20T06:41:14.927731Z","shell.execute_reply.started":"2023-05-20T06:34:24.472998Z","shell.execute_reply":"2023-05-20T06:41:14.926502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:43:52.759625Z","iopub.execute_input":"2023-05-20T06:43:52.760432Z","iopub.status.idle":"2023-05-20T06:43:52.768122Z","shell.execute_reply.started":"2023-05-20T06:43:52.760386Z","shell.execute_reply":"2023-05-20T06:43:52.766893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# exporting data as given\ndf = pd.DataFrame(y)\ndf.columns = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9']\nfilepath = [i.split('/')[-1] for i in test_data.file_paths]\ndf1 = pd.DataFrame(filepath)\ndf1.columns = ['img']\ndf = df1.join(df)\ndf.to_csv('/kaggle/working/output.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:44:28.198314Z","iopub.execute_input":"2023-05-20T06:44:28.199285Z","iopub.status.idle":"2023-05-20T06:44:29.412279Z","shell.execute_reply.started":"2023-05-20T06:44:28.199238Z","shell.execute_reply":"2023-05-20T06:44:29.411088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-20T06:44:39.184434Z","iopub.execute_input":"2023-05-20T06:44:39.185670Z","iopub.status.idle":"2023-05-20T06:44:39.211558Z","shell.execute_reply.started":"2023-05-20T06:44:39.185612Z","shell.execute_reply":"2023-05-20T06:44:39.210416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}