{"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":"# **Distracted Driver Detection**\n\n*Problem statement*: Given the dataset consisting of driver images in car and corresponding labels for 10 nos. categories (e.g. safe driving, texting, talking etc.), your task is to build a classification model to predict the category for that image.\n\nI have used a pretrained model and tried to implement my code with the help of EfficientNets.","metadata":{}},{"cell_type":"markdown","source":"**Importing Required Libraries**","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras import Model\nfrom keras.layers import Input, GlobalAveragePooling2D, BatchNormalization, Dropout, Dense\nfrom tensorflow.keras.applications import EfficientNetB3\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:19.545392Z","iopub.execute_input":"2021-09-08T14:46:19.545865Z","iopub.status.idle":"2021-09-08T14:46:25.16399Z","shell.execute_reply.started":"2021-09-08T14:46:19.545738Z","shell.execute_reply":"2021-09-08T14:46:25.162853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ImageDataGenerator class is used to ease out our working with the images.\n* EfficientNet has been imported to increase our accuracy.\n* EarlyStopping makes sure that we don't unnecessarily train our model further.\n* Few filters are added from Keras too.","metadata":{}},{"cell_type":"markdown","source":"**Making Directories**","metadata":{}},{"cell_type":"code","source":"base_dir = '../input/state-farm-distracted-driver-detection'\ntrain_dir = os.path.join(base_dir, 'imgs/train/')\ntest_dir = os.path.join(base_dir, 'imgs/test/')\ndata = pd.read_csv(os.path.join(base_dir, 'driver_imgs_list.csv'))","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:25.16603Z","iopub.execute_input":"2021-09-08T14:46:25.166457Z","iopub.status.idle":"2021-09-08T14:46:25.207473Z","shell.execute_reply.started":"2021-09-08T14:46:25.166399Z","shell.execute_reply":"2021-09-08T14:46:25.206326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reviewing Dataset**","metadata":{}},{"cell_type":"code","source":"data.head()\nclass_count = data.classname.value_counts()\nfig = class_count.plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:25.210048Z","iopub.execute_input":"2021-09-08T14:46:25.210484Z","iopub.status.idle":"2021-09-08T14:46:25.475348Z","shell.execute_reply.started":"2021-09-08T14:46:25.210446Z","shell.execute_reply":"2021-09-08T14:46:25.473827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Working With ImageDataGenerator Class**","metadata":{}},{"cell_type":"code","source":"IMAGE_SIZE = (224,224)\nBATCH_SIZE = 32\n\ntrain_gen = ImageDataGenerator(\n    width_shift_range=0.3,\n    height_shift_range=0.3,\n    shear_range=0.3,\n    zoom_range=0.4,\n    validation_split=0.2)\n\ntest_gen = ImageDataGenerator()\n\ntrain_data= train_gen.flow_from_directory(\n    train_dir,\n    target_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE,\n    seed=42,\n    subset='training'\n)\n\nval_data = train_gen.flow_from_directory(\n    train_dir,\n    target_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE,\n    seed=42,\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:25.478007Z","iopub.execute_input":"2021-09-08T14:46:25.478496Z","iopub.status.idle":"2021-09-08T14:46:39.479079Z","shell.execute_reply.started":"2021-09-08T14:46:25.47845Z","shell.execute_reply":"2021-09-08T14:46:39.476836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Making The Model**","metadata":{}},{"cell_type":"markdown","source":"Using EfficientNets to make our model","metadata":{}},{"cell_type":"code","source":"def define_model(num_classes):\n    inputs = Input(shape=(224,224,3))\n    base_model = EfficientNetB3(include_top=False, weights='imagenet')(inputs)\n    x = GlobalAveragePooling2D()(base_model)\n    x = BatchNormalization()(x)\n    x = Dropout(0.2)(x)\n    output = Dense(units=num_classes, activation='softmax')(x)\n    \n    model = Model(inputs=inputs, outputs=output)\n    model.compile(optimizer=tf.optimizers.Adam(learning_rate=1e-4), \n                  loss='categorical_crossentropy',\n                 metrics=['accuracy'])\n    return model\n\nmodel = define_model(10)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:39.480931Z","iopub.execute_input":"2021-09-08T14:46:39.481395Z","iopub.status.idle":"2021-09-08T14:46:48.116733Z","shell.execute_reply.started":"2021-09-08T14:46:39.481337Z","shell.execute_reply":"2021-09-08T14:46:48.115705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Our Model**","metadata":{}},{"cell_type":"code","source":"checkpoint_callback = ModelCheckpoint('best_model.hdf5', save_best_only=True, monitor='val_loss', mode='min')\nes = EarlyStopping(monitor='val_loss', patience=5)\nhistory = model.fit(train_data, epochs=20, validation_data=val_data, callbacks=[es, checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2021-09-08T14:46:48.119732Z","iopub.execute_input":"2021-09-08T14:46:48.120085Z","iopub.status.idle":"2021-09-08T17:24:13.338439Z","shell.execute_reply.started":"2021-09-08T14:46:48.120056Z","shell.execute_reply":"2021-09-08T17:24:13.337214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Evaluating Our Model**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,5))\nplt.subplot(1,2,1)\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Loss')\nplt.xlabel('epoch')\nplt.ylabel('loss')\nplt.legend(['train', 'val'])\n\nplt.subplot(1,2,2)\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Accuracy')\nplt.xlabel('epoch')\nplt.ylabel('acc')\nplt.legend(['train', 'val'])","metadata":{"execution":{"iopub.status.busy":"2021-09-08T17:24:13.341906Z","iopub.execute_input":"2021-09-08T17:24:13.342231Z","iopub.status.idle":"2021-09-08T17:24:13.756574Z","shell.execute_reply.started":"2021-09-08T17:24:13.342203Z","shell.execute_reply":"2021-09-08T17:24:13.755545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Predictions**","metadata":{}},{"cell_type":"code","source":"test_dir = os.path.join(base_dir, 'imgs')\n\ntest_data = test_gen.flow_from_directory(\n    test_dir,\n    shuffle=False,\n    target_size=IMAGE_SIZE,\n    classes=['test'],\n    batch_size=BATCH_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T17:24:13.758118Z","iopub.execute_input":"2021-09-08T17:24:13.758693Z","iopub.status.idle":"2021-09-08T17:26:56.254325Z","shell.execute_reply.started":"2021-09-08T17:24:13.758654Z","shell.execute_reply":"2021-09-08T17:26:56.252883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T17:26:56.257792Z","iopub.execute_input":"2021-09-08T17:26:56.258226Z","iopub.status.idle":"2021-09-08T17:42:58.62429Z","shell.execute_reply.started":"2021-09-08T17:26:56.258187Z","shell.execute_reply":"2021-09-08T17:42:58.623097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = os.path.join(base_dir, \"imgs/test\")\n\ntest_ids = sorted(os.listdir(test_imgs))\npred_df = pd.DataFrame(columns = ['img','c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])\nfor i in range(len(preds)):\n    pred_df.loc[i, 'img'] = test_ids[i]\n    pred_df.loc[i, 'c0':'c9'] = preds[i]\n    \npred_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-08T17:42:58.628714Z","iopub.execute_input":"2021-09-08T17:42:58.629033Z","iopub.status.idle":"2021-09-08T17:54:06.805007Z","shell.execute_reply.started":"2021-09-08T17:42:58.629006Z","shell.execute_reply":"2021-09-08T17:54:06.803481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Completed**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}