{"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":"markdown","source":"# Problem description\n\nAccording to the CDC motor vehicle safety division, one in five car accidents is caused by a distracted driver. Sadly, this translates to 425,000 people injured and 3,000 people killed by distracted driving every year.\n\nState Farm hopes to improve these alarming statistics, and better insure their customers, by testing whether dashboard cameras can automatically detect drivers engaging in distracted behaviors. Given a dataset of 2D dashboard camera images, State Farm is challenging Kagglers to classify each driver's behavior. Are they driving attentively, wearing their seatbelt, or taking a selfie with their friends in the backseat?","metadata":{}},{"cell_type":"code","source":"import os, shutil\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport random\n\nimport tensorflow as tf\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:50:56.702957Z","iopub.execute_input":"2022-12-16T11:50:56.703339Z","iopub.status.idle":"2022-12-16T11:50:58.347578Z","shell.execute_reply.started":"2022-12-16T11:50:56.703252Z","shell.execute_reply":"2022-12-16T11:50:58.346703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = \"../input/state-farm-distracted-driver-detection/\"\nimg_folder = os.path.join(base_dir, 'imgs/')\ntrain_imgs = os.path.join(img_folder, 'train/')\ntest_imgs = os.path.join(img_folder, 'test/')\ndriver_imgs_list = pd.read_csv(os.path.join(base_dir, 'driver_imgs_list.csv'))\nsample_sub = pd.read_csv(os.path.join(base_dir, 'sample_submission.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:04.749475Z","iopub.execute_input":"2022-12-16T11:51:04.749915Z","iopub.status.idle":"2022-12-16T11:51:04.862682Z","shell.execute_reply.started":"2022-12-16T11:51:04.749822Z","shell.execute_reply":"2022-12-16T11:51:04.861758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list.head()\ntrain_img_folder = list(\"train\" +\"/\" + driver_imgs_list['classname'] + \"/\" + driver_imgs_list['img'])\ny_true = list(driver_imgs_list['classname'])\ntest_img_folder = sorted(os.listdir(test_imgs))\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T12:54:26.707452Z","iopub.execute_input":"2022-12-16T12:54:26.707947Z","iopub.status.idle":"2022-12-16T12:54:26.771623Z","shell.execute_reply.started":"2022-12-16T12:54:26.707903Z","shell.execute_reply":"2022-12-16T12:54:26.770791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:14.159201Z","iopub.execute_input":"2022-12-16T11:51:14.159545Z","iopub.status.idle":"2022-12-16T11:51:14.169707Z","shell.execute_reply.started":"2022-12-16T11:51:14.159489Z","shell.execute_reply":"2022-12-16T11:51:14.168555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:16.976668Z","iopub.execute_input":"2022-12-16T11:51:16.977019Z","iopub.status.idle":"2022-12-16T11:51:17.00325Z","shell.execute_reply.started":"2022-12-16T11:51:16.976989Z","shell.execute_reply":"2022-12-16T11:51:17.002433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\n\nclass_def = {'c0': 'safe driving',\n'c1': 'texting - right',\n'c2': 'talking on the phone - right',\n'c3': 'texting - left',\n'c4': 'talking on the phone - left',\n'c5': 'operating the radio',\n'c6': 'drinking',\n'c7': 'reaching behind',\n'c8': 'hair and makeup',\n'c9': 'talking to passenger'}","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:19.485218Z","iopub.execute_input":"2022-12-16T11:51:19.485583Z","iopub.status.idle":"2022-12-16T11:51:19.490998Z","shell.execute_reply.started":"2022-12-16T11:51:19.485546Z","shell.execute_reply":"2022-12-16T11:51:19.489691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Display 20 sample images","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 18))\ncolumns = 5\nrows = 4\nfor i in range(1, columns*rows +1):\n    pic_idx = random.randint(0, driver_imgs_list.shape[0])\n    im = Image.open(r\"../input/state-farm-distracted-driver-detection/imgs/train/\"+ \n                    str(driver_imgs_list.loc[pic_idx, 'classname']) +'/' \n                    +str(driver_imgs_list.loc[pic_idx, 'img' ]))\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(im)\n    plt.title('State of driving: ' + class_def[(driver_imgs_list.loc[pic_idx, 'classname'])])\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:21.279104Z","iopub.execute_input":"2022-12-16T11:51:21.279434Z","iopub.status.idle":"2022-12-16T11:51:24.294215Z","shell.execute_reply.started":"2022-12-16T11:51:21.279402Z","shell.execute_reply":"2022-12-16T11:51:24.292961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# size of each image is (640, 480)\nim = Image.open(\"../input/state-farm-distracted-driver-detection/imgs/train/c7/img_100702.jpg\")\nw, h = im.size\nprint(w, h)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:24.29555Z","iopub.execute_input":"2022-12-16T11:51:24.295847Z","iopub.status.idle":"2022-12-16T11:51:24.304125Z","shell.execute_reply.started":"2022-12-16T11:51:24.295817Z","shell.execute_reply":"2022-12-16T11:51:24.302229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:24.935649Z","iopub.execute_input":"2022-12-16T11:51:24.935987Z","iopub.status.idle":"2022-12-16T11:51:24.941111Z","shell.execute_reply.started":"2022-12-16T11:51:24.935951Z","shell.execute_reply":"2022-12-16T11:51:24.939966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = (256, 256)\nval_frac = 0.12\nbatch_size = 16\ntrain_dir = \"../input/state-farm-distracted-driver-detection/imgs/train/\"\n\ntrain_generator = tf.keras.preprocessing.image_dataset_from_directory(train_dir,\n                                                             labels = 'inferred',\n                                                             label_mode='categorical',\n                                                             image_size=image_size,\n                                                             batch_size=batch_size,\n                                                             seed=1,\n                                                             shuffle=True,\n                                                             validation_split=val_frac,\n                                                             subset='training')\nval_generator = tf.keras.preprocessing.image_dataset_from_directory(train_dir,\n                                                             labels = 'inferred',\n                                                             label_mode='categorical',\n                                                             image_size=image_size,\n                                                             batch_size=batch_size,\n                                                             seed=1,\n                                                             shuffle=True,\n                                                             validation_split=val_frac,\n                                                             subset='validation')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:26.870855Z","iopub.execute_input":"2022-12-16T11:51:26.871197Z","iopub.status.idle":"2022-12-16T11:51:33.220883Z","shell.execute_reply.started":"2022-12-16T11:51:26.871164Z","shell.execute_reply":"2022-12-16T11:51:33.219955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize(image,label):\n    image = tf.cast(image/255. ,tf.float32)\n    return image,label\n\ntrain_generator = train_generator.map(normalize)\nval_generator = val_generator.map(normalize)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:33.222317Z","iopub.execute_input":"2022-12-16T11:51:33.222841Z","iopub.status.idle":"2022-12-16T11:51:33.272271Z","shell.execute_reply.started":"2022-12-16T11:51:33.222798Z","shell.execute_reply":"2022-12-16T11:51:33.271482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:33.274035Z","iopub.execute_input":"2022-12-16T11:51:33.274386Z","iopub.status.idle":"2022-12-16T11:51:33.734032Z","shell.execute_reply.started":"2022-12-16T11:51:33.274347Z","shell.execute_reply":"2022-12-16T11:51:33.732348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"callback = tf.keras.callbacks.EarlyStopping(monitor='loss', mode='min', patience=4, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T12:00:05.112353Z","iopub.execute_input":"2021-07-17T12:00:05.112925Z","iopub.status.idle":"2021-07-17T12:00:05.121843Z","shell.execute_reply.started":"2021-07-17T12:00:05.112879Z","shell.execute_reply":"2021-07-17T12:00:05.120323Z"}}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu',input_shape=(256, 256, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(256, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(512, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:33.90404Z","iopub.execute_input":"2022-12-16T11:51:33.904318Z","iopub.status.idle":"2022-12-16T11:51:34.018973Z","shell.execute_reply.started":"2022-12-16T11:51:33.904288Z","shell.execute_reply":"2022-12-16T11:51:34.018191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:37.865998Z","iopub.execute_input":"2022-12-16T11:51:37.866335Z","iopub.status.idle":"2022-12-16T11:51:37.879647Z","shell.execute_reply.started":"2022-12-16T11:51:37.866303Z","shell.execute_reply":"2022-12-16T11:51:37.87848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile('rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:42.965874Z","iopub.execute_input":"2022-12-16T11:51:42.966225Z","iopub.status.idle":"2022-12-16T11:51:42.977874Z","shell.execute_reply.started":"2022-12-16T11:51:42.966193Z","shell.execute_reply":"2022-12-16T11:51:42.976682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\ntrain_generator,\nsteps_per_epoch=240,\nepochs=10,\nvalidation_data=val_generator,\nvalidation_steps=60)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:51:44.203016Z","iopub.execute_input":"2022-12-16T11:51:44.203335Z","iopub.status.idle":"2022-12-16T11:53:32.594259Z","shell.execute_reply.started":"2022-12-16T11:51:44.203302Z","shell.execute_reply":"2022-12-16T11:53:32.593153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('distracted_driver.h5')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T11:54:45.643099Z","iopub.execute_input":"2022-12-16T11:54:45.643479Z","iopub.status.idle":"2022-12-16T11:54:45.76742Z","shell.execute_reply.started":"2022-12-16T11:54:45.643437Z","shell.execute_reply":"2022-12-16T11:54:45.766448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_loss = history.history['loss']\ntr_acc = history.history['accuracy']\nval_loss = history.history['val_loss']\nval_acc = history.history['val_accuracy']\nepochs = range(1, len(tr_loss)+1)\n\nplt.clf()\nfig, (ax1, ax2) = plt.subplots(1,2, figsize=(18,5))\nfig\n\nax1.plot(epochs, tr_loss, 'b', label='Training loss')\nax1.plot(epochs, val_loss, 'r', label='Validation loss')\nax1.set_title('Training & Validation loss')\nax1.set_xlabel('epochs')\nax1.set_ylabel('loss')\nax1.legend()\n\nax2.plot(epochs, tr_acc, 'b', label='Training acc')\nax2.plot(epochs, val_acc, 'r', label='Validation acc')\nax2.set_title('Training & Validation acc')\nax2.set_xlabel('epochs')\nax2.set_ylabel('accuracy')\nax2.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction on test/train images & submission","metadata":{}},{"cell_type":"code","source":"#change class if train then class= \"train\" else classes=['test']\n\nimg_path = \"../input/state-farm-distracted-driver-detection/imgs/\" \ntest_generator = test_datagen.flow_from_directory( directory=img_path, target_size=image_size, color_mode=\"rgb\", batch_size=1, class_mode='categorical', shuffle=False, classes=['test'] )","metadata":{"execution":{"iopub.status.busy":"2022-12-16T13:19:36.537989Z","iopub.execute_input":"2022-12-16T13:19:36.538356Z","iopub.status.idle":"2022-12-16T13:20:22.162208Z","shell.execute_reply.started":"2022-12-16T13:19:36.53832Z","shell.execute_reply":"2022-12-16T13:20:22.161232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change the no. of steps - get it from above output \npreds = model.predict(test_generator, steps=79726)\nprint(len(preds))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T13:23:19.822577Z","iopub.execute_input":"2022-12-16T13:23:19.822931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate csv for train dataset  \n\ntest_ids = sorted((train_img_folder))\ncls1= ['safe driving', 'texting - right', 'talking on the phone - right',\n                                  'texting - left', 'talking on the phone - left', 'operating the radio', \n                                  'drinking', 'reaching behind', 'hair and makeup', 'talking to passenger']\nfile_path = []\ngt_class = []\npd_class = []\npd_score = []\nfor i in range(len(test_ids)):\n    for j in range(len(cls1)+1):\n        file_path.append(test_ids[i])      \n        if j == 0:\n            gt_class.append(class_def[y_true[i]])\n            pd_class.append(\"\")\n            pd_score.append(\"\")\n\n        else:\n            gt_class.append(\"\")\n            pd_class.append(cls1[j-1])\n            pd_score.append(preds[i][j-1])\n\n\npred_df = pd.DataFrame(list(zip(file_path , gt_class,pd_class, pd_score)),columns = ['file_path','gt_class','pd_class','pd_score'])\n\n# get your desired output\nprint(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T12:44:18.749609Z","iopub.execute_input":"2022-12-16T12:44:18.749956Z","iopub.status.idle":"2022-12-16T12:44:19.107535Z","shell.execute_reply.started":"2022-12-16T12:44:18.749925Z","shell.execute_reply":"2022-12-16T12:44:19.106446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# final trained csv file - predictions_train_new.csv\n\npred_df.to_csv('predictions_train_new.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T12:44:28.235964Z","iopub.execute_input":"2022-12-16T12:44:28.236365Z","iopub.status.idle":"2022-12-16T12:44:28.781806Z","shell.execute_reply.started":"2022-12-16T12:44:28.236319Z","shell.execute_reply":"2022-12-16T12:44:28.780914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generate csv file for test data,\n# in current dataset there is no ground truth class for test data\n# taking dummy gt_class, ground_truth class = max_score(all prediction class)\n\ndata = pd.read_csv('/kaggle/working/predictions1.csv')\ntest_ids = sorted((test_img_folder))\ncls1= ['safe driving', 'texting - right', 'talking on the phone - right',\n                                  'texting - left', 'talking on the phone - left', 'operating the radio', \n                                  'drinking', 'reaching behind', 'hair and makeup', 'talking to passenger']\nfile_path = []\ngt_class = []\npd_class = []\npd_score = []\nfor i in range(len(test_ids)):\n    c1 = np.max(preds[i],axis=0)\n    result = np.where(c1==preds[i])[0][0]\n    for j in range(len(cls1)+1):\n        file_path.append(test_ids[i]) \n        if j == 0:\n            gt_class.append(cls1[result])\n            pd_class.append(\"\")\n            pd_score.append(\"\")\n\n        else:\n            gt_class.append(\"\")\n            pd_class.append(cls1[j-1])\n            pd_score.append(preds[i][j-1])\n\n\npred_df = pd.DataFrame(list(zip(file_path , gt_class,pd_class, pd_score)),columns = ['file_path','gt_class','pd_class','pd_score'])\n\n# get your desired output\npred_df.to_csv('predictions_test_new.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T13:48:07.557995Z","iopub.execute_input":"2022-12-16T13:48:07.558321Z","iopub.status.idle":"2022-12-16T13:48:11.554575Z","shell.execute_reply.started":"2022-12-16T13:48:07.55829Z","shell.execute_reply":"2022-12-16T13:48:11.552326Z"},"trusted":true},"execution_count":null,"outputs":[]}]}