{"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":"# Making our CNN Model\nfrom keras.layers import MaxPooling2D,Flatten,Dense\nfrom keras.models import Sequential\n\nfrom keras.utils import generic_utils\nfrom keras.layers import Dense,MaxPooling2D,Conv2D,Flatten,GlobalAveragePooling2D","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-30T18:14:45.934515Z","iopub.execute_input":"2023-03-30T18:14:45.934857Z","iopub.status.idle":"2023-03-30T18:14:54.477771Z","shell.execute_reply.started":"2023-03-30T18:14:45.934825Z","shell.execute_reply":"2023-03-30T18:14:54.475625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import models\nimport keras \nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:16:11.097040Z","iopub.execute_input":"2023-03-30T18:16:11.097414Z","iopub.status.idle":"2023-03-30T18:16:11.104775Z","shell.execute_reply.started":"2023-03-30T18:16:11.097380Z","shell.execute_reply":"2023-03-30T18:16:11.103728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label File\npd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:16:15.922651Z","iopub.execute_input":"2023-03-30T18:16:15.923385Z","iopub.status.idle":"2023-03-30T18:16:15.970748Z","shell.execute_reply.started":"2023-03-30T18:16:15.923344Z","shell.execute_reply":"2023-03-30T18:16:15.969727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading ImageDataGenrator\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:16:27.412590Z","iopub.execute_input":"2023-03-30T18:16:27.413319Z","iopub.status.idle":"2023-03-30T18:16:27.418888Z","shell.execute_reply.started":"2023-03-30T18:16:27.413279Z","shell.execute_reply":"2023-03-30T18:16:27.417308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making an object of ImageData Genrator\nimageGen = ImageDataGenerator(rescale=1.0/255)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:16:35.103171Z","iopub.execute_input":"2023-03-30T18:16:35.103555Z","iopub.status.idle":"2023-03-30T18:16:35.108614Z","shell.execute_reply.started":"2023-03-30T18:16:35.103519Z","shell.execute_reply":"2023-03-30T18:16:35.107363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting Training Genrator \ntrain_gen=imageGen.flow_from_directory(\n    '../input/state-farm-distracted-driver-detection/imgs/train',\n    batch_size=32,\n    target_size=(100,100),\n    class_mode = 'categorical',\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:17:01.113164Z","iopub.execute_input":"2023-03-30T18:17:01.114004Z","iopub.status.idle":"2023-03-30T18:17:15.837235Z","shell.execute_reply.started":"2023-03-30T18:17:01.113962Z","shell.execute_reply":"2023-03-30T18:17:15.836128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making our CNN Model\nfrom keras.layers import MaxPooling2D,Flatten,Dense\nfrom keras.models import Sequential\n\nfrom keras.utils import generic_utils\nfrom keras.layers import Dense,MaxPooling2D,Conv2D,Flatten,GlobalAveragePooling2D","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:17:15.841115Z","iopub.execute_input":"2023-03-30T18:17:15.841505Z","iopub.status.idle":"2023-03-30T18:17:15.851566Z","shell.execute_reply.started":"2023-03-30T18:17:15.841471Z","shell.execute_reply":"2023-03-30T18:17:15.850282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import models","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:17:17.162841Z","iopub.execute_input":"2023-03-30T18:17:17.163213Z","iopub.status.idle":"2023-03-30T18:17:17.168310Z","shell.execute_reply.started":"2023-03-30T18:17:17.163179Z","shell.execute_reply":"2023-03-30T18:17:17.167130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32,(3,3),activation=\"relu\",input_shape =(100,100,3)),\n    MaxPooling2D(2,2),\n    Conv2D(64,(3,3),activation=\"relu\"),\n    MaxPooling2D(2,2),\n    Flatten(),\n    Dense(1024,activation='relu'),\n    Dense(512,activation='relu'),\n    Dense(10,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:17:34.139269Z","iopub.execute_input":"2023-03-30T18:17:34.139969Z","iopub.status.idle":"2023-03-30T18:17:34.204934Z","shell.execute_reply.started":"2023-03-30T18:17:34.139928Z","shell.execute_reply":"2023-03-30T18:17:34.203954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']  )\n","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:18:04.825697Z","iopub.execute_input":"2023-03-30T18:18:04.826794Z","iopub.status.idle":"2023-03-30T18:18:04.847498Z","shell.execute_reply.started":"2023-03-30T18:18:04.826750Z","shell.execute_reply":"2023-03-30T18:18:04.846405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:18:07.208313Z","iopub.execute_input":"2023-03-30T18:18:07.208708Z","iopub.status.idle":"2023-03-30T18:18:07.233192Z","shell.execute_reply.started":"2023-03-30T18:18:07.208649Z","shell.execute_reply":"2023-03-30T18:18:07.232403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting our model\nmodel.fit(train_gen,epochs=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:18:14.940750Z","iopub.execute_input":"2023-03-30T18:18:14.941902Z","iopub.status.idle":"2023-03-30T18:22:38.317793Z","shell.execute_reply.started":"2023-03-30T18:18:14.941846Z","shell.execute_reply":"2023-03-30T18:22:38.316698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making an object of ImageData Genrator\nimageGen = ImageDataGenerator(rescale=1.0/255)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:22:38.319892Z","iopub.execute_input":"2023-03-30T18:22:38.320546Z","iopub.status.idle":"2023-03-30T18:22:38.325598Z","shell.execute_reply.started":"2023-03-30T18:22:38.320506Z","shell.execute_reply":"2023-03-30T18:22:38.324430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making a test directory in output folder ( that can be readable and writeable )\nimport os\nos.mkdir('/kaggle/working/test1')\nos.mkdir('/kaggle/working/test1/all_classes')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:27:44.986541Z","iopub.execute_input":"2023-03-30T18:27:44.987230Z","iopub.status.idle":"2023-03-30T18:27:44.992527Z","shell.execute_reply.started":"2023-03-30T18:27:44.987191Z","shell.execute_reply":"2023-03-30T18:27:44.991029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing copyfile so copy the testing images from the input test folder to ouput test folder\nfrom shutil import copyfile","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:27:50.303727Z","iopub.execute_input":"2023-03-30T18:27:50.304431Z","iopub.status.idle":"2023-03-30T18:27:50.309041Z","shell.execute_reply.started":"2023-03-30T18:27:50.304389Z","shell.execute_reply":"2023-03-30T18:27:50.307992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copying from the input test folder to ouput test folder\nd = pd.read_csv('../input/state-farm-distracted-driver-detection/sample_submission.csv')\nfor row in d.values:\n    file_name = row[0]\n    copyfile( '../input/state-farm-distracted-driver-detection/imgs/test/'+file_name , './test1/all_classes/'+file_name )","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:28:46.367597Z","iopub.execute_input":"2023-03-30T18:28:46.368324Z","iopub.status.idle":"2023-03-30T18:38:59.233289Z","shell.execute_reply.started":"2023-03-30T18:28:46.368286Z","shell.execute_reply":"2023-03-30T18:38:59.232156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting Testing Genrator \ntest_gen=imageGen.flow_from_directory(\n    '/kaggle/working/test1',\n    class_mode=None,\n    shuffle=False,\n    target_size=(100,100),\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:39:46.891994Z","iopub.execute_input":"2023-03-30T18:39:46.892924Z","iopub.status.idle":"2023-03-30T18:39:48.263703Z","shell.execute_reply.started":"2023-03-30T18:39:46.892868Z","shell.execute_reply":"2023-03-30T18:39:48.262708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting from test Genrator \ntest_gen.reset()\npredictions = model.predict_generator(test_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:39:52.982985Z","iopub.execute_input":"2023-03-30T18:39:52.983548Z","iopub.status.idle":"2023-03-30T18:45:50.889069Z","shell.execute_reply.started":"2023-03-30T18:39:52.983498Z","shell.execute_reply":"2023-03-30T18:45:50.887989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the shape of prediction\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:46:05.250114Z","iopub.execute_input":"2023-03-30T18:46:05.250484Z","iopub.status.idle":"2023-03-30T18:46:05.258028Z","shell.execute_reply.started":"2023-03-30T18:46:05.250450Z","shell.execute_reply":"2023-03-30T18:46:05.256695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting the Prediction same as sample submission dataFrame\n# Making the dictionary of The Predcition\nd1 = pd.read_csv('/kaggle/input/state-farm-distracted-driver-detection/sample_submission.csv')\nd ={\n    'img':list(d1.values[:,0]),\n}\nfor i in range(0,10):\n    d['c' + str(i)] = list(predictions[:,i])\nd","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:46:16.571595Z","iopub.execute_input":"2023-03-30T18:46:16.572336Z","iopub.status.idle":"2023-03-30T18:46:16.962614Z","shell.execute_reply.started":"2023-03-30T18:46:16.572294Z","shell.execute_reply":"2023-03-30T18:46:16.961583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting the Prediction to DataFrame\ndf = pd.DataFrame(d)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:46:33.175982Z","iopub.execute_input":"2023-03-30T18:46:33.176578Z","iopub.status.idle":"2023-03-30T18:46:33.340545Z","shell.execute_reply.started":"2023-03-30T18:46:33.176538Z","shell.execute_reply":"2023-03-30T18:46:33.339566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting DataFrame into CSV File\ndf.to_csv('/kaggle/working/Prediction.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:46:57.926737Z","iopub.execute_input":"2023-03-30T18:46:57.927777Z","iopub.status.idle":"2023-03-30T18:46:58.662269Z","shell.execute_reply.started":"2023-03-30T18:46:57.927736Z","shell.execute_reply":"2023-03-30T18:46:58.661185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting Classes from Predictions\npredicted_class_indices=np.argmax(predictions,axis=1)\nlabels = (train_gen.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions_in_class = [labels[k] for k in predicted_class_indices]","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:47:01.305185Z","iopub.execute_input":"2023-03-30T18:47:01.305570Z","iopub.status.idle":"2023-03-30T18:47:01.334446Z","shell.execute_reply.started":"2023-03-30T18:47:01.305533Z","shell.execute_reply":"2023-03-30T18:47:01.333392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_gen.filenames\nfile=[]\nfor f in filenames:\n    file.append(f.split('/')[1])\nresults=pd.DataFrame({\"Filename\":file ,\n                      \"Predictions\":predictions_in_class})\nresults","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:47:19.964162Z","iopub.execute_input":"2023-03-30T18:47:19.965121Z","iopub.status.idle":"2023-03-30T18:47:20.015192Z","shell.execute_reply.started":"2023-03-30T18:47:19.965065Z","shell.execute_reply":"2023-03-30T18:47:20.014075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Image of Testing Data\nfrom IPython.display import Image\nImage(filename='/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_100064.jpg') ","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:51:38.538755Z","iopub.execute_input":"2023-03-30T18:51:38.539127Z","iopub.status.idle":"2023-03-30T18:51:38.552859Z","shell.execute_reply.started":"2023-03-30T18:51:38.539094Z","shell.execute_reply":"2023-03-30T18:51:38.551790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting the class of Image\nresults.loc[results['Filename'] == 'img_100064.jpg']\n# c1 => texting","metadata":{"execution":{"iopub.status.busy":"2023-03-30T18:51:45.847799Z","iopub.execute_input":"2023-03-30T18:51:45.848940Z","iopub.status.idle":"2023-03-30T18:51:45.865347Z","shell.execute_reply.started":"2023-03-30T18:51:45.848884Z","shell.execute_reply":"2023-03-30T18:51:45.864340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Image of Testing Data\nfrom IPython.display import Image\nImage(filename='../input/state-farm-distracted-driver-detection/imgs/test/img_100068.jpg') ","metadata":{"execution":{"iopub.status.busy":"2023-03-30T19:01:25.529349Z","iopub.execute_input":"2023-03-30T19:01:25.530264Z","iopub.status.idle":"2023-03-30T19:01:25.539171Z","shell.execute_reply.started":"2023-03-30T19:01:25.530225Z","shell.execute_reply":"2023-03-30T19:01:25.538088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting the class of Image\nresults.loc[results['Filename'] == 'img_100068.jpg']\n# c5 => operating the radio","metadata":{"execution":{"iopub.status.busy":"2023-03-30T19:01:34.649272Z","iopub.execute_input":"2023-03-30T19:01:34.649642Z","iopub.status.idle":"2023-03-30T19:01:34.665641Z","shell.execute_reply.started":"2023-03-30T19:01:34.649607Z","shell.execute_reply":"2023-03-30T19:01:34.664565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Image of Testing Data\nfrom IPython.display import Image\nImage(filename='../input/state-farm-distracted-driver-detection/imgs/test/img_25.jpg') ","metadata":{"execution":{"iopub.status.busy":"2023-03-30T19:02:12.356558Z","iopub.execute_input":"2023-03-30T19:02:12.357259Z","iopub.status.idle":"2023-03-30T19:02:12.367209Z","shell.execute_reply.started":"2023-03-30T19:02:12.357222Z","shell.execute_reply":"2023-03-30T19:02:12.365971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting the class of Image\nresults.loc[results['Filename'] == 'img_25.jpg']\n# c5 => operating the radio","metadata":{"execution":{"iopub.status.busy":"2023-03-30T19:02:20.171625Z","iopub.execute_input":"2023-03-30T19:02:20.172138Z","iopub.status.idle":"2023-03-30T19:02:20.189088Z","shell.execute_reply.started":"2023-03-30T19:02:20.172090Z","shell.execute_reply":"2023-03-30T19:02:20.187708Z"},"trusted":true},"execution_count":null,"outputs":[]}]}