{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nimport pandas as pd\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\nfrom keras import Model\nfrom keras.layers import Input, GlobalAveragePooling2D, BatchNormalization, Dropout, Dense\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\n\n\n# import cv2\n# import tensorflow as tf\n# import keras\n# import scipy\n# from PIL import Image\n# # import imutils as imu\n# import glob\n# import matplotlib.pyplot as plt","metadata":{"_uuid":"36b361f1-a7b7-49f0-8cb0-0a945d5e3b04","_cell_guid":"04b977b8-d09a-4656-ac00-d0a1a7bf2802","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:30.505348Z","iopub.execute_input":"2023-01-18T00:44:30.505922Z","iopub.status.idle":"2023-01-18T00:44:30.517065Z","shell.execute_reply.started":"2023-01-18T00:44:30.505859Z","shell.execute_reply":"2023-01-18T00:44:30.515370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualizing & Preparing Data**","metadata":{"_uuid":"e2eb1d2b-e54b-4b2c-b682-37176c047171","_cell_guid":"463a98e7-ef23-4e4c-a931-ea7c1b0bd5b7","trusted":true}},{"cell_type":"code","source":"## Choose Dataset\n\nimport os;\n# os.listdir('../input/state-farm-distracted-driver-detection')\n\n# dir = \"../input/state-farm-distracted-driver-detection\"\n# train_dir = os.path.join(dir , 'imgs/train/')\n# DrvImgList = pd.read_csv(os.path.join(dir , 'driver_imgs_list.csv'))\n\n\ndir = \"../input/statefarmdatasettrainingsplitv2\"\ntrain_dir = os.path.join(dir , 'imgs/train/')\nDrvImgList = pd.read_csv(os.path.join(dir , 'driver_imgs_list.csv'))","metadata":{"_uuid":"c107ce3c-081f-4fdb-b8af-5ef3d0603599","_cell_guid":"2fd3a414-618a-4374-8bab-50f96084807a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:30.529500Z","iopub.execute_input":"2023-01-18T00:44:30.529927Z","iopub.status.idle":"2023-01-18T00:44:30.556063Z","shell.execute_reply.started":"2023-01-18T00:44:30.529880Z","shell.execute_reply":"2023-01-18T00:44:30.554630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Overview of data\n\nclass_imgs = DrvImgList.classname.value_counts()\nclass_imgs.plot(kind='bar')","metadata":{"_uuid":"51dd91e7-1772-4866-a737-3d91bd1fbda8","_cell_guid":"60df67da-4e34-4501-a8ee-5a131d05188b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:30.558553Z","iopub.execute_input":"2023-01-18T00:44:30.559735Z","iopub.status.idle":"2023-01-18T00:44:30.797947Z","shell.execute_reply.started":"2023-01-18T00:44:30.559687Z","shell.execute_reply":"2023-01-18T00:44:30.797001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train & Test TensorFlow**","metadata":{"_uuid":"7043ac88-75d1-408a-a43b-08b9774ae0d8","_cell_guid":"589baf4e-a8e6-49e1-9ec1-6f271e7e116a","trusted":true}},{"cell_type":"code","source":"IMAGE_SIZE = (240,240)\nBATCH_SIZE = 32\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  train_dir,\n  validation_split=0.25,\n  subset=\"training\",\n  seed=123,\n  image_size=IMAGE_SIZE,\n  batch_size=BATCH_SIZE)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  train_dir,\n  validation_split=0.25,\n  subset=\"validation\",\n  seed=123,\n  image_size=IMAGE_SIZE,\n  batch_size=BATCH_SIZE)","metadata":{"_uuid":"1ff8a9c7-4717-4273-9510-021fb111bf7e","_cell_guid":"08fecca6-8c34-4543-90a6-0ef21a1f2f61","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:30.799325Z","iopub.execute_input":"2023-01-18T00:44:30.799865Z","iopub.status.idle":"2023-01-18T00:44:33.425418Z","shell.execute_reply.started":"2023-01-18T00:44:30.799830Z","shell.execute_reply":"2023-01-18T00:44:33.424263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pathlib\n\nclass_names=train_ds.class_names\n\nplt.figure(figsize=(15, 15))\nfor images, labels in train_ds.take(1):\n  uniq_idx = np.unique(labels,return_index=True)\n  label_idx = uniq_idx[0]\n  img_idx= uniq_idx[1]\n  for i in range(10):\n    ax = plt.subplot(4, 3, i + 1)\n    plt.imshow(images[img_idx[i]].numpy().astype(\"uint8\"))\n    plt.title(class_names[label_idx[i]])\n    plt.axis(\"off\")\n    \n# 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":{"_uuid":"df21c922-ea43-4d58-affc-31369725c81a","_cell_guid":"f35fb55d-8841-49d3-bff6-7d7a43adb184","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:33.426891Z","iopub.execute_input":"2023-01-18T00:44:33.427512Z","iopub.status.idle":"2023-01-18T00:44:35.008976Z","shell.execute_reply.started":"2023-01-18T00:44:33.427460Z","shell.execute_reply":"2023-01-18T00:44:35.008090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" \n**Model Setup**","metadata":{}},{"cell_type":"code","source":"def ml_model(num_classes):\n    inputs = Input(shape=(240,240,3))\n    base_model = EfficientNetB0(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    return model\n\nmodel = ml_model(10)\n\nmodel.compile(optimizer=tf.optimizers.Adam(learning_rate=1e-4), \n                  loss='sparse_categorical_crossentropy',\n                 metrics=['accuracy'])\nmodel.summary()","metadata":{"_uuid":"20fec630-fd18-4d7f-b258-e25c76a2db62","_cell_guid":"a5287678-b9cc-45ed-9b34-740650d41c6c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:35.011572Z","iopub.execute_input":"2023-01-18T00:44:35.011971Z","iopub.status.idle":"2023-01-18T00:44:37.578236Z","shell.execute_reply.started":"2023-01-18T00:44:35.011929Z","shell.execute_reply":"2023-01-18T00:44:37.576950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Test & Predict**","metadata":{}},{"cell_type":"code","source":"checkpt = ModelCheckpoint('best_model.hdf5', mode='min', monitor='val_loss', save_best_only=True)\nes = EarlyStopping(monitor='val_loss', patience=2)\nexecution = model.fit(train_ds, epochs=4, validation_data=val_ds, callbacks=[es, checkpt])","metadata":{"_uuid":"cd3f4417-681f-4eb3-876b-4f2b6b56f50c","_cell_guid":"6ddb721d-9d66-4237-af6c-4d998f4e2afd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T00:44:37.579889Z","iopub.execute_input":"2023-01-18T00:44:37.580278Z","iopub.status.idle":"2023-01-18T01:37:28.744635Z","shell.execute_reply.started":"2023-01-18T00:44:37.580244Z","shell.execute_reply":"2023-01-18T01:37:28.743429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = os.path.join(dir, 'imgs')\ntest_gen=ImageDataGenerator()\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":{"_uuid":"85e339a7-1a55-4ef1-889a-3a72962a49e4","_cell_guid":"134e1ba8-83a2-47c1-aa63-4b19ad61182b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T01:37:28.746802Z","iopub.execute_input":"2023-01-18T01:37:28.748247Z","iopub.status.idle":"2023-01-18T01:37:46.309402Z","shell.execute_reply.started":"2023-01-18T01:37:28.748205Z","shell.execute_reply":"2023-01-18T01:37:46.307981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_data)\n    \n# predictions","metadata":{"_uuid":"85e339a7-1a55-4ef1-889a-3a72962a49e4","_cell_guid":"134e1ba8-83a2-47c1-aa63-4b19ad61182b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T01:37:46.310830Z","iopub.execute_input":"2023-01-18T01:37:46.311368Z","iopub.status.idle":"2023-01-18T01:42:35.940174Z","shell.execute_reply.started":"2023-01-18T01:37:46.311330Z","shell.execute_reply":"2023-01-18T01:42:35.939066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class_names\nclass_names_written = ['c0: safe driving','c1: texting - right','c2: talking on the phone - right','c3: texting - left','c4: talking on the phone - left','c5: operating the radio','c6: drinking','c7: reaching behind','c8: hair and makeup','c9: talking to passenger']\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":"2023-01-18T03:26:31.125025Z","iopub.execute_input":"2023-01-18T03:26:31.125443Z","iopub.status.idle":"2023-01-18T03:26:31.130644Z","shell.execute_reply.started":"2023-01-18T03:26:31.125408Z","shell.execute_reply":"2023-01-18T03:26:31.129667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)\n\ntest_imgs = os.path.join(dir, \"imgs/test\")\n\ntest_ids = sorted(os.listdir(test_imgs))\npredictions_out = pd.DataFrame(columns=['img','c0','c1','c2','c3','c4','c5','c6','c7','c8','c9','ypred','ytrue'])\nfor i in range(len(predictions)):\n    predictions_out.loc[i,'img'] = test_ids[i]\n    predictions_out.loc[i , 'c0':'c9'] = predictions[i]  \n    \npredictions_out.to_csv('submission.csv',index = False)","metadata":{"_uuid":"85e339a7-1a55-4ef1-889a-3a72962a49e4","_cell_guid":"134e1ba8-83a2-47c1-aa63-4b19ad61182b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-01-18T01:59:37.106016Z","iopub.execute_input":"2023-01-18T01:59:37.106702Z","iopub.status.idle":"2023-01-18T01:59:56.024949Z","shell.execute_reply.started":"2023-01-18T01:59:37.106666Z","shell.execute_reply":"2023-01-18T01:59:56.023521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Truidx=DrvImgList.classname\n\nfor i in range(len(predictions)):\n    ytrueidx=np.where(DrvImgList==predictions_out.img[i])\n    imgidx=ytrueidx[0]\n    predictions_out.loc[i,'ytrue'] = Truidx[np.asscalar(imgidx)]\n    rowper=predictions_out.iloc[i,1:11]\n    predictions_out.loc[i,'ypred'] = 'c'+str(np.argmax(rowper))","metadata":{"execution":{"iopub.status.busy":"2023-01-18T03:21:29.852810Z","iopub.execute_input":"2023-01-18T03:21:29.853252Z","iopub.status.idle":"2023-01-18T03:22:40.424739Z","shell.execute_reply.started":"2023-01-18T03:21:29.853217Z","shell.execute_reply":"2023-01-18T03:22:40.423298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import plot_confusion_matrix\nfrom sklearn.metrics import ConfusionMatrixDisplay\n\ncnf_matrix = confusion_matrix(predictions_out.ytrue, predictions_out.ypred, normalize='true')\n\nfigure = plt.figure(figsize=(13, 9))\nsns.heatmap(cnf_matrix, xticklabels=class_names_written, yticklabels=class_names_written, annot=True,cmap=plt.cm.Blues)\nplt.tight_layout()\nplt.title('Normalized Confusion Matrix')\nplt.ylabel('True label')\nplt.xlabel('Predicted label')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T03:32:20.812387Z","iopub.execute_input":"2023-01-18T03:32:20.812816Z","iopub.status.idle":"2023-01-18T03:32:21.778072Z","shell.execute_reply.started":"2023-01-18T03:32:20.812781Z","shell.execute_reply":"2023-01-18T03:32:21.777092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" \n**Summary of Loss & Accuracy**","metadata":{}},{"cell_type":"code","source":"plt.plot(execution.history['loss'])\nplt.plot(execution.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper right')\nplt.show()\n\nplt.plot(execution.history['accuracy'])\nplt.plot(execution.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='lower right')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-18T01:42:56.906073Z","iopub.status.idle":"2023-01-18T01:42:56.906923Z","shell.execute_reply.started":"2023-01-18T01:42:56.906706Z","shell.execute_reply":"2023-01-18T01:42:56.906726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids;\n","metadata":{"execution":{"iopub.status.busy":"2023-01-18T01:42:56.907979Z","iopub.status.idle":"2023-01-18T01:42:56.908360Z","shell.execute_reply.started":"2023-01-18T01:42:56.908173Z","shell.execute_reply":"2023-01-18T01:42:56.908190Z"},"trusted":true},"execution_count":null,"outputs":[]}]}